2019-01-20 07:26:13 +08:00
< h3 align = "center" >
2018-05-02 14:52:11 +08:00
< br / >
2020-06-07 03:07:23 +08:00
< img src = "https://user-images.githubusercontent.com/168240/83951171-85f48c80-a7e4-11ea-896e-529c28ffa18e.png" alt = "merkletree.js logo" width = "600" / >
2018-05-02 14:52:11 +08:00
< br / >
< br / >
< br / >
2019-01-20 07:26:13 +08:00
< / h3 >
# MerkleTree.js
2017-07-22 15:31:30 +08:00
2017-08-08 11:29:54 +08:00
> Construct [Merkle Trees](https://en.wikipedia.org/wiki/Merkle_tree) and verify proofs in JavaScript.
2017-07-22 15:31:30 +08:00
2019-12-19 09:30:24 +08:00
[![License ](http://img.shields.io/badge/license-MIT-blue.svg )](https://raw.githubusercontent.com/miguelmota/merkletreejs/master/LICENSE)
[![Build Status ](https://travis-ci.org/miguelmota/merkletreejs.svg?branch=master )](https://travis-ci.org/miguelmota/merkletreejs)
[![dependencies Status ](https://david-dm.org/miguelmota/merkletreejs/status.svg )](https://david-dm.org/miguelmota/merkletreejs)
[![NPM version ](https://badge.fury.io/js/merkletreejs.svg )](http://badge.fury.io/js/merkletreejs)
[![PRs Welcome ](https://img.shields.io/badge/PRs-welcome-brightgreen.svg )](#contributing)
2019-01-20 07:43:57 +08:00
2019-01-20 07:26:13 +08:00
## Contents
2018-10-23 14:38:33 +08:00
- [Install ](#install )
2018-12-10 11:10:43 +08:00
- [Getting started ](#Getting-started )
2020-06-07 03:07:23 +08:00
- [Diagrams ](#diagrams )
2018-10-23 14:40:56 +08:00
- [Documentation ](#documentation )
2018-10-23 14:38:33 +08:00
- [Test ](#test )
- [FAQ ](#faq )
- [Notes ](#notes )
- [Resources ](#resources )
2019-12-19 09:30:24 +08:00
- [Contributing ](#contributing )
2018-10-23 14:38:33 +08:00
- [License ](#license )
2018-10-23 14:40:56 +08:00
## Install
2017-07-22 15:31:30 +08:00
2020-06-07 04:52:26 +08:00
From [NPM ](https://www.npmjs.com/package/merkletreejs ):
2017-07-22 15:31:30 +08:00
```bash
2018-07-10 01:25:43 +08:00
npm install merkletreejs
2017-07-22 15:31:30 +08:00
```
2020-06-07 04:52:26 +08:00
### CDN
2020-06-07 05:12:00 +08:00
Available on [jsDelivr ](https://www.jsdelivr.com/ ) CDN:
2020-06-07 04:52:26 +08:00
```html
< script src = "https://cdn.jsdelivr.net/npm/merkletreejs@latest/merkletree.js" > < / script >
```
2018-12-10 11:10:43 +08:00
## Getting started
Construct tree, generate proof, and verify proof:
2018-12-10 11:12:10 +08:00
```js
2019-03-30 10:53:32 +08:00
const { MerkleTree } = require('merkletreejs')
2018-12-10 11:10:43 +08:00
const SHA256 = require('crypto-js/sha256')
const leaves = ['a', 'b', 'c'].map(x => SHA256(x))
const tree = new MerkleTree(leaves, SHA256)
const root = tree.getRoot().toString('hex')
const leaf = SHA256('a')
const proof = tree.getProof(leaf)
console.log(tree.verify(proof, leaf, root)) // true
const badLeaves = ['a', 'x', 'c'].map(x => SHA256(x))
const badTree = new MerkleTree(badLeaves, SHA256)
const badLeaf = SHA256('x')
const badProof = tree.getProof(badLeaf)
console.log(tree.verify(badProof, leaf, root)) // false
```
Print tree to console:
```js
MerkleTree.print(tree)
```
Output
```bash
└─ 311d2e46f49b15fff8b746b74ad57f2cc9e0d9939fda94387141a2d3fdf187ae
├─ 176f0f307632fdd5831875eb709e2f68d770b102262998b214ddeb3f04164ae1
│ ├─ 3ac225168df54212a25c1c01fd35bebfea408fdac2e31ddd6f80a4bbf9a5f1cb
│ └─ b5553de315e0edf504d9150af82dafa5c4667fa618ed0a6f19c69b41166c5510
└─ 0b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b2
└─ 0b42b6393c1f53060fe3ddbfcd7aadcca894465a5a438f69c87d790b2299b9b2
```
2020-06-07 03:07:23 +08:00
## Diagrams
2020-06-07 04:07:26 +08:00
▾ Visualization of Merkle Tree
2020-06-07 03:07:23 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616375-15330c32-9671-11e8-9057-6e61c312c856.png" alt = "Merkle Tree" width = "500" >
2020-06-07 04:07:26 +08:00
▾ Visualization of Merkle Tree Proof
2020-06-07 03:07:23 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616387-27ec860a-9671-11e8-9f3f-0b871a6581a6.png" alt = "Merkle Tree Proof" width = "420" >
2020-06-07 04:07:26 +08:00
▾ Visualization of Invalid Merkle Tree Proofs
2020-06-07 03:07:23 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616398-33e20584-9671-11e8-9f62-9f48ce412898.png" alt = "Merkle Tree Proof" width = "420" >
2020-06-07 04:07:26 +08:00
▾ Visualization of Bitcoin Merkle Tree
2020-06-07 03:07:23 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616417-46d3293e-9671-11e8-81c3-8cdf7f8ddd77.png" alt = "Merkle Tree Proof" width = "420" >
2018-10-23 14:40:56 +08:00
## Documentation
2019-06-08 06:15:16 +08:00
<!-- :%s/// -->
2019-06-07 15:45:17 +08:00
<!-- :%s/ \[Options \]()/ \[Options \](#options) -->
2017-07-22 15:31:30 +08:00
2020-06-07 04:07:26 +08:00
<!-- BEGIN DOCUMENTATION -->
2020-06-07 03:26:33 +08:00
# Class: MerkleTree
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Class reprensenting a Merkle Tree
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`namespace`** MerkleTree
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
## Hierarchy
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
* **MerkleTree**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
## Index
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
### Constructors
* [constructor ](_index_.merkletree.md#constructor )
2019-06-07 15:29:42 +08:00
### Methods
2020-10-30 17:07:08 +08:00
* [bufferToHex ](_index_.merkletree.md#buffertohex )
* [bufferify ](_index_.merkletree.md#bufferify )
2020-06-07 03:26:33 +08:00
* [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 )
2020-10-30 17:07:08 +08:00
* [toJSON ](_index_.merkletree.md#tojson )
2020-06-07 03:26:33 +08:00
* [toString ](_index_.merkletree.md#tostring )
* [verify ](_index_.merkletree.md#verify )
* [verifyMultiProof ](_index_.merkletree.md#verifymultiproof )
2020-10-30 17:07:08 +08:00
* [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 )
2019-06-08 06:15:16 +08:00
2019-06-07 15:29:42 +08:00
## Constructors
### constructor
2020-06-07 04:07:26 +08:00
\+ **new MerkleTree** (`leaves`: any[], `hashAlgorithm` : any, `options` : [Options ](#options )): *[MerkleTree](_index_.merkletree.md)*
2020-06-07 03:26:33 +08:00
**`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)
```
2019-06-07 15:29:42 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Default | Description |
------ | ------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`leaves` | any[] | - | Array of hashed leaves. Each leaf must be a Buffer. |
2020-06-07 03:26:33 +08:00
`hashAlgorithm` | any | SHA256 | Algorithm used for hashing leaves and nodes |
2020-10-30 17:07:08 +08:00
`options` | [Options ](#options ) | {} | Additional options |
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *[MerkleTree](_index_.merkletree.md)*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
## Methods
2019-06-08 06:15:16 +08:00
2020-10-30 17:07:08 +08:00
### 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*
___
2020-06-07 03:26:33 +08:00
### getDepth
▸ **getDepth** (): *number*
getDepth
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the tree depth (number of layers)
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const depth = tree.getDepth()
```
**Returns:** *number*
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexLayers
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
▸ **getHexLayers** (): *string[]*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
getHexLayers
**`desc`** Returns multi-dimensional array of all layers of Merkle Tree, including leaves and root as hex strings.
2019-06-07 15:45:17 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const layers = tree.getHexLayers()
```
2019-06-07 15:45:17 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *string[]*
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexLayersFlat
2020-06-07 04:07:26 +08:00
▸ **getHexLayersFlat** (): *string[]*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
getHexLayersFlat
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns single flat array of all layers of Merkle Tree, including leaves and root as hex string.
**`example`**
```js
const layers = tree.getHexLayersFlat()
```
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *string[]*
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexLeaves
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
▸ **getHexLeaves** (): *string[]*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
getHexLeaves
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns array of leaves of Merkle Tree as hex strings.
**`example`**
```js
const leaves = tree.getHexLeaves()
```
**Returns:** *string[]*
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexMultiProof
2020-06-07 04:07:26 +08:00
▸ **getHexMultiProof** (`tree`: Buffer[], `indices` : number[]): *string[]*
2019-06-07 15:45:17 +08:00
2020-06-07 03:26:33 +08:00
getHexMultiProof
2019-06-07 15:45:17 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the multiproof for given tree indices as hex strings.
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getHexMultiProof(indices)
```
2019-06-08 06:15:16 +08:00
2020-06-07 03:26:33 +08:00
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`tree` | Buffer[] | - |
`indices` | number[] | Tree indices. |
2019-06-08 06:15:16 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *string[]*
2019-06-08 06:15:16 +08:00
2020-06-07 03:26:33 +08:00
- Multiproofs as hex strings.
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexProof
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
▸ **getHexProof** (`leaf`: Buffer, `index?` : number): *string[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getHexProof
**`desc`** Returns the proof for a target leaf as hex strings.
**`example`**
```js
const proof = tree.getHexProof(leaves[2])
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`leaf` | Buffer | Target leaf |
`index?` | number | - |
2020-06-07 03:26:33 +08:00
**Returns:** *string[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
- Proof array as hex strings.
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getHexRoot
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
▸ **getHexRoot** (): *string*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getHexRoot
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the Merkle root hash as a hex string.
**`example`**
```js
const root = tree.getHexRoot()
```
**Returns:** *string*
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getLayers
2020-06-07 04:07:26 +08:00
▸ **getLayers** (): *Buffer[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getLayers
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns multi-dimensional array of all layers of Merkle Tree, including leaves and root.
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const layers = tree.getLayers()
```
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer[]*
2020-06-07 03:07:23 +08:00
___
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
### getLayersAsObject
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
▸ **getLayersAsObject** (): *any*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getLayersAsObject
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the layers as nested objects instead of an array.
**`example`**
```js
const layersObj = tree.getLayersAsObject()
```
**Returns:** *any*
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getLayersFlat
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **getLayersFlat** (): *Buffer[]*
2020-06-07 03:26:33 +08:00
getLayersFlat
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns single flat array of all layers of Merkle Tree, including leaves and root.
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const layers = tree.getLayersFlat()
```
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer[]*
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### getLeaves
2020-06-07 04:07:26 +08:00
▸ **getLeaves** (`values?`: any[]): *Buffer[]*
2020-06-07 03:26:33 +08:00
getLeaves
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns array of leaves of Merkle Tree.
**`example`**
```js
const leaves = tree.getLeaves()
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Name | Type |
------ | ------ |
2020-06-07 04:07:26 +08:00
`values?` | any[] |
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer[]*
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getMultiProof
2020-06-07 04:07:26 +08:00
▸ **getMultiProof** (`tree?`: any[], `indices?` : any[]): *Buffer[]*
2020-06-07 03:26:33 +08:00
getMultiProof
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the multiproof for given tree indices.
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`tree?` | any[] | - |
`indices?` | any[] | Tree indices. |
2020-06-07 03:26:33 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
- Multiproofs
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getProof
2020-06-07 04:07:26 +08:00
▸ **getProof** (`leaf`: Buffer, `index?` : number): *any[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
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)
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`leaf` | Buffer | Target leaf |
`index?` | number | - |
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**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.
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getProofFlags
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **getProofFlags** (`leaves`: Buffer[], `proofs` : Buffer[]): *boolean[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getProofFlags
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns list of booleans where proofs should be used instead of hashing.
Proof flags are used in the Solidity multiproof verifiers.
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
const proofFlags = tree.getProofFlags(leaves, proof)
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type |
------ | ------ |
2020-06-07 04:07:26 +08:00
`leaves` | Buffer[] |
`proofs` | Buffer[] |
2020-06-07 03:26:33 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *boolean[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
- Boolean flags
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getProofIndices
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **getProofIndices** (`treeIndices`: number[], `depth` : number): *number[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getProofIndices
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`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 ]
```
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**Parameters:**
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`treeIndices` | number[] | Tree indices |
`depth` | number | Tree depth; number of layers. |
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *number[]*
2020-06-07 03:26:33 +08:00
- Proof indices
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### getRoot
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **getRoot** (): *Buffer*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
getRoot
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns the Merkle root hash as a Buffer.
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
const root = tree.getRoot()
```
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer*
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### print
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
▸ **print** (): *void*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
print
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Prints out a visual representation of the merkle tree.
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
tree.print()
```
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *void*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
___
2020-06-07 03:07:23 +08:00
2020-10-30 17:07:08 +08:00
### toJSON
▸ **toJSON** (): *void*
**Returns:** *void*
___
2020-06-07 03:26:33 +08:00
### toString
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **toString** (): *string*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
toString
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns a visual representation of the merkle tree as a string.
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**`example`**
```js
console.log(tree.toString())
```
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *string*
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### verify
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **verify** (`proof`: any[], `targetNode` : Buffer, `root` : Buffer): *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
verify
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`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:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`proof` | any[] | Array of proof objects that should connect target node to Merkle root. |
`targetNode` | Buffer | Target node Buffer |
`root` | Buffer | Merkle root Buffer |
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### verifyMultiProof
2020-06-07 03:07:23 +08:00
2020-06-07 04:07:26 +08:00
▸ **verifyMultiProof** (`root`: Buffer, `indices` : number[], `leaves` : Buffer[], `depth` : number, `proof` : Buffer[]): *boolean*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
verifyMultiProof
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns true if the multiproofs can connect the leaves to the Merkle root.
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`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)
```
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`root` | Buffer | Merkle tree root |
`indices` | number[] | Leave indices |
`leaves` | Buffer[] | Leaf values at indices. |
`depth` | number | Tree depth |
`proof` | Buffer[] | Multiproofs given indices |
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *boolean*
2019-06-07 15:29:42 +08:00
___
2020-10-30 17:07:08 +08:00
### `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*
___
2020-06-07 03:26:33 +08:00
### `Static` bufferify
2020-06-07 04:07:26 +08:00
▸ **bufferify** (`value`: any): *Buffer*
2020-06-07 03:26:33 +08:00
bufferify
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**`desc`** Returns a buffer type for the given value.
**`example`**
```js
const buf = MerkleTree.bufferify('0x1234')
```
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Name | Type |
------ | ------ |
2020-06-07 04:07:26 +08:00
`value` | any |
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer*
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
___
2020-06-07 03:26:33 +08:00
### `Static` getMultiProof
2020-06-07 04:07:26 +08:00
▸ **getMultiProof** (`tree`: Buffer[], `indices` : number[]): *Buffer[]*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
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)
```
2019-06-07 15:29:42 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
2020-06-07 04:07:26 +08:00
`tree` | Buffer[] | Tree as a flat array. |
`indices` | number[] | Tree indices. |
2019-06-07 15:29:42 +08:00
2020-06-07 04:07:26 +08:00
**Returns:** *Buffer[]*
2020-06-07 03:26:33 +08:00
- Multiproofs
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### `Static` isHexString
2020-06-07 04:07:26 +08:00
▸ **isHexString** (`v`: string): *boolean*
2020-06-07 03:26:33 +08:00
isHexString
**`desc`** Returns true if value is a hex string.
**`example`**
```js
console.log(MerkleTree.isHexString('0x1234'))
```
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
Name | Type |
------ | ------ |
2020-06-07 04:07:26 +08:00
`v` | string |
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *boolean*
2020-06-07 03:07:23 +08:00
___
2020-10-30 17:07:08 +08:00
### `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.
___
2020-06-07 03:26:33 +08:00
### `Static` print
▸ **print** (`tree`: any): *void*
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
print
**`desc`** Prints out a visual representation of the given merkle tree.
**`example`**
```js
MerkleTree.print(tree)
```
2020-06-07 03:07:23 +08:00
**Parameters:**
2020-06-07 03:26:33 +08:00
Name | Type | Description |
------ | ------ | ------ |
`tree` | any | Merkle tree instance. |
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
**Returns:** *void*
2020-06-07 03:07:23 +08:00
2020-10-30 17:07:08 +08:00
___
### `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 | 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 | object |
**Returns:** *any[]*
- Marshalled proof
2020-06-07 03:26:33 +08:00
# Interface: Options
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
## Hierarchy
2020-06-07 03:07:23 +08:00
2020-06-07 03:26:33 +08:00
* **Options**
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
## Index
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
### Properties
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
* [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
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
### `Optional` duplicateOdd
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
• **duplicateOdd** ? : *boolean*
If set to `true` , an odd node will be duplicated and combined to make a pair to generate the layer hash.
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### `Optional` hashLeaves
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
• **hashLeaves** ? : *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
If set to `true` , the leaves will hashed using the set hashing algorithms.
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
___
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
### `Optional` isBitcoinTree
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
• **isBitcoinTree** ? : *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
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.
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### `Optional` sort
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
• **sort** ? : *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
If set to `true` , the leaves and hashing pairs will be sorted.
___
### `Optional` sortLeaves
• **sortLeaves** ? : *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
If set to `true` , the leaves will be sorted.
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:26:33 +08:00
### `Optional` sortPairs
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
• **sortPairs** ? : *boolean*
2019-06-07 15:29:42 +08:00
2020-06-07 03:26:33 +08:00
If set to `true` , the hashing pairs will be sorted.
2019-06-08 06:15:16 +08:00
2020-06-07 04:07:26 +08:00
<!-- END DOCUMENTATION -->
2019-06-07 15:29:42 +08:00
2018-10-23 14:38:33 +08:00
## Test
2017-07-22 15:31:30 +08:00
2017-07-22 15:38:06 +08:00
```bash
2017-07-22 15:31:30 +08:00
npm test
```
2018-10-23 14:38:33 +08:00
## FAQ
- Q: How do you verify merkle proofs in Solidity?
2018-10-23 14:46:17 +08:00
- A: Check out the example repo [merkletreejs-solidity ](https://github.com/miguelmota/merkletreejs-solidity ) on how to generate merkle proofs with this library and verify them in Solidity.
2018-10-23 14:38:33 +08:00
2020-06-02 14:29:33 +08:00
- Q: How do you verify merkle [multiproofs ](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs ) in Solidity?
- A: Check out the example repo [merkletreejs-multiproof-solidity ](https://github.com/miguelmota/merkletreejs-multiproof-solidity ) on how to generate merkle multiproofs with this library and verify them in Solidity.
2018-10-23 14:38:33 +08:00
## Notes
2017-07-23 13:12:37 +08:00
2017-07-23 13:16:42 +08:00
As is, this implemenation is vulnerable to a [second pre-image attack ](https://en.wikipedia.org/wiki/Merkle_tree#Second_preimage_attack ). Use a difference hashing algorithm function for leaves and nodes, so that `H(x) != H'(x)` .
2017-07-23 13:12:37 +08:00
2017-07-23 13:16:42 +08:00
Also, as is, this implementation is vulnerable to a forgery attack for an unbalanced tree, where the last leaf node can be duplicated to create an artificial balanced tree, resulting in the same Merkle root hash. Do not accept unbalanced tree to prevent this.
2017-07-23 13:12:37 +08:00
2018-07-27 02:31:04 +08:00
More info [here ](https://bitcointalk.org/?topic=102395 ).
2018-10-23 14:38:33 +08:00
## Resources
2017-07-22 15:31:30 +08:00
- [Bitcoin mining the hard way: the algorithms, protocols, and bytes ](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html )
2017-07-22 15:38:06 +08:00
2017-07-22 15:31:30 +08:00
- [Bitcoin Talk - Merkle Trees ](https://bitcointalk.org/index.php?topic=403231.msg9054025#msg9054025 )
2017-07-22 15:38:06 +08:00
2017-07-22 15:31:30 +08:00
- [How Log Proofs Work ](https://www.certificate-transparency.org/log-proofs-work )
2017-07-22 15:38:06 +08:00
2017-07-22 16:05:52 +08:00
- [Raiden Merkle Tree Implemenation ](https://github.com/raiden-network/raiden/blob/f9cf12571891cdf54feb4667cd2fffcb3d5daa89/raiden/mtree.py )
2017-07-22 15:31:30 +08:00
- [Why aren't Solidity sha3 hashes not matching what other sha3 libraries produce? ](https://ethereum.stackexchange.com/questions/559/why-arent-solidity-sha3-hashes-not-matching-what-other-sha3-libraries-produce )
2017-07-23 13:12:37 +08:00
- [What is the purpose of using different hash functions for the leaves and internals of a hash tree? ](https://crypto.stackexchange.com/questions/2106/what-is-the-purpose-of-using-different-hash-functions-for-the-leaves-and-interna )
2017-08-08 11:54:56 +08:00
- [Why is the full Merkle path needed to verify a transaction? ](https://bitcoin.stackexchange.com/questions/50674/why-is-the-full-merkle-path-needed-to-verify-a-transaction )
- [Where is Double hashing performed in Bitcoin? ](https://bitcoin.stackexchange.com/questions/8443/where-is-double-hashing-performed-in-bitcoin )
2020-06-02 14:33:03 +08:00
- [Compact Merkle Multiproofs ](https://arxiv.org/pdf/2002.07648.pdf )
- [Eth 2.0 specs - Merkle Multiproofs ](https://github.com/ethereum/eth2.0-specs/blob/dev/ssz/merkle-proofs.md#merkle-multiproofs )
2019-12-19 09:30:24 +08:00
## Contributing
Pull requests are welcome!
For contributions please create a new branch and submit a pull request for review.
2018-10-23 14:38:33 +08:00
## License
2017-07-22 15:31:30 +08:00
2018-10-23 14:40:56 +08:00
[MIT ](LICENSE )