-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (85 loc) · 3.04 KB
/
index.js
File metadata and controls
112 lines (85 loc) · 3.04 KB
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
const crypto = require('crypto');
const lpad = (number, length) => number.toString().padStart(length, '0');
const reverseHex = (hexString) => Buffer.from(hexString, 'hex').reverse().toString('hex');
const sha256 = (buffer) => crypto.createHash('sha256').update(buffer).digest();
const combineLeftAndRight = (left, right) => {
let bufLeft = Buffer.from(left, 'hex');
let bufRight = Buffer.from(right, 'hex');
bufLeft.reverse();
bufRight.reverse();
let bufCombined = Buffer.concat([bufLeft,bufRight]);
let bufHashed = sha256(sha256(bufCombined));
bufHashed.reverse();
return bufHashed.toString('hex');
};
const buildPMT = (leaves, filteredHash) => {
const matches = [];
let filteredInLeaves = false;
for (const hash of leaves) {
if (filteredHash == hash) {
filteredInLeaves = true;
matches.push(1);
} else {
matches.push(0);
}
}
if (!filteredInLeaves) {
throw new Error('Filtered hash provided is not part of the leaves');
}
const bits = [];
const hashes = [];
let height = 0;
const width = (height) => {
return (leaves.length + (1 << height) - 1) >>> height;
};
const hash = (height, pos, leaves) => {
if (height === 0) {
return leaves[pos];
}
const left = hash(height - 1, pos * 2, leaves);
let right;
if (pos * 2 + 1 < width(height - 1)) {
right = hash(height - 1, pos * 2 + 1, leaves);
} else {
right = left;
}
return combineLeftAndRight(left, right);
};
const traverse = (height, pos, leaves, matches) => {
let parent = 0;
for (let p = pos << height; p < ((pos + 1) << height) && p < leaves.length; p++) {
parent |= matches[p];
}
bits.push(parent);
if (height === 0 || !parent) {
hashes.push(hash(height, pos, leaves));
return;
}
traverse(height - 1, pos * 2, leaves, matches);
if (pos * 2 + 1 < width(height - 1)) {
traverse(height - 1, pos * 2 + 1, leaves, matches);
}
};
while (width(height) > 1) {
height += 1;
}
if (leaves.length > 1) {
traverse(height, 0, leaves, matches);
} else {
// If there is only one hash, use that hash as the only value without performing any other operation
hashes.push(filteredHash);
bits.push(1);
}
const flags = Buffer.allocUnsafe((bits.length + 7) / 8 | 0);
flags.fill(0);
for (let p = 0; p < bits.length; p++) {
flags[p / 8 | 0] |= bits[p] << (p % 8);
}
return {
totalTX : leaves.length,
hashes : hashes,
flags : parseInt(flags.toString('hex'), 16),
hex: `${reverseHex(lpad(leaves.length.toString(16), 8))}${lpad(hashes.length.toString(16), 2)}${hashes.map(reverseHex).join('')}${lpad(flags.length.toString(16), 2)}${flags.toString('hex')}`
};
};
module.exports = { buildPMT };