-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
131 lines (110 loc) · 2.87 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
var createHash = require('crypto').createHash;
/**
* @param {Buffer} buf1
* @return {Buffer}
*/
function sha256x2 (buf1, buf2) {
var buf = createHash('sha256').update(buf1).update(buf2).digest();
return createHash('sha256').update(buf).digest();
}
/**
* Reverse CURRENT buffer
* @param {Buffer} buf
* @return {Buffer}
*/
function reverse (buf) {
return Array.prototype.reverse.call(buf);
}
/**
* @param {Buffer} buf1
* @param {Buffer} buf2
* @return {Buffer}
*/
function isEqual (buf1, buf2) {
if (buf1.length !== buf2.length) {
return false;
}
for (var i = 0; i < buf1.length; ++i) {
if (buf1[i] !== buf2[i]) {
return false;
}
}
return true;
}
/**
* @typedef {Object} ProofObject
* @param {string} txId
* @param {number} txIndex
* @param {string[]} sibling
*/
/**
* @param {string[]} txIds
* @param {number} txIndex
* @return {ProofObject}
*/
exports.getProof = function (txIds, txIndex) {
var proof = {
txId: txIds[txIndex],
txIndex: txIndex,
sibling: []
};
var tree = new Array(txIds.length);
for (var i = 0; i < tree.length; ++i) {
tree[i] = reverse(new Buffer(txIds[i], 'hex'));
}
var target = tree[txIndex];
while (tree.length !== 1) {
var newTree = new Array(~~((tree.length + 1) / 2));
for (var j = 0; j < tree.length; j += 2) {
var hash1 = tree[j];
var hash2 = tree[Math.min(j + 1, tree.length - 1)];
newTree[j / 2] = sha256x2(hash1, hash2);
if (isEqual(target, hash1)) {
proof.sibling.push(reverse(hash2).toString('hex'));
target = newTree[j / 2];
} else if (isEqual(target, hash2)) {
proof.sibling.push(reverse(hash1).toString('hex'));
target = newTree[j / 2];
}
}
tree = newTree;
}
return proof;
};
/**
* @param {ProofObject} proofObj
* @return {string}
*/
exports.getTxMerkle = function (proofObj) {
var target = reverse(new Buffer(proofObj.txId, 'hex'));
var txIndex = proofObj.txIndex;
var sibling = proofObj.sibling;
for (var i = 0; i < proofObj.sibling.length; ++i, txIndex = ~~(txIndex / 2)) {
if (txIndex % 2 === 1) {
target = sha256x2(reverse(new Buffer(sibling[i], 'hex')), target);
} else {
target = sha256x2(target, reverse(new Buffer(sibling[i], 'hex')));
}
}
return reverse(target).toString('hex');
};
/**
* @param {string[]} txIds
* @return {string}
*/
exports.getMerkleRoot = function (txIds) {
var tree = new Array(txIds.length);
for (var i = 0; i < tree.length; ++i) {
tree[i] = reverse(new Buffer(txIds[i], 'hex'));
}
while (tree.length !== 1) {
var newTree = new Array(~~((tree.length + 1) / 2));
for (var j = 0; j < tree.length; j += 2) {
var hash1 = tree[j];
var hash2 = tree[Math.min(j + 1, tree.length - 1)];
newTree[j / 2] = sha256x2(hash1, hash2);
}
tree = newTree;
}
return reverse(tree[0]).toString('hex');
};