diff --git a/index.js b/index.js index 532caf3..3f33c7a 100644 --- a/index.js +++ b/index.js @@ -151,25 +151,55 @@ class MerkleTree { return [] } - for (let i = 0; i < this.layers.length - 1; i++) { - const layer = this.layers[i] - const isRightNode = index % 2 - const pairIndex = (isRightNode ? index - 1 : index) + if (this.isBitcoinTree) { - if (pairIndex < layer.length) { - proof.push({ - position: isRightNode ? 'left': 'right', - data: layer[pairIndex] - }) + // Proof Generation for Bitcoin Trees + + for (let i = 0; i < this.layers.length - 1; i++) { + const layer = this.layers[i] + const isRightNode = index % 2 + const pairIndex = (isRightNode ? index - 1 : index) + + if (pairIndex < layer.length) { + proof.push({ + position: isRightNode ? 'left': 'right', + data: layer[pairIndex] + }) + } + + // set index to parent index + index = (index / 2)|0 + + } + + return proof + + } else { + + // Proof Generation for Non-Bitcoin Trees + + for (let i = 0; i < this.layers.length; i++) { + const layer = this.layers[i] + const isRightNode = index % 2 + const pairIndex = (isRightNode ? index - 1 : index + 1) + + if (pairIndex < layer.length) { + proof.push({ + position: isRightNode ? 'left': 'right', + data: layer[pairIndex] + }) + } + + // set index to parent index + index = (index / 2)|0 + + } + + return proof + + } } - // set index to parent index - index = (index / 2)|0 - } - - return proof - } - /** * verify * @desc Returns true if the proof path (array of hashes) can connect the target node