From 8da6f51d55e6c5c78fdf407924d3a227f64f2e3e Mon Sep 17 00:00:00 2001 From: Donlee Date: Tue, 10 Jul 2018 22:29:30 +0100 Subject: [PATCH] Support for non-bitcoin tree proofs --- index.js | 62 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 16 deletions(-) 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