update readme

This commit is contained in:
Miguel Mota 2017-07-22 00:38:06 -07:00
parent d501d30f39
commit 13c629711e
2 changed files with 18 additions and 14 deletions

View File

@ -33,7 +33,7 @@ npm install m-tree
**Kind**: global class **Kind**: global class
* [MerkleTree](#MerkleTree) * [MerkleTree](#MerkleTree)
* [new MerkleTree(leaves, hashAlgo, options)](#new_MerkleTree_new) * [new MerkleTree(leaves, hashAlgorithm, options)](#new_MerkleTree_new)
* [.getLeaves()](#MerkleTree+getLeaves) ⇒ <code>Array</code> * [.getLeaves()](#MerkleTree+getLeaves) ⇒ <code>Array</code>
* [.getLayers()](#MerkleTree+getLayers) ⇒ <code>Array</code> * [.getLayers()](#MerkleTree+getLayers) ⇒ <code>Array</code>
* [.getRoot()](#MerkleTree+getRoot) ⇒ <code>Buffer</code> * [.getRoot()](#MerkleTree+getRoot) ⇒ <code>Buffer</code>
@ -42,7 +42,7 @@ npm install m-tree
<a name="new_MerkleTree_new"></a> <a name="new_MerkleTree_new"></a>
### new MerkleTree(leaves, hashAlgo, options) ### new MerkleTree(leaves, hashAlgorithm, options)
Constructs a Merkle Tree. Constructs a Merkle Tree.
All nodes and leaves are stored as Buffers. All nodes and leaves are stored as Buffers.
Lonely leaf nodes are promoted to the next level up without being hashed again. Lonely leaf nodes are promoted to the next level up without being hashed again.
@ -51,9 +51,9 @@ Lonely leaf nodes are promoted to the next level up without being hashed again.
| Param | Type | Description | | Param | Type | Description |
| --- | --- | --- | | --- | --- | --- |
| leaves | <code>Array</code> | Array of hashed leaves. Each leaf must be a Buffer. | | leaves | <code>Array</code> | Array of hashed leaves. Each leaf must be a Buffer. |
| hashAlgo | <code>function</code> | Algorithm used for hashing leaves and nodes | | hashAlgorithm | <code>function</code> | Algorithm used for hashing leaves and nodes |
| options | <code>Object</code> | Additional options | | options | <code>Object</code> | Additional options |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, generates the Merkle Tree with the Bitcoin Merkle Tree implementation. Enable it when you need to replicate Bitcoin constructed Merkle Trees. | | 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. |
**Example** **Example**
```js ```js
@ -157,7 +157,7 @@ const tree = new MerkleTree(leaves, sha256)
``` ```
* [MerkleTree](#MerkleTree) : <code>object</code> * [MerkleTree](#MerkleTree) : <code>object</code>
* [new MerkleTree(leaves, hashAlgo, options)](#new_MerkleTree_new) * [new MerkleTree(leaves, hashAlgorithm, options)](#new_MerkleTree_new)
* [.getLeaves()](#MerkleTree+getLeaves) ⇒ <code>Array</code> * [.getLeaves()](#MerkleTree+getLeaves) ⇒ <code>Array</code>
* [.getLayers()](#MerkleTree+getLayers) ⇒ <code>Array</code> * [.getLayers()](#MerkleTree+getLayers) ⇒ <code>Array</code>
* [.getRoot()](#MerkleTree+getRoot) ⇒ <code>Buffer</code> * [.getRoot()](#MerkleTree+getRoot) ⇒ <code>Buffer</code>
@ -166,7 +166,7 @@ const tree = new MerkleTree(leaves, sha256)
<a name="new_MerkleTree_new"></a> <a name="new_MerkleTree_new"></a>
### new MerkleTree(leaves, hashAlgo, options) ### new MerkleTree(leaves, hashAlgorithm, options)
Constructs a Merkle Tree. Constructs a Merkle Tree.
All nodes and leaves are stored as Buffers. All nodes and leaves are stored as Buffers.
Lonely leaf nodes are promoted to the next level up without being hashed again. Lonely leaf nodes are promoted to the next level up without being hashed again.
@ -175,9 +175,9 @@ Lonely leaf nodes are promoted to the next level up without being hashed again.
| Param | Type | Description | | Param | Type | Description |
| --- | --- | --- | | --- | --- | --- |
| leaves | <code>Array</code> | Array of hashed leaves. Each leaf must be a Buffer. | | leaves | <code>Array</code> | Array of hashed leaves. Each leaf must be a Buffer. |
| hashAlgo | <code>function</code> | Algorithm used for hashing leaves and nodes | | hashAlgorithm | <code>function</code> | Algorithm used for hashing leaves and nodes |
| options | <code>Object</code> | Additional options | | options | <code>Object</code> | Additional options |
| options.isBitcoinTree | <code>Boolean</code> | If set to `true`, generates the Merkle Tree with the Bitcoin Merkle Tree implementation. Enable it when you need to replicate Bitcoin constructed Merkle Trees. | | 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. |
**Example** **Example**
```js ```js
@ -281,16 +281,20 @@ This implementation is vulnerable to a forgery attack (for an unbalanced merkle
# Test # Test
``bash ```bash
npm test npm test
``` ```
# Credits/Resources # Credits/Resources
- [Bitcoin mining the hard way: the algorithms, protocols, and bytes](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html) - [Bitcoin mining the hard way: the algorithms, protocols, and bytes](http://www.righto.com/2014/02/bitcoin-mining-hard-way-algorithms.html)
- [Raiden Merkle Tree Implemenation](https://github.com/raiden-network/raiden/blob/f9cf12571891cdf54feb4667cd2fffcb3d5daa89/raiden/mtree.py) - [Raiden Merkle Tree Implemenation](https://github.com/raiden-network/raiden/blob/f9cf12571891cdf54feb4667cd2fffcb3d5daa89/raiden/mtree.py)
- [Bitcoin Talk - Merkle Trees](https://bitcointalk.org/index.php?topic=403231.msg9054025#msg9054025) - [Bitcoin Talk - Merkle Trees](https://bitcointalk.org/index.php?topic=403231.msg9054025#msg9054025)
- [How Log Proofs Work](https://www.certificate-transparency.org/log-proofs-work) - [How Log Proofs Work](https://www.certificate-transparency.org/log-proofs-work)
- [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) - [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 # License

View File

@ -12,10 +12,10 @@ class MerkleTree {
* All nodes and leaves are stored as Buffers. * All nodes and leaves are stored as Buffers.
* Lonely leaf nodes are promoted to the next level up without being hashed again. * Lonely leaf nodes are promoted to the next level up without being hashed again.
* @param {Array} leaves - Array of hashed leaves. Each leaf must be a Buffer. * @param {Array} leaves - Array of hashed leaves. Each leaf must be a Buffer.
* @param {Function} hashAlgo - Algorithm used for hashing leaves and nodes * @param {Function} hashAlgorithm - Algorithm used for hashing leaves and nodes
* @param {Object} options - Additional options * @param {Object} options - Additional options
* @param {Boolean} options.isBitcoinTree - If set to `true`, generates the Merkle * @param {Boolean} options.isBitcoinTree - If set to `true`, constructs the Merkle
* Tree with the Bitcoin Merkle Tree implementation. Enable it when you need * 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. * to replicate Bitcoin constructed Merkle Trees.
* @example * @example
* const MerkleTree = require('m-tree') * const MerkleTree = require('m-tree')
@ -30,8 +30,8 @@ class MerkleTree {
* *
* const tree = new MerkleTree(leaves, sha256) * const tree = new MerkleTree(leaves, sha256)
*/ */
constructor(leaves, hashAlgo, options={}) { constructor(leaves, hashAlgorithm, options={}) {
this.hashAlgo = hashAlgo this.hashAlgo = hashAlgorithm
this.leaves = leaves this.leaves = leaves
this.layers = [leaves] this.layers = [leaves]
this.isBitcoinTree = !!options.isBitcoinTree this.isBitcoinTree = !!options.isBitcoinTree