Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Simplify BcoinClient's getMerkleProof #187

Open
ErinHales opened this issue Aug 10, 2020 · 0 comments
Open

Simplify BcoinClient's getMerkleProof #187

ErinHales opened this issue Aug 10, 2020 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers javascript Needs work in JS folder

Comments

@ErinHales
Copy link
Contributor

In getMerkleProof, it builds the merkle tree twice (once inside merkle.createRoot and once inside merkle.createBranch. I think this could be simplified to where this only happens once.

async getMerkleProof(txid, height) {
    const block = await super.execute('getblockbyheight', [height]);

    let index = -1;
    const txs = [];
    for (const [i, tx] of Object.entries(block.tx)) {
      if (tx === txid) { index = i >>> 0; } // cast to uint from string
      txs.push(Buffer.from(tx, 'hex').reverse());
    }

    assert(index >= 0, 'Transaction not in block.');

    const [root] = merkle.createRoot(hash256, txs.slice());
    assert.bufferEqual(Buffer.from(block.merkleroot, 'hex').reverse(), root);

    const branch = merkle.createBranch(hash256, index, txs.slice());

    const proof = [];
    for (const hash of branch) { proof.push(hash.toString('hex')); }

    return [proof, index];
  }
@prestwich prestwich added enhancement New feature or request good first issue Good for newcomers javascript Needs work in JS folder labels Aug 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers javascript Needs work in JS folder
Projects
None yet
Development

No branches or pull requests

2 participants