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
```bash
2018-07-10 01:25:43 +08:00
npm install merkletreejs
2017-07-22 15:31:30 +08:00
```
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
Visualization of Merkle Tree
< img src = "https://user-images.githubusercontent.com/168240/43616375-15330c32-9671-11e8-9057-6e61c312c856.png" alt = "Merkle Tree" width = "500" >
Visualization of Merkle Tree Proof
< img src = "https://user-images.githubusercontent.com/168240/43616387-27ec860a-9671-11e8-9f3f-0b871a6581a6.png" alt = "Merkle Tree Proof" width = "420" >
Visualization of Invalid Merkle Tree Proofs
< img src = "https://user-images.githubusercontent.com/168240/43616398-33e20584-9671-11e8-9f62-9f48ce412898.png" alt = "Merkle Tree Proof" width = "420" >
Visualization of Bitcoin Merkle Tree
< 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
2019-06-07 15:45:17 +08:00
## Class
2019-06-07 15:29:42 +08:00
## Hierarchy
**MerkleTree**
### Constructors
2020-06-07 03:07:23 +08:00
* [constructor ](api-classes-index-merkletree.md#constructor )
2019-06-07 15:29:42 +08:00
### Properties
2020-06-07 03:07:23 +08:00
* [duplicateOdd ](api-classes-index-merkletree.md#duplicateodd )
* [hashAlgo ](api-classes-index-merkletree.md#hashalgo )
* [hashLeaves ](api-classes-index-merkletree.md#hashleaves )
* [isBitcoinTree ](api-classes-index-merkletree.md#isbitcointree )
* [layers ](api-classes-index-merkletree.md#layers )
* [leaves ](api-classes-index-merkletree.md#leaves )
* [sort ](api-classes-index-merkletree.md#sort )
* [sortLeaves ](api-classes-index-merkletree.md#sortleaves )
* [sortPairs ](api-classes-index-merkletree.md#sortpairs )
2019-06-07 15:29:42 +08:00
### Methods
2020-06-07 03:07:23 +08:00
* [_bufferIndexOf ](api-classes-index-merkletree.md#_bufferindexof )
* [_bufferToHex ](api-classes-index-merkletree.md#_buffertohex )
* [_bufferify ](api-classes-index-merkletree.md#_bufferify )
* [_bufferifyFn ](api-classes-index-merkletree.md#_bufferifyfn )
* [_createHashes ](api-classes-index-merkletree.md#_createhashes )
* [_getPairNode ](api-classes-index-merkletree.md#_getpairnode )
* [_isHexString ](api-classes-index-merkletree.md#_ishexstring )
* [_log2 ](api-classes-index-merkletree.md#_log2 )
* [_toTreeString ](api-classes-index-merkletree.md#_totreestring )
* [_zip ](api-classes-index-merkletree.md#_zip )
* [getDepth ](api-classes-index-merkletree.md#getdepth )
* [getHexLayers ](api-classes-index-merkletree.md#gethexlayers )
* [getHexLayersFlat ](api-classes-index-merkletree.md#gethexlayersflat )
* [getHexLeaves ](api-classes-index-merkletree.md#gethexleaves )
* [getHexMultiProof ](api-classes-index-merkletree.md#gethexmultiproof )
* [getHexProof ](api-classes-index-merkletree.md#gethexproof )
* [getHexRoot ](api-classes-index-merkletree.md#gethexroot )
* [getLayers ](api-classes-index-merkletree.md#getlayers )
* [getLayersAsObject ](api-classes-index-merkletree.md#getlayersasobject )
* [getLayersFlat ](api-classes-index-merkletree.md#getlayersflat )
* [getLeaves ](api-classes-index-merkletree.md#getleaves )
* [getMultiProof ](api-classes-index-merkletree.md#getmultiproof )
* [getProof ](api-classes-index-merkletree.md#getproof )
* [getProofFlags ](api-classes-index-merkletree.md#getproofflags )
* [getProofIndices ](api-classes-index-merkletree.md#getproofindices )
* [getRoot ](api-classes-index-merkletree.md#getroot )
* [print ](api-classes-index-merkletree.md#print )
* [toString ](api-classes-index-merkletree.md#tostring )
* [verify ](api-classes-index-merkletree.md#verify )
* [verifyMultiProof ](api-classes-index-merkletree.md#verifymultiproof )
* [bufferify ](api-classes-index-merkletree.md#bufferify )
* [getMultiProof ](api-classes-index-merkletree.md#getmultiproof-1 )
* [isHexString ](api-classes-index-merkletree.md#ishexstring )
* [print ](api-classes-index-merkletree.md#print-1 )
2019-06-07 15:29:42 +08:00
2019-06-08 06:15:16 +08:00
---
2019-06-07 15:29:42 +08:00
## Constructors
< a id = "constructor" > < / a >
### constructor
2020-06-07 03:07:23 +08:00
⊕ **new MerkleTree** (leaves: *`any`* , hashAlgorithm?: *`any`* , options?: *[Options](api-interfaces-index-options.md)* ): [MerkleTree ](api-classes-index-merkletree.md )
2019-06-07 15:29:42 +08:00
**Parameters:**
| Name | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| leaves | `any` | - | Array of hashed leaves. Each leaf must be a Buffer. |
2020-06-07 03:07:23 +08:00
| `Default value` hashAlgorithm | `any` | SHA256 | Algorithm used for hashing leaves and nodes |
| `Default value` options | [Options ](api-interfaces-index-options.md ) | {} | Additional options |
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** [MerkleTree ](api-classes-index-merkletree.md )
2019-06-07 15:29:42 +08:00
2019-06-08 06:15:16 +08:00
___
2019-06-07 15:29:42 +08:00
## Properties
< a id = "duplicateodd" > < / a >
### duplicateOdd
**● duplicateOdd**: *`boolean`*
___
< a id = "hashalgo" > < / a >
### hashAlgo
2019-06-07 15:45:17 +08:00
**● hashAlgo**: *`function`*
#### Type declaration
2020-06-07 03:07:23 +08:00
▸(value: *[TValue](api-modules-index-module.md#tvalue)* ): [THashAlgo ](api-modules-index-module.md#thashalgo )
2019-06-07 15:29:42 +08:00
2019-06-07 15:45:17 +08:00
**Parameters:**
| Name | Type |
| ------ | ------ |
2020-06-07 03:07:23 +08:00
| value | [TValue ](api-modules-index-module.md#tvalue ) |
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** [THashAlgo ](api-modules-index-module.md#thashalgo )
2019-06-07 15:29:42 +08:00
___
< a id = "hashleaves" > < / a >
### hashLeaves
**● hashLeaves**: *`boolean`*
___
< a id = "isbitcointree" > < / a >
### isBitcoinTree
**● isBitcoinTree**: *`boolean`*
___
< a id = "layers" > < / a >
### layers
2020-06-07 03:07:23 +08:00
**● layers**: *[TLayer](api-modules-index-module.md#tlayer)[]*
2019-06-07 15:29:42 +08:00
___
< a id = "leaves" > < / a >
### leaves
2020-06-07 03:07:23 +08:00
**● leaves**: *[TLeaf](api-modules-index-module.md#tleaf)[]*
___
< a id = "sort" > < / a >
### sort
**● sort**: *`boolean`*
2019-06-07 15:45:17 +08:00
___
2019-06-08 06:15:16 +08:00
< a id = "sortleaves" > < / a >
2019-06-07 15:45:17 +08:00
2019-06-08 06:15:16 +08:00
### sortLeaves
2019-06-07 15:29:42 +08:00
2019-06-08 06:15:16 +08:00
**● sortLeaves**: *`boolean`*
___
< a id = "sortpairs" > < / a >
### sortPairs
**● sortPairs**: *`boolean`*
2019-06-07 15:29:42 +08:00
___
## Methods
2020-06-07 03:07:23 +08:00
< a id = "_bufferindexof" > < / a >
### `<Private>` _bufferIndexOf
▸ **_bufferIndexOf** (arr: *`any`* , el: *`any`* ): `number`
**Parameters:**
| Name | Type |
| ------ | ------ |
| arr | `any` |
| el | `any` |
**Returns:** `number`
* Index number
___
< a id = "_buffertohex" > < / a >
### `<Private>` _bufferToHex
▸ **_bufferToHex** (value: *`Buffer`* ): `string`
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| value | `Buffer` | \- |
**Returns:** `string`
___
< a id = "_bufferify" > < / a >
### `<Private>` _bufferify
▸ **_bufferify** (x: *`any`* ): `any`
**Parameters:**
| Name | Type |
| ------ | ------ |
| x | `any` |
**Returns:** `any`
___
< a id = "_bufferifyfn" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### `<Private>` _bufferifyFn
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **_bufferifyFn** (f: *`any`* ): `(Anonymous function)`
**Parameters:**
| Name | Type |
| ------ | ------ |
| f | `any` |
**Returns:** `(Anonymous function)`
___
< a id = "_createhashes" > < / a >
### `<Private>` _createHashes
▸ **_createHashes** (nodes: *`any`* ): `void`
2019-06-07 15:29:42 +08:00
**Parameters:**
| Name | Type |
| ------ | ------ |
| nodes | `any` |
**Returns:** `void`
___
2020-06-07 03:07:23 +08:00
< a id = "_getpairnode" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### `<Private>` _getPairNode
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **_getPairNode** (layer: *`any`* , idx: *`any`* ): `any`
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
| Name | Type | Description |
| ------ | ------ | ------ |
| layer | `any` | Tree layer |
| idx | `any` |
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `any`
* Node
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
___
< a id = "_ishexstring" > < / a >
### `<Private>` _isHexString
▸ **_isHexString** (v: *`any`* ): `boolean`
**Parameters:**
| Name | Type |
| ------ | ------ |
| v | `any` |
**Returns:** `boolean`
___
< a id = "_log2" > < / a >
### `<Private>` _log2
▸ **_log2** (x: *`any`* ): `any`
**Parameters:**
| Name | Type |
| ------ | ------ |
| x | `any` |
**Returns:** `any`
___
< a id = "_totreestring" > < / a >
### `<Private>` _toTreeString
▸ **_toTreeString** (): `any`
**Returns:** `any`
___
< a id = "_zip" > < / a >
### `<Private>` _zip
▸ **_zip** (a: *`any`* , b: *`any`* ): `any`
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| a | `any` | first array |
| b | `any` | second array |
**Returns:** `any`
___
< a id = "getdepth" > < / a >
### getDepth
▸ **getDepth** (): `number`
**Returns:** `number`
___
< a id = "gethexlayers" > < / a >
### getHexLayers
▸ **getHexLayers** (): `any`
**Returns:** `any`
___
< a id = "gethexlayersflat" > < / a >
### getHexLayersFlat
▸ **getHexLayersFlat** (): `any`
**Returns:** `any`
___
< a id = "gethexleaves" > < / a >
### getHexLeaves
▸ **getHexLeaves** (): `string` []
**Returns:** `string` []
___
< a id = "gethexmultiproof" > < / a >
### getHexMultiProof
▸ **getHexMultiProof** (tree: *`any`* , indices: *`any`* ): `string` []
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| tree | `any` |
| indices | `any` | Tree indices. |
**Returns:** `string` []
* Multiproofs as hex strings.
___
< a id = "gethexproof" > < / a >
### getHexProof
▸ **getHexProof** (leaf: *`any`* , index: *`any`* ): `string` []
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| leaf | `any` | Target leaf |
| `Optional` index | `any` |
**Returns:** `string` []
* Proof array as hex strings.
___
< a id = "gethexroot" > < / a >
### getHexRoot
▸ **getHexRoot** (): `string`
**Returns:** `string`
___
< a id = "getlayers" > < / a >
### getLayers
▸ **getLayers** (): `any` []
2019-06-07 15:29:42 +08:00
2019-06-07 15:45:17 +08:00
**Returns:** `any` []
2019-06-07 15:29:42 +08:00
___
< a id = "getlayersasobject" > < / a >
### getLayersAsObject
▸ **getLayersAsObject** (): `any`
**Returns:** `any`
2020-06-07 03:07:23 +08:00
___
< a id = "getlayersflat" > < / a >
### getLayersFlat
▸ **getLayersFlat** (): `any`
**Returns:** `any`
2019-06-07 15:29:42 +08:00
___
< a id = "getleaves" > < / a >
### getLeaves
2020-06-07 03:07:23 +08:00
▸ **getLeaves** (data: *`any`[]* ): `any` []
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:07:23 +08:00
| Name | Type |
| ------ | ------ |
| `Optional` data | `any` [] |
2019-06-07 15:29:42 +08:00
2019-06-07 15:45:17 +08:00
**Returns:** `any` []
2019-06-07 15:29:42 +08:00
___
2020-06-07 03:07:23 +08:00
< a id = "getmultiproof" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### getMultiProof
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **getMultiProof** (tree: *`any`* , indices: *`any`* ): `any` []
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:07:23 +08:00
| Name | Type | Description |
| ------ | ------ | ------ |
| tree | `any` |
| indices | `any` | Tree indices. |
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `any` []
* Multiproofs
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
___
< a id = "getproof" > < / a >
### getProof
▸ **getProof** (leaf: *`any`* , index: *`any`* ): `any` []
2019-06-07 15:29:42 +08:00
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| leaf | `any` | Target leaf |
| `Optional` index | `any` |
**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
< a id = "getproofflags" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### getProofFlags
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **getProofFlags** (els: *`any`* , proofs: *`any`* ): `any` []
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:07:23 +08:00
| Name | Type |
| ------ | ------ |
| els | `any` |
| proofs | `any` |
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `any` []
* Boolean flags
___
< a id = "getproofindices" > < / a >
### getProofIndices
▸ **getProofIndices** (treeIndices: *`any`* , depth: *`any`* ): `any` []
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| treeIndices | `any` | Tree indices |
| depth | `any` | Tree depth; number of layers. |
**Returns:** `any` []
* Proof indices
___
< a id = "getroot" > < / a >
### getRoot
▸ **getRoot** (): `any`
2019-06-07 15:29:42 +08:00
**Returns:** `any`
___
< a id = "print" > < / a >
### print
▸ **print** (): `void`
**Returns:** `void`
___
< a id = "tostring" > < / a >
### toString
▸ **toString** (): `any`
**Returns:** `any`
___
< a id = "verify" > < / a >
### verify
▸ **verify** (proof: *`any`* , targetNode: *`any`* , root: *`any`* ): `boolean`
**Parameters:**
| Name | Type | Description |
| ------ | ------ | ------ |
| proof | `any` | Array of proof objects that should connect target node to Merkle root. |
| targetNode | `any` | Target node Buffer |
| root | `any` | Merkle root Buffer |
**Returns:** `boolean`
___
2020-06-07 03:07:23 +08:00
< a id = "verifymultiproof" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### verifyMultiProof
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **verifyMultiProof** (root: *`any`* , indices: *`any`* , leaves: *`any`* , depth: *`any`* , proof: *`any`* ): `any`
2019-06-07 15:29:42 +08:00
**Parameters:**
2020-06-07 03:07:23 +08:00
| Name | Type | Description |
| ------ | ------ | ------ |
| root | `any` | Merkle tree root |
| indices | `any` | Leave indices |
| leaves | `any` | Leaf values at indices. |
| depth | `any` | Tree depth |
| proof | `any` | Multiproofs given indices |
2019-06-07 15:29:42 +08:00
**Returns:** `any`
___
2020-06-07 03:07:23 +08:00
< a id = "bufferify" > < / a >
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
### `<Static>` bufferify
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
▸ **bufferify** (x: *`any`* ): `any`
2019-06-07 15:29:42 +08:00
**Parameters:**
| Name | Type |
| ------ | ------ |
2020-06-07 03:07:23 +08:00
| x | `any` |
2019-06-08 06:15:16 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `any`
2019-06-08 06:15:16 +08:00
2020-06-07 03:07:23 +08:00
___
< a id = "getmultiproof-1" > < / a >
2019-06-08 06:15:16 +08:00
2020-06-07 03:07:23 +08:00
### `<Static>` getMultiProof
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
▸ **getMultiProof** (tree: *`any`* , indices: *`any`* ): `any` []
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
| Name | Type | Description |
| ------ | ------ | ------ |
| tree | `any` | Tree as a flat array. |
| indices | `any` | Tree indices. |
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `any` []
* Multiproofs
2019-06-07 16:00:36 +08:00
2019-06-07 15:45:17 +08:00
___
2020-06-07 03:07:23 +08:00
< a id = "ishexstring" > < / a >
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
### `<Static>` isHexString
2019-06-07 16:00:36 +08:00
2020-06-07 03:07:23 +08:00
▸ **isHexString** (v: *`any`* ): `boolean`
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
| Name | Type |
| ------ | ------ |
| v | `any` |
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `boolean`
2019-06-07 16:00:36 +08:00
2019-06-07 15:45:17 +08:00
___
2020-06-07 03:07:23 +08:00
< a id = "print-1" > < / a >
2019-06-08 06:15:16 +08:00
2020-06-07 03:07:23 +08:00
### `<Static>` print
2019-06-08 06:15:16 +08:00
2020-06-07 03:07:23 +08:00
▸ **print** (tree: *`any`* ): `void`
2019-06-07 15:45:17 +08:00
2020-06-07 03:07:23 +08:00
**Parameters:**
2019-06-07 15:29:42 +08:00
2020-06-07 03:07:23 +08:00
| Name | Type | Description |
| ------ | ------ | ------ |
| tree | `any` | Merkle tree instance. |
2019-06-07 16:00:36 +08:00
2020-06-07 03:07:23 +08:00
**Returns:** `void`
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 )