@@ -60,7 +60,9 @@ export class SimpleMerkleTree {
60
60
hashedValues . sort ( ( a , b ) => compare ( a . hash , b . hash ) ) ;
61
61
}
62
62
63
- const tree = makeMerkleTree ( hashedValues . map ( v => v . hash ) , hashPair ) ;
63
+ const tree = hashPair
64
+ ? makeMerkleTree ( hashedValues . map ( v => v . hash ) , hashPair )
65
+ : makeMerkleTree ( hashedValues . map ( v => v . hash ) ) ;
64
66
65
67
const indexedValues = values . map ( value => ( { value : toHex ( value ) , treeIndex : 0 } ) ) ;
66
68
for ( const [ leafIndex , { valueIndex } ] of hashedValues . entries ( ) ) {
@@ -70,7 +72,9 @@ export class SimpleMerkleTree {
70
72
return new SimpleMerkleTree ( tree , indexedValues , hashPair ) ;
71
73
}
72
74
73
- static load ( data : MerkleTreeData < BytesLike > , hashPair ?: HashPairFn ) : SimpleMerkleTree {
75
+ static load ( data : MerkleTreeData < BytesLike > ) : SimpleMerkleTree ;
76
+ static load ( data : MerkleTreeData < BytesLike > , hashPair : HashPairFn ) : SimpleMerkleTree ;
77
+ static load ( data : MerkleTreeData < BytesLike > , hashPair ?: HashPairFn ) : SimpleMerkleTree {
74
78
switch ( data . format ) {
75
79
case 'simple-v1' :
76
80
if ( hashPair !== undefined ) throwError ( `Format '${ data . format } ' does not support custom hashing functions` ) ;
@@ -89,19 +93,16 @@ export class SimpleMerkleTree {
89
93
) ;
90
94
}
91
95
92
- static verify ( root : BytesLike , leaf : BytesLike , proof : BytesLike [ ] , hashPair ?: HashPairFn ) : boolean {
93
- return toHex ( root ) === processProof ( leaf , proof , hashPair ) ;
96
+ static verify ( root : BytesLike , leaf : BytesLike , proof : BytesLike [ ] ) : boolean ;
97
+ static verify ( root : BytesLike , leaf : BytesLike , proof : BytesLike [ ] , hashPair : HashPairFn ) : boolean ;
98
+ static verify ( root : BytesLike , leaf : BytesLike , proof : BytesLike [ ] , hashPair ?: HashPairFn ) : boolean {
99
+ return toHex ( root ) === ( hashPair ? processProof ( leaf , proof , hashPair ) : processProof ( leaf , proof ) ) ;
94
100
}
95
101
96
- static verifyMultiProof ( root : BytesLike , multiproof : MultiProof < BytesLike , BytesLike > , hashPair ?: HashPairFn ) : boolean {
97
- return toHex ( root ) === processMultiProof (
98
- {
99
- leaves : multiproof . leaves ,
100
- proof : multiproof . proof ,
101
- proofFlags : multiproof . proofFlags ,
102
- } ,
103
- hashPair ,
104
- ) ;
102
+ static verifyMultiProof ( root : BytesLike , multiproof : MultiProof < BytesLike , BytesLike > ) : boolean ;
103
+ static verifyMultiProof ( root : BytesLike , multiproof : MultiProof < BytesLike , BytesLike > , hashPair : HashPairFn ) : boolean ;
104
+ static verifyMultiProof ( root : BytesLike , multiproof : MultiProof < BytesLike , BytesLike > , hashPair ?: HashPairFn ) : boolean {
105
+ return toHex ( root ) === ( hashPair ? processMultiProof ( multiproof , hashPair ) : processMultiProof ( multiproof ) ) ;
105
106
}
106
107
107
108
dump ( ) : MerkleTreeData < BytesLike > {
@@ -130,8 +131,10 @@ export class SimpleMerkleTree {
130
131
for ( let i = 0 ; i < this . values . length ; i ++ ) {
131
132
this . validateValue ( i ) ;
132
133
}
133
- if ( ! isValidMerkleTree ( this . tree , this . hashPair ) ) {
134
- throwError ( 'Merkle tree is invalid' ) ;
134
+ if ( this . hashPair ) {
135
+ isValidMerkleTree ( this . tree , this . hashPair ) || throwError ( 'Merkle tree is invalid' ) ;
136
+ } else {
137
+ isValidMerkleTree ( this . tree ) || throwError ( 'Merkle tree is invalid' ) ;
135
138
}
136
139
}
137
140
@@ -184,7 +187,9 @@ export class SimpleMerkleTree {
184
187
}
185
188
186
189
private _verify ( leafHash : BytesLike , proof : BytesLike [ ] ) : boolean {
187
- return this . root === processProof ( leafHash , proof , this . hashPair ) ;
190
+ return this . hashPair
191
+ ? SimpleMerkleTree . verify ( this . root , leafHash , proof , this . hashPair )
192
+ : SimpleMerkleTree . verify ( this . root , leafHash , proof ) ;
188
193
}
189
194
190
195
verifyMultiProof ( multiproof : MultiProof < BytesLike , number | BytesLike > ) : boolean {
@@ -196,7 +201,9 @@ export class SimpleMerkleTree {
196
201
}
197
202
198
203
private _verifyMultiProof ( multiproof : MultiProof < BytesLike , BytesLike > ) : boolean {
199
- return this . root === processMultiProof ( multiproof , this . hashPair ) ;
204
+ return this . hashPair
205
+ ? SimpleMerkleTree . verifyMultiProof ( this . root , multiproof , this . hashPair )
206
+ : SimpleMerkleTree . verifyMultiProof ( this . root , multiproof ) ;
200
207
}
201
208
202
209
private validateValue ( valueIndex : number ) : HexString {
0 commit comments