merkletreejs
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -8,4 +8,6 @@ interface Options { | ||
isBitcoinTree: boolean; | ||
/** If set to `true`, the leaves and hashing pairs will be sorted. */ | ||
sort: boolean; | ||
/** If set to `true`, the leaves will be sorted. */ | ||
sortLeaves: boolean; | ||
/** If set to `true`, the hashing pairs will be sorted. */ | ||
sortPairs: boolean; | ||
} | ||
@@ -23,3 +25,4 @@ /** | ||
layers: any[]; | ||
sort: boolean; | ||
sortLeaves: boolean; | ||
sortPairs: boolean; | ||
/** | ||
@@ -26,0 +29,0 @@ * @desc Constructs a Merkle Tree. |
@@ -37,3 +37,4 @@ "use strict"; | ||
this.hashLeaves = !!options.hashLeaves; | ||
this.sort = !!options.sort; | ||
this.sortLeaves = !!options.sortLeaves; | ||
this.sortPairs = !!options.sortPairs; | ||
this.duplicateOdd = !!options.duplicateOdd; | ||
@@ -45,2 +46,5 @@ this.hashAlgo = bufferifyFn(hashAlgorithm); | ||
this.leaves = leaves.map(bufferify); | ||
if (this.sortLeaves) { | ||
this.leaves = this.leaves.sort(Buffer.compare); | ||
} | ||
this.layers = [this.leaves]; | ||
@@ -79,16 +83,13 @@ this.createHashes(this.leaves); | ||
var data = null; | ||
var combined = null; | ||
if (this.isBitcoinTree) { | ||
var combined = [reverse(left), reverse(right)]; | ||
if (this.sort) { | ||
combined.sort(Buffer.compare); | ||
} | ||
data = Buffer.concat(combined); | ||
combined = [reverse(left), reverse(right)]; | ||
} | ||
else { | ||
var combined = [left, right]; | ||
if (this.sort) { | ||
combined.sort(Buffer.compare); | ||
} | ||
data = Buffer.concat(combined); | ||
combined = [left, right]; | ||
} | ||
if (this.sortPairs) { | ||
combined.sort(Buffer.compare); | ||
} | ||
data = Buffer.concat(combined); | ||
var hash = this.hashAlgo(data); | ||
@@ -95,0 +96,0 @@ // double hash if bitcoin tree |
@@ -31,3 +31,4 @@ --- | ||
* [leaves](api-classes-index-merkletree.md#leaves) | ||
* [sort](api-classes-index-merkletree.md#sort) | ||
* [sortLeaves](api-classes-index-merkletree.md#sortleaves) | ||
* [sortPairs](api-classes-index-merkletree.md#sortpairs) | ||
@@ -143,10 +144,17 @@ ### Methods | ||
___ | ||
<a id="sort"></a> | ||
<a id="sortleaves"></a> | ||
### sort | ||
### sortLeaves | ||
**● sort**: *`boolean`* | ||
**● sortLeaves**: *`boolean`* | ||
___ | ||
<a id="sortpairs"></a> | ||
### sortPairs | ||
**● sortPairs**: *`boolean`* | ||
___ | ||
## Methods | ||
@@ -153,0 +161,0 @@ |
@@ -20,3 +20,4 @@ --- | ||
* [isBitcoinTree](api-interfaces-index-options.md#isbitcointree) | ||
* [sort](api-interfaces-index-options.md#sort) | ||
* [sortLeaves](api-interfaces-index-options.md#sortleaves) | ||
* [sortPairs](api-interfaces-index-options.md#sortpairs) | ||
@@ -54,11 +55,20 @@ --- | ||
___ | ||
<a id="sort"></a> | ||
<a id="sortleaves"></a> | ||
### sort | ||
### sortLeaves | ||
**● sort**: *`boolean`* | ||
**● sortLeaves**: *`boolean`* | ||
If set to `true`, the leaves and hashing pairs will be sorted. | ||
If set to `true`, the leaves will be sorted. | ||
___ | ||
<a id="sortpairs"></a> | ||
### sortPairs | ||
**● sortPairs**: *`boolean`* | ||
If set to `true`, the hashing pairs will be sorted. | ||
___ | ||
@@ -503,9 +503,2 @@ --- | ||
### Properties | ||
* [duplicateOdd](#duplicateodd) | ||
* [hashLeaves](#hashleaves) | ||
* [isBitcoinTree](#isbitcointree) | ||
* [sort](#sort) | ||
Properties | ||
@@ -518,2 +511,4 @@ ---------- | ||
If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash. | ||
* * * | ||
@@ -525,2 +520,4 @@ | ||
If set to `true`, the leaves will hashed using the set hashing algorithms. | ||
* * * | ||
@@ -532,2 +529,4 @@ | ||
If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree implementation](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html). Enable it when you need to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again. | ||
* * * | ||
@@ -539,2 +538,4 @@ | ||
If set to `true`, the leaves and hashing pairs will be sorted. | ||
Test | ||
@@ -541,0 +542,0 @@ ---- |
35
index.ts
@@ -12,4 +12,6 @@ import * as reverse from 'buffer-reverse' | ||
isBitcoinTree: boolean | ||
/** If set to `true`, the leaves and hashing pairs will be sorted. */ | ||
sort: boolean | ||
/** If set to `true`, the leaves will be sorted. */ | ||
sortLeaves: boolean | ||
/** If set to `true`, the hashing pairs will be sorted. */ | ||
sortPairs: boolean | ||
} | ||
@@ -29,3 +31,4 @@ | ||
layers: any[] | ||
sort: boolean | ||
sortLeaves: boolean | ||
sortPairs: boolean | ||
@@ -57,3 +60,4 @@ /** | ||
this.hashLeaves = !!options.hashLeaves | ||
this.sort = !!options.sort | ||
this.sortLeaves = !!options.sortLeaves | ||
this.sortPairs = !!options.sortPairs | ||
this.duplicateOdd = !!options.duplicateOdd | ||
@@ -66,2 +70,6 @@ this.hashAlgo = bufferifyFn(hashAlgorithm) | ||
this.leaves = leaves.map(bufferify) | ||
if (this.sortLeaves) { | ||
this.leaves = this.leaves.sort(Buffer.compare) | ||
} | ||
this.layers = [this.leaves] | ||
@@ -108,19 +116,16 @@ this.createHashes(this.leaves) | ||
let data = null | ||
let combined = null | ||
if (this.isBitcoinTree) { | ||
let combined = [reverse(left), reverse(right)] | ||
if (this.sort) { | ||
combined.sort(Buffer.compare) | ||
} | ||
data = Buffer.concat(combined) | ||
combined = [reverse(left), reverse(right)] | ||
} else { | ||
let combined = [left, right] | ||
if (this.sort) { | ||
combined.sort(Buffer.compare) | ||
} | ||
combined = [left, right] | ||
} | ||
data = Buffer.concat(combined) | ||
if (this.sortPairs) { | ||
combined.sort(Buffer.compare) | ||
} | ||
data = Buffer.concat(combined) | ||
let hash = this.hashAlgo(data) | ||
@@ -127,0 +132,0 @@ |
{ | ||
"name": "merkletreejs", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Construct Merkle Trees and verify proofs", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -94,3 +94,3 @@ <h3 align="center"> | ||
<!-- :%s// --> | ||
<!-- :%s/// --> | ||
<!-- :%s/\[Options\]()/\[Options\](#options) --> | ||
@@ -120,3 +120,4 @@ | ||
* [leaves](#leaves) | ||
* [sort](#sort) | ||
* [sortLeaves](#sortleaves) | ||
* [sortPairs](#sortpairs) | ||
@@ -138,2 +139,4 @@ ### Methods | ||
--- | ||
## Constructors | ||
@@ -145,3 +148,3 @@ | ||
⊕ **new MerkleTree**(leaves: *`any`*, hashAlgorithm: *`any`*, options?: *[Options](#options) | ||
⊕ **new MerkleTree**(leaves: *`any`*, hashAlgorithm: *`any`*, options?: *[Options]() | ||
@@ -171,6 +174,8 @@ *__desc__*: Constructs a Merkle Tree. All nodes and leaves are stored as Buffers. Lonely leaf nodes are promoted to the next level up without being hashed again. | ||
| hashAlgorithm | `any` | - | Algorithm used for hashing leaves and nodes | | ||
| `Default value` options | [Options](#options) | {} as any | Additional options | | ||
| `Default value` options | [Options]() | {} as any | Additional options | | ||
**Returns:** [MerkleTree]() | ||
___ | ||
## Properties | ||
@@ -231,10 +236,17 @@ | ||
___ | ||
<a id="sort"></a> | ||
<a id="sortleaves"></a> | ||
### sort | ||
### sortLeaves | ||
**● sort**: *`boolean`* | ||
**● sortLeaves**: *`boolean`* | ||
___ | ||
<a id="sortpairs"></a> | ||
### sortPairs | ||
**● sortPairs**: *`boolean`* | ||
___ | ||
## Methods | ||
@@ -441,2 +453,12 @@ | ||
### Properties | ||
* [duplicateOdd](#duplicateodd) | ||
* [hashLeaves](#hashleaves) | ||
* [isBitcoinTree](#isbitcointree) | ||
* [sortLeaves](#sortleaves) | ||
* [sortPairs](#sortpairs) | ||
--- | ||
## Properties | ||
@@ -471,11 +493,19 @@ | ||
___ | ||
<a id="sort"></a> | ||
<a id="sortleaves"></a> | ||
### sort | ||
### sortLeaves | ||
**● sort**: *`boolean`* | ||
**● sortLeaves**: *`boolean`* | ||
If set to `true`, the leaves and hashing pairs will be sorted. | ||
If set to `true`, the leaves will be sorted. | ||
___ | ||
<a id="sortpairs"></a> | ||
### sortPairs | ||
**● sortPairs**: *`boolean`* | ||
If set to `true`, the hashing pairs will be sorted. | ||
## Test | ||
@@ -482,0 +512,0 @@ |
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
61996
835
543