2017-07-22 15:31:30 +08:00
# Merkle Tree
> Construct [Merkle Trees](https://en.wikipedia.org/wiki/Merkle_tree) and verify via proofs in JavaScript.
< img src = "https://github.com/miguelmota/merkle-tree/blob/master/diagrams/merkle-tree.png?raw=true" width = "500" >
# Install
```bash
npm install m-tree
```
## 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-07-22 15:38:06 +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. |
2017-07-22 15:31:30 +08:00
**Example**
```js
const MerkleTree = require('m-tree')
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
const leaves = ['a', 'b', 'c'].map(x => sha3(x))
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
const leaves = ['a', 'b', 'a'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha3)
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-07-22 15:38:06 +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. |
2017-07-22 15:31:30 +08:00
**Example**
```js
const MerkleTree = require('m-tree')
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
const leaves = ['a', 'b', 'c'].map(x => sha3(x))
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
const leaves = ['a', 'b', 'a'].map(x => sha3(x))
const tree = new MerkleTree(leaves, sha3)
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
<!--
merkle-lib
Warnings
This implementation is vulnerable to a forgery attack (as a second pre-image attack), see these[1][2] crypto.stackexchange questions for an explanation. To avoid this vulnerability, you should pre-hash your leaves using a different hash function than the function provided such that H(x) != H'(x).
This implementation is vulnerable to a forgery attack (for an unbalanced merkle tree), wherein, in an unbalanced merkle tree, the last leaf node can be duplicated to create an artificial balanced tree, resulting in the same root hash. To avoid this vulnerability [in this implementation], do not accept unbalanced merkle trees in your application.
-->
# Test
2017-07-22 15:38:06 +08:00
```bash
2017-07-22 15:31:30 +08:00
npm test
```
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 )
# License
MIT