merkle-tools
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -1,2 +0,12 @@ | ||
'use strict' | ||
/* Copyright 2017 Tierion | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
@@ -10,3 +20,3 @@ var sha3512 = require('js-sha3').sha3_512 | ||
var MerkleTools = function (treeOptions) { | ||
// in case 'new' was omitted | ||
// in case 'new' was omitted | ||
if (!(this instanceof MerkleTools)) { | ||
@@ -26,9 +36,9 @@ return new MerkleTools(treeOptions) | ||
case 'SHA3-224': | ||
return new Buffer(sha3224.array(value)) | ||
return Buffer.from(sha3224.array(value)) | ||
case 'SHA3-256': | ||
return new Buffer(sha3256.array(value)) | ||
return Buffer.from(sha3256.array(value)) | ||
case 'SHA3-384': | ||
return new Buffer(sha3384.array(value)) | ||
return Buffer.from(sha3384.array(value)) | ||
case 'SHA3-512': | ||
return new Buffer(sha3512.array(value)) | ||
return Buffer.from(sha3512.array(value)) | ||
default: | ||
@@ -44,7 +54,7 @@ return crypto.createHash(hashType).update(value).digest() | ||
/// ///////////////////////////////////////// | ||
// Public Primary functions | ||
/// ///////////////////////////////////////// | ||
/// ///////////////////////////////////////// | ||
// Public Primary functions | ||
/// ///////////////////////////////////////// | ||
// Resets the current tree to empty | ||
// Resets the current tree to empty | ||
this.resetTree = function () { | ||
@@ -57,4 +67,4 @@ tree = {} | ||
// Add a leaf to the tree | ||
// Accepts hash value as a Buffer or hex string | ||
// Add a leaf to the tree | ||
// Accepts hash value as a Buffer or hex string | ||
this.addLeaf = function (value, doHash) { | ||
@@ -66,4 +76,4 @@ tree.isReady = false | ||
// Add a leaves to the tree | ||
// Accepts hash values as an array of Buffers or hex strings | ||
// Add a leaves to the tree | ||
// Accepts hash values as an array of Buffers or hex strings | ||
this.addLeaves = function (valuesArray, doHash) { | ||
@@ -77,11 +87,10 @@ tree.isReady = false | ||
// Returns a leaf at the given index | ||
// Returns a leaf at the given index | ||
this.getLeaf = function (index) { | ||
var leafLevelIndex = tree.levels.length - 1 | ||
if (index < 0 || index > tree.levels[leafLevelIndex].length - 1) return null // index is out of array bounds | ||
if (index < 0 || index > tree.leaves.length - 1) return null // index is out of array bounds | ||
return tree.levels[leafLevelIndex][index] | ||
return tree.leaves[index] | ||
} | ||
// Returns the number of leaves added to the tree | ||
// Returns the number of leaves added to the tree | ||
this.getLeafCount = function () { | ||
@@ -91,3 +100,3 @@ return tree.leaves.length | ||
// Returns the ready state of the tree | ||
// Returns the ready state of the tree | ||
this.getTreeReadyState = function () { | ||
@@ -97,3 +106,3 @@ return tree.isReady | ||
// Generates the merkle tree | ||
// Generates the merkle tree | ||
this.makeTree = function (doubleHash) { | ||
@@ -112,3 +121,3 @@ tree.isReady = false | ||
// Generates a Bitcoin style merkle tree | ||
// Generates a Bitcoin style merkle tree | ||
this.makeBTCTree = function (doubleHash) { | ||
@@ -127,3 +136,3 @@ tree.isReady = false | ||
// Returns the merkle root value for the tree | ||
// Returns the merkle root value for the tree | ||
this.getMerkleRoot = function () { | ||
@@ -134,3 +143,3 @@ if (!tree.isReady || tree.levels.length === 0) return null | ||
// Returns the proof for a leaf at the given index as an array of merkle siblings in hex format | ||
// Returns the proof for a leaf at the given index as an array of merkle siblings in hex format | ||
this.getProof = function (index, asBinary) { | ||
@@ -144,3 +153,3 @@ if (!tree.isReady) return null | ||
var currentLevelNodeCount = tree.levels[x].length | ||
// skip if this is an odd end node | ||
// skip if this is an odd end node | ||
if (index === currentLevelNodeCount - 1 && currentLevelNodeCount % 2 === 1) { | ||
@@ -151,3 +160,3 @@ index = Math.floor(index / 2) | ||
// determine the sibling for the current index and get its value | ||
// determine the sibling for the current index and get its value | ||
var isRightNode = index % 2 | ||
@@ -157,3 +166,3 @@ var siblingIndex = isRightNode ? (index - 1) : (index + 1) | ||
if (asBinary) { | ||
proof.push(new Buffer(isRightNode ? [0x00] : [0x01])) | ||
proof.push(Buffer.from(isRightNode ? [0x00] : [0x01])) | ||
proof.push(tree.levels[x][siblingIndex]) | ||
@@ -175,5 +184,5 @@ } else { | ||
// Takes a proof array, a target hash value, and a merkle root | ||
// Checks the validity of the proof and return true or false | ||
this.validateProof = function (proof, targetHash, merkleRoot) { | ||
// Takes a proof array, a target hash value, and a merkle root | ||
// Checks the validity of the proof and return true or false | ||
this.validateProof = function (proof, targetHash, merkleRoot, doubleHash) { | ||
targetHash = _getBuffer(targetHash) | ||
@@ -186,5 +195,5 @@ merkleRoot = _getBuffer(merkleRoot) | ||
if (proof[x].left) { // then the sibling is a left node | ||
proofHash = hashFunction(Buffer.concat([_getBuffer(proof[x].left), proofHash])) | ||
if (doubleHash) { proofHash = hashFunction(hashFunction(Buffer.concat([_getBuffer(proof[x].left), proofHash]))) } else { proofHash = hashFunction(Buffer.concat([_getBuffer(proof[x].left), proofHash])) } | ||
} else if (proof[x].right) { // then the sibling is a right node | ||
proofHash = hashFunction(Buffer.concat([proofHash, _getBuffer(proof[x].right)])) | ||
if (doubleHash) { proofHash = hashFunction(hashFunction(Buffer.concat([proofHash, _getBuffer(proof[x].right)]))) } else { proofHash = hashFunction(Buffer.concat([proofHash, _getBuffer(proof[x].right)])) } | ||
} else { // no left or right designation exists, proof is invalid | ||
@@ -198,8 +207,8 @@ return false | ||
/// /////////////////////////////////////// | ||
// Private Utility functions | ||
/// /////////////////////////////////////// | ||
/// /////////////////////////////////////// | ||
// Private Utility functions | ||
/// /////////////////////////////////////// | ||
// Internally, trees are made of nodes containing Buffer values only | ||
// This helps ensure that leaves being added are Buffers, and will convert hex to Buffer if needed | ||
// Internally, trees are made of nodes containing Buffer values only | ||
// This helps ensure that leaves being added are Buffers, and will convert hex to Buffer if needed | ||
function _getBuffer (value) { | ||
@@ -209,3 +218,3 @@ if (value instanceof Buffer) { // we already have a buffer, so return it | ||
} else if (_isHex(value)) { // the value is a hex string, convert to buffer and return | ||
return new Buffer(value, 'hex') | ||
return Buffer.from(value, 'hex') | ||
} else { // the value is neither buffer nor hex string, will not process this, throw error | ||
@@ -221,4 +230,4 @@ throw new Error("Bad hex value - '" + value + "'") | ||
// Calculates the next level of node when building the merkle tree | ||
// These values are calcalated off of the current highest level, level 0 and will be prepended to the levels array | ||
// Calculates the next level of node when building the merkle tree | ||
// These values are calcalated off of the current highest level, level 0 and will be prepended to the levels array | ||
function _calculateNextLevel (doubleHash) { | ||
@@ -229,3 +238,3 @@ var nodes = [] | ||
for (var x = 0; x < topLevelCount; x += 2) { | ||
if (x + 1 <= topLevelCount - 1) { // concatonate and hash the pair, add to the next level array, doubleHash if requested | ||
if (x + 1 <= topLevelCount - 1) { // concatenate and hash the pair, add to the next level array, doubleHash if requested | ||
if (doubleHash) { | ||
@@ -243,3 +252,3 @@ nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))) | ||
// This version uses the BTC method of duplicating the odd ending nodes | ||
// This version uses the BTC method of duplicating the odd ending nodes | ||
function _calculateBTCNextLevel (doubleHash) { | ||
@@ -253,3 +262,3 @@ var nodes = [] | ||
for (var x = 0; x < topLevelCount; x += 2) { | ||
// concatonate and hash the pair, add to the next level array, doubleHash if requested | ||
// concatenate and hash the pair, add to the next level array, doubleHash if requested | ||
if (doubleHash) { | ||
@@ -256,0 +265,0 @@ nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))) |
{ | ||
"name": "merkle-tools", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Tools for creating merkle trees, generating merkle proofs, and verification of merkle proofs.", | ||
"main": "merkletools.js", | ||
"scripts": { | ||
"test": "mocha test/*.js" | ||
"test": "mocha test/*.js", | ||
"benchmark": "node benchmark.js" | ||
}, | ||
@@ -20,3 +21,3 @@ "repository": { | ||
"author": "Jason Bukowski <jason@tierion.com> (https://tierion.com)", | ||
"license": "MIT", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
@@ -31,4 +32,4 @@ "url": "https://github.com/Tierion/merkle-tools/issues" | ||
"benchmark": "^2.1.3", | ||
"mocha": "^2.5.3" | ||
"mocha": "^3.2.0" | ||
} | ||
} |
@@ -7,2 +7,3 @@ # merkle-tools | ||
Tools for creating Merkle trees, generating merkle proofs, and verification of merkle proofs. | ||
@@ -151,5 +152,5 @@ ## Installation | ||
### validateProof(proof, targetHash, merkleRoot) | ||
### validateProof(proof, targetHash, merkleRoot, doubleHash) | ||
Returns a boolean indicating whether or not the proof is valid and correctly connects the targetHash to the merkleRoot. Proof is a proof array as supplied by the 'getProof' method. The targetHash and merkleRoot parameters must be Buffers or hex strings. | ||
Returns a boolean indicating whether or not the proof is valid and correctly connects the targetHash to the merkleRoot. Proof is a proof array as supplied by the 'getProof' method. The targetHash and merkleRoot parameters must be Buffers or hex strings. Setting doubleHash to true will double each hash operation to match the Bitcoin merkle tree style. | ||
@@ -156,0 +157,0 @@ ```js |
@@ -7,7 +7,7 @@ /* global describe, it */ | ||
var bLeft = new Buffer('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex') | ||
var bRight = new Buffer('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex') | ||
var bLeft = Buffer.from('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex') | ||
var bRight = Buffer.from('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex') | ||
var mRoot = crypto.createHash('sha256').update(Buffer.concat([bLeft, bRight])).digest() | ||
var bLeftmd5 = new Buffer('0cc175b9c0f1b6a831c399e269772661', 'hex') | ||
var bRightmd5 = new Buffer('92eb5ffee6ae2fec3ad71c777531578f', 'hex') | ||
var bLeftmd5 = Buffer.from('0cc175b9c0f1b6a831c399e269772661', 'hex') | ||
var bRightmd5 = Buffer.from('92eb5ffee6ae2fec3ad71c777531578f', 'hex') | ||
var mRootmd5 = crypto.createHash('md5').update(Buffer.concat([bLeftmd5, bRightmd5])).digest() | ||
@@ -50,2 +50,54 @@ | ||
describe('getLeaf after running makeTree', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
merkleTools.makeTree() | ||
it('returned leaf should be in right spot', function () { | ||
assert.equal(merkleTools.getLeaf(0).toString('hex'), 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
}) | ||
}) | ||
describe('getLeaf out of bounds returns null after running makeTree', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
merkleTools.makeTree() | ||
it('out of bounds leaf position returns null', function () { | ||
assert.equal(merkleTools.getLeaf(3), null) | ||
assert.equal(merkleTools.getLeaf(-1), null) | ||
}) | ||
}) | ||
describe('getLeaf before running makeTree', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
it('returned leaf should be in right spot', function () { | ||
assert.equal(merkleTools.getLeaf(0).toString('hex'), 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
}) | ||
}) | ||
describe('getLeaf out of bounds returns null before running makeTree', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
it('out of bounds leaf position returns null', function () { | ||
assert.equal(merkleTools.getLeaf(3), null) | ||
assert.equal(merkleTools.getLeaf(-1), null) | ||
}) | ||
}) | ||
describe('reset tree', function () { | ||
@@ -224,3 +276,3 @@ var merkleTools = new MerkleTools() | ||
var merkleTools = new MerkleTools( | ||
{ hashType: 'md5' }) | ||
{ hashType: 'md5' }) | ||
merkleTools.addLeaves([bLeftmd5, bRightmd5]) | ||
@@ -266,3 +318,3 @@ merkleTools.makeTree() | ||
it('binary proof array should be correct', function () { | ||
var expectedResult = [new Buffer([0x01]), new Buffer('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex')] | ||
var expectedResult = [Buffer.from([0x01]), Buffer.from('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex')] | ||
assert.deepEqual(proof, expectedResult) | ||
@@ -280,3 +332,3 @@ }) | ||
it('binary proof array should be correct', function () { | ||
var expectedResult = [new Buffer([0x00]), new Buffer('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex')] | ||
var expectedResult = [Buffer.from([0x00]), Buffer.from('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex')] | ||
assert.deepEqual(proof, expectedResult) | ||
@@ -363,2 +415,18 @@ }) | ||
}) | ||
describe('validate good proof BTC doubleHash', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'1a02db5db5a24c5edc5b653051d8aaaddec3f9abc30354f7df358c49fe40f735', | ||
'd3f3eb471e368a27f5320ff7a961bed748519139435cf8348e84ebd6225d7150', | ||
'7cbcf6b5378e3e43b39734baa578efa501d02abf90289547f0e6621ee959f0e3' | ||
]) | ||
merkleTools.makeBTCTree(true) | ||
var proof = merkleTools.getProof(1) | ||
var isValid = merkleTools.validateProof(proof, 'd3f3eb471e368a27f5320ff7a961bed748519139435cf8348e84ebd6225d7150', '502606f374e3e0ec3b7022bfe6631b9a9c9f5cf6dcbe74f171ef5b14676c9ee0', true) | ||
it('proof should be valid', function () { | ||
assert.equal(isValid, true) | ||
}) | ||
}) | ||
}) | ||
@@ -365,0 +433,0 @@ |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
63338
747
217
0