UNPKG

730 BJavaScriptView Raw
1'use strict';
2Object.defineProperty(exports, '__esModule', { value: true });
3exports.fastMerkleRoot = void 0;
4function fastMerkleRoot(values, digestFn) {
5 if (!Array.isArray(values)) throw TypeError('Expected values Array');
6 if (typeof digestFn !== 'function')
7 throw TypeError('Expected digest Function');
8 let length = values.length;
9 const results = values.concat();
10 while (length > 1) {
11 let j = 0;
12 for (let i = 0; i < length; i += 2, ++j) {
13 const left = results[i];
14 const right = i + 1 === length ? left : results[i + 1];
15 const data = Buffer.concat([left, right]);
16 results[j] = digestFn(data);
17 }
18 length = j;
19 }
20 return results[0];
21}
22exports.fastMerkleRoot = fastMerkleRoot;