-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
deepSparseMerkleSubTree.ts
47 lines (38 loc) · 1.42 KB
/
deepSparseMerkleSubTree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { decompactProof, verifyProof } from './proofs';
import SparseMerkleTree from './sparseMerkleTree';
import type SparseCompactMerkleProof from './types/sparseCompactMerkleProof';
import type SparseMerkleProof from './types/sparseMerkleProof';
class DeepSparseMerkleSubTree extends SparseMerkleTree {
constructor(root: string) {
super();
this.setRoot(root);
}
verify(proof: SparseMerkleProof, key: string, value: string): boolean {
const [result] = verifyProof(proof, this.root, key, value);
return result;
}
addBranch(proof: SparseMerkleProof, key: string, value: string): boolean {
const [result, updates] = verifyProof(proof, this.root, key, value);
if (!result) {
return false;
}
for (let i = 0; i < updates.length; i += 1) {
this.set(updates[i][0], updates[i][1]);
}
if (proof.SiblingData !== '') {
if (proof.SideNodes.length > 0) {
this.set(proof.SideNodes[0], proof.SiblingData);
}
}
return true;
}
verifyCompact(proof: SparseCompactMerkleProof, key: string, value: string): boolean {
const decompactedProof = decompactProof(proof);
return this.verify(decompactedProof, key, value);
}
addBranchCompact(proof: SparseCompactMerkleProof, key: string, value: string): boolean {
const decompactedProof = decompactProof(proof);
return this.addBranch(decompactedProof, key, value);
}
}
export default DeepSparseMerkleSubTree;