-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
150 lines (115 loc) · 3.18 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const { sha3, bufferToHex } = require('ethereumjs-util');
const web3 = require('web3');
const soliditySha3 = el => Buffer.from(web3.utils.soliditySha3.apply(undefined, el).slice(2), 'hex');
class MerkleTree {
constructor(elements) {
// Hash elements
this.elements = elements.map(soliditySha3);
// Deduplicate elements
this.elements = this.bufDedup(this.elements);
// Sort elements
this.elements.sort(Buffer.compare);
// Create layers
this.layers = this.getLayers(this.elements);
}
toJSON() {
return JSON.stringify({
elements: this.elements.map(e => e.toString('hex'))
});
}
static fromJSON(obj) {
const tree = new MerkleTree([]);
tree.elements = obj.elements.map(e => new Buffer(e, 'hex'));
tree.layers = tree.getLayers(tree.elements);
return tree;
}
getLayers(elements) {
if (elements.length == 0) {
return [[""]];
}
const layers = [];
layers.push(elements);
// Get next layer until we reach the root
while (layers[layers.length - 1].length > 1) {
layers.push(this.getNextLayer(layers[layers.length - 1]));
}
return layers;
}
getNextLayer(elements) {
return elements.reduce((layer, el, idx, arr) => {
if (idx % 2 === 0) {
// Hash the current element with its pair element
layer.push(this.combinedHash(el, arr[idx + 1]));
}
return layer;
}, []);
}
combinedHash(first, second) {
if (!first) { return second; }
if (!second) { return first; }
return sha3(this.sortAndConcat(first, second));
}
getRoot() {
return this.layers[this.layers.length - 1][0];
}
getHexRoot() {
return bufferToHex(this.getRoot());
}
getProof(el) {
let idx = this.bufIndexOf(el, this.elements);
if (idx === -1) {
throw new Error("Element does not exist in Merkle tree");
}
return this.layers.reduce((proof, layer) => {
const pairElement = this.getPairElement(idx, layer);
if (pairElement) {
proof.push(pairElement);
}
idx = Math.floor(idx / 2);
return proof;
}, []);
}
getHexProof(el) {
const proof = this.getProof(el);
return this.bufArrToHex(proof);
}
getPairElement(idx, layer) {
const pairIdx = idx % 2 === 0 ? idx + 1 : idx - 1;
if (pairIdx < layer.length) {
return layer[pairIdx];
} else {
return null;
}
}
bufIndexOf(el, arr) {
let hash = el;
// Convert element to 32 byte hash if it is not one already
if (el.length !== 32 || !Buffer.isBuffer(el)) {
hash = sha3(el);
} else {
hash = el;
}
hash = el;
for (let i = 0; i < arr.length; i++) {
if (hash.equals(arr[i])) {
return i;
}
}
return -1;
}
bufDedup(elements) {
return elements.filter((el, idx) => {
return this.bufIndexOf(el, elements) === idx;
});
}
bufArrToHex(arr) {
if (arr.some(el => !Buffer.isBuffer(el))) {
throw new Error("Array is not an array of buffers");
}
return "0x" + arr.map(el => el.toString("hex")).join("");
}
sortAndConcat(...args) {
return Buffer.concat([...args].sort(Buffer.compare));
}
}
module.exports = MerkleTree;