merkle-tools
Advanced tools
Comparing version 1.1.0 to 1.2.1
@@ -111,3 +111,3 @@ /*jslint node: true */ | ||
// Returns the proof for a leaf at the given index as an array of merkle siblings in hex format | ||
this.getProof = function (index) { | ||
this.getProof = function (index, asBinary) { | ||
if (!tree.isReady) return null; | ||
@@ -130,9 +130,15 @@ var currentRowIndex = tree.levels.length - 1; | ||
var siblingIndex = isRightNode ? (index - 1) : (index + 1); | ||
var sibling = {}; | ||
var siblingPosition = isRightNode ? 'left' : 'right'; | ||
var siblingValue = tree.levels[x][siblingIndex].toString('hex'); | ||
sibling[siblingPosition] = siblingValue; | ||
proof.push(sibling); | ||
if (asBinary) { | ||
proof.push(new Buffer(isRightNode ? [0x00] : [0x01])); | ||
proof.push(tree.levels[x][siblingIndex]); | ||
} else { | ||
var sibling = {}; | ||
var siblingPosition = isRightNode ? 'left' : 'right'; | ||
var siblingValue = tree.levels[x][siblingIndex].toString('hex'); | ||
sibling[siblingPosition] = siblingValue; | ||
proof.push(sibling); | ||
} | ||
index = Math.floor(index / 2); // set index to the parent index | ||
@@ -139,0 +145,0 @@ } |
{ | ||
"name": "merkle-tools", | ||
"version": "1.1.0", | ||
"version": "1.2.1", | ||
"description": "Tools for creating merkle trees, generating merkle proofs, and verification of merkle proofs.", | ||
@@ -5,0 +5,0 @@ "main": "merkletools.js", |
@@ -79,3 +79,3 @@ # merkle-tools | ||
Removes all the leaves from the tree, prepararing to to begin creating a new tree. | ||
Removes all the leaves from the tree, prepararing to begin creating a new tree. | ||
@@ -110,5 +110,5 @@ ```js | ||
### getProof(index) | ||
### getProof(index, asBinary) | ||
Returns the proof as an array of hash objects for the leaf at the given index. If the tree is not ready or no leaf exists at the given index, null is returned. | ||
Returns the proof as an array of hash objects or array of Buffers for the leaf at the given index. If the tree is not ready or no leaf exists at the given index, null is returned. | ||
@@ -118,2 +118,3 @@ ```js | ||
// By default, an array of hash objects is returned | ||
// example: | ||
@@ -123,4 +124,18 @@ // proof == [ | ||
// { right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' }, | ||
// { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' }, | ||
// { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' } | ||
// ] | ||
var proof = merkleTools.getProof(2, true); | ||
// With asBinary set to true, an array of Buffers is returned | ||
// 0x00 indicated 'left', 0x01 indicates 'right' | ||
// example: | ||
// proof == [ | ||
// <Buffer 01>, | ||
// <Buffer 09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5>, | ||
// <Buffer 01> | ||
// <Buffer ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f>, | ||
// <Buffer 00> | ||
// <Buffer eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8> | ||
// ] | ||
``` | ||
@@ -127,0 +142,0 @@ |
@@ -227,2 +227,32 @@ var should = require('should'); | ||
describe("proof left node binary", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(0, true); | ||
it("binary proof array should be correct", function () { | ||
var expectedResult = [new Buffer([0x01]), new Buffer('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex')]; | ||
proof.should.deepEqual(expectedResult); | ||
}); | ||
}); | ||
describe("proof right node binary", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(1, true); | ||
it("binary proof array should be correct", function () { | ||
var expectedResult = [new Buffer([0x00]), new Buffer('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex')]; | ||
proof.should.deepEqual(expectedResult); | ||
}); | ||
}); | ||
describe("proof one node", function () { | ||
@@ -229,0 +259,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39898
572
207