Skip to content

Commit 4f7b811

Browse files
committed
test error conditions in standard merkle tree
1 parent 132ae7a commit 4f7b811

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/standard.test.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import assert from 'assert/strict';
2+
import { keccak256 } from 'ethereum-cryptography/keccak';
3+
import { hex } from './bytes';
24
import { StandardMerkleTree } from './standard';
35

6+
const zeroBytes = new Uint8Array(32);
7+
const zero = hex(zeroBytes);
8+
49
const characters = (s: string) => {
510
const l = s.split('').map(c => [c]);
611
const t = StandardMerkleTree.of(l, ['string']);
@@ -60,7 +65,38 @@ describe('standard merkle tree', () => {
6065
const { t } = characters('a');
6166
assert.throws(
6267
() => t.getProof(1),
63-
'Error: Index out of bounds',
64-
)
68+
/^Error: Index out of bounds$/,
69+
);
70+
});
71+
72+
it('reject unrecognized tree dump', () => {
73+
assert.throws(
74+
() => StandardMerkleTree.load({ format: 'nonstandard' } as any),
75+
/^Error: Unknown format 'nonstandard'$/,
76+
);
77+
});
78+
79+
it('reject malformed tree dump', () => {
80+
const t1 = StandardMerkleTree.load({
81+
format: 'standard-v1',
82+
tree: [zero],
83+
values: [{ value: ['0'], treeIndex: 0 }],
84+
leafEncoding: ['uint256'],
85+
});
86+
assert.throws(
87+
() => t1.getProof(0),
88+
/^Error: Merkle tree does not contain the expected value$/,
89+
);
90+
91+
const t2 = StandardMerkleTree.load({
92+
format: 'standard-v1',
93+
tree: [zero, zero, hex(keccak256(keccak256(zeroBytes)))],
94+
values: [{ value: ['0'], treeIndex: 2 }],
95+
leafEncoding: ['uint256'],
96+
});
97+
assert.throws(
98+
() => t2.getProof(0),
99+
/^Error: Unable to prove value$/,
100+
);
65101
});
66102
});

0 commit comments

Comments
 (0)