Update docs

This commit is contained in:
Miguel Mota 2021-12-09 20:51:15 -08:00
parent f4be5df429
commit 92ea38768c
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
12 changed files with 2441 additions and 901 deletions

1
.gitignore vendored
View File

@ -110,7 +110,6 @@ example.js
#####################
build
dist
docs
# Files to ignore #
#####################

899
README.md
View File

@ -110,904 +110,7 @@ Output:
## Documentation
<!-- :%s/// -->
<!-- :%s/\[Options\]()/\[Options\](#options) -->
<!-- BEGIN DOCUMENTATION -->
# Class: MerkleTree
Class reprensenting a Merkle Tree
**`namespace`** MerkleTree
## Hierarchy
* **MerkleTree**
## Index
### Constructors
* [constructor](_index_.merkletree.md#constructor)
### Methods
* [bufferToHex](_index_.merkletree.md#buffertohex)
* [bufferify](_index_.merkletree.md#bufferify)
* [getDepth](_index_.merkletree.md#getdepth)
* [getHexLayers](_index_.merkletree.md#gethexlayers)
* [getHexLayersFlat](_index_.merkletree.md#gethexlayersflat)
* [getHexLeaves](_index_.merkletree.md#gethexleaves)
* [getHexMultiProof](_index_.merkletree.md#gethexmultiproof)
* [getHexProof](_index_.merkletree.md#gethexproof)
* [getHexRoot](_index_.merkletree.md#gethexroot)
* [getLayers](_index_.merkletree.md#getlayers)
* [getLayersAsObject](_index_.merkletree.md#getlayersasobject)
* [getLayersFlat](_index_.merkletree.md#getlayersflat)
* [getLeaves](_index_.merkletree.md#getleaves)
* [getMultiProof](_index_.merkletree.md#getmultiproof)
* [getProof](_index_.merkletree.md#getproof)
* [getProofFlags](_index_.merkletree.md#getproofflags)
* [getProofIndices](_index_.merkletree.md#getproofindices)
* [getRoot](_index_.merkletree.md#getroot)
* [print](_index_.merkletree.md#print)
* [toJSON](_index_.merkletree.md#tojson)
* [toString](_index_.merkletree.md#tostring)
* [verify](_index_.merkletree.md#verify)
* [verifyMultiProof](_index_.merkletree.md#verifymultiproof)
* [bufferToHex](_index_.merkletree.md#static-buffertohex)
* [static:bufferify](_index_.merkletree.md#static-bufferify)
* [static:getMultiProof](_index_.merkletree.md#static-getmultiproof)
* [static:isHexString](_index_.merkletree.md#static-ishexstring)
* [static:marshalLeaves](_index_.merkletree.md#static-marshalleaves)
* [static:marshalProof](_index_.merkletree.md#static-marshalproof)
* [static:print](_index_.merkletree.md#static-print)
* [static:unmarshalLeaves](_index_.merkletree.md#static-unmarshalleaves)
* [static:unmarshalProof](_index_.merkletree.md#static-unmarshalproof)
## Constructors
### constructor
\+ **new MerkleTree**(`leaves`: any[], `hashFn`: any, `options`: [Options](#options)): *[MerkleTree](_index_.merkletree.md)*
**`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`**
```js
const MerkleTree = require('merkletreejs')
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
const leaves = ['a', 'b', 'c'].map(x => keccak(x))
const tree = new MerkleTree(leaves, sha256)
```
**Parameters:**
Name | Type | Default | Description |
------ | ------ | ------ | ------ |
`leaves` | any[] | - | Array of hashed leaves. Each leaf must be a Buffer. |
`hashFn` | any | SHA256 | Hash function to use for hashing leaves and nodes |
`options` | [Options](#options) | {} | Additional options |
**Returns:** *[MerkleTree](_index_.merkletree.md)*
## Methods
### bufferToHex
**bufferToHex**(`value`: Buffer): *string*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = tree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | Buffer |
**Returns:** *string*
___
### bufferify
**bufferify**(`value`: any): *Buffer*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = tree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### getDepth
**getDepth**(): *number*
getDepth
**`desc`** Returns the tree depth (number of layers)
**`example`**
```js
const depth = tree.getDepth()
```
**Returns:** *number*
___
### getHexLayers
**getHexLayers**(): *string[]*
getHexLayers
**`desc`** Returns multi-dimensional array of all layers of Merkle Tree, including leaves and root as hex strings.
**`example`**
```js
const layers = tree.getHexLayers()
```
**Returns:** *string[]*
___
### getHexLayersFlat
**getHexLayersFlat**(): *string[]*
getHexLayersFlat
**`desc`** Returns single flat array of all layers of Merkle Tree, including leaves and root as hex string.
**`example`**
```js
const layers = tree.getHexLayersFlat()
```
**Returns:** *string[]*
___
### getHexLeaves
**getHexLeaves**(): *string[]*
getHexLeaves
**`desc`** Returns array of leaves of Merkle Tree as hex strings.
**`example`**
```js
const leaves = tree.getHexLeaves()
```
**Returns:** *string[]*
___
### getHexMultiProof
**getHexMultiProof**(`tree`: Buffer[], `indices`: number[]): *string[]*
getHexMultiProof
**`desc`** Returns the multiproof for given tree indices as hex strings.
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getHexMultiProof(indices)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree` | Buffer[] | - |
`indices` | number[] | Tree indices. |
**Returns:** *string[]*
- Multiproofs as hex strings.
___
### getHexProof
**getHexProof**(`leaf`: Buffer, `index?`: number): *string[]*
getHexProof
**`desc`** Returns the proof for a target leaf as hex strings.
**`example`**
```js
const proof = tree.getHexProof(leaves[2])
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`leaf` | Buffer | Target leaf |
`index?` | number | - |
**Returns:** *string[]*
- Proof array as hex strings.
___
### getPositionalHexProof
**getPositionalHexProof**(`leaf`: Buffer, `index?`: number): *(string | number)[][]*
getPositionalHexProof
**`desc`** Returns the proof for a target leaf as hex strings and corresponding position as binary.
**`example`**
```js
const proof = tree.getPositionalHexProof(leaves[2])
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`leaf` | Buffer | Target leaf |
`index?` | number | - |
**Returns:** *(string | number)[][]*
- Proof array as hex strings with position.
___
### getHexRoot
**getHexRoot**(): *string*
getHexRoot
**`desc`** Returns the Merkle root hash as a hex string.
**`example`**
```js
const root = tree.getHexRoot()
```
**Returns:** *string*
___
### getLayers
**getLayers**(): *Buffer[]*
getLayers
**`desc`** Returns multi-dimensional array of all layers of Merkle Tree, including leaves and root.
**`example`**
```js
const layers = tree.getLayers()
```
**Returns:** *Buffer[]*
___
### getLayersAsObject
**getLayersAsObject**(): *any*
getLayersAsObject
**`desc`** Returns the layers as nested objects instead of an array.
**`example`**
```js
const layersObj = tree.getLayersAsObject()
```
**Returns:** *any*
___
### getLayersFlat
**getLayersFlat**(): *Buffer[]*
getLayersFlat
**`desc`** Returns single flat array of all layers of Merkle Tree, including leaves and root.
**`example`**
```js
const layers = tree.getLayersFlat()
```
**Returns:** *Buffer[]*
___
### getLeaves
**getLeaves**(`values?`: any[]): *Buffer[]*
getLeaves
**`desc`** Returns array of leaves of Merkle Tree.
**`example`**
```js
const leaves = tree.getLeaves()
```
**Parameters:**
Name | Type |
------ | ------ |
`values?` | any[] |
**Returns:** *Buffer[]*
___
### getMultiProof
**getMultiProof**(`tree?`: any[], `indices?`: any[]): *Buffer[]*
getMultiProof
**`desc`** Returns the multiproof for given tree indices.
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree?` | any[] | - |
`indices?` | any[] | Tree indices. |
**Returns:** *Buffer[]*
- Multiproofs
___
### getProof
**getProof**(`leaf`: Buffer, `index?`: number): *any[]*
getProof
**`desc`** Returns the proof for a target leaf.
**`example`**
```js
const proof = tree.getProof(leaves[2])
```
**`example`**
```js
const leaves = ['a', 'b', 'a'].map(x => keccak(x))
const tree = new MerkleTree(leaves, keccak)
const proof = tree.getProof(leaves[2], 2)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`leaf` | Buffer | Target leaf |
`index?` | number | - |
**Returns:** *any[]*
- Array of objects containing a position property of type string
with values of 'left' or 'right' and a data property of type Buffer.
___
### getProofFlags
**getProofFlags**(`leaves`: Buffer[], `proofs`: Buffer[]): *boolean[]*
getProofFlags
**`desc`** Returns list of booleans where proofs should be used instead of hashing.
Proof flags are used in the Solidity multiproof verifiers.
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
const proofFlags = tree.getProofFlags(leaves, proof)
```
**Parameters:**
Name | Type |
------ | ------ |
`leaves` | Buffer[] |
`proofs` | Buffer[] |
**Returns:** *boolean[]*
- Boolean flags
___
### getProofIndices
**getProofIndices**(`treeIndices`: number[], `depth`: number): *number[]*
getProofIndices
**`desc`** Returns the proof indices for given tree indices.
**`example`**
```js
const proofIndices = tree.getProofIndices([2,5,6], 4)
console.log(proofIndices) // [ 23, 20, 19, 8, 3 ]
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`treeIndices` | number[] | Tree indices |
`depth` | number | Tree depth; number of layers. |
**Returns:** *number[]*
- Proof indices
___
### getRoot
**getRoot**(): *Buffer*
getRoot
**`desc`** Returns the Merkle root hash as a Buffer.
**`example`**
```js
const root = tree.getRoot()
```
**Returns:** *Buffer*
___
### print
**print**(): *void*
print
**`desc`** Prints out a visual representation of the merkle tree.
**`example`**
```js
tree.print()
```
**Returns:** *void*
___
### toJSON
**toJSON**(): *void*
**Returns:** *void*
___
### toString
**toString**(): *string*
toString
**`desc`** Returns a visual representation of the merkle tree as a string.
**`example`**
```js
console.log(tree.toString())
```
**Returns:** *string*
___
### verify
**verify**(`proof`: any[], `targetNode`: Buffer, `root`: Buffer): *boolean*
verify
**`desc`** Returns true if the proof path (array of hashes) can connect the target node
to the Merkle root.
**`example`**
```js
const root = tree.getRoot()
const proof = tree.getProof(leaves[2])
const verified = tree.verify(proof, leaves[2], root)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`proof` | any[] | Array of proof objects that should connect target node to Merkle root. |
`targetNode` | Buffer | Target node Buffer |
`root` | Buffer | Merkle root Buffer |
**Returns:** *boolean*
___
### verifyMultiProof
**verifyMultiProof**(`root`: Buffer, `indices`: number[], `leaves`: Buffer[], `depth`: number, `proof`: Buffer[]): *boolean*
verifyMultiProof
**`desc`** Returns true if the multiproofs can connect the leaves to the Merkle root.
**`example`**
```js
const root = tree.getRoot()
const treeFlat = tree.getLayersFlat()
const depth = tree.getDepth()
const indices = [2, 5, 6]
const proofLeaves = indices.map(i => leaves[i])
const proof = tree.getMultiProof(treeFlat, indices)
const verified = tree.verifyMultiProof(root, indices, proofLeaves, depth, proof)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`root` | Buffer | Merkle tree root |
`indices` | number[] | Leave indices |
`leaves` | Buffer[] | Leaf values at indices. |
`depth` | number | Tree depth |
`proof` | Buffer[] | Multiproofs given indices |
**Returns:** *boolean*
___
### `Static` bufferToHex
**bufferToHex**(`value`: Buffer): *string*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | Buffer |
**Returns:** *string*
___
### `Static` bufferify
**bufferify**(`value`: any): *Buffer*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = MerkleTree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### `Static` getMultiProof
**getMultiProof**(`tree`: Buffer[], `indices`: number[]): *Buffer[]*
getMultiProof
**`desc`** Returns the multiproof for given tree indices.
**`example`**
```js
const flatTree = tree.getLayersFlat()
const indices = [2, 5, 6]
const proof = MerkleTree.getMultiProof(flatTree, indices)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree` | Buffer[] | Tree as a flat array. |
`indices` | number[] | Tree indices. |
**Returns:** *Buffer[]*
- Multiproofs
___
### `Static` isHexString
**isHexString**(`v`: string): *boolean*
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
**Parameters:**
Name | Type |
------ | ------ |
`v` | string |
**Returns:** *boolean*
___
### `Static` marshalLeaves
**marshalLeaves**(`leaves`: any[]): *string*
marshalLeaves
**`desc`** Returns array of leaves of Merkle Tree as a JSON string.
**`example`**
```js
const jsonStr = MerkleTree.marshalLeaves(leaves)
```
**Parameters:**
Name | Type |
------ | ------ |
`leaves` | any[] |
**Returns:** *string*
- List of leaves as JSON string
___
### `Static` marshalProof
**marshalProof**(`proof`: any[]): *string*
marshalProof
**`desc`** Returns proof array as JSON string.
**`example`**
```js
const jsonStr = MerkleTree.marshalProof(proof)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`proof` | any[] | Merkle tree proof array |
**Returns:** *string*
- Proof array as JSON string.
___
### `Static` print
**print**(`tree`: any): *void*
print
**`desc`** Prints out a visual representation of the given merkle tree.
**`example`**
```js
MerkleTree.print(tree)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree` | any | Merkle tree instance. |
**Returns:** *void*
___
### `Static` unmarshalLeaves
**unmarshalLeaves**(`jsonStr`: string | object): *Buffer[]*
unmarshalLeaves
**`desc`** Returns array of leaves of Merkle Tree as a Buffers.
**`example`**
```js
const leaves = MerkleTree.unmarshalLeaves(jsonStr)
```
**Parameters:**
Name | Type |
------ | ------ |
`jsonStr` | string &#124; object |
**Returns:** *Buffer[]*
- Unmarshalled list of leaves
___
### `Static` unmarshalProof
**unmarshalProof**(`jsonStr`: string | object): *any[]*
unmarshalProof
**`desc`** Returns the proof for a target leaf as a list of Buffers.
**`example`**
```js
const proof = MerkleTree.unmarshalProof(jsonStr)
```
**Parameters:**
Name | Type |
------ | ------ |
`jsonStr` | string &#124; object |
**Returns:** *any[]*
- Marshalled proof
# Interface: Options
## Hierarchy
* **Options**
## Index
### Properties
* [duplicateOdd](_index_.options.md#optional-duplicateodd)
* [hashLeaves](_index_.options.md#optional-hashleaves)
* [isBitcoinTree](_index_.options.md#optional-isbitcointree)
* [sort](_index_.options.md#optional-sort)
* [sortLeaves](_index_.options.md#optional-sortleaves)
* [sortPairs](_index_.options.md#optional-sortpairs)
## Properties
### `Optional` duplicateOdd
**duplicateOdd**? : *boolean*
If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash.
___
### `Optional` hashLeaves
**hashLeaves**? : *boolean*
If set to `true`, the leaves will hashed using the set hashing functions.
___
### `Optional` isBitcoinTree
**isBitcoinTree**? : *boolean*
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.
___
### `Optional` sort
**sort**? : *boolean*
If set to `true`, the leaves and hashing pairs will be sorted.
___
### `Optional` sortLeaves
**sortLeaves**? : *boolean*
If set to `true`, the leaves will be sorted.
___
### `Optional` sortPairs
**sortPairs**? : *boolean*
If set to `true`, the hashing pairs will be sorted.
<!-- END DOCUMENTATION -->
See [documentation](docs/classes/_src_merkletree_.merkletree.md) (under [docs/](docs/))
## Test

View File

@ -0,0 +1,300 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/Base"](../modules/_src_base_.md) [Base](_src_base_.base.md)
# Class: Base
## Hierarchy
* **Base**
↳ [MerkleMountainRange](_src_merklemountainrange_.merklemountainrange.md)
↳ [MerkleTree](_src_merkletree_.merkletree.md)
## Index
### Methods
* [_bufferIndexOf](_src_base_.base.md#protected-_bufferindexof)
* [_bufferifyFn](_src_base_.base.md#protected-_bufferifyfn)
* [_isHexString](_src_base_.base.md#protected-_ishexstring)
* [_log2](_src_base_.base.md#protected-_log2)
* [_zip](_src_base_.base.md#protected-_zip)
* [bufferToHex](_src_base_.base.md#buffertohex)
* [bufferify](_src_base_.base.md#bufferify)
* [print](_src_base_.base.md#print)
* [bufferToHex](_src_base_.base.md#static-buffertohex)
* [bufferify](_src_base_.base.md#static-bufferify)
* [isHexString](_src_base_.base.md#static-ishexstring)
* [print](_src_base_.base.md#static-print)
## Methods
### `Protected` _bufferIndexOf
**_bufferIndexOf**(`array`: Buffer[], `element`: Buffer): *number*
bufferIndexOf
**`desc`** Returns the first index of which given buffer is found in array.
**`example`**
```js
const index = tree.bufferIndexOf(haystack, needle)
```
**Parameters:**
Name | Type |
------ | ------ |
`array` | Buffer[] |
`element` | Buffer |
**Returns:** *number*
- Index number
___
### `Protected` _bufferifyFn
**_bufferifyFn**(`f`: any): *any*
bufferifyFn
**`desc`** Returns a function that will bufferify the return value.
**`example`**
```js
const fn = tree.bufferifyFn((value) => sha256(value))
```
**Parameters:**
Name | Type |
------ | ------ |
`f` | any |
**Returns:** *any*
___
### `Protected` _isHexString
**_isHexString**(`value`: string): *boolean*
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | string |
**Returns:** *boolean*
___
### `Protected` _log2
**_log2**(`n`: number): *number*
log2
**`desc`** Returns the log2 of number.
**Parameters:**
Name | Type |
------ | ------ |
`n` | number |
**Returns:** *number*
___
### `Protected` _zip
**_zip**(`a`: any[], `b`: any[]): *any[][]*
zip
**`desc`** Returns true if value is a hex string.
**`example`**
```js
const zipped = tree.zip(['a', 'b'],['A', 'B'])
console.log(zipped) // [ [ 'a', 'A' ], [ 'b', 'B' ] ]
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`a` | any[] | first array |
`b` | any[] | second array |
**Returns:** *any[][]*
___
### bufferToHex
**bufferToHex**(`value`: Buffer, `withPrefix`: boolean): *string*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = tree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type | Default |
------ | ------ | ------ |
`value` | Buffer | - |
`withPrefix` | boolean | true |
**Returns:** *string*
___
### bufferify
**bufferify**(`value`: any): *Buffer*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = tree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### print
**print**(): *void*
print
**`desc`** Prints out a visual representation of the merkle tree.
**`example`**
```js
tree.print()
```
**Returns:** *void*
___
### `Static` bufferToHex
**bufferToHex**(`value`: Buffer, `withPrefix`: boolean): *string*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type | Default |
------ | ------ | ------ |
`value` | Buffer | - |
`withPrefix` | boolean | true |
**Returns:** *string*
___
### `Static` bufferify
**bufferify**(`value`: any): *Buffer*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = MerkleTree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### `Static` isHexString
**isHexString**(`v`: string): *boolean*
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
**Parameters:**
Name | Type |
------ | ------ |
`v` | string |
**Returns:** *boolean*
___
### `Static` print
**print**(`tree`: any): *void*
print
**`desc`** Prints out a visual representation of the given merkle tree.
**`example`**
```js
MerkleTree.print(tree)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree` | any | Merkle tree instance. |
**Returns:** *void*

View File

@ -0,0 +1,771 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/MerkleMountainRange"](../modules/_src_merklemountainrange_.md) [MerkleMountainRange](_src_merklemountainrange_.merklemountainrange.md)
# Class: MerkleMountainRange
**`desc`** The index of this MMR implementation starts from 1 not 0.
## Hierarchy
* [Base](_src_base_.base.md)
↳ **MerkleMountainRange**
## Index
### Constructors
* [constructor](_src_merklemountainrange_.merklemountainrange.md#constructor)
### Properties
* [data](_src_merklemountainrange_.merklemountainrange.md#data)
* [hashBranchFn](_src_merklemountainrange_.merklemountainrange.md#hashbranchfn)
* [hashLeafFn](_src_merklemountainrange_.merklemountainrange.md#hashleaffn)
* [hashes](_src_merklemountainrange_.merklemountainrange.md#hashes)
* [peakBaggingFn](_src_merklemountainrange_.merklemountainrange.md#peakbaggingfn)
* [root](_src_merklemountainrange_.merklemountainrange.md#root)
* [size](_src_merklemountainrange_.merklemountainrange.md#size)
* [width](_src_merklemountainrange_.merklemountainrange.md#width)
### Methods
* [_bufferIndexOf](_src_merklemountainrange_.merklemountainrange.md#protected-_bufferindexof)
* [_bufferifyFn](_src_merklemountainrange_.merklemountainrange.md#protected-_bufferifyfn)
* [_isHexString](_src_merklemountainrange_.merklemountainrange.md#protected-_ishexstring)
* [_log2](_src_merklemountainrange_.merklemountainrange.md#protected-_log2)
* [_zip](_src_merklemountainrange_.merklemountainrange.md#protected-_zip)
* [append](_src_merklemountainrange_.merklemountainrange.md#append)
* [bufferToHex](_src_merklemountainrange_.merklemountainrange.md#buffertohex)
* [bufferify](_src_merklemountainrange_.merklemountainrange.md#bufferify)
* [getChildren](_src_merklemountainrange_.merklemountainrange.md#getchildren)
* [getHexRoot](_src_merklemountainrange_.merklemountainrange.md#gethexroot)
* [getLeafIndex](_src_merklemountainrange_.merklemountainrange.md#getleafindex)
* [getMerkleProof](_src_merklemountainrange_.merklemountainrange.md#getmerkleproof)
* [getNode](_src_merklemountainrange_.merklemountainrange.md#getnode)
* [getPeakIndexes](_src_merklemountainrange_.merklemountainrange.md#getpeakindexes)
* [getPeaks](_src_merklemountainrange_.merklemountainrange.md#getpeaks)
* [getRoot](_src_merklemountainrange_.merklemountainrange.md#getroot)
* [getSize](_src_merklemountainrange_.merklemountainrange.md#getsize)
* [hashBranch](_src_merklemountainrange_.merklemountainrange.md#hashbranch)
* [hashLeaf](_src_merklemountainrange_.merklemountainrange.md#hashleaf)
* [heightAt](_src_merklemountainrange_.merklemountainrange.md#heightat)
* [isLeaf](_src_merklemountainrange_.merklemountainrange.md#isleaf)
* [mountainHeight](_src_merklemountainrange_.merklemountainrange.md#mountainheight)
* [numOfPeaks](_src_merklemountainrange_.merklemountainrange.md#numofpeaks)
* [peakBagging](_src_merklemountainrange_.merklemountainrange.md#peakbagging)
* [peakMapToPeaks](_src_merklemountainrange_.merklemountainrange.md#peakmaptopeaks)
* [peakUpdate](_src_merklemountainrange_.merklemountainrange.md#peakupdate)
* [peaksToPeakMap](_src_merklemountainrange_.merklemountainrange.md#peakstopeakmap)
* [print](_src_merklemountainrange_.merklemountainrange.md#print)
* [rollUp](_src_merklemountainrange_.merklemountainrange.md#rollup)
* [verify](_src_merklemountainrange_.merklemountainrange.md#verify)
* [bufferToHex](_src_merklemountainrange_.merklemountainrange.md#static-buffertohex)
* [bufferify](_src_merklemountainrange_.merklemountainrange.md#static-bufferify)
* [isHexString](_src_merklemountainrange_.merklemountainrange.md#static-ishexstring)
* [print](_src_merklemountainrange_.merklemountainrange.md#static-print)
## Constructors
### constructor
\+ **new MerkleMountainRange**(`hashFn`: any, `leaves`: any[], `hashLeafFn?`: any, `peakBaggingFn?`: any, `hashBranchFn?`: any): *[MerkleMountainRange](_src_merklemountainrange_.merklemountainrange.md)*
**Parameters:**
Name | Type | Default |
------ | ------ | ------ |
`hashFn` | any | SHA256 |
`leaves` | any[] | [] |
`hashLeafFn?` | any | - |
`peakBaggingFn?` | any | - |
`hashBranchFn?` | any | - |
**Returns:** *[MerkleMountainRange](_src_merklemountainrange_.merklemountainrange.md)*
## Properties
### data
**data**: *any*
___
### hashBranchFn
**hashBranchFn**: *any*
___
### hashLeafFn
**hashLeafFn**: *any*
___
### hashes
**hashes**: *any*
___
### peakBaggingFn
**peakBaggingFn**: *any*
___
### root
**root**: *Buffer* = Buffer.alloc(0)
___
### size
**size**: *number* = 0
___
### width
**width**: *number* = 0
## Methods
### `Protected` _bufferIndexOf
**_bufferIndexOf**(`array`: Buffer[], `element`: Buffer): *number*
*Inherited from [Base](_src_base_.base.md).[_bufferIndexOf](_src_base_.base.md#protected-_bufferindexof)*
bufferIndexOf
**`desc`** Returns the first index of which given buffer is found in array.
**`example`**
```js
const index = tree.bufferIndexOf(haystack, needle)
```
**Parameters:**
Name | Type |
------ | ------ |
`array` | Buffer[] |
`element` | Buffer |
**Returns:** *number*
- Index number
___
### `Protected` _bufferifyFn
**_bufferifyFn**(`f`: any): *any*
*Inherited from [Base](_src_base_.base.md).[_bufferifyFn](_src_base_.base.md#protected-_bufferifyfn)*
bufferifyFn
**`desc`** Returns a function that will bufferify the return value.
**`example`**
```js
const fn = tree.bufferifyFn((value) => sha256(value))
```
**Parameters:**
Name | Type |
------ | ------ |
`f` | any |
**Returns:** *any*
___
### `Protected` _isHexString
**_isHexString**(`value`: string): *boolean*
*Inherited from [Base](_src_base_.base.md).[_isHexString](_src_base_.base.md#protected-_ishexstring)*
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | string |
**Returns:** *boolean*
___
### `Protected` _log2
**_log2**(`n`: number): *number*
*Inherited from [Base](_src_base_.base.md).[_log2](_src_base_.base.md#protected-_log2)*
log2
**`desc`** Returns the log2 of number.
**Parameters:**
Name | Type |
------ | ------ |
`n` | number |
**Returns:** *number*
___
### `Protected` _zip
**_zip**(`a`: any[], `b`: any[]): *any[][]*
*Inherited from [Base](_src_base_.base.md).[_zip](_src_base_.base.md#protected-_zip)*
zip
**`desc`** Returns true if value is a hex string.
**`example`**
```js
const zipped = tree.zip(['a', 'b'],['A', 'B'])
console.log(zipped) // [ [ 'a', 'A' ], [ 'b', 'B' ] ]
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`a` | any[] | first array |
`b` | any[] | second array |
**Returns:** *any[][]*
___
### append
**append**(`data`: Buffer | string): *void*
**`desc`** This only stores the hashed value of the leaf.
If you need to retrieve the detail data later, use a map to store them.
**Parameters:**
Name | Type |
------ | ------ |
`data` | Buffer &#124; string |
**Returns:** *void*
___
### bufferToHex
**bufferToHex**(`value`: Buffer, `withPrefix`: boolean): *string*
*Inherited from [Base](_src_base_.base.md).[bufferToHex](_src_base_.base.md#buffertohex)*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = tree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type | Default |
------ | ------ | ------ |
`value` | Buffer | - |
`withPrefix` | boolean | true |
**Returns:** *string*
___
### bufferify
**bufferify**(`value`: any): *Buffer*
*Inherited from [Base](_src_base_.base.md).[bufferify](_src_base_.base.md#static-bufferify)*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = tree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### getChildren
**getChildren**(`index`: number): *number[]*
**`desc`** It returns the children when it is a parent node.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
**Returns:** *number[]*
___
### getHexRoot
**getHexRoot**(): *any*
**Returns:** *any*
___
### getLeafIndex
**getLeafIndex**(`width`: number): *number*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
**Returns:** *number*
___
### getMerkleProof
**getMerkleProof**(`index`: number): *object*
**`desc`** It returns a merkle proof for a leaf. Note that the index starts from 1.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
**Returns:** *object*
* **peakBagging**: *any[]*
* **root**: *Buffer*
* **siblings**: *any[]*
* **width**: *number*
___
### getNode
**getNode**(`index`: number): *any*
**`dev`** It returns the hash value of a node for the given position. Note that the index starts from 1.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
**Returns:** *any*
___
### getPeakIndexes
**getPeakIndexes**(`width`: number): *number[]*
**`desc`** It returns all peaks of the smallest merkle mountain range tree which includes
the given index(size).
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
**Returns:** *number[]*
___
### getPeaks
**getPeaks**(): *any[]*
**Returns:** *any[]*
___
### getRoot
**getRoot**(): *any*
**`desc`** It returns the root value of the tree.
**Returns:** *any*
___
### getSize
**getSize**(`width`: number): *number*
**`desc`** It returns the size of the tree.
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
**Returns:** *number*
___
### hashBranch
**hashBranch**(`index`: number, `left`: any, `right`: any): *any*
**`desc`** It returns the hash a parent node with hash(M | Left child | Right child)
M is the index of the node.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
`left` | any |
`right` | any |
**Returns:** *any*
___
### hashLeaf
**hashLeaf**(`index`: number, `dataHash`: Buffer | string): *any*
**`desc`** It returns the hash of a leaf node with hash(M | DATA )
M is the index of the node.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
`dataHash` | Buffer &#124; string |
**Returns:** *any*
___
### heightAt
**heightAt**(`index`: number): *number*
**`desc`** It returns the height of the index.
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
**Returns:** *number*
___
### isLeaf
**isLeaf**(`index`: number): *boolean*
**`desc`** It returns whether the index is the leaf node or not
**Parameters:**
Name | Type |
------ | ------ |
`index` | number |
**Returns:** *boolean*
___
### mountainHeight
**mountainHeight**(`size`: number): *number*
**`desc`** It returns the height of the highest peak.
**Parameters:**
Name | Type |
------ | ------ |
`size` | number |
**Returns:** *number*
___
### numOfPeaks
**numOfPeaks**(`width`: number): *number*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
**Returns:** *number*
___
### peakBagging
**peakBagging**(`width`: number, `peaks`: any[]): *any*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
`peaks` | any[] |
**Returns:** *any*
___
### peakMapToPeaks
**peakMapToPeaks**(`width`: number, `peakMap`: any): *any[]*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
`peakMap` | any |
**Returns:** *any[]*
___
### peakUpdate
**peakUpdate**(`width`: number, `prevPeakMap`: any, `itemHash`: any): *object*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
`prevPeakMap` | any |
`itemHash` | any |
**Returns:** *object*
___
### peaksToPeakMap
**peaksToPeakMap**(`width`: number, `peaks`: any[]): *object*
**Parameters:**
Name | Type |
------ | ------ |
`width` | number |
`peaks` | any[] |
**Returns:** *object*
___
### print
**print**(): *void*
*Inherited from [Base](_src_base_.base.md).[print](_src_base_.base.md#print)*
print
**`desc`** Prints out a visual representation of the merkle tree.
**`example`**
```js
tree.print()
```
**Returns:** *void*
___
### rollUp
**rollUp**(`root`: any, `width`: number, `peaks`: any[], `itemHashes`: any[]): *any*
**Parameters:**
Name | Type |
------ | ------ |
`root` | any |
`width` | number |
`peaks` | any[] |
`itemHashes` | any[] |
**Returns:** *any*
___
### verify
**verify**(`root`: any, `width`: number, `index`: number, `value`: Buffer | string, `peaks`: any[], `siblings`: any[]): *boolean*
**`desc`** It returns true when the given params verifies that the given value exists in the tree or reverts the transaction.
**Parameters:**
Name | Type |
------ | ------ |
`root` | any |
`width` | number |
`index` | number |
`value` | Buffer &#124; string |
`peaks` | any[] |
`siblings` | any[] |
**Returns:** *boolean*
___
### `Static` bufferToHex
**bufferToHex**(`value`: Buffer, `withPrefix`: boolean): *string*
*Inherited from [Base](_src_base_.base.md).[bufferToHex](_src_base_.base.md#buffertohex)*
bufferToHex
**`desc`** Returns a hex string with 0x prefix for given buffer.
**`example`**
```js
const hexStr = MerkleTree.bufferToHex(Buffer.from('A'))
```
**Parameters:**
Name | Type | Default |
------ | ------ | ------ |
`value` | Buffer | - |
`withPrefix` | boolean | true |
**Returns:** *string*
___
### `Static` bufferify
**bufferify**(`value`: any): *Buffer*
*Inherited from [Base](_src_base_.base.md).[bufferify](_src_base_.base.md#static-bufferify)*
bufferify
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = MerkleTree.bufferify('0x1234')
```
**Parameters:**
Name | Type |
------ | ------ |
`value` | any |
**Returns:** *Buffer*
___
### `Static` isHexString
**isHexString**(`v`: string): *boolean*
*Inherited from [Base](_src_base_.base.md).[isHexString](_src_base_.base.md#static-ishexstring)*
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
**Parameters:**
Name | Type |
------ | ------ |
`v` | string |
**Returns:** *boolean*
___
### `Static` print
**print**(`tree`: any): *void*
*Inherited from [Base](_src_base_.base.md).[print](_src_base_.base.md#print)*
print
**`desc`** Prints out a visual representation of the given merkle tree.
**`example`**
```js
MerkleTree.print(tree)
```
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
`tree` | any | Merkle tree instance. |
**Returns:** *void*

File diff suppressed because it is too large Load Diff

12
docs/globals.md Normal file
View File

@ -0,0 +1,12 @@
[merkletreejs](README.md) [Globals](globals.md)
# merkletreejs
## Index
### Modules
* ["src/Base"](modules/_src_base_.md)
* ["src/MerkleMountainRange"](modules/_src_merklemountainrange_.md)
* ["src/MerkleTree"](modules/_src_merkletree_.md)
* ["src/index"](modules/_src_index_.md)

View File

@ -0,0 +1,75 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/MerkleTree"](../modules/_src_merkletree_.md) [Options](_src_merkletree_.options.md)
# Interface: Options
## Hierarchy
* **Options**
## Index
### Properties
* [duplicateOdd](_src_merkletree_.options.md#optional-duplicateodd)
* [fillDefaultHash](_src_merkletree_.options.md#optional-filldefaulthash)
* [hashLeaves](_src_merkletree_.options.md#optional-hashleaves)
* [isBitcoinTree](_src_merkletree_.options.md#optional-isbitcointree)
* [sort](_src_merkletree_.options.md#optional-sort)
* [sortLeaves](_src_merkletree_.options.md#optional-sortleaves)
* [sortPairs](_src_merkletree_.options.md#optional-sortpairs)
## Properties
### `Optional` duplicateOdd
**duplicateOdd**? : *boolean*
If set to `true`, an odd node will be duplicated and combined to make a pair to generate the layer hash.
___
### `Optional` fillDefaultHash
**fillDefaultHash**? : *[TFillDefaultHash](../modules/_src_merkletree_.md#tfilldefaulthash) | Buffer | string*
If defined, the resulting hash of this function will be used to fill in odd numbered layers.
___
### `Optional` hashLeaves
**hashLeaves**? : *boolean*
If set to `true`, the leaves will hashed using the set hashing algorithms.
___
### `Optional` isBitcoinTree
**isBitcoinTree**? : *boolean*
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.
___
### `Optional` sort
**sort**? : *boolean*
If set to `true`, the leaves and hashing pairs will be sorted.
___
### `Optional` sortLeaves
**sortLeaves**? : *boolean*
If set to `true`, the leaves will be sorted.
___
### `Optional` sortPairs
**sortPairs**? : *boolean*
If set to `true`, the hashing pairs will be sorted.

View File

@ -0,0 +1,9 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/Base"](_src_base_.md)
# Module: "src/Base"
## Index
### Classes
* [Base](../classes/_src_base_.base.md)

View File

@ -0,0 +1,15 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/index"](_src_index_.md)
# Module: "src/index"
## Index
### References
* [MerkleMountainRange](_src_index_.md#merklemountainrange)
## References
### MerkleMountainRange
**MerkleMountainRange**:

View File

@ -0,0 +1,9 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/MerkleMountainRange"](_src_merklemountainrange_.md)
# Module: "src/MerkleMountainRange"
## Index
### Classes
* [MerkleMountainRange](../classes/_src_merklemountainrange_.merklemountainrange.md)

View File

@ -0,0 +1,79 @@
[merkletreejs](../README.md) [Globals](../globals.md) ["src/MerkleTree"](_src_merkletree_.md)
# Module: "src/MerkleTree"
## Index
### Classes
* [MerkleTree](../classes/_src_merkletree_.merkletree.md)
### Interfaces
* [Options](../interfaces/_src_merkletree_.options.md)
### Type aliases
* [TFillDefaultHash](_src_merkletree_.md#tfilldefaulthash)
* [THashFn](_src_merkletree_.md#thashfn)
* [THashFnResult](_src_merkletree_.md#thashfnresult)
* [TLayer](_src_merkletree_.md#tlayer)
* [TLeaf](_src_merkletree_.md#tleaf)
* [TValue](_src_merkletree_.md#tvalue)
## Type aliases
### TFillDefaultHash
Ƭ **TFillDefaultHash**: *function*
#### Type declaration:
▸ (`idx?`: number, `hashFn?`: [THashFn](_src_merkletree_.md#thashfn)): *[THashFnResult](_src_merkletree_.md#thashfnresult)*
**Parameters:**
Name | Type |
------ | ------ |
`idx?` | number |
`hashFn?` | [THashFn](_src_merkletree_.md#thashfn) |
___
### THashFn
Ƭ **THashFn**: *function*
#### Type declaration:
▸ (`value`: [TValue](_src_merkletree_.md#tvalue)): *Buffer*
**Parameters:**
Name | Type |
------ | ------ |
`value` | [TValue](_src_merkletree_.md#tvalue) |
___
### THashFnResult
Ƭ **THashFnResult**: *Buffer | string*
___
### TLayer
Ƭ **TLayer**: *any*
___
### TLeaf
Ƭ **TLeaf**: *Buffer*
___
### TValue
Ƭ **TValue**: *Buffer | string | number | null | undefined*

View File

@ -1,6 +1,6 @@
{
"name": "merkletreejs",
"version": "0.2.25",
"version": "0.2.26",
"description": "Construct Merkle Trees and verify proofs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
@ -16,7 +16,7 @@
"build:browser": "browserify -t [ babelify --presets [ @babel/preset-env ] ] dist/index.js | uglifyjs > merkletree.js",
"lint": "standardx --fix src/*.ts test/*.js",
"lint:example": "standardx --fix example/*.js",
"docs": "rimraf docs/ && typedoc --plugin typedoc-plugin-markdown --hideSources --theme markdown --hideGenerator --excludeExternals --excludePrivate --out docs index.ts",
"docs": "rimraf docs/ && typedoc --plugin typedoc-plugin-markdown --hideSources --theme markdown --hideGenerator --excludeExternals --excludePrivate --out docs src/",
"prepare": "npm run lint && npm run build"
},
"repository": {