sort option

This commit is contained in:
Miguel Mota 2019-06-16 17:46:12 -07:00
parent 29e7b7e6f8
commit e8964cd2b3
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
6 changed files with 37 additions and 1 deletions

View File

@ -116,6 +116,7 @@ Class reprensenting a Merkle Tree
* [isBitcoinTree](#isbitcointree)
* [layers](#layers)
* [leaves](#leaves)
* [sort](#sort)
* [sortLeaves](#sortleaves)
* [sortPairs](#sortpairs)
@ -449,6 +450,7 @@ ___
* [duplicateOdd](#duplicateodd)
* [hashLeaves](#hashleaves)
* [isBitcoinTree](#isbitcointree)
* [sort](#sort)
* [sortLeaves](#sortleaves)
* [sortPairs](#sortpairs)
@ -485,6 +487,12 @@ If set to `true`, constructs the Merkle Tree using the [Bitcoin Merkle Tree impl
___
<a id="sortleaves"></a>
### sort
**● sort**: *`boolean`*
If set to `true`, the leaves and hashing pairs will be sorted.
### sortLeaves
**● sortLeaves**: *`boolean`*

3
dist/index.d.ts vendored
View File

@ -9,6 +9,8 @@ interface Options {
sortLeaves: boolean;
/** If set to `true`, the hashing pairs will be sorted. */
sortPairs: boolean;
/** If set to `true`, the leaves and hashing pairs will be sorted. */
sort: boolean;
}
/**
* Class reprensenting a Merkle Tree
@ -23,6 +25,7 @@ export declare class MerkleTree {
layers: any[];
sortLeaves: boolean;
sortPairs: boolean;
sort: boolean;
/**
* @desc Constructs a Merkle Tree.
* All nodes and leaves are stored as Buffers.

5
dist/index.js vendored
View File

@ -36,6 +36,11 @@ var MerkleTree = /** @class */ (function () {
this.hashLeaves = !!options.hashLeaves;
this.sortLeaves = !!options.sortLeaves;
this.sortPairs = !!options.sortPairs;
this.sort = !!options.sort;
if (this.sort) {
this.sortLeaves = true;
this.sortPairs = true;
}
this.duplicateOdd = !!options.duplicateOdd;
this.hashAlgo = bufferifyFn(hashAlgorithm);
if (this.hashLeaves) {

View File

@ -13,6 +13,8 @@ interface Options {
sortLeaves: boolean
/** If set to `true`, the hashing pairs will be sorted. */
sortPairs: boolean
/** If set to `true`, the leaves and hashing pairs will be sorted. */
sort: boolean
}
@ -29,6 +31,7 @@ export class MerkleTree {
layers: any[]
sortLeaves: boolean
sortPairs: boolean
sort: boolean
/**
* @desc Constructs a Merkle Tree.
@ -57,6 +60,13 @@ export class MerkleTree {
this.hashLeaves = !!options.hashLeaves
this.sortLeaves = !!options.sortLeaves
this.sortPairs = !!options.sortPairs
this.sort = !!options.sort
if (this.sort) {
this.sortLeaves = true
this.sortPairs = true
}
this.duplicateOdd = !!options.duplicateOdd
this.hashAlgo = bufferifyFn(hashAlgorithm)
if (this.hashLeaves) {

View File

@ -1,6 +1,6 @@
{
"name": "merkletreejs",
"version": "0.1.5",
"version": "0.1.6",
"description": "Construct Merkle Trees and verify proofs",
"main": "dist/index.js",
"types": "typings/merkletreejs/*",

View File

@ -60,6 +60,16 @@ test('sha3 with sort leaves and sort pairs option', t => {
t.equal(tree.getRoot().toString('hex'), root)
})
test('sha3 with sort option', t => {
t.plan(1)
const leaves = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha3, {sort: true})
const root = '60219f87561939610b484575e45c6e81156a53b86d7cd16640d930d14f21758e'
t.equal(tree.getRoot().toString('hex'), root)
})
test('sha256 with sha256 leaves and sort pairs option and duplicate odd option', t => {
t.plan(1)