Namespace
Array of hashed leaves. Each leaf must be a Buffer.
Optional
hashFn: anyOptional
options: OptionsAdditional options
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.
const MerkleTree = require('merkletreejs')
const crypto = require('crypto')
function sha256(data) {
// returns Buffer
return crypto.createHash('sha256').update(data).digest()
}
const leaves = ['a', 'b', 'c'].map(value => keccak(value))
const tree = new MerkleTree(leaves, sha256)
addLeaf
Optional
shouldHash: booleanaddLeaves
Optional
shouldHash: booleanbufferToHex
Optional
withPrefix: booleangetHexMultiProof
Tree indices.
getHexProof
Target leaf
Optional
index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getLeafIndex
getPositionalHexProof
Target leaf
Optional
index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getProof
Target leaf
Optional
index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getProofFlags
verifyMultiProof
Returns true if the multiproofs can connect the leaves to the Merkle root.
const leaves = tree.getLeaves()
const root = tree.getRoot()
const treeFlat = tree.getLayersFlat()
const leavesCount = leaves.length
const proofIndices = [2, 5, 6]
const proofLeaves = proofIndices.map(i => leaves[i])
const proof = tree.getMultiProof(treeFlat, indices)
const verified = tree.verifyMultiProof(root, proofIndices, proofLeaves, leavesCount, proof)
Static
bigStatic
binaryStatic
bufferbufferToHex
Optional
withPrefix: booleanStatic
bufferifyStatic
getStatic
hexStatic
isStatic
linearStatic
marshalStatic
marshalStatic
marshalStatic
printStatic
unmarshalStatic
unmarshalStatic
unmarshalOptional
hashFn: anyOptional
options: OptionsStatic
verify
Class reprensenting a Merkle Tree MerkleTree