Merklie
Library for creating Merkle trees, generating merkle proofs, and verification of merkle proofs.
Installation
$ npm install --save merklie
Create Merklie Object
const Merklie = const treeOptions = hashType: 'md5' // optional, defaults to 'sha256'// valid hashTypes include all crypto hash algorithms// such as 'MD5', 'SHA1', 'SHA224', 'SHA256', 'SHA384', 'SHA512'// as well as the SHA3 family of algorithms// including 'SHA3-224', 'SHA3-256', 'SHA3-384', and 'SHA3-512' const merklie = treeOptions // treeOptions is optional
Methods
addLeaf(value, doHash, returnType)
Adds a value as a leaf to the tree. The value must be either a Buffer or a hex string, otherwise set the optional doHash to true to have your value hashed prior to being added to the tree. It returns the index of the leaf added.
const hexData = '05ae04314577b2783b4be98211d1b72476c59e9c413cfb2afa2f0c68e0d93911'const otherData = 'Some text data, perhaps' merkliemerklie
addLeaves(valueArray, doHash, returnType)
Adds an array of values as leaves to the tree. The values must be either a Buffers or a hex strings, otherwise set the optional doHash to true to have your values hashed prior to being added to the tree. Returns the indexes of the leaves added.
const hexData = '05ae04314577b2783b4be98211d1b72476c59e9c413cfb2afa2f0c68e0d93911' 'c5ed1192d909d1af814f64c7dc9e6a4983a63891a2c59ed14448d90271cb5519' '4bac27393bdd9777ce02453256c5577cd02275510b2227f473d03f533924f877'const otherData = 'l' 'm' 'n' 'o' 'p' merkliemerklie
getLeafCount()
Returns the number of leaves that are currently added to the tree.
const leafCount = merklie
getLeaf(index, asBinary)
@param index: number, hash, or Buffer
@param asBinary: boolean
Returns the value of the leaf at the given index as a hash or a Buffer. Returns null if no leaf exists at the given index.
const leafValue = merklie
resetTree()
Removes all the leaves from the tree, prepararing to begin creating a new tree.
merklie
makeTree(doubleHash)
Generates the merkle tree using the leaves that have been added.
const doubleHash = false // true to hash pairs twice as the tree is constructed merklie
makeBTCTree(doubleHash)
Generates the merkle tree with the flawed Bitcoin merkle tree implementation. This should only be used when you need to replicate Bitcoin constructed merkle trees.
const doubleHash = true // true to hash pairs twice as the tree is constructed merklie
getTreeReadyState()
Returns boolean indicating if the tree is built and ready to supply its root and proofs. The Ready state is True only after the tree is built with 'makeTree'. Adding leaves or resetting the tree will change the ready state to False.
const isReady = merklie
getMerkleRoot()
Returns the merkle root of the tree as a Buffer. If the tree is not ready, null is returned.
const rootValue = merklie
getProof(index, asBinary)
@param index: can be a number, hash, buffer
@param asBinary: boolean
Returns the proof as an object of hash objects or array of Buffers for the leaf at the given index. If the tree is not ready or no leaf exists at the given index, null is returned.
const proof = merklie // By default, an array of hash objects is returned// example: // proof = {// 0 :{ right: '09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5' },// 1: { right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' },// 2: { left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' }// ] const proof = merklie // With asBinary set to true, an array of Buffers is returned // 0x00 indicated 'left', 0x01 indicates 'right'// example: // proof == {// 0: { <Buffer 01>: <Buffer 09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5>},// 1: { <Buffer 01>: <Buffer ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f>},// 2: { <Buffer 00>: <Buffer eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8>}// }
The proof object contains a set of merkle sibling objects. Each object contains the sibling hash, with the key value of either right or left. The right or left value tells you where that sibling was in relation to the current hash being evaluated. This information is needed for proof validation, as explained in the following section.
validateProof(proof, targetHash, merkleRoot, doubleHash)
Returns a boolean indicating whether or not the proof is valid and correctly connects the targetHash to the merkleRoot. Proof is a proof object as supplied by the 'getProof' method. The targetHash and merkleRoot parameters must be Buffers or hex strings. Setting doubleHash to true will double each hash operation to match the Bitcoin merkle tree style.
const proof = right: '09096dbc49b7909917e13b795ebf289ace50b870440f10424af8845fb7761ea5' right: 'ed2456914e48c1e17b7bd922177291ef8b7f553edf1b1f66b6fc1a076524b22f' left: 'eac53dde9661daf47a428efea28c81a021c06d64f98eeabbdcff442d992153a8' const targetHash = '36e0fd847d927d68475f32a94efff30812ee3ce87c7752973f4dd7476aa2e97e'const merkleRoot = 'b8b1f39aa2e3fc2dde37f3df04e829f514fb98369b522bfb35c663befa896766' const isValid = merklie
The proof process uses all the proof objects in the array to attempt to prove a relationship between the targetHash and the merkleRoot values. The steps to validate a proof are:
- Concatenate targetHash and the first hash in the proof array. The right or left designation specifies which side of the concatenation that the proof hash value should be on.
- Hash the resulting value.
- Concatenate the resulting hash with the next hash in the proof array, using the same left and right rules.
- Hash that value and continue the process until you’ve gone through each item in the proof array.
- The final hash value should equal the merkleRoot value if the proof is valid, otherwise the proof is invalid.
dehydrate(toString)
Returns a dehydrated version (from buffers to hashes) of the leaves to JSON. If toString is set to true, it will stringify the result
rehydrate(dehydratedLeaves)
Returns a boolean if the operation was successful and rebuilds the hash tree from the dehydrated leaves.
reHash(value, noHash)
Returns the hash of a given document.
Common Usage
Creating a tree and generating the proofs
const Merklie = const merklie = // no options, defaults to sha-256 hash type // add some leaves to the treemerkliemerkliemerklie // we must indicate these values need to be hashed merklie const proof0 = merklieconst proof1 = merklieconst proof2 = merklie // use this when done with this tree and you intend on creating a new onemerklie
Notes
About tree generation using makeTree()
-
Internally, leaves are stored as Buffers. When the tree is built, it is generated by hashing together the Buffer values.
-
Lonely leaf nodes are promoted to the next level up, as depicted below.
ROOT=Hash(H+E) / \ / \ H=Hash(F+G) E / \ \ / \ \ F=Hash(A+B) G=Hash(C+D) E / \ / \ \ / \ / \ \ A B C D E
Benchmarks
// 75000 leaves x 0.77 ops/sec ±9.02% (6 runs sampled) moe: 01179683061334842 rme: 902469439155465 sem: 0045884210864832436 deviation: 01123929038691075 mean: 13071723098333334 sample: 1339671007 1233346814 1365459061 1187010785 1488181433 1229364759 variance: 0012632164840130439