-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
binaryMerkleTree.ts
117 lines (105 loc) · 2.92 KB
/
binaryMerkleTree.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// A set of useful helper methods for testing binary Merkle trees.
import { EMPTY, hash } from '@fuel-ts/merkle-shared';
import Node from './types/node';
/**
* Slice off the '0x' on each argument to simulate abi.encodePacked
*/
export function hashLeaf(data: string): string {
return hash('0x00'.concat(data.slice(2)));
}
/**
* Slice off the '0x' on each argument to simulate abi.encodePacked
* hash(prefix + left + right)
*/
export function hashNode(left: string, right: string): string {
return hash('0x01'.concat(left.slice(2)).concat(right.slice(2)));
}
/**
* Construct tree
*/
export function constructTree(data: string[]): Node[] {
const nodes = [];
for (let i = 0; i < data.length; i += 1) {
const hashed = hashLeaf(data[i]);
const leaf = new Node(-1, -1, -1, hashed, data[i]);
leaf.index = i;
nodes.push(leaf);
}
const nodesList = [...nodes];
let pNodes = [...nodes];
let size = (nodes.length + 1) >> 1;
let odd = nodes.length & 1;
// eslint-disable-next-line no-constant-condition
while (true) {
let i = 0;
for (; i < size - odd; i += 1) {
const j = i << 1;
const hashed = hashNode(pNodes[j].hash, pNodes[j + 1].hash);
nodes[i] = new Node(pNodes[j].index, pNodes[j + 1].index, -1, hashed, '');
const nextIndex = nodesList.length;
nodes[i].index = nextIndex;
nodesList[pNodes[j].index].parent = nextIndex;
nodesList[pNodes[j + 1].index].parent = nextIndex;
nodesList.push(nodes[i]);
}
if (size === 1) {
break;
}
if (odd === 1) {
nodes[i] = pNodes[i << 1];
}
odd = size & 1;
size = (size + 1) >> 1;
pNodes = [...nodes];
}
return nodesList;
}
/**
* Compute the merkle root
*/
export function calcRoot(data: string[]): string {
if (!data.length) {
return EMPTY;
}
const nodes = [];
for (let i = 0; i < data.length; i += 1) {
const hashed = hashLeaf(data[i]);
nodes.push(new Node(-1, -1, -1, hashed, data[i]));
}
let pNodes = nodes;
let size = (nodes.length + 1) >> 1;
let odd = nodes.length & 1;
// eslint-disable-next-line no-constant-condition
while (true) {
let i = 0;
for (; i < size - odd; i += 1) {
const j = i << 1;
const hashed = hashNode(pNodes[j].hash, pNodes[j + 1].hash);
nodes[i] = new Node(pNodes[j].index, pNodes[j + 1].index, -1, hashed, '');
}
if (odd === 1) {
nodes[i] = pNodes[i << 1];
}
if (size === 1) {
break;
}
odd = size & 1;
size = (size + 1) >> 1;
pNodes = nodes;
}
return nodes[0].hash;
}
/**
* Get proof for the leaf
*/
export function getProof(nodes: Node[], id: number): string[] {
const proof: string[] = [];
for (let prev = id, cur = nodes[id].parent; cur !== -1; prev = cur, cur = nodes[cur].parent) {
if (nodes[cur].left === prev) {
proof.push(nodes[nodes[cur].right].hash);
} else {
proof.push(nodes[nodes[cur].left].hash);
}
}
return proof;
}