sort option
This commit is contained in:
parent
29e7b7e6f8
commit
e8964cd2b3
|
@ -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`*
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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) {
|
||||
|
|
10
index.ts
10
index.ts
|
@ -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) {
|
||||
|
|
|
@ -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/*",
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue