merkletreejs
Advanced tools
Comparing version 0.2.32 to 0.3.0
@@ -15,3 +15,3 @@ /// <reference types="node" /> | ||
isBitcoinTree?: boolean; | ||
/** If set to `true`, the leaves will be sorted. */ | ||
/** If set to `true`, the leaves will be sorted. Recommended for use of multiProofs. */ | ||
sortLeaves?: boolean; | ||
@@ -24,2 +24,4 @@ /** If set to `true`, the hashing pairs will be sorted. */ | ||
fillDefaultHash?: TFillDefaultHash | Buffer | string; | ||
/** If set to `true`, the resulting tree will be a complete tree. Recommended for use of multiProofs. */ | ||
complete?: boolean; | ||
} | ||
@@ -41,2 +43,3 @@ /** | ||
private fillDefaultHash; | ||
private complete; | ||
/** | ||
@@ -43,0 +46,0 @@ * @desc Constructs a Merkle Tree. |
@@ -50,2 +50,11 @@ "use strict"; | ||
this.fillDefaultHash = null; | ||
this.complete = false; | ||
if (options.complete) { | ||
if (options.isBitcoinTree) { | ||
throw new Error('option "complete" is incompatible with "isBitcoinTree"'); | ||
} | ||
if (options.duplicateOdd) { | ||
throw new Error('option "complete" is incompatible with "duplicateOdd"'); | ||
} | ||
} | ||
this.isBitcoinTree = !!options.isBitcoinTree; | ||
@@ -55,2 +64,3 @@ this.hashLeaves = !!options.hashLeaves; | ||
this.sortPairs = !!options.sortPairs; | ||
this.complete = !!options.complete; | ||
if (options.fillDefaultHash) { | ||
@@ -98,4 +108,11 @@ if (typeof options.fillDefaultHash === 'function') { | ||
this.layers.push([]); | ||
const layerLimit = this.complete && layerIndex === 1 && !Number.isInteger(Math.log2(nodes.length)) | ||
? (2 * nodes.length) - (Math.pow(2, Math.ceil(Math.log2(nodes.length)))) | ||
: nodes.length; | ||
for (let i = 0; i < nodes.length; i += 2) { | ||
if (i + 1 === nodes.length) { | ||
if (i >= layerLimit) { | ||
this.layers[layerIndex].push(...nodes.slice(layerLimit)); | ||
break; | ||
} | ||
else if (i + 1 === nodes.length) { | ||
if (nodes.length % 2 === 1) { | ||
@@ -661,2 +678,5 @@ let data = nodes[nodes.length - 1]; | ||
getMultiProof(tree, indices) { | ||
if (!this.complete) { | ||
console.warn('Warning: For correct multiProofs it\'s strongly recommended to set complete: true'); | ||
} | ||
if (!indices) { | ||
@@ -780,3 +800,3 @@ indices = tree; | ||
if (leaves.every(Number.isInteger)) { | ||
ids = leaves.sort((a, b) => a === b ? 0 : a > b ? 1 : -1); // Indices where passed | ||
ids = [...leaves].sort((a, b) => a === b ? 0 : a > b ? 1 : -1); // Indices where passed | ||
} | ||
@@ -783,0 +803,0 @@ else { |
{ | ||
"name": "merkletreejs", | ||
"version": "0.2.32", | ||
"version": "0.3.0", | ||
"description": "Construct Merkle Trees and verify proofs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
<h3 align="center"> | ||
<br /> | ||
<img src="https://user-images.githubusercontent.com/168240/83951171-85f48c80-a7e4-11ea-896e-529c28ffa18e.png" alt="merkletree.js logo" width="600" /> | ||
<img src="https://user-images.githubusercontent.com/4885186/193118010-2a9f5129-6232-42bd-8efe-dfb29753508e.png" alt="merkletree.js logo" width="600" /> | ||
<br /> | ||
@@ -74,4 +74,4 @@ <br /> | ||
const badLeaf = SHA256('x') | ||
const badProof = tree.getProof(badLeaf) | ||
console.log(tree.verify(badProof, leaf, root)) // false | ||
const badProof = badTree.getProof(badLeaf) | ||
console.log(badTree.verify(badProof, badLeaf, root)) // false | ||
``` | ||
@@ -169,2 +169,6 @@ | ||
- [What is complete binary tree?](https://xlinux.nist.gov/dads/HTML/completeBinaryTree.html) | ||
- [What is perfect binary tree?](https://xlinux.nist.gov/dads/HTML/perfectBinaryTree.html) | ||
## Contributing | ||
@@ -171,0 +175,0 @@ |
Sorry, the diff of this file is too big to display
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
268834
3212
193