|
1 | 1 | import assert from 'assert/strict';
|
| 2 | +import { keccak256 } from 'ethereum-cryptography/keccak'; |
| 3 | +import { hex } from './bytes'; |
2 | 4 | import { StandardMerkleTree } from './standard';
|
3 | 5 |
|
| 6 | +const zeroBytes = new Uint8Array(32); |
| 7 | +const zero = hex(zeroBytes); |
| 8 | + |
4 | 9 | const characters = (s: string) => {
|
5 | 10 | const l = s.split('').map(c => [c]);
|
6 | 11 | const t = StandardMerkleTree.of(l, ['string']);
|
@@ -60,7 +65,38 @@ describe('standard merkle tree', () => {
|
60 | 65 | const { t } = characters('a');
|
61 | 66 | assert.throws(
|
62 | 67 | () => 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 | + ); |
65 | 101 | });
|
66 | 102 | });
|
0 commit comments