wallet-storage-client
Version:
Client only Wallet Storage
39 lines • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertProofToMerklePath = convertProofToMerklePath;
const sdk_1 = require("@bsv/sdk");
function convertProofToMerklePath(txid, proof) {
const blockHeight = proof.height;
const treeHeight = proof.nodes.length;
const path = Array(treeHeight).fill(0).map(() => ([]));
let index = proof.index;
for (let level = 0; level < treeHeight; level++) {
const node = proof.nodes[level];
const isOdd = index % 2 === 1;
const offset = isOdd ? index - 1 : index + 1;
const leaf = { offset };
if (node === '*' || (level === 0 && node === txid)) {
leaf.duplicate = true;
}
else {
leaf.hash = node;
}
path[level].push(leaf);
if (level === 0) {
const txidLeaf = {
offset: proof.index,
hash: txid,
txid: true,
};
if (isOdd) {
path[0].push(txidLeaf);
}
else {
path[0].unshift(txidLeaf);
}
}
index = index >> 1;
}
return new sdk_1.MerklePath(blockHeight, path);
}
//# sourceMappingURL=tscProofToMerklePath.js.map