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)
Array of hashed leaves. Each leaf must be a Buffer.
Optional hashFn: anyOptional options: OptionsAdditional options
Private calculatePrivate completePrivate concatenatorPrivate createPrivate duplicatePrivate fillPrivate getPrivate getgetPairNode
Returns the node at the index for given layer.
Tree layer
Index at layer.
const node = tree.getPairNode(layer, index)
Private getPrivate hashPrivate hashPrivate isPrivate isPrivate layersPrivate leavesPrivate processPrivate sortPrivate sortPrivate sortPrivate verifybinarySearch
Returns the first index of which given item is found in array using binary search.
const index = tree.binarySearch(array, element, Buffer.compare)
Protected buffergetHexMultiProof
Returns the multiproof for given tree indices as hex strings.
const indices = [2, 5, 6]
const proof = tree.getHexMultiProof(indices)
Tree indices.
getHexProof
Returns the proof for a target leaf as hex strings.
const proof = tree.getHexProof(leaves[2])
Target leaf
Optional index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getMultiProof
Returns the multiproof for given tree indices.
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
Optional tree: any[]Optional indices: any[]Tree indices.
getPositionalHexProof
Returns the proof for a target leaf as hex strings and the position in binary (left == 0).
const proof = tree.getPositionalHexProof(leaves[2])
Target leaf
Optional index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getProof
Returns the proof for a target leaf.
const proof = tree.getProof(leaves[2])
const leaves = ['a', 'b', 'a'].map(value => keccak(value))
const tree = new MerkleTree(leaves, keccak)
const proof = tree.getProof(leaves[2], 2)
Target leaf
Optional index: numberTarget leaf index in leaves array. Use if there are leaves containing duplicate data in order to distinguish it.
getProofFlags
Returns list of booleans where proofs should be used instead of hashing. Proof flags are used in the Solidity multiproof verifiers.
const indices = [2, 5, 6]
const proof = tree.getMultiProof(indices)
const proofFlags = tree.getProofFlags(leaves, proof)
getProofIndices
Returns the proof indices for given tree indices.
const proofIndices = tree.getProofIndices([2,5,6], 4)
console.log(proofIndices) // [ 23, 20, 19, 8, 3 ]
Tree indices
Tree depth; number of layers.
getProofs
Returns the proofs for all leaves.
const proofs = tree.getProofs()
const leaves = ['a', 'b', 'a'].map(value => keccak(value))
const tree = new MerkleTree(leaves, keccak)
const proofs = tree.getProofs()
getProofsDFS
Get all proofs through single traverse
const layers = tree.getLayers()
const index = 0;
let proof = [];
let proofs = [];
const proof = tree.getProofsDFS(layers, index, proof, proofs)
Current layer index in traverse.
Current tarvese node index in traverse.
Proof chain for single leaf.
Proofs for all leaves
Protected islinearSearch
Returns the first index of which given item is found in array using linear search.
const index = tree.linearSearch(array, element, (a, b) => a === b)
Protected log2Protected toverify
Returns true if the proof path (array of hashes) can connect the target node to the Merkle root.
const root = tree.getRoot()
const proof = tree.getProof(leaves[2])
const verified = tree.verify(proof, leaves[2], root)
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)
Protected zipStatic bigStatic binarybinarySearch
Returns the first index of which given item is found in array using binary search.
const index = MerkleTree.binarySearch(array, element, Buffer.compare)
Static bufferStatic bufferifyStatic getgetMultiProof
Returns the multiproof for given tree indices.
const flatTree = tree.getLayersFlat()
const indices = [2, 5, 6]
const proof = MerkleTree.getMultiProof(flatTree, indices)
Tree as a flat array.
Tree indices.
Static hexStatic isStatic linearlinearSearch
Returns the first index of which given item is found in array using linear search.
const index = MerkleTree.linearSearch(array, element, (a, b) => a === b)
Static marshalStatic marshalStatic marshalStatic printStatic unmarshalStatic unmarshalStatic unmarshalOptional hashFn: anyOptional options: OptionsStatic verifyverify
Returns true if the proof path (array of hashes) can connect the target node to the Merkle root.
const verified = MerkleTree.verify(proof, leaf, root, sha256, options)
Generated using TypeDoc
Class reprensenting a Merkle Tree
Namespace
MerkleTree