2018-05-02 14:52:11 +08:00
< h1 align = "center" >
< br / >
2018-05-02 14:25:56 +08:00
< img src = "https://user-images.githubusercontent.com/168240/39508295-ceeb1576-4d96-11e8-90aa-b2a56825567d.png" alt = "Merkle Tree" width = "600" / >
2018-05-02 14:52:11 +08:00
< br / >
< br / >
< br / >
< / h1 >
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
2017-07-22 16:36:53 +08:00
Diagram of Merkle Tree
2017-07-22 15:31:30 +08:00
2018-08-03 07:30:11 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616375-15330c32-9671-11e8-9057-6e61c312c856.png" alt = "Merkle Tree" width = "500" >
2017-07-22 16:36:53 +08:00
Diagram of Merkle Tree Proof
2018-08-03 07:30:11 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616387-27ec860a-9671-11e8-9f3f-0b871a6581a6.png" alt = "Merkle Tree Proof" width = "420" >
2017-07-22 16:35:21 +08:00
2017-08-08 11:29:54 +08:00
Diagram of Invalid Merkle Tree Proofs
2018-08-03 07:30:11 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616398-33e20584-9671-11e8-9f62-9f48ce412898.png" alt = "Merkle Tree Proof" width = "420" >
2017-08-08 11:29:54 +08:00
2017-08-08 11:54:56 +08:00
Diagram of Bitcoin Merkle Tree
2018-08-03 07:31:05 +08:00
< img src = "https://user-images.githubusercontent.com/168240/43616417-46d3293e-9671-11e8-81c3-8cdf7f8ddd77.png" alt = "Merkle Tree Proof" width = "420" >
2017-08-08 11:54:56 +08:00
2017-07-22 15:31:30 +08:00
# Install
```bash
2018-07-10 01:25:43 +08:00
npm install merkletreejs
2017-07-22 15:31:30 +08:00
```
## Classes
< dl >
< dt > < a href = "#MerkleTree" > MerkleTree< / a > < / dt >
< dd > < / dd >
< / dl >
## Objects
< dl >
< dt > < a href = "#MerkleTree" > MerkleTree< / a > : < code > object< / code > < / dt >
< dd > < p > Class reprensenting a Merkle Tree< / p >
< / dd >
< / dl >
< a name = "MerkleTree" > < / a >
## MerkleTree
**Kind**: global class
* [MerkleTree ](#MerkleTree )
2017-07-22 15:38:06 +08:00
* [new MerkleTree(leaves, hashAlgorithm, options) ](#new_MerkleTree_new )
2017-07-22 16:05:52 +08:00
* [.getLeaves() ](#MerkleTree+getLeaves ) ⇒ < code > Array.< Buffer> </ code >
* [.getLayers() ](#MerkleTree+getLayers ) ⇒ < code > Array.< Buffer> </ code >
2017-07-22 15:31:30 +08:00
* [.getRoot() ](#MerkleTree+getRoot ) ⇒ < code > Buffer</ code >
2017-07-22 16:05:52 +08:00
* [.getProof(leaf, [index])](#MerkleTree+getProof) ⇒ < code > Array.< Buffer> </ code >
2017-07-22 15:31:30 +08:00
* [.verify(proof, targetNode, root) ](#MerkleTree+verify ) ⇒ < code > Boolean</ code >
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "new_MerkleTree_new" > < / a >
2017-07-22 15:38:06 +08:00
### new MerkleTree(leaves, hashAlgorithm, options)
2017-07-22 15:31:30 +08:00
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.
| Param | Type | Description |
| --- | --- | --- |
2017-07-22 16:05:52 +08:00
| leaves | < code > Array.< Buffer> < / code > | Array of hashed leaves. Each leaf must be a Buffer. |
2017-07-22 15:38:06 +08:00
| hashAlgorithm | < code > function< / code > | Algorithm used for hashing leaves and nodes |
2017-07-22 15:31:30 +08:00
| options | < code > Object< / code > | Additional options |
2017-08-08 11:54:56 +08:00
| options.isBitcoinTree | < code > Boolean</ code > | 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. |
2017-07-22 15:31:30 +08:00
**Example**
```js
2018-07-10 01:25:43 +08:00
const MerkleTree = require('merkletreejs')
2017-07-22 15:31:30 +08:00
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
2018-07-10 03:57:31 +08:00
const leaves = ['a', 'b', 'c'].map(x => sha256(x))
2017-07-22 15:31:30 +08:00
const tree = new MerkleTree(leaves, sha256)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
2017-07-22 16:05:52 +08:00
< a name = "MerkleTree+getLeaves" > < / a >
2017-07-22 15:44:19 +08:00
2017-07-22 16:05:52 +08:00
### merkleTree.getLeaves() ⇒ <code>Array.<Buffer></code>
Returns array of leaves of Merkle Tree.
2017-07-22 15:44:19 +08:00
2017-07-22 16:05:52 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
2017-07-22 15:31:30 +08:00
**Example**
```js
const leaves = tree.getLeaves()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getLayers" > < / a >
2017-07-22 16:05:52 +08:00
### merkleTree.getLayers() ⇒ <code>Array.<Buffer></code>
Returns array of all layers of Merkle Tree, including leaves and root.
2017-07-22 15:31:30 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
**Example**
```js
const layers = tree.getLayers()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getRoot" > < / a >
### merkleTree.getRoot() ⇒ <code>Buffer</code>
2017-07-22 16:05:52 +08:00
Returns the Merkle root hash as a Buffer.
2017-07-22 15:31:30 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
**Example**
```js
const root = tree.getRoot()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getProof" > < / a >
2017-07-22 16:05:52 +08:00
### merkleTree.getProof(leaf, [index]) ⇒ <code>Array.<Buffer></code>
2017-07-22 15:31:30 +08:00
Returns the proof for a target leaf.
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
2017-07-22 16:05:52 +08:00
**Returns**: < code > Array.< Buffer> < / code > - - Array of Buffer hashes.
2017-07-22 15:31:30 +08:00
| Param | Type | Description |
| --- | --- | --- |
| leaf | < code > Buffer< / code > | Target leaf |
| [index] | < code > Number< / code > | Target leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it. |
**Example**
```js
const proof = tree.getProof(leaves[2])
```
**Example**
```js
2018-07-10 03:57:31 +08:00
const leaves = ['a', 'b', 'a'].map(x => sha256(x))
const tree = new MerkleTree(leaves, sha256)
2017-07-22 15:31:30 +08:00
const proof = tree.getProof(leaves[2], 2)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+verify" > < / a >
### merkleTree.verify(proof, targetNode, root) ⇒ <code>Boolean</code>
Returns true if the proof path (array of hashes) can connect the target node
to the Merkle root.
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
| Param | Type | Description |
| --- | --- | --- |
2017-07-22 16:05:52 +08:00
| proof | < code > Array.< Buffer> < / code > | Array of proof Buffer hashes that should connect target node to Merkle root. |
2017-07-22 15:31:30 +08:00
| targetNode | < code > Buffer< / code > | Target node Buffer |
| root | < code > Buffer< / code > | Merkle root Buffer |
**Example**
```js
const root = tree.getRoot()
const proof = tree.getProof(leaves[2])
const verified = tree.verify(proof, leaves[2], root)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree" > < / a >
## MerkleTree : <code>object</code>
Class reprensenting a Merkle Tree
**Kind**: global namespace
* [MerkleTree ](#MerkleTree ) : < code > object</ code >
2017-07-22 15:38:06 +08:00
* [new MerkleTree(leaves, hashAlgorithm, options) ](#new_MerkleTree_new )
2017-07-22 16:05:52 +08:00
* [.getLeaves() ](#MerkleTree+getLeaves ) ⇒ < code > Array.< Buffer> </ code >
* [.getLayers() ](#MerkleTree+getLayers ) ⇒ < code > Array.< Buffer> </ code >
2017-07-22 15:31:30 +08:00
* [.getRoot() ](#MerkleTree+getRoot ) ⇒ < code > Buffer</ code >
2017-07-22 16:05:52 +08:00
* [.getProof(leaf, [index])](#MerkleTree+getProof) ⇒ < code > Array.< Buffer> </ code >
2017-07-22 15:31:30 +08:00
* [.verify(proof, targetNode, root) ](#MerkleTree+verify ) ⇒ < code > Boolean</ code >
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "new_MerkleTree_new" > < / a >
2017-07-22 15:38:06 +08:00
### new MerkleTree(leaves, hashAlgorithm, options)
2017-07-22 15:31:30 +08:00
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.
| Param | Type | Description |
| --- | --- | --- |
2017-07-22 16:05:52 +08:00
| leaves | < code > Array.< Buffer> < / code > | Array of hashed leaves. Each leaf must be a Buffer. |
2017-07-22 15:38:06 +08:00
| hashAlgorithm | < code > function< / code > | Algorithm used for hashing leaves and nodes |
2017-07-22 15:31:30 +08:00
| options | < code > Object< / code > | Additional options |
2017-08-08 11:54:56 +08:00
| options.isBitcoinTree | < code > Boolean</ code > | 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. |
2017-07-22 15:31:30 +08:00
**Example**
```js
2018-07-10 01:25:43 +08:00
const MerkleTree = require('merkletreejs')
2017-07-22 15:31:30 +08:00
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
2018-07-10 03:57:31 +08:00
const leaves = ['a', 'b', 'c'].map(x => sha256(x))
2017-07-22 15:31:30 +08:00
const tree = new MerkleTree(leaves, sha256)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
2017-07-22 16:05:52 +08:00
< a name = "MerkleTree+getLeaves" > < / a >
2017-07-22 15:44:19 +08:00
2017-07-22 16:05:52 +08:00
### merkleTree.getLeaves() ⇒ <code>Array.<Buffer></code>
Returns array of leaves of Merkle Tree.
2017-07-22 15:44:19 +08:00
2017-07-22 16:05:52 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
2017-07-22 15:31:30 +08:00
**Example**
```js
const leaves = tree.getLeaves()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getLayers" > < / a >
2017-07-22 16:05:52 +08:00
### merkleTree.getLayers() ⇒ <code>Array.<Buffer></code>
Returns array of all layers of Merkle Tree, including leaves and root.
2017-07-22 15:31:30 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
**Example**
```js
const layers = tree.getLayers()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getRoot" > < / a >
### merkleTree.getRoot() ⇒ <code>Buffer</code>
2017-07-22 16:05:52 +08:00
Returns the Merkle root hash as a Buffer.
2017-07-22 15:31:30 +08:00
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
**Example**
```js
const root = tree.getRoot()
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+getProof" > < / a >
2017-07-22 16:05:52 +08:00
### merkleTree.getProof(leaf, [index]) ⇒ <code>Array.<Buffer></code>
2017-07-22 15:31:30 +08:00
Returns the proof for a target leaf.
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
2017-07-22 16:05:52 +08:00
**Returns**: < code > Array.< Buffer> < / code > - - Array of Buffer hashes.
2017-07-22 15:31:30 +08:00
| Param | Type | Description |
| --- | --- | --- |
| leaf | < code > Buffer< / code > | Target leaf |
| [index] | < code > Number< / code > | Target leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it. |
**Example**
```js
const proof = tree.getProof(leaves[2])
```
**Example**
```js
2018-07-10 03:57:31 +08:00
const leaves = ['a', 'b', 'a'].map(x => sha256(x))
const tree = new MerkleTree(leaves, sha256)
2017-07-22 15:31:30 +08:00
const proof = tree.getProof(leaves[2], 2)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
< a name = "MerkleTree+verify" > < / a >
### merkleTree.verify(proof, targetNode, root) ⇒ <code>Boolean</code>
Returns true if the proof path (array of hashes) can connect the target node
to the Merkle root.
**Kind**: instance method of [<code>MerkleTree</code> ](#MerkleTree )
| Param | Type | Description |
| --- | --- | --- |
2017-07-22 16:05:52 +08:00
| proof | < code > Array.< Buffer> < / code > | Array of proof Buffer hashes that should connect target node to Merkle root. |
2017-07-22 15:31:30 +08:00
| targetNode | < code > Buffer< / code > | Target node Buffer |
| root | < code > Buffer< / code > | Merkle root Buffer |
**Example**
```js
const root = tree.getRoot()
const proof = tree.getProof(leaves[2])
const verified = tree.verify(proof, leaves[2], root)
```
2017-07-22 16:05:52 +08:00
* * *
2017-07-22 15:31:30 +08:00
# Test
2017-07-22 15:38:06 +08:00
```bash
2017-07-22 15:31:30 +08:00
npm test
```
2017-07-23 13:12:37 +08:00
# Notes
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 ).
2017-07-22 16:05:52 +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 )
2017-07-22 15:31:30 +08:00
# License
MIT