Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix verifyWitness embedded extension-branch #24

Merged
merged 1 commit into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,21 @@ export function VerifyWitness(root: Buffer, key: Buffer, witness: Witness) {
throw new VerificationError(`Key does not match the proof ${
node.type}: expected ${node.key}, but got ${currentKey}`);
}
cld = node.value;
cld = node.value as (Buffer | Buffer[][]);
currentKey = currentKey.slice(node.key.length);
if (currentKey.length === 0) {
if (currentKey.length === 0 ||
(cld.length === 17 && currentKey.length === 1)) {
// The value is in an embedded branch. Extract it.
if (cld.length === 17) {
cld = (cld[currentKey[0]] as Buffer[])[1];
currentKey = currentKey.slice(1);
}
if (idx !== witness.proof.length - 1) {
throw new VerificationError(
`Key length mismatch (${node.type}): expected ${
idx + 1} but got ${witness.proof.length}`);
}
if (!cld.equals(witness.value!)) {
if (!(cld as Buffer).equals(witness.value!)) {
throw new VerificationError(
`Value mismatch: expected ${witness.value} but got ${cld}`);
}
Expand Down
16 changes: 16 additions & 0 deletions src/proof.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,20 @@ describe(
VerifyWitness(tree.root, Buffer.from('key2'), w2);
VerifyWitness(tree.root, Buffer.from('key3'), w3);
});


it('should create a merkle proof with an extension and embedded branch',
async () => {
await tree.put(Buffer.from('a'), Buffer.from('a'));
await tree.put(Buffer.from('b'), Buffer.from('b'));
await tree.put(Buffer.from('c'), Buffer.from('c'));

const w1 = await tree.get(Buffer.from('a'));
const w2 = await tree.get(Buffer.from('b'));
const w3 = await tree.get(Buffer.from('c'));

VerifyWitness(tree.root, Buffer.from('a'), w1);
VerifyWitness(tree.root, Buffer.from('b'), w2);
VerifyWitness(tree.root, Buffer.from('c'), w3);
});
});