merkle-tools
Advanced tools
Comparing version 1.3.1 to 1.3.2
@@ -1,246 +0,243 @@ | ||
/*jslint node: true */ | ||
'use strict'; | ||
'use strict' | ||
var sha3_512 = require('js-sha3').sha3_512; | ||
var sha3_384 = require('js-sha3').sha3_384; | ||
var sha3_256 = require('js-sha3').sha3_256; | ||
var sha3_224 = require('js-sha3').sha3_224; | ||
var crypto = require('crypto'); | ||
var sha3512 = require('js-sha3').sha3_512 | ||
var sha3384 = require('js-sha3').sha3_384 | ||
var sha3256 = require('js-sha3').sha3_256 | ||
var sha3224 = require('js-sha3').sha3_224 | ||
var crypto = require('crypto') | ||
var MerkleTools = function (treeOptions) { | ||
// in case 'new' was omitted | ||
if (!(this instanceof MerkleTools)) { | ||
return new MerkleTools(treeOptions); | ||
if (!(this instanceof MerkleTools)) { | ||
return new MerkleTools(treeOptions) | ||
} | ||
var hashType = 'sha256' | ||
if (treeOptions) { // if tree options were supplied, then process them | ||
if (treeOptions.hashType !== undefined) { // set the hash function to the user's choice | ||
hashType = treeOptions.hashType | ||
} | ||
} | ||
var hashType = 'sha256'; | ||
if (treeOptions) { //if tree options were supplied, then process them | ||
if (treeOptions.hashType !== undefined) { // set the hash function to the user's choice | ||
hashType = treeOptions.hashType; | ||
} | ||
var hashFunction = function (value) { | ||
switch (hashType) { | ||
case 'SHA3-224': | ||
return new Buffer(sha3224.array(value)) | ||
case 'SHA3-256': | ||
return new Buffer(sha3256.array(value)) | ||
case 'SHA3-384': | ||
return new Buffer(sha3384.array(value)) | ||
case 'SHA3-512': | ||
return new Buffer(sha3512.array(value)) | ||
default: | ||
return crypto.createHash(hashType).update(value).digest() | ||
} | ||
var hashFunction = function (value) { | ||
switch (hashType) { | ||
case 'SHA3-224': | ||
return new Buffer(sha3_224.array(value)); | ||
case 'SHA3-256': | ||
return new Buffer(sha3_256.array(value)); | ||
case 'SHA3-384': | ||
return new Buffer(sha3_384.array(value)); | ||
case 'SHA3-512': | ||
return new Buffer(sha3_512.array(value)); | ||
default: | ||
return crypto.createHash(hashType).update(value).digest(); | ||
} | ||
}; | ||
} | ||
var tree = {}; | ||
tree.leaves = []; | ||
tree.levels = []; | ||
tree.isReady = false; | ||
var tree = {} | ||
tree.leaves = [] | ||
tree.levels = [] | ||
tree.isReady = false | ||
//////////////////////////////////////////// | ||
/// ///////////////////////////////////////// | ||
// Public Primary functions | ||
//////////////////////////////////////////// | ||
/// ///////////////////////////////////////// | ||
// Resets the current tree to empty | ||
this.resetTree = function () { | ||
tree = {}; | ||
tree.leaves = []; | ||
tree.levels = []; | ||
tree.isReady = false; | ||
}; | ||
this.resetTree = function () { | ||
tree = {} | ||
tree.leaves = [] | ||
tree.levels = [] | ||
tree.isReady = false | ||
} | ||
// Add a leaf to the tree | ||
// Accepts hash value as a Buffer or hex string | ||
this.addLeaf = function (value, doHash) { | ||
tree.isReady = false; | ||
if (doHash) value = hashFunction(value); | ||
tree.leaves.push(_getBuffer(value)); | ||
}; | ||
this.addLeaf = function (value, doHash) { | ||
tree.isReady = false | ||
if (doHash) value = hashFunction(value) | ||
tree.leaves.push(_getBuffer(value)) | ||
} | ||
// Add a leaves to the tree | ||
// Accepts hash values as an array of Buffers or hex strings | ||
this.addLeaves = function (valuesArray, doHash) { | ||
tree.isReady = false; | ||
valuesArray.forEach(function (value) { | ||
if (doHash) value = hashFunction(value); | ||
tree.leaves.push(_getBuffer(value)); | ||
}); | ||
}; | ||
this.addLeaves = function (valuesArray, doHash) { | ||
tree.isReady = false | ||
valuesArray.forEach(function (value) { | ||
if (doHash) value = hashFunction(value) | ||
tree.leaves.push(_getBuffer(value)) | ||
}) | ||
} | ||
// 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 | ||
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 | ||
return tree.levels[leafLevelIndex][index]; | ||
}; | ||
return tree.levels[leafLevelIndex][index] | ||
} | ||
// Returns the number of leaves added to the tree | ||
this.getLeafCount = function () { | ||
return tree.leaves.length; | ||
}; | ||
this.getLeafCount = function () { | ||
return tree.leaves.length | ||
} | ||
// Returns the ready state of the tree | ||
this.getTreeReadyState = function () { | ||
return tree.isReady; | ||
}; | ||
this.getTreeReadyState = function () { | ||
return tree.isReady | ||
} | ||
// Generates the merkle tree | ||
this.makeTree = function (doubleHash) { | ||
tree.isReady = false; | ||
var leafCount = tree.leaves.length; | ||
if (leafCount > 0) { // skip this whole process if there are no leaves added to the tree | ||
tree.levels = []; | ||
tree.levels.unshift(tree.leaves); | ||
while (tree.levels[0].length > 1) { | ||
tree.levels.unshift(_calculateNextLevel(doubleHash)); | ||
} | ||
} | ||
tree.isReady = true; | ||
}; | ||
// Generates the merkle tree | ||
this.makeTree = function (doubleHash) { | ||
tree.isReady = false | ||
var leafCount = tree.leaves.length | ||
if (leafCount > 0) { // skip this whole process if there are no leaves added to the tree | ||
tree.levels = [] | ||
tree.levels.unshift(tree.leaves) | ||
while (tree.levels[0].length > 1) { | ||
tree.levels.unshift(_calculateNextLevel(doubleHash)) | ||
} | ||
} | ||
tree.isReady = true | ||
} | ||
// Generates a Bitcoin style merkle tree | ||
this.makeBTCTree = function (doubleHash) { | ||
tree.isReady = false; | ||
var leafCount = tree.leaves.length; | ||
if (leafCount > 0) { // skip this whole process if there are no leaves added to the tree | ||
tree.levels = []; | ||
tree.levels.unshift(tree.leaves); | ||
while (tree.levels[0].length > 1) { | ||
tree.levels.unshift(_calculateBTCNextLevel(doubleHash)); | ||
} | ||
} | ||
tree.isReady = true; | ||
}; | ||
// Generates a Bitcoin style merkle tree | ||
this.makeBTCTree = function (doubleHash) { | ||
tree.isReady = false | ||
var leafCount = tree.leaves.length | ||
if (leafCount > 0) { // skip this whole process if there are no leaves added to the tree | ||
tree.levels = [] | ||
tree.levels.unshift(tree.leaves) | ||
while (tree.levels[0].length > 1) { | ||
tree.levels.unshift(_calculateBTCNextLevel(doubleHash)) | ||
} | ||
} | ||
tree.isReady = true | ||
} | ||
// Returns the merkle root value for the tree | ||
this.getMerkleRoot = function () { | ||
if (!tree.isReady || tree.levels.length === 0) return null; | ||
return tree.levels[0][0]; | ||
}; | ||
this.getMerkleRoot = function () { | ||
if (!tree.isReady || tree.levels.length === 0) return null | ||
return tree.levels[0][0] | ||
} | ||
// Returns the proof for a leaf at the given index as an array of merkle siblings in hex format | ||
this.getProof = function (index, asBinary) { | ||
if (!tree.isReady) return null; | ||
var currentRowIndex = tree.levels.length - 1; | ||
if (index < 0 || index > tree.levels[currentRowIndex].length - 1) return null; // the index it out of the bounds of the leaf array | ||
this.getProof = function (index, asBinary) { | ||
if (!tree.isReady) return null | ||
var currentRowIndex = tree.levels.length - 1 | ||
if (index < 0 || index > tree.levels[currentRowIndex].length - 1) return null // the index it out of the bounds of the leaf array | ||
var proof = []; | ||
for (var x = currentRowIndex; x > 0; x--) { | ||
var currentLevelNodeCount = tree.levels[x].length; | ||
var proof = [] | ||
for (var x = currentRowIndex; x > 0; x--) { | ||
var currentLevelNodeCount = tree.levels[x].length | ||
// skip if this is an odd end node | ||
if (index == currentLevelNodeCount - 1 && currentLevelNodeCount % 2 == 1) { | ||
index = Math.floor(index / 2); | ||
continue; | ||
} | ||
if (index === currentLevelNodeCount - 1 && currentLevelNodeCount % 2 === 1) { | ||
index = Math.floor(index / 2) | ||
continue | ||
} | ||
// determine the sibling for the current index and get its value | ||
var isRightNode = index % 2; | ||
var siblingIndex = isRightNode ? (index - 1) : (index + 1); | ||
var isRightNode = index % 2 | ||
var siblingIndex = isRightNode ? (index - 1) : (index + 1) | ||
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; | ||
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); | ||
} | ||
proof.push(sibling) | ||
} | ||
index = Math.floor(index / 2); // set index to the parent index | ||
} | ||
index = Math.floor(index / 2) // set index to the parent index | ||
} | ||
return proof; | ||
}; | ||
return proof | ||
} | ||
// 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) { | ||
targetHash = _getBuffer(targetHash); | ||
merkleRoot = _getBuffer(merkleRoot); | ||
if (proof.length === 0) return targetHash.toString('hex') == merkleRoot.toString('hex'); // no siblings, single item tree, so the hash should also be the root | ||
this.validateProof = function (proof, targetHash, merkleRoot) { | ||
targetHash = _getBuffer(targetHash) | ||
merkleRoot = _getBuffer(merkleRoot) | ||
if (proof.length === 0) return targetHash.toString('hex') === merkleRoot.toString('hex') // no siblings, single item tree, so the hash should also be the root | ||
var proofHash = targetHash; | ||
for (var x = 0; x < proof.length; x++) { | ||
if (proof[x].left) { // then the sibling is a left node | ||
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)])); | ||
} else { // no left or right designation exists, proof is invalid | ||
return false; | ||
} | ||
} | ||
var proofHash = targetHash | ||
for (var x = 0; x < proof.length; x++) { | ||
if (proof[x].left) { // then the sibling is a left node | ||
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)])) | ||
} else { // no left or right designation exists, proof is invalid | ||
return false | ||
} | ||
} | ||
return proofHash.toString('hex') == merkleRoot.toString('hex'); | ||
}; | ||
return proofHash.toString('hex') === merkleRoot.toString('hex') | ||
} | ||
////////////////////////////////////////// | ||
/// /////////////////////////////////////// | ||
// 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 | ||
function _getBuffer(value) { | ||
if (value instanceof Buffer) { // we already have a buffer, so return it | ||
return value; | ||
} else if (_isHex(value)) { // the value is a hex string, convert to buffer and return | ||
return new Buffer(value, 'hex'); | ||
} else { // the value is neither buffer nor hex string, will not process this, throw error | ||
throw new Error("Bad hex value - '" + value + "'"); | ||
} | ||
function _getBuffer (value) { | ||
if (value instanceof Buffer) { // we already have a buffer, so return it | ||
return value | ||
} else if (_isHex(value)) { // the value is a hex string, convert to buffer and return | ||
return new Buffer(value, 'hex') | ||
} else { // the value is neither buffer nor hex string, will not process this, throw error | ||
throw new Error("Bad hex value - '" + value + "'") | ||
} | ||
} | ||
function _isHex(value) { | ||
var hexRegex = /^[0-9A-Fa-f]{2,}$/; | ||
return hexRegex.test(value); | ||
} | ||
function _isHex (value) { | ||
var hexRegex = /^[0-9A-Fa-f]{2,}$/ | ||
return hexRegex.test(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 | ||
function _calculateNextLevel(doubleHash) { | ||
var nodes = []; | ||
var topLevel = tree.levels[0]; | ||
var topLevelCount = topLevel.length; | ||
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 (doubleHash) { | ||
nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))); | ||
} else { | ||
nodes.push(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]]))); | ||
} | ||
} | ||
else { // this is an odd ending node, promote up to the next level by itself | ||
nodes.push(topLevel[x]); | ||
} | ||
function _calculateNextLevel (doubleHash) { | ||
var nodes = [] | ||
var topLevel = tree.levels[0] | ||
var topLevelCount = topLevel.length | ||
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 (doubleHash) { | ||
nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))) | ||
} else { | ||
nodes.push(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]]))) | ||
} | ||
return nodes; | ||
} else { // this is an odd ending node, promote up to the next level by itself | ||
nodes.push(topLevel[x]) | ||
} | ||
} | ||
return nodes | ||
} | ||
// This version uses the BTC method of duplicating the odd ending nodes | ||
function _calculateBTCNextLevel(doubleHash) { | ||
var nodes = []; | ||
var topLevel = tree.levels[0]; | ||
var topLevelCount = topLevel.length; | ||
if (topLevelCount % 2 === 1) { // there is an odd count, duplicate the last element | ||
topLevel.push(topLevel[topLevelCount - 1]); | ||
} | ||
for (var x = 0; x < topLevelCount; x += 2) { | ||
function _calculateBTCNextLevel (doubleHash) { | ||
var nodes = [] | ||
var topLevel = tree.levels[0] | ||
var topLevelCount = topLevel.length | ||
if (topLevelCount % 2 === 1) { // there is an odd count, duplicate the last element | ||
topLevel.push(topLevel[topLevelCount - 1]) | ||
} | ||
for (var x = 0; x < topLevelCount; x += 2) { | ||
// concatonate and hash the pair, add to the next level array, doubleHash if requested | ||
if (doubleHash) { | ||
nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))); | ||
} else { | ||
nodes.push(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]]))); | ||
} | ||
} | ||
return nodes; | ||
if (doubleHash) { | ||
nodes.push(hashFunction(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]])))) | ||
} else { | ||
nodes.push(hashFunction(Buffer.concat([topLevel[x], topLevel[x + 1]]))) | ||
} | ||
} | ||
}; | ||
return nodes | ||
} | ||
} | ||
module.exports = MerkleTools; | ||
module.exports = MerkleTools |
{ | ||
"name": "merkle-tools", | ||
"version": "1.3.1", | ||
"version": "1.3.2", | ||
"description": "Tools for creating merkle trees, generating merkle proofs, and verification of merkle proofs.", | ||
@@ -26,8 +26,8 @@ "main": "merkletools.js", | ||
"dependencies": { | ||
"js-sha3": "^0.5.2" | ||
"js-sha3": "^0.5.7" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^2.5.3", | ||
"should": "^9.0.2" | ||
"benchmark": "^2.1.3", | ||
"mocha": "^2.5.3" | ||
} | ||
} |
# merkle-tools | ||
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
[![npm](https://img.shields.io/npm/l/merkle-tools.svg)](https://www.npmjs.com/package/merkle-tools) | ||
[![npm](https://img.shields.io/npm/v/merkle-tools.svg)](https://www.npmjs.com/package/merkle-tools) | ||
Tools for creating Merkle trees, generating merkle proofs, and verification of merkle proofs. | ||
@@ -17,7 +17,7 @@ ## Installation | ||
```js | ||
var merkletools = require('merkle-tools'); | ||
var MerkleTools = require('merkle-tools') | ||
var treeOptions = { | ||
hashType: 'md5' // optional, defaults to 'sha256' | ||
}; | ||
} | ||
// valid hashTypes include all crypto hash algorithms | ||
@@ -28,3 +28,3 @@ // such as 'MD5', 'SHA1', 'SHA224', 'SHA256', 'SHA384', 'SHA512' | ||
var merkleTools = new merkletools(treeOptions); // treeOptions is optional | ||
var merkleTools = new MerkleTools(treeOptions) // treeOptions is optional | ||
``` | ||
@@ -39,7 +39,7 @@ | ||
```js | ||
var hexData = '05ae04314577b2783b4be98211d1b72476c59e9c413cfb2afa2f0c68e0d93911'; | ||
var otherData = 'Some text data, perhaps'; | ||
var hexData = '05ae04314577b2783b4be98211d1b72476c59e9c413cfb2afa2f0c68e0d93911' | ||
var otherData = 'Some text data, perhaps' | ||
merkleTools.addLeaf(hexData); | ||
merkleTools.addLeaf(otherData, true); | ||
merkleTools.addLeaf(hexData) | ||
merkleTools.addLeaf(otherData, true) | ||
``` | ||
@@ -53,7 +53,7 @@ | ||
var hexData = ['05ae04314577b2783b4be98211d1b72476c59e9c413cfb2afa2f0c68e0d93911', 'c5ed1192d909d1af814f64c7dc9e6a4983a63891a2c59ed14448d90271cb5519', | ||
'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877']; | ||
var otherData = ['l', 'm', 'n', 'o', 'p']; | ||
'4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877'] | ||
var otherData = ['l', 'm', 'n', 'o', 'p'] | ||
merkleTools.addLeaves(hexData); | ||
merkleTools.addLeaves(otherData, true); | ||
merkleTools.addLeaves(hexData) | ||
merkleTools.addLeaves(otherData, true) | ||
``` | ||
@@ -66,3 +66,3 @@ | ||
```js | ||
var leafCount = merkleTools.getLeafCount(); | ||
var leafCount = merkleTools.getLeafCount() | ||
``` | ||
@@ -75,3 +75,3 @@ | ||
```js | ||
var leafValue = merkleTools.getLeaf(5); | ||
var leafValue = merkleTools.getLeaf(5) | ||
``` | ||
@@ -84,3 +84,3 @@ | ||
```js | ||
merkleTools.resetTree(); | ||
merkleTools.resetTree() | ||
``` | ||
@@ -93,5 +93,5 @@ | ||
```js | ||
var doubleHash = false; // true to hash pairs twice as the tree is constructed | ||
var doubleHash = false // true to hash pairs twice as the tree is constructed | ||
merkleTools.makeTree(doubleHash); | ||
merkleTools.makeTree(doubleHash) | ||
``` | ||
@@ -105,5 +105,5 @@ | ||
```js | ||
var doubleHash = true; // true to hash pairs twice as the tree is constructed | ||
var doubleHash = true // true to hash pairs twice as the tree is constructed | ||
merkleTools.makeBTCTree(doubleHash); | ||
merkleTools.makeBTCTree(doubleHash) | ||
``` | ||
@@ -113,6 +113,6 @@ | ||
Returns boolean indicating if the tree is build and ready to supply its root and proofs. The Ready state is True only after the tree is built with 'makeTree'. Adding leaves or restting the tree will change the ready state to False. | ||
Returns boolean indicating if the tree is built and ready to supply its root and proofs. The Ready state is True only after the tree is built with 'makeTree'. Adding leaves or resetting the tree will change the ready state to False. | ||
```js | ||
var isReady = merkleTools.getTreeReadyState(); | ||
var isReady = merkleTools.getTreeReadyState() | ||
``` | ||
@@ -125,3 +125,3 @@ | ||
```js | ||
var rootValue = merkleTools.getMerkleRoot(); | ||
var rootValue = merkleTools.getMerkleRoot() | ||
``` | ||
@@ -134,3 +134,3 @@ | ||
```js | ||
var proof = merkleTools.getProof(2); | ||
var proof = merkleTools.getProof(2) | ||
@@ -145,3 +145,3 @@ // By default, an array of hash objects is returned | ||
var proof = merkleTools.getProof(2, true); | ||
var proof = merkleTools.getProof(2, true) | ||
@@ -172,7 +172,7 @@ // With asBinary set to true, an array of Buffers is returned | ||
{ left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' }, | ||
]; | ||
var targetHash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e'; | ||
var merkleRoot = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766'; | ||
] | ||
var targetHash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e' | ||
var merkleRoot = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766' | ||
var isValid = merkleTools.validateProof(proof, targetHash, merkleRoot); | ||
var isValid = merkleTools.validateProof(proof, targetHash, merkleRoot) | ||
``` | ||
@@ -193,18 +193,18 @@ | ||
```js | ||
var merkletools = require('merkle-tools'); | ||
var MerkleTools = require('merkle-tools') | ||
var merkleTools = new merkletools(); // no options, defaults to sha-256 hash type | ||
var merkleTools = new MerkleTools() // no options, defaults to sha-256 hash type | ||
// add some leaves to the tree | ||
merkleTools.addLeaf('7d49f074d2c3fa193e305bc109892f20760cbbecc218b43394a9356da35a72b3'); | ||
merkleTools.addLeaf('ba78a656108137a01f104b82a3554cedffce9f36e8a4149d68e0310b0943c09d'); | ||
merkleTools.addLeaves(['x', 'y', 'z'], true); // we must indicate these values need to be hashed | ||
merkleTools.addLeaf('7d49f074d2c3fa193e305bc109892f20760cbbecc218b43394a9356da35a72b3') | ||
merkleTools.addLeaf('ba78a656108137a01f104b82a3554cedffce9f36e8a4149d68e0310b0943c09d') | ||
merkleTools.addLeaves(['x', 'y', 'z'], true) // we must indicate these values need to be hashed | ||
merkleTools.makeTree(); | ||
merkleTools.makeTree() | ||
var proof0 = merkleTools.getProof(0); | ||
var proof1 = merkleTools.getProof(1); | ||
var proof2 = merkleTools.getProof(2); | ||
var proof0 = merkleTools.getProof(0) | ||
var proof1 = merkleTools.getProof(1) | ||
var proof2 = merkleTools.getProof(2) | ||
merkleTools.resetTree(); // use this when done with this tree and you intend on creating a new one | ||
merkleTools.resetTree() // use this when done with this tree and you intend on creating a new one | ||
@@ -217,3 +217,3 @@ ``` | ||
1. Internally, leaves are stored as Buffers. When the tree is build, it is generated by hashing together the Buffer values. | ||
1. Internally, leaves are stored as Buffers. When the tree is built, it is generated by hashing together the Buffer values. | ||
2. Lonely leaf nodes are promoted to the next level up, as depicted below. | ||
@@ -220,0 +220,0 @@ |
934
test/main.js
@@ -1,566 +0,512 @@ | ||
var should = require('should'); | ||
var assert = require('assert'); | ||
var crypto = require('crypto'); | ||
var merkletools = require('../merkletools.js'); | ||
/* global describe, it */ | ||
var assert = require('assert') | ||
var crypto = require('crypto') | ||
var MerkleTools = require('../merkletools.js') | ||
var bLeft = new Buffer('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex'); | ||
var bRight = new Buffer('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 mRootmd5 = crypto.createHash('md5').update(Buffer.concat([bLeftmd5, bRightmd5])).digest(); | ||
var bLeft = new Buffer('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', 'hex') | ||
var bRight = new Buffer('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 mRootmd5 = crypto.createHash('md5').update(Buffer.concat([bLeftmd5, bRightmd5])).digest() | ||
describe("Test basic functions", function () { | ||
describe("make tree with no leaves", function () { | ||
describe('Test basic functions', function () { | ||
describe('make tree with no leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.makeTree() | ||
var merkleTools = new merkletools(); | ||
merkleTools.makeTree(); | ||
it('merkle root value should be null', function () { | ||
assert.equal(merkleTools.getMerkleRoot(), null) | ||
}) | ||
}) | ||
it("merkle root value should be null", function () { | ||
assert.equal(merkleTools.getMerkleRoot(), null); | ||
}); | ||
describe('make tree with addLeaves hex', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
merkleTools.makeTree() | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')) | ||
}) | ||
}) | ||
describe("make tree with addLeaves hex", function () { | ||
describe('make tree with addLeaves buffers', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
bLeft, bRight | ||
]) | ||
merkleTools.makeTree() | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]); | ||
merkleTools.makeTree(); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')); | ||
}); | ||
describe('reset tree', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]) | ||
merkleTools.makeTree() | ||
merkleTools.resetTree() | ||
}); | ||
it('tree should be empty', function () { | ||
assert.equal(merkleTools.getLeafCount(), 0) | ||
assert.equal(merkleTools.getTreeReadyState(), false) | ||
}) | ||
}) | ||
describe("make tree with addLeaves buffers", function () { | ||
describe('make tree with addLeaf hex', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
merkleTools.addLeaf('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c') | ||
merkleTools.makeTree() | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
bLeft, bRight | ||
]); | ||
merkleTools.makeTree(); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')); | ||
}); | ||
describe('make tree with addLeaves hex', function () { | ||
var hashes = [] | ||
hashes.push('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
hashes.push('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c') | ||
}); | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves(hashes) | ||
merkleTools.makeTree() | ||
var targetProof0 = merkleTools.getProof(0) | ||
var targetProof1 = merkleTools.getProof(1) | ||
describe("reset tree", function () { | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')) | ||
assert.equal(targetProof0.length, 1) | ||
assert.equal(targetProof1.length, 1) | ||
}) | ||
}) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb', | ||
'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c' | ||
]); | ||
merkleTools.makeTree(); | ||
merkleTools.resetTree(); | ||
describe('make tree with addLeaf buffers', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.addLeaf(bRight) | ||
merkleTools.makeTree() | ||
it("tree should be empty", function () { | ||
assert.equal(merkleTools.getLeafCount(), 0); | ||
assert.equal(merkleTools.getTreeReadyState(), false); | ||
}); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')) | ||
}) | ||
}) | ||
describe("make tree with addLeaf hex", function () { | ||
describe('make tree with addLeaf bad hex', function () { | ||
var merkleTools = new MerkleTools() | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb'); | ||
merkleTools.addLeaf('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c'); | ||
merkleTools.makeTree(); | ||
it('error should be thrown', function () { | ||
assert.throws(function () { merkleTools.addLeaf('nothexandnothashed') }, Error) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')); | ||
}); | ||
describe('make tree with 1 leaf', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb' | ||
]) | ||
merkleTools.makeTree() | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb') | ||
}) | ||
}) | ||
describe("make tree with addLeaves hex", function () { | ||
describe('make tree with 5 leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]) | ||
merkleTools.makeTree() | ||
var hashes = []; | ||
hashes.push('a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb'); | ||
hashes.push('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c'); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
}) | ||
}) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves(hashes); | ||
merkleTools.makeTree(); | ||
var targetProof0 = merkleTools.getProof(0); | ||
var targetProof1 = merkleTools.getProof(1); | ||
describe('make tree with 5 leaves individually needing hashing', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf('a', true) | ||
merkleTools.addLeaf('b', true) | ||
merkleTools.addLeaf('c', true) | ||
merkleTools.addLeaf('d', true) | ||
merkleTools.addLeaf('e', true) | ||
merkleTools.makeTree() | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')); | ||
assert.equal(targetProof0.length, 1); | ||
assert.equal(targetProof1.length, 1); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
}) | ||
}) | ||
}); | ||
describe('make tree with 5 leaves at once needing hashing', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves(['a', 'b', 'c', 'd', 'e'], true) | ||
merkleTools.makeTree() | ||
describe("make tree with addLeaf buffers", function () { | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
}) | ||
}) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
describe('make tree using makeBTCTree odd leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRoot.toString('hex')); | ||
}); | ||
var hashes = [ | ||
'1a02db5db5a24c5edc5b653051d8aaaddec3f9abc30354f7df358c49fe40f735', | ||
'd3f3eb471e368a27f5320ff7a961bed748519139435cf8348e84ebd6225d7150', | ||
'7cbcf6b5378e3e43b39734baa578efa501d02abf90289547f0e6621ee959f0e3' | ||
] | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join('') | ||
} | ||
merkleTools.addLeaves(hashes) | ||
merkleTools.makeBTCTree(true) | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), '7099d100635a0e5f62ef12a8420c99426a408951078f191a0f63ddedc4dcd198') | ||
}) | ||
}) | ||
describe("make tree with addLeaf bad hex", function () { | ||
describe('make tree using makeBTCTree one leaf', function () { | ||
var merkleTools = new MerkleTools() | ||
var merkleTools = new merkletools(); | ||
var hashes = [ | ||
'9c397f783042029888ec02f0a461cfa2cc8e3c7897f476e338720a2a86731c60' | ||
] | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join('') | ||
} | ||
merkleTools.addLeaves(hashes) | ||
merkleTools.makeBTCTree(true) | ||
it("error should be thrown", function () { | ||
assert.throws(function () { merkleTools.addLeaf('nothexandnothashed'); }, Error); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), '9c397f783042029888ec02f0a461cfa2cc8e3c7897f476e338720a2a86731c60') | ||
}) | ||
}) | ||
}); | ||
describe('make tree using makeBTCTree even leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
describe("make tree with 1 leaf", function () { | ||
var hashes = [ | ||
'6584fd6a4d0a96e27f1f0f8549a206bc9367134064d45decd2116ca7d73e6cc4', | ||
'7e2087abb091d059749a6bfd36840743d818de95a39975c18fc5969459eb00b2', | ||
'd45f9b209556d52db69a900703dacd934701bb523cd2a03bf48ec658133e511a', | ||
'5ec499041da320458cf1719d06af02fecc97d3178739f4d331c4fb84c764933d' | ||
] | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join('') | ||
} | ||
merkleTools.addLeaves(hashes) | ||
merkleTools.makeTree(true) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb' | ||
]); | ||
merkleTools.makeTree(); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), 'b02c190b3a4d8a32b2f053ffd6353495fb857ad03ff600002c581a3a2232f696') | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'); | ||
}); | ||
describe('make tree using md5', function () { | ||
var merkleTools = new MerkleTools( | ||
{ hashType: 'md5' }) | ||
merkleTools.addLeaves([bLeftmd5, bRightmd5]) | ||
merkleTools.makeTree() | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRootmd5.toString('hex')) | ||
}) | ||
}) | ||
describe("make tree with 5 leaves", function () { | ||
describe('proof left node', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.addLeaf(bRight) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(0) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]); | ||
merkleTools.makeTree(); | ||
it('proof array should be correct', function () { | ||
assert.equal(proof[0].right, 'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c') | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
}); | ||
describe('proof right node', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.addLeaf(bRight) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(1) | ||
}); | ||
it('proof array should be correct', function () { | ||
assert.equal(proof[0].left, 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
}) | ||
}) | ||
describe("make tree with 5 leaves individually needing hashing", function () { | ||
describe('proof left node binary', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.addLeaf(bRight) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(0, true) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf('a', true); | ||
merkleTools.addLeaf('b', true); | ||
merkleTools.addLeaf('c', true); | ||
merkleTools.addLeaf('d', true); | ||
merkleTools.addLeaf('e', true); | ||
merkleTools.makeTree(); | ||
it('binary proof array should be correct', function () { | ||
var expectedResult = [new Buffer([0x01]), new Buffer('cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c', 'hex')] | ||
assert.deepEqual(proof, expectedResult) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
}); | ||
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')] | ||
assert.deepEqual(proof, expectedResult) | ||
}) | ||
}) | ||
describe("make tree with 5 leaves at once needing hashing", function () { | ||
describe('proof one node', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(0) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves(['a', 'b', 'c', 'd', 'e'], true); | ||
merkleTools.makeTree(); | ||
it('proof array should be correct', function () { | ||
assert.deepEqual(proof, []) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
}); | ||
describe('validate bad proof 2 leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaf(bLeft) | ||
merkleTools.addLeaf(bRight) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(1) | ||
var isValid = merkleTools.validateProof(proof, bRight, 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb') | ||
}); | ||
it('proof should be invalid', function () { | ||
assert.equal(isValid, false) | ||
}) | ||
}) | ||
describe("make tree using makeBTCTree odd leaves", function () { | ||
describe('validate bad proof 5 leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(3) | ||
var isValid = merkleTools.validateProof(proof, 'badc3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
var merkleTools = new merkletools(); | ||
it('proof should be invalid', function () { | ||
assert.equal(isValid, false) | ||
}) | ||
}) | ||
var hashes = [ | ||
'1a02db5db5a24c5edc5b653051d8aaaddec3f9abc30354f7df358c49fe40f735', | ||
'd3f3eb471e368a27f5320ff7a961bed748519139435cf8348e84ebd6225d7150', | ||
'7cbcf6b5378e3e43b39734baa578efa501d02abf90289547f0e6621ee959f0e3' | ||
]; | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join(''); | ||
} | ||
merkleTools.addLeaves(hashes); | ||
merkleTools.makeBTCTree(true); | ||
describe('validate good proof 5 leaves', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(4) | ||
var isValid = merkleTools.validateProof(proof, '3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), '7099d100635a0e5f62ef12a8420c99426a408951078f191a0f63ddedc4dcd198'); | ||
}); | ||
it('proof should be valid', function () { | ||
assert.equal(isValid, true) | ||
}) | ||
}) | ||
}); | ||
describe('validate good proof 5 leaves B', function () { | ||
var merkleTools = new MerkleTools() | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]) | ||
merkleTools.makeTree() | ||
var proof = merkleTools.getProof(1) | ||
var isValid = merkleTools.validateProof(proof, '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba') | ||
describe("make tree using makeBTCTree one leaf", function () { | ||
it('proof should be valid', function () { | ||
assert.equal(isValid, true) | ||
}) | ||
}) | ||
}) | ||
var merkleTools = new merkletools(); | ||
describe('Test other hash functions', function () { | ||
describe('make SHA224 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA224' }) | ||
merkleTools.addLeaves([ | ||
'90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809', | ||
'35f757ad7f998eb6dd3dd1cd3b5c6de97348b84a951f13de25355177' | ||
]) | ||
merkleTools.makeTree() | ||
var hashes = [ | ||
'9c397f783042029888ec02f0a461cfa2cc8e3c7897f476e338720a2a86731c60' | ||
]; | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join(''); | ||
} | ||
merkleTools.addLeaves(hashes); | ||
merkleTools.makeBTCTree(true); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'f48bc49bb77d3a3b1c8f8a70db693f41d879189cd1919f8326067ad7') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '35f757ad7f998eb6dd3dd1cd3b5c6de97348b84a951f13de25355177') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809', 'f48bc49bb77d3a3b1c8f8a70db693f41d879189cd1919f8326067ad7'), true) | ||
}) | ||
}) | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), '9c397f783042029888ec02f0a461cfa2cc8e3c7897f476e338720a2a86731c60'); | ||
}); | ||
describe('make SHA256 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA256' }) | ||
merkleTools.addLeaves([ | ||
'1516f000de6cff5c8c63eef081ebcec2ad2fdcf7034db16045d024a90341e07d', | ||
'e20af19f85f265579ead2578859bf089c92b76a048606983ad83f27ba8f32f1a' | ||
]) | ||
merkleTools.makeTree() | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '77c654b3d1605f78ed091cbd420c939c3feff7d57dc30c171fa45a5a3c81fd7d') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'e20af19f85f265579ead2578859bf089c92b76a048606983ad83f27ba8f32f1a') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '1516f000de6cff5c8c63eef081ebcec2ad2fdcf7034db16045d024a90341e07d', '77c654b3d1605f78ed091cbd420c939c3feff7d57dc30c171fa45a5a3c81fd7d'), true) | ||
}) | ||
}) | ||
describe("make tree using makeBTCTree even leaves", function () { | ||
describe('make SHA384 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA384' }) | ||
merkleTools.addLeaves([ | ||
'84ae8c6367d64899aef44a951edfa4833378b9e213f916c5eb8492cc37cb951c726e334dace7dbe4bb1dc80c1efe33d0', | ||
'368c89a00446010def75ad7b179cea9a3d24f8cbb7e2755a28638d194809e7b614eb45453665032860b6c1a135fb6e8b' | ||
]) | ||
merkleTools.makeTree() | ||
var merkleTools = new merkletools(); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'c363aa3b824e3f3b927034fab826eff61a9bfa2030ae9fc4598992edf9f3e42f8b497d6742946caf7a771429eb1745cf') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '368c89a00446010def75ad7b179cea9a3d24f8cbb7e2755a28638d194809e7b614eb45453665032860b6c1a135fb6e8b') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '84ae8c6367d64899aef44a951edfa4833378b9e213f916c5eb8492cc37cb951c726e334dace7dbe4bb1dc80c1efe33d0', 'c363aa3b824e3f3b927034fab826eff61a9bfa2030ae9fc4598992edf9f3e42f8b497d6742946caf7a771429eb1745cf'), true) | ||
}) | ||
}) | ||
var hashes = [ | ||
'6584fd6a4d0a96e27f1f0f8549a206bc9367134064d45decd2116ca7d73e6cc4', | ||
'7e2087abb091d059749a6bfd36840743d818de95a39975c18fc5969459eb00b2', | ||
'd45f9b209556d52db69a900703dacd934701bb523cd2a03bf48ec658133e511a', | ||
'5ec499041da320458cf1719d06af02fecc97d3178739f4d331c4fb84c764933d' | ||
]; | ||
for (var x = 0; x < hashes.length; x++) { | ||
hashes[x] = hashes[x].match(/.{2}/g).reverse().join(''); | ||
} | ||
merkleTools.addLeaves(hashes); | ||
merkleTools.makeTree(true); | ||
describe('make SHA512 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA512' }) | ||
merkleTools.addLeaves([ | ||
'c0a8907588c1da716ce31cbef05da1a65986ec23afb75cd42327634dd53d754be6c00a22d6862a42be5f51187a8dff695c530a797f7704e4eb4b473a14ab416e', | ||
'df1e07eccb2a2d4e1b30d11e646ba13ddc426c1aefbefcff3639405762f216fdcc40a684f3d1855e6d465f99fd9547e53fa8a485f18649fedec5448b45963976' | ||
]) | ||
merkleTools.makeTree() | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex').match(/.{2}/g).reverse().join(''), 'b02c190b3a4d8a32b2f053ffd6353495fb857ad03ff600002c581a3a2232f696'); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd9d27704a3a785d204257bfa2b217a1890e55453b6686f091fa1be8aa2b265bc06c285a909459996e093546677c3f392458d7b1fc34a994a86689ed4100e8337') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'df1e07eccb2a2d4e1b30d11e646ba13ddc426c1aefbefcff3639405762f216fdcc40a684f3d1855e6d465f99fd9547e53fa8a485f18649fedec5448b45963976') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), 'c0a8907588c1da716ce31cbef05da1a65986ec23afb75cd42327634dd53d754be6c00a22d6862a42be5f51187a8dff695c530a797f7704e4eb4b473a14ab416e', 'd9d27704a3a785d204257bfa2b217a1890e55453b6686f091fa1be8aa2b265bc06c285a909459996e093546677c3f392458d7b1fc34a994a86689ed4100e8337'), true) | ||
}) | ||
}) | ||
}); | ||
describe('make SHA3-224 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA3-224' }) | ||
merkleTools.addLeaves([ | ||
'6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f', | ||
'08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a' | ||
]) | ||
merkleTools.makeTree() | ||
describe("make tree using md5", function () { | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f', '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a'), true) | ||
}) | ||
}) | ||
var merkleTools = new merkletools({ hashType: 'md5' }); | ||
merkleTools.addLeaves([bLeftmd5, bRightmd5]); | ||
merkleTools.makeTree(); | ||
describe('make SHA3-256 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA3-256' }) | ||
merkleTools.addLeaves([ | ||
'1d7d4ea1cc029ca460e486642830c284657ea0921235c46298b51f0ed1bb7bf7', | ||
'89b9e14eae37e999b096a6f604adefe7feea4dc240ccecb5e4e92785cffc7070' | ||
]) | ||
merkleTools.makeTree() | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), mRootmd5.toString('hex')); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '6edf674f5ce762e096c3081aee2a0a977732e07f4d704baf34f5e3804db03343') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '89b9e14eae37e999b096a6f604adefe7feea4dc240ccecb5e4e92785cffc7070') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '1d7d4ea1cc029ca460e486642830c284657ea0921235c46298b51f0ed1bb7bf7', '6edf674f5ce762e096c3081aee2a0a977732e07f4d704baf34f5e3804db03343'), true) | ||
}) | ||
}) | ||
}); | ||
describe('make SHA3-384 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA3-384' }) | ||
merkleTools.addLeaves([ | ||
'e222605f939aa69b964a0a03d7075676bb3dbb40c3bd10b22f0adcb149434e7c1085c206f0e3371470a49817aa6d5b16', | ||
'ae331b6f8643ed7e404471c81be9a74f73fc84ffd5140a0ec9aa8596fa0d0a2ded5f7b780bb2fbfc4e2226ee2a04a2fa' | ||
]) | ||
merkleTools.makeTree() | ||
describe("proof left node", function () { | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'bd54df0015fa0d4fee713fbf5c8ae232c93239c75fb9d41c7dd7a9278711764a6ee83c81766b3945ed94030254537b57') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'ae331b6f8643ed7e404471c81be9a74f73fc84ffd5140a0ec9aa8596fa0d0a2ded5f7b780bb2fbfc4e2226ee2a04a2fa') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), 'e222605f939aa69b964a0a03d7075676bb3dbb40c3bd10b22f0adcb149434e7c1085c206f0e3371470a49817aa6d5b16', 'bd54df0015fa0d4fee713fbf5c8ae232c93239c75fb9d41c7dd7a9278711764a6ee83c81766b3945ed94030254537b57'), true) | ||
}) | ||
}) | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(0); | ||
describe('make SHA3-512 tree with 2 leaves', function () { | ||
var merkleTools = new MerkleTools({ hashType: 'SHA3-512' }) | ||
merkleTools.addLeaves([ | ||
'004a237ea808cd9375ee9db9f85625948a890c54e2c30f736f54c969074eb56f0ff3d43dafb4b40d5d974acc1c2a68c046fa4d7c2c20cab6df956514040d0b8b', | ||
'0b43a85d08c05252d0e23c96bc6b1bda11dfa787049ff452b3c86f4c6135e870c058c05131f199ef8619cfac937a736bbc936a667e4d96a5bf68e4056ce5fdce' | ||
]) | ||
merkleTools.makeTree() | ||
it("proof array should be correct", function () { | ||
assert.equal(proof[0].right, 'cb4990b9a8936bbc137ddeb6dcab4620897b099a450ecdc5f3e86ef4b3a7135c'); | ||
}); | ||
}); | ||
describe("proof right node", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(1); | ||
it("proof array should be correct", function () { | ||
assert.equal(proof[0].left, 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb'); | ||
}); | ||
}); | ||
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 () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(0); | ||
it("proof array should be correct", function () { | ||
assert.deepEqual(proof, []); | ||
}); | ||
}); | ||
describe("validate bad proof 2 leaves", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaf(bLeft); | ||
merkleTools.addLeaf(bRight); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(1); | ||
var isValid = merkleTools.validateProof(proof, bRight, 'a292780cc748697cb499fdcc8cb89d835609f11e502281dfe3f6690b1cc23dcb'); | ||
it("proof should be invalid", function () { | ||
assert.equal(isValid, false); | ||
}); | ||
}); | ||
describe("validate bad proof 5 leaves", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(3); | ||
var isValid = merkleTools.validateProof(proof, 'badc3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
it("proof should be invalid", function () { | ||
assert.equal(isValid, false); | ||
}); | ||
}); | ||
describe("validate good proof 5 leaves", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(4); | ||
var isValid = merkleTools.validateProof(proof, '3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
it("proof should be valid", function () { | ||
assert.equal(isValid, true); | ||
}); | ||
}); | ||
describe("validate good proof 5 leaves B", function () { | ||
var merkleTools = new merkletools(); | ||
merkleTools.addLeaves([ | ||
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', | ||
'3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', | ||
'2e7d2c03a9507ae265ecf5b5356885a53393a2029d241394997265a1a25aefc6', | ||
'18ac3e7343f016890c510e93f935261169d9e3f565436429830faf0934f4f8e4', | ||
'3f79bb7b435b05321651daefd374cdc681dc06faa65e374e38337b88ca046dea' | ||
]); | ||
merkleTools.makeTree(); | ||
var proof = merkleTools.getProof(1); | ||
var isValid = merkleTools.validateProof(proof, '3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d', 'd71f8983ad4ee170f8129f1ebcdd7440be7798d8e1c80420bf11f1eced610dba'); | ||
it("proof should be valid", function () { | ||
assert.equal(isValid, true); | ||
}); | ||
}); | ||
}); | ||
describe("Test other hash functions", function () { | ||
describe("make SHA224 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA224' }); | ||
merkleTools.addLeaves([ | ||
'90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809', | ||
'35f757ad7f998eb6dd3dd1cd3b5c6de97348b84a951f13de25355177' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'f48bc49bb77d3a3b1c8f8a70db693f41d879189cd1919f8326067ad7'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '35f757ad7f998eb6dd3dd1cd3b5c6de97348b84a951f13de25355177'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809', 'f48bc49bb77d3a3b1c8f8a70db693f41d879189cd1919f8326067ad7'), true); | ||
}); | ||
}); | ||
describe("make SHA256 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA256' }); | ||
merkleTools.addLeaves([ | ||
'1516f000de6cff5c8c63eef081ebcec2ad2fdcf7034db16045d024a90341e07d', | ||
'e20af19f85f265579ead2578859bf089c92b76a048606983ad83f27ba8f32f1a' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '77c654b3d1605f78ed091cbd420c939c3feff7d57dc30c171fa45a5a3c81fd7d'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'e20af19f85f265579ead2578859bf089c92b76a048606983ad83f27ba8f32f1a'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '1516f000de6cff5c8c63eef081ebcec2ad2fdcf7034db16045d024a90341e07d', '77c654b3d1605f78ed091cbd420c939c3feff7d57dc30c171fa45a5a3c81fd7d'), true); | ||
}); | ||
}); | ||
describe("make SHA384 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA384' }); | ||
merkleTools.addLeaves([ | ||
'84ae8c6367d64899aef44a951edfa4833378b9e213f916c5eb8492cc37cb951c726e334dace7dbe4bb1dc80c1efe33d0', | ||
'368c89a00446010def75ad7b179cea9a3d24f8cbb7e2755a28638d194809e7b614eb45453665032860b6c1a135fb6e8b' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'c363aa3b824e3f3b927034fab826eff61a9bfa2030ae9fc4598992edf9f3e42f8b497d6742946caf7a771429eb1745cf'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '368c89a00446010def75ad7b179cea9a3d24f8cbb7e2755a28638d194809e7b614eb45453665032860b6c1a135fb6e8b'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '84ae8c6367d64899aef44a951edfa4833378b9e213f916c5eb8492cc37cb951c726e334dace7dbe4bb1dc80c1efe33d0', 'c363aa3b824e3f3b927034fab826eff61a9bfa2030ae9fc4598992edf9f3e42f8b497d6742946caf7a771429eb1745cf'), true); | ||
}); | ||
}); | ||
describe("make SHA512 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA512' }); | ||
merkleTools.addLeaves([ | ||
'c0a8907588c1da716ce31cbef05da1a65986ec23afb75cd42327634dd53d754be6c00a22d6862a42be5f51187a8dff695c530a797f7704e4eb4b473a14ab416e', | ||
'df1e07eccb2a2d4e1b30d11e646ba13ddc426c1aefbefcff3639405762f216fdcc40a684f3d1855e6d465f99fd9547e53fa8a485f18649fedec5448b45963976' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'd9d27704a3a785d204257bfa2b217a1890e55453b6686f091fa1be8aa2b265bc06c285a909459996e093546677c3f392458d7b1fc34a994a86689ed4100e8337'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'df1e07eccb2a2d4e1b30d11e646ba13ddc426c1aefbefcff3639405762f216fdcc40a684f3d1855e6d465f99fd9547e53fa8a485f18649fedec5448b45963976'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), 'c0a8907588c1da716ce31cbef05da1a65986ec23afb75cd42327634dd53d754be6c00a22d6862a42be5f51187a8dff695c530a797f7704e4eb4b473a14ab416e', 'd9d27704a3a785d204257bfa2b217a1890e55453b6686f091fa1be8aa2b265bc06c285a909459996e093546677c3f392458d7b1fc34a994a86689ed4100e8337'), true); | ||
}); | ||
}); | ||
describe("make SHA3-224 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA3-224' }); | ||
merkleTools.addLeaves([ | ||
'6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f', | ||
'08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '08db5633d406804d044a3e67683e179b5ee51249ed2139c239d1e65a'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '6ed712b9472b671fd70bb950dc4ccfce197c92a7969f6bc2aa6b6d9f', '674bc9f53d5c666174cdd3ccb9df04768dfb7759655e7d937aef0c3a'), true); | ||
}); | ||
}); | ||
describe("make SHA3-256 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA3-256' }); | ||
merkleTools.addLeaves([ | ||
'1d7d4ea1cc029ca460e486642830c284657ea0921235c46298b51f0ed1bb7bf7', | ||
'89b9e14eae37e999b096a6f604adefe7feea4dc240ccecb5e4e92785cffc7070' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '6edf674f5ce762e096c3081aee2a0a977732e07f4d704baf34f5e3804db03343'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '89b9e14eae37e999b096a6f604adefe7feea4dc240ccecb5e4e92785cffc7070'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '1d7d4ea1cc029ca460e486642830c284657ea0921235c46298b51f0ed1bb7bf7', '6edf674f5ce762e096c3081aee2a0a977732e07f4d704baf34f5e3804db03343'), true); | ||
}); | ||
}); | ||
describe("make SHA3-384 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA3-384' }); | ||
merkleTools.addLeaves([ | ||
'e222605f939aa69b964a0a03d7075676bb3dbb40c3bd10b22f0adcb149434e7c1085c206f0e3371470a49817aa6d5b16', | ||
'ae331b6f8643ed7e404471c81be9a74f73fc84ffd5140a0ec9aa8596fa0d0a2ded5f7b780bb2fbfc4e2226ee2a04a2fa' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), 'bd54df0015fa0d4fee713fbf5c8ae232c93239c75fb9d41c7dd7a9278711764a6ee83c81766b3945ed94030254537b57'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, 'ae331b6f8643ed7e404471c81be9a74f73fc84ffd5140a0ec9aa8596fa0d0a2ded5f7b780bb2fbfc4e2226ee2a04a2fa'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), 'e222605f939aa69b964a0a03d7075676bb3dbb40c3bd10b22f0adcb149434e7c1085c206f0e3371470a49817aa6d5b16', 'bd54df0015fa0d4fee713fbf5c8ae232c93239c75fb9d41c7dd7a9278711764a6ee83c81766b3945ed94030254537b57'), true); | ||
}); | ||
}); | ||
describe("make SHA3-512 tree with 2 leaves", function () { | ||
var merkleTools = new merkletools({ hashType: 'SHA3-512' }); | ||
merkleTools.addLeaves([ | ||
'004a237ea808cd9375ee9db9f85625948a890c54e2c30f736f54c969074eb56f0ff3d43dafb4b40d5d974acc1c2a68c046fa4d7c2c20cab6df956514040d0b8b', | ||
'0b43a85d08c05252d0e23c96bc6b1bda11dfa787049ff452b3c86f4c6135e870c058c05131f199ef8619cfac937a736bbc936a667e4d96a5bf68e4056ce5fdce' | ||
]); | ||
merkleTools.makeTree(); | ||
it("merkle root value should be correct", function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '3dff3f19b67628591d294cba2c07ed20d20d83e1624af8c1dca8fcf096127b9f86435e2d6a84ca4cee526525cacd1c628bf06ee938983413afafbb4598c5862a'); | ||
}); | ||
it("proof array should be correct", function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '0b43a85d08c05252d0e23c96bc6b1bda11dfa787049ff452b3c86f4c6135e870c058c05131f199ef8619cfac937a736bbc936a667e4d96a5bf68e4056ce5fdce'); | ||
}); | ||
it("proof should be valid", function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '004a237ea808cd9375ee9db9f85625948a890c54e2c30f736f54c969074eb56f0ff3d43dafb4b40d5d974acc1c2a68c046fa4d7c2c20cab6df956514040d0b8b', '3dff3f19b67628591d294cba2c07ed20d20d83e1624af8c1dca8fcf096127b9f86435e2d6a84ca4cee526525cacd1c628bf06ee938983413afafbb4598c5862a'), true); | ||
}); | ||
}); | ||
}); | ||
it('merkle root value should be correct', function () { | ||
assert.equal(merkleTools.getMerkleRoot().toString('hex'), '3dff3f19b67628591d294cba2c07ed20d20d83e1624af8c1dca8fcf096127b9f86435e2d6a84ca4cee526525cacd1c628bf06ee938983413afafbb4598c5862a') | ||
}) | ||
it('proof array should be correct', function () { | ||
assert.equal(merkleTools.getProof(0)[0].right, '0b43a85d08c05252d0e23c96bc6b1bda11dfa787049ff452b3c86f4c6135e870c058c05131f199ef8619cfac937a736bbc936a667e4d96a5bf68e4056ce5fdce') | ||
}) | ||
it('proof should be valid', function () { | ||
assert.equal(merkleTools.validateProof(merkleTools.getProof(0), '004a237ea808cd9375ee9db9f85625948a890c54e2c30f736f54c969074eb56f0ff3d43dafb4b40d5d974acc1c2a68c046fa4d7c2c20cab6df956514040d0b8b', '3dff3f19b67628591d294cba2c07ed20d20d83e1624af8c1dca8fcf096127b9f86435e2d6a84ca4cee526525cacd1c628bf06ee938983413afafbb4598c5862a'), true) | ||
}) | ||
}) | ||
}) |
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
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
46270
8
680
1
Updatedjs-sha3@^0.5.7