-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmerkle.js
239 lines (211 loc) · 5.38 KB
/
merkle.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var crypto = require('crypto');
var through = require('through');
var REGEXP = {
'md5': "^[0-9a-f]{32}$",
'sha1': "^[0-9a-f]{40}$",
'ripemd160': "^[0-9a-f]{40}$",
'sha256': "^[0-9a-f]{64}$",
'sha512': "^[0-9a-f]{128}$",
'whirlpool': "^[0-9a-f]{128}$",
'DEFAULT': "^$"
};
function Merkle (hashFunc, hashFuncName, useUpperCaseForHash) {
var that = this;
var resFunc = function () {
return root();
};
var regexpStr = REGEXP[hashFuncName] || REGEXP.DEFAULT;
if (useUpperCaseForHash) {
// Use only capital letters if upper case is enabled
regexpStr = regexpStr.replace('a', 'A').replace('f', 'F');
}
that.hashResultRegexp = new RegExp(regexpStr);
that.leaves = [];
that.treeDepth = 0;
that.rows = [];
that.nodesCount = 0;
function feed(anyData) {
var data = String(anyData);
if(data && data.match(that.hashResultRegexp)){
// Push leaf without hashing it since it is already a hash
that.leaves.push(data);
}
else{
var hash = hashFunc(data);
if (useUpperCaseForHash) {
hash = hash.toUpperCase();
}
that.leaves.push(hash);
}
return that;
}
function depth() {
// Compute tree depth
if(!that.treeDepth){
var pow = 0;
while(Math.pow(2, pow) < that.leaves.length){
pow++;
}
that.treeDepth = pow;
}
return that.treeDepth;
}
function levels() {
return depth() + 1;
}
function nodes() {
return that.nodesCount;
}
function root() {
return that.rows[0][0];
}
function level(i) {
return that.rows[i];
}
function compute() {
var theDepth = depth();
if(that.rows.length == 0){
// Compute the nodes of each level
for (var i = 0; i < theDepth; i++) {
that.rows.push([]);
}
that.rows[theDepth] = that.leaves;
for (var j = theDepth-1; j >= 0; j--) {
that.rows[j] = getNodes(that.rows[j+1]);
that.nodesCount += that.rows[j].length;
}
}
}
function getNodes(leaves) {
var remainder = leaves.length % 2;
var nodes = [];
var hash;
for (var i = 0; i < leaves.length - 1; i = i + 2) {
hash = hashFunc(leaves[i] + leaves[i+1]);
if (useUpperCaseForHash) {
hash = hash.toUpperCase();
}
nodes[i/2] = hash;
}
if(remainder === 1){
nodes[((leaves.length-remainder)/2)] = leaves[leaves.length - 1];
}
return nodes;
}
function getProofPath(index, excludeParent) {
var proofPath = [];
for (var currentLevel = depth(); currentLevel > 0; currentLevel--) {
var currentLevelNodes = level(currentLevel);
var currentLevelCount = currentLevelNodes.length;
// if this is an odd end node to be promoted up, skip to avoid proofs with null values
if (index == currentLevelCount - 1 && currentLevelCount % 2 == 1) {
index = Math.floor(index / 2);
continue;
}
var nodes = {};
if (index % 2) { // the index is the right node
nodes.left = currentLevelNodes[index - 1];
nodes.right = currentLevelNodes[index];
} else {
nodes.left = currentLevelNodes[index];
nodes.right = currentLevelNodes[index + 1];
}
index = Math.floor(index / 2); // set index to the parent index
if (!excludeParent) {
proofPath.push({
parent: level(currentLevel - 1)[index],
left: nodes.left,
right: nodes.right
});
} else {
proofPath.push({
left: nodes.left,
right: nodes.right
});
}
}
return proofPath;
}
// PUBLIC
/**
* Return the stream, with resulting stream begin root hash string.
**/
var stream = through(
function write (data) {
feed('' + data);
},
function end () {
compute();
this.emit('data', resFunc());
this.emit('end');
});
/**
* Return the stream, but resulting stream will be json.
**/
stream.json = function () {
resFunc = function() {
return {
root: root(),
level: level(),
depth: depth(),
levels: levels(),
nodes: nodes(),
getProofPath: getProofPath
};
};
return this;
};
/**
* Computes merkle tree synchronously, returning json result.
**/
stream.sync = function (leaves) {
leaves.forEach(function(leaf){
feed(leaf);
});
compute();
resFunc = function() {
return {
root: root,
level: level,
depth: depth,
levels: levels,
nodes: nodes,
getProofPath: getProofPath
};
};
return resFunc();
};
/**
* Computes merkle tree asynchronously, returning json as callback result.
**/
stream.async = function (leaves, done) {
leaves.forEach(function(leaf){
feed(leaf);
});
compute();
resFunc = function() {
return {
root: root,
level: level,
depth: depth,
levels: levels,
nodes: nodes,
getProofPath: getProofPath
};
};
done(null, resFunc());
};
return stream;
}
module.exports = function (hashFuncName, useUpperCaseForHash) {
return new Merkle(function (input) {
if (hashFuncName === 'none') {
return input;
} else {
var hash = crypto.createHash(hashFuncName);
return hash.update(input).digest('hex');
}
}, hashFuncName,
// Use upper case y default
useUpperCaseForHash !== false);
};