Update README
This commit is contained in:
parent
8868540114
commit
6f0ee08069
217
README.md
217
README.md
|
@ -91,9 +91,10 @@ Output
|
|||
|
||||
## Documentation
|
||||
|
||||
[merkletreejs](../README.md) > ["index"](../modules/_index_.md) > [MerkleTree](../classes/_index_.merkletree.md)
|
||||
<!-- :%s// -->
|
||||
<!-- :%s/\[Options\]()/\[Options\](#options) -->
|
||||
|
||||
## Class: MerkleTree
|
||||
## Class
|
||||
|
||||
Class reprensenting a Merkle Tree
|
||||
|
||||
|
@ -103,38 +104,34 @@ Class reprensenting a Merkle Tree
|
|||
|
||||
**MerkleTree**
|
||||
|
||||
## Index
|
||||
|
||||
### Constructors
|
||||
|
||||
* [constructor](_index_.merkletree.md#constructor)
|
||||
* [constructor](#constructor)
|
||||
|
||||
### Properties
|
||||
|
||||
* [_sort](_index_.merkletree.md#_sort)
|
||||
* [duplicateOdd](_index_.merkletree.md#duplicateodd)
|
||||
* [hashAlgo](_index_.merkletree.md#hashalgo)
|
||||
* [hashLeaves](_index_.merkletree.md#hashleaves)
|
||||
* [isBitcoinTree](_index_.merkletree.md#isbitcointree)
|
||||
* [layers](_index_.merkletree.md#layers)
|
||||
* [leaves](_index_.merkletree.md#leaves)
|
||||
* [duplicateOdd](#duplicateodd)
|
||||
* [hashAlgo](#hashalgo)
|
||||
* [hashLeaves](#hashleaves)
|
||||
* [isBitcoinTree](#isbitcointree)
|
||||
* [layers](#layers)
|
||||
* [leaves](#leaves)
|
||||
* [sort](#sort)
|
||||
|
||||
### Methods
|
||||
|
||||
* [createHashes](_index_.merkletree.md#createhashes)
|
||||
* [getLayers](_index_.merkletree.md#getlayers)
|
||||
* [getLayersAsObject](_index_.merkletree.md#getlayersasobject)
|
||||
* [getLeaves](_index_.merkletree.md#getleaves)
|
||||
* [getProof](_index_.merkletree.md#getproof)
|
||||
* [getRoot](_index_.merkletree.md#getroot)
|
||||
* [print](_index_.merkletree.md#print)
|
||||
* [toString](_index_.merkletree.md#tostring)
|
||||
* [toTreeString](_index_.merkletree.md#totreestring)
|
||||
* [verify](_index_.merkletree.md#verify)
|
||||
* [bufferify](_index_.merkletree.md#bufferify)
|
||||
* [print](_index_.merkletree.md#print-1)
|
||||
|
||||
---
|
||||
* [createHashes](#createhashes)
|
||||
* [getLayers](#getlayers)
|
||||
* [getLayersAsObject](#getlayersasobject)
|
||||
* [getLeaves](#getleaves)
|
||||
* [getProof](#getproof)
|
||||
* [getRoot](#getroot)
|
||||
* [print](#print)
|
||||
* [toString](#tostring)
|
||||
* [toTreeString](#totreestring)
|
||||
* [verify](#verify)
|
||||
* [bufferify](#bufferify)
|
||||
* [print](#print-1)
|
||||
|
||||
## Constructors
|
||||
|
||||
|
@ -142,19 +139,24 @@ Class reprensenting a Merkle Tree
|
|||
|
||||
### constructor
|
||||
|
||||
⊕ **new MerkleTree**(leaves: *`any`*, hashAlgorithm: *`any`*, options?: *`any`*): [MerkleTree](_index_.merkletree.md)
|
||||
|
||||
*Defined in [index.ts:16](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L16)*
|
||||
⊕ **new MerkleTree**(leaves: *`any`*, hashAlgorithm: *`any`*, options?: *[Options](#options)
|
||||
|
||||
*__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.
|
||||
|
||||
*__example__*: const MerkleTree = require('merkletreejs') const crypto = require('crypto')
|
||||
*__example__*:
|
||||
```js
|
||||
const MerkleTree = require('merkletreejs')
|
||||
const crypto = require('crypto')
|
||||
|
||||
function sha256(data) { // returns Buffer return crypto.createHash('sha256').update(data).digest() }
|
||||
function sha256(data) {
|
||||
// returns Buffer
|
||||
return crypto.createHash('sha256').update(data).digest()
|
||||
}
|
||||
|
||||
const leaves = \['a', 'b', 'c'\].map(x => sha3(x))
|
||||
const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
|
||||
const tree = new MerkleTree(leaves, sha256)
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
|
@ -162,39 +164,35 @@ const tree = new MerkleTree(leaves, sha256)
|
|||
| ------ | ------ | ------ | ------ |
|
||||
| leaves | `any` | - | Array of hashed leaves. Each leaf must be a Buffer. |
|
||||
| hashAlgorithm | `any` | - | Algorithm used for hashing leaves and nodes |
|
||||
| `Default value` options | `any` | {} as any | Additional options |
|
||||
| `Default value` options | [Options](#options) | {} as any | Additional options |
|
||||
|
||||
**Returns:** [MerkleTree](_index_.merkletree.md)
|
||||
|
||||
___
|
||||
**Returns:** [MerkleTree]()
|
||||
|
||||
## Properties
|
||||
|
||||
<a id="_sort"></a>
|
||||
|
||||
### _sort
|
||||
|
||||
**● _sort**: *`boolean`*
|
||||
|
||||
*Defined in [index.ts:15](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L15)*
|
||||
|
||||
___
|
||||
<a id="duplicateodd"></a>
|
||||
|
||||
### duplicateOdd
|
||||
|
||||
**● duplicateOdd**: *`boolean`*
|
||||
|
||||
*Defined in [index.ts:16](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L16)*
|
||||
|
||||
___
|
||||
<a id="hashalgo"></a>
|
||||
|
||||
### hashAlgo
|
||||
|
||||
**● hashAlgo**: *`any`*
|
||||
**● hashAlgo**: *`function`*
|
||||
|
||||
*Defined in [index.ts:10](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L10)*
|
||||
#### Type declaration
|
||||
▸(value: *`any`*): `any`
|
||||
|
||||
**Parameters:**
|
||||
|
||||
| Name | Type |
|
||||
| ------ | ------ |
|
||||
| value | `any` |
|
||||
|
||||
**Returns:** `any`
|
||||
|
||||
___
|
||||
<a id="hashleaves"></a>
|
||||
|
@ -203,8 +201,6 @@ ___
|
|||
|
||||
**● hashLeaves**: *`boolean`*
|
||||
|
||||
*Defined in [index.ts:11](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L11)*
|
||||
|
||||
___
|
||||
<a id="isbitcointree"></a>
|
||||
|
||||
|
@ -212,25 +208,26 @@ ___
|
|||
|
||||
**● isBitcoinTree**: *`boolean`*
|
||||
|
||||
*Defined in [index.ts:14](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L14)*
|
||||
|
||||
___
|
||||
<a id="layers"></a>
|
||||
|
||||
### layers
|
||||
|
||||
**● layers**: *`any`*
|
||||
|
||||
*Defined in [index.ts:13](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L13)*
|
||||
**● layers**: *`any`[]*
|
||||
|
||||
___
|
||||
<a id="leaves"></a>
|
||||
|
||||
### leaves
|
||||
|
||||
**● leaves**: *`any`*
|
||||
**● leaves**: *`any`[]*
|
||||
|
||||
*Defined in [index.ts:12](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L12)*
|
||||
___
|
||||
<a id="sort"></a>
|
||||
|
||||
### sort
|
||||
|
||||
**● sort**: *`boolean`*
|
||||
|
||||
___
|
||||
|
||||
|
@ -242,8 +239,6 @@ ___
|
|||
|
||||
▸ **createHashes**(nodes: *`any`*): `void`
|
||||
|
||||
*Defined in [index.ts:60](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L60)*
|
||||
|
||||
**Parameters:**
|
||||
|
||||
| Name | Type |
|
||||
|
@ -257,17 +252,18 @@ ___
|
|||
|
||||
### getLayers
|
||||
|
||||
▸ **getLayers**(): `any`
|
||||
|
||||
*Defined in [index.ts:145](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L145)*
|
||||
▸ **getLayers**(): `any`[]
|
||||
|
||||
getLayers
|
||||
|
||||
*__desc__*: Returns array of all layers of Merkle Tree, including leaves and root.
|
||||
|
||||
*__example__*: const layers = tree.getLayers()
|
||||
*__example__*:
|
||||
```js
|
||||
const layers = tree.getLayers()
|
||||
```
|
||||
|
||||
**Returns:** `any`
|
||||
**Returns:** `any`[]
|
||||
|
||||
___
|
||||
<a id="getlayersasobject"></a>
|
||||
|
@ -276,8 +272,6 @@ ___
|
|||
|
||||
▸ **getLayersAsObject**(): `any`
|
||||
|
||||
*Defined in [index.ts:295](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L295)*
|
||||
|
||||
**Returns:** `any`
|
||||
|
||||
___
|
||||
|
@ -285,17 +279,18 @@ ___
|
|||
|
||||
### getLeaves
|
||||
|
||||
▸ **getLeaves**(): `any`
|
||||
|
||||
*Defined in [index.ts:134](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L134)*
|
||||
▸ **getLeaves**(): `any`[]
|
||||
|
||||
getLeaves
|
||||
|
||||
*__desc__*: Returns array of leaves of Merkle Tree.
|
||||
|
||||
*__example__*: const leaves = tree.getLeaves()
|
||||
*__example__*:
|
||||
```js
|
||||
const leaves = tree.getLeaves()
|
||||
```
|
||||
|
||||
**Returns:** `any`
|
||||
**Returns:** `any`[]
|
||||
|
||||
___
|
||||
<a id="getproof"></a>
|
||||
|
@ -304,15 +299,21 @@ ___
|
|||
|
||||
▸ **getProof**(leaf: *`any`*, index?: *`any`*): `any`[]
|
||||
|
||||
*Defined in [index.ts:176](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L176)*
|
||||
|
||||
getProof
|
||||
|
||||
*__desc__*: Returns the proof for a target leaf.
|
||||
|
||||
*__example__*: const proof = tree.getProof(leaves\[2\])
|
||||
*__example__*:
|
||||
```js
|
||||
const proof = tree.getProof(leaves[2])
|
||||
```
|
||||
|
||||
*__example__*: const leaves = \['a', 'b', 'a'\].map(x => sha3(x)) const tree = new MerkleTree(leaves, sha3) const proof = tree.getProof(leaves\[2\], 2)
|
||||
*__example__*:
|
||||
```js
|
||||
const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
const tree = new MerkleTree(leaves, sha3)
|
||||
const proof = tree.getProof(leaves[2], 2)
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
|
@ -331,13 +332,14 @@ ___
|
|||
|
||||
▸ **getRoot**(): `any`
|
||||
|
||||
*Defined in [index.ts:156](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L156)*
|
||||
|
||||
getRoot
|
||||
|
||||
*__desc__*: Returns the Merkle root hash as a Buffer.
|
||||
|
||||
*__example__*: const root = tree.getRoot()
|
||||
*__example__*:
|
||||
```js
|
||||
const root = tree.getRoot()
|
||||
```
|
||||
|
||||
**Returns:** `any`
|
||||
|
||||
|
@ -348,8 +350,6 @@ ___
|
|||
|
||||
▸ **print**(): `void`
|
||||
|
||||
*Defined in [index.ts:324](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L324)*
|
||||
|
||||
**Returns:** `void`
|
||||
|
||||
___
|
||||
|
@ -359,8 +359,6 @@ ___
|
|||
|
||||
▸ **toString**(): `any`
|
||||
|
||||
*Defined in [index.ts:335](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L335)*
|
||||
|
||||
**Returns:** `any`
|
||||
|
||||
___
|
||||
|
@ -370,8 +368,6 @@ ___
|
|||
|
||||
▸ **toTreeString**(): `any`
|
||||
|
||||
*Defined in [index.ts:329](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L329)*
|
||||
|
||||
**Returns:** `any`
|
||||
|
||||
___
|
||||
|
@ -381,13 +377,16 @@ ___
|
|||
|
||||
▸ **verify**(proof: *`any`*, targetNode: *`any`*, root: *`any`*): `boolean`
|
||||
|
||||
*Defined in [index.ts:258](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L258)*
|
||||
|
||||
verify
|
||||
|
||||
*__desc__*: Returns true if the proof path (array of hashes) can connect the target node to the Merkle root.
|
||||
|
||||
*__example__*: const root = tree.getRoot() const proof = tree.getProof(leaves\[2\]) const verified = tree.verify(proof, leaves\[2\], root)
|
||||
*__example__*:
|
||||
```js
|
||||
const root = tree.getRoot()
|
||||
const proof = tree.getProof(leaves[2])
|
||||
const verified = tree.verify(proof, leaves[2], root)
|
||||
```
|
||||
|
||||
**Parameters:**
|
||||
|
||||
|
@ -406,8 +405,6 @@ ___
|
|||
|
||||
▸ **bufferify**(x: *`any`*): `any`
|
||||
|
||||
*Defined in [index.ts:340](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L340)*
|
||||
|
||||
**Parameters:**
|
||||
|
||||
| Name | Type |
|
||||
|
@ -423,8 +420,6 @@ ___
|
|||
|
||||
▸ **print**(tree: *`any`*): `void`
|
||||
|
||||
*Defined in [index.ts:345](https://github.com/miguelmota/merkletreejs/blob/49ef70e/index.ts#L345)*
|
||||
|
||||
**Parameters:**
|
||||
|
||||
| Name | Type |
|
||||
|
@ -433,7 +428,45 @@ ___
|
|||
|
||||
**Returns:** `void`
|
||||
|
||||
## Interface
|
||||
|
||||
## Options
|
||||
|
||||
### Properties
|
||||
|
||||
* [duplicateOdd](#duplicateodd)
|
||||
* [hashLeaves](#hashleaves)
|
||||
* [isBitcoinTree](#isbitcointree)
|
||||
* [sort](#sort)
|
||||
|
||||
## Properties
|
||||
|
||||
<a id="duplicateodd"></a>
|
||||
|
||||
### duplicateOdd
|
||||
|
||||
**● duplicateOdd**: *`boolean`*
|
||||
|
||||
___
|
||||
<a id="hashleaves"></a>
|
||||
|
||||
### hashLeaves
|
||||
|
||||
**● hashLeaves**: *`boolean`*
|
||||
|
||||
___
|
||||
<a id="isbitcointree"></a>
|
||||
|
||||
### isBitcoinTree
|
||||
|
||||
**● isBitcoinTree**: *`boolean`*
|
||||
|
||||
___
|
||||
<a id="sort"></a>
|
||||
|
||||
### sort
|
||||
|
||||
**● sort**: *`boolean`*
|
||||
|
||||
|
||||
## Test
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
interface Options {
|
||||
duplicateOdd: boolean;
|
||||
hashLeaves: boolean;
|
||||
isBitcoinTree: boolean;
|
||||
sort: boolean;
|
||||
}
|
||||
/**
|
||||
* Class reprensenting a Merkle Tree
|
||||
* @namespace MerkleTree
|
||||
*/
|
||||
export declare class MerkleTree {
|
||||
hashAlgo: any;
|
||||
hashLeaves: boolean;
|
||||
leaves: any;
|
||||
layers: any;
|
||||
isBitcoinTree: boolean;
|
||||
_sort: boolean;
|
||||
duplicateOdd: boolean;
|
||||
hashAlgo: (value: any) => any;
|
||||
hashLeaves: boolean;
|
||||
isBitcoinTree: boolean;
|
||||
leaves: any[];
|
||||
layers: any[];
|
||||
sort: boolean;
|
||||
/**
|
||||
* @desc Constructs a Merkle Tree.
|
||||
* All nodes and leaves are stored as Buffers.
|
||||
|
@ -24,42 +30,50 @@ export declare class MerkleTree {
|
|||
* to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again.
|
||||
* @param {Boolean} options.duplicateOdd - If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash.
|
||||
* @example
|
||||
* const MerkleTree = require('merkletreejs')
|
||||
* const crypto = require('crypto')
|
||||
*```js
|
||||
*const MerkleTree = require('merkletreejs')
|
||||
*const crypto = require('crypto')
|
||||
*
|
||||
* function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
* }
|
||||
*function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
*}
|
||||
*
|
||||
* const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*
|
||||
* const tree = new MerkleTree(leaves, sha256)
|
||||
*const tree = new MerkleTree(leaves, sha256)
|
||||
*```
|
||||
*/
|
||||
constructor(leaves: any, hashAlgorithm: any, options?: any);
|
||||
constructor(leaves: any, hashAlgorithm: any, options?: Options);
|
||||
createHashes(nodes: any): void;
|
||||
/**
|
||||
* getLeaves
|
||||
* @desc Returns array of leaves of Merkle Tree.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const leaves = tree.getLeaves()
|
||||
*```js
|
||||
*const leaves = tree.getLeaves()
|
||||
*```
|
||||
*/
|
||||
getLeaves(): any;
|
||||
getLeaves(): any[];
|
||||
/**
|
||||
* getLayers
|
||||
* @desc Returns array of all layers of Merkle Tree, including leaves and root.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const layers = tree.getLayers()
|
||||
*```js
|
||||
*const layers = tree.getLayers()
|
||||
*```
|
||||
*/
|
||||
getLayers(): any;
|
||||
getLayers(): any[];
|
||||
/**
|
||||
* getRoot
|
||||
* @desc Returns the Merkle root hash as a Buffer.
|
||||
* @return {Buffer}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*```
|
||||
*/
|
||||
getRoot(): any;
|
||||
/**
|
||||
|
@ -70,13 +84,17 @@ export declare class MerkleTree {
|
|||
* Use if there are leaves containing duplicate data in order to distinguish it.
|
||||
* @return {Object[]} - Array of objects containing a position property of type string
|
||||
* with values of 'left' or 'right' and a data property of type Buffer.
|
||||
* @example
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
*@example
|
||||
* ```js
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*```
|
||||
*
|
||||
* @example
|
||||
* const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
* const tree = new MerkleTree(leaves, sha3)
|
||||
* const proof = tree.getProof(leaves[2], 2)
|
||||
*```js
|
||||
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
*const tree = new MerkleTree(leaves, sha3)
|
||||
*const proof = tree.getProof(leaves[2], 2)
|
||||
*```
|
||||
*/
|
||||
getProof(leaf: any, index?: any): any[];
|
||||
/**
|
||||
|
@ -89,10 +107,11 @@ export declare class MerkleTree {
|
|||
* @param {Buffer} root - Merkle root Buffer
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
* const verified = tree.verify(proof, leaves[2], root)
|
||||
*
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*const verified = tree.verify(proof, leaves[2], root)
|
||||
*```
|
||||
*/
|
||||
verify(proof: any, targetNode: any, root: any): boolean;
|
||||
getLayersAsObject(): any;
|
||||
|
|
|
@ -22,23 +22,25 @@ var MerkleTree = /** @class */ (function () {
|
|||
* to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again.
|
||||
* @param {Boolean} options.duplicateOdd - If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash.
|
||||
* @example
|
||||
* const MerkleTree = require('merkletreejs')
|
||||
* const crypto = require('crypto')
|
||||
*```js
|
||||
*const MerkleTree = require('merkletreejs')
|
||||
*const crypto = require('crypto')
|
||||
*
|
||||
* function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
* }
|
||||
*function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
*}
|
||||
*
|
||||
* const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*
|
||||
* const tree = new MerkleTree(leaves, sha256)
|
||||
*const tree = new MerkleTree(leaves, sha256)
|
||||
*```
|
||||
*/
|
||||
function MerkleTree(leaves, hashAlgorithm, options) {
|
||||
if (options === void 0) { options = {}; }
|
||||
this.isBitcoinTree = !!options.isBitcoinTree;
|
||||
this.hashLeaves = !!options.hashLeaves;
|
||||
this._sort = !!options.sort;
|
||||
this.sort = !!options.sort;
|
||||
this.duplicateOdd = !!options.duplicateOdd;
|
||||
this.hashAlgo = bufferifyFn(hashAlgorithm);
|
||||
if (this.hashLeaves) {
|
||||
|
@ -80,14 +82,14 @@ var MerkleTree = /** @class */ (function () {
|
|||
var data = null;
|
||||
if (this.isBitcoinTree) {
|
||||
var combined = [reverse(left), reverse(right)];
|
||||
if (this._sort) {
|
||||
if (this.sort) {
|
||||
combined.sort(Buffer.compare);
|
||||
}
|
||||
data = Buffer.concat(combined);
|
||||
}
|
||||
else {
|
||||
var combined = [left, right];
|
||||
if (this._sort) {
|
||||
if (this.sort) {
|
||||
combined.sort(Buffer.compare);
|
||||
}
|
||||
data = Buffer.concat(combined);
|
||||
|
@ -107,7 +109,9 @@ var MerkleTree = /** @class */ (function () {
|
|||
* @desc Returns array of leaves of Merkle Tree.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const leaves = tree.getLeaves()
|
||||
*```js
|
||||
*const leaves = tree.getLeaves()
|
||||
*```
|
||||
*/
|
||||
MerkleTree.prototype.getLeaves = function () {
|
||||
return this.leaves;
|
||||
|
@ -117,7 +121,9 @@ var MerkleTree = /** @class */ (function () {
|
|||
* @desc Returns array of all layers of Merkle Tree, including leaves and root.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const layers = tree.getLayers()
|
||||
*```js
|
||||
*const layers = tree.getLayers()
|
||||
*```
|
||||
*/
|
||||
MerkleTree.prototype.getLayers = function () {
|
||||
return this.layers;
|
||||
|
@ -127,7 +133,9 @@ var MerkleTree = /** @class */ (function () {
|
|||
* @desc Returns the Merkle root hash as a Buffer.
|
||||
* @return {Buffer}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*```
|
||||
*/
|
||||
MerkleTree.prototype.getRoot = function () {
|
||||
return this.layers[this.layers.length - 1][0] || Buffer.from([]);
|
||||
|
@ -140,13 +148,17 @@ var MerkleTree = /** @class */ (function () {
|
|||
* Use if there are leaves containing duplicate data in order to distinguish it.
|
||||
* @return {Object[]} - Array of objects containing a position property of type string
|
||||
* with values of 'left' or 'right' and a data property of type Buffer.
|
||||
* @example
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
*@example
|
||||
* ```js
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*```
|
||||
*
|
||||
* @example
|
||||
* const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
* const tree = new MerkleTree(leaves, sha3)
|
||||
* const proof = tree.getProof(leaves[2], 2)
|
||||
*```js
|
||||
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
*const tree = new MerkleTree(leaves, sha3)
|
||||
*const proof = tree.getProof(leaves[2], 2)
|
||||
*```
|
||||
*/
|
||||
MerkleTree.prototype.getProof = function (leaf, index) {
|
||||
leaf = bufferify(leaf);
|
||||
|
@ -207,10 +219,11 @@ var MerkleTree = /** @class */ (function () {
|
|||
* @param {Buffer} root - Merkle root Buffer
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
* const verified = tree.verify(proof, leaves[2], root)
|
||||
*
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*const verified = tree.verify(proof, leaves[2], root)
|
||||
*```
|
||||
*/
|
||||
MerkleTree.prototype.verify = function (proof, targetNode, root) {
|
||||
var hash = bufferify(targetNode);
|
||||
|
|
80
index.ts
80
index.ts
|
@ -2,18 +2,25 @@ import * as reverse from 'buffer-reverse'
|
|||
import * as CryptoJS from 'crypto-js'
|
||||
import * as treeify from 'treeify'
|
||||
|
||||
interface Options {
|
||||
duplicateOdd: boolean
|
||||
hashLeaves: boolean
|
||||
isBitcoinTree: boolean
|
||||
sort: boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Class reprensenting a Merkle Tree
|
||||
* @namespace MerkleTree
|
||||
*/
|
||||
export class MerkleTree {
|
||||
hashAlgo: any
|
||||
hashLeaves: boolean
|
||||
leaves: any
|
||||
layers: any
|
||||
isBitcoinTree: boolean
|
||||
_sort: boolean
|
||||
duplicateOdd: boolean
|
||||
hashAlgo: (value:any) => any
|
||||
hashLeaves: boolean
|
||||
isBitcoinTree: boolean
|
||||
leaves: any[]
|
||||
layers: any[]
|
||||
sort: boolean
|
||||
|
||||
/**
|
||||
* @desc Constructs a Merkle Tree.
|
||||
|
@ -29,22 +36,24 @@ export class MerkleTree {
|
|||
* to replicate Bitcoin constructed Merkle Trees. In Bitcoin Merkle Trees, single nodes are combined with themselves, and each output hash is hashed again.
|
||||
* @param {Boolean} options.duplicateOdd - If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash.
|
||||
* @example
|
||||
* const MerkleTree = require('merkletreejs')
|
||||
* const crypto = require('crypto')
|
||||
*```js
|
||||
*const MerkleTree = require('merkletreejs')
|
||||
*const crypto = require('crypto')
|
||||
*
|
||||
* function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
* }
|
||||
*function sha256(data) {
|
||||
* // returns Buffer
|
||||
* return crypto.createHash('sha256').update(data).digest()
|
||||
*}
|
||||
*
|
||||
* const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*const leaves = ['a', 'b', 'c'].map(x => sha3(x))
|
||||
*
|
||||
* const tree = new MerkleTree(leaves, sha256)
|
||||
*const tree = new MerkleTree(leaves, sha256)
|
||||
*```
|
||||
*/
|
||||
constructor(leaves, hashAlgorithm, options={} as any) {
|
||||
constructor(leaves, hashAlgorithm, options:Options={} as any) {
|
||||
this.isBitcoinTree = !!options.isBitcoinTree
|
||||
this.hashLeaves = !!options.hashLeaves
|
||||
this._sort = !!options.sort
|
||||
this.sort = !!options.sort
|
||||
this.duplicateOdd = !!options.duplicateOdd
|
||||
this.hashAlgo = bufferifyFn(hashAlgorithm)
|
||||
if (this.hashLeaves) {
|
||||
|
@ -96,14 +105,14 @@ export class MerkleTree {
|
|||
|
||||
if (this.isBitcoinTree) {
|
||||
let combined = [reverse(left), reverse(right)]
|
||||
if (this._sort) {
|
||||
if (this.sort) {
|
||||
combined.sort(Buffer.compare)
|
||||
}
|
||||
|
||||
data = Buffer.concat(combined)
|
||||
} else {
|
||||
let combined = [left, right]
|
||||
if (this._sort) {
|
||||
if (this.sort) {
|
||||
combined.sort(Buffer.compare)
|
||||
}
|
||||
|
||||
|
@ -129,7 +138,9 @@ export class MerkleTree {
|
|||
* @desc Returns array of leaves of Merkle Tree.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const leaves = tree.getLeaves()
|
||||
*```js
|
||||
*const leaves = tree.getLeaves()
|
||||
*```
|
||||
*/
|
||||
getLeaves() {
|
||||
return this.leaves
|
||||
|
@ -140,7 +151,9 @@ export class MerkleTree {
|
|||
* @desc Returns array of all layers of Merkle Tree, including leaves and root.
|
||||
* @return {Buffer[]}
|
||||
* @example
|
||||
* const layers = tree.getLayers()
|
||||
*```js
|
||||
*const layers = tree.getLayers()
|
||||
*```
|
||||
*/
|
||||
getLayers() {
|
||||
return this.layers
|
||||
|
@ -151,7 +164,9 @@ export class MerkleTree {
|
|||
* @desc Returns the Merkle root hash as a Buffer.
|
||||
* @return {Buffer}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*```
|
||||
*/
|
||||
getRoot() {
|
||||
return this.layers[this.layers.length-1][0] || Buffer.from([])
|
||||
|
@ -165,13 +180,17 @@ export class MerkleTree {
|
|||
* Use if there are leaves containing duplicate data in order to distinguish it.
|
||||
* @return {Object[]} - Array of objects containing a position property of type string
|
||||
* with values of 'left' or 'right' and a data property of type Buffer.
|
||||
* @example
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
*@example
|
||||
* ```js
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*```
|
||||
*
|
||||
* @example
|
||||
* const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
* const tree = new MerkleTree(leaves, sha3)
|
||||
* const proof = tree.getProof(leaves[2], 2)
|
||||
*```js
|
||||
*const leaves = ['a', 'b', 'a'].map(x => sha3(x))
|
||||
*const tree = new MerkleTree(leaves, sha3)
|
||||
*const proof = tree.getProof(leaves[2], 2)
|
||||
*```
|
||||
*/
|
||||
getProof(leaf, index?) {
|
||||
leaf = bufferify(leaf)
|
||||
|
@ -250,10 +269,11 @@ export class MerkleTree {
|
|||
* @param {Buffer} root - Merkle root Buffer
|
||||
* @return {Boolean}
|
||||
* @example
|
||||
* const root = tree.getRoot()
|
||||
* const proof = tree.getProof(leaves[2])
|
||||
* const verified = tree.verify(proof, leaves[2], root)
|
||||
*
|
||||
*```js
|
||||
*const root = tree.getRoot()
|
||||
*const proof = tree.getProof(leaves[2])
|
||||
*const verified = tree.verify(proof, leaves[2], root)
|
||||
*```
|
||||
*/
|
||||
verify(proof, targetNode, root) {
|
||||
let hash = bufferify(targetNode)
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
"scripts": {
|
||||
"test": "tape test/*.js",
|
||||
"build": "rm -rf dist/ && tsc",
|
||||
"docs:md": "typedoc --theme markdown index.ts --out docs"
|
||||
"docs:md": "rm -rf docs/ && typedoc --theme markdown index.ts --out docs --mdEngine github --mdDocusaurus --mdHideSources"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
|
|
|
@ -386,7 +386,7 @@ test('sha256 - no leaves', t => {
|
|||
t.equal(tree.getRoot().toString('hex'), root)
|
||||
})
|
||||
|
||||
test('sha256 - 1,000,000 leaves', t => {
|
||||
test.skip('sha256 - 1,000,000 leaves', t => {
|
||||
t.plan(1)
|
||||
|
||||
const values = []
|
||||
|
|
Loading…
Reference in New Issue