Class: MerkleTree

merkletree.MerkleTree(elements, ordered)

MerkleTree A special thanks to blog posts by ameensol and raiden-network team and their work on merkle proofs A class the implements both ordered and unordered leaf nodes for a merkele tree leverages keccak256 hashes if the leaves are to be unordered, then the intermediate nodes are ordered by their buffer values and concatenated prior to; used primarily for validation of objects. By default, order is not preserved and we use Buffer.Compare order

Constructor

new MerkleTree(elements, ordered)

Parameters:
Name Type Description
elements Array.<Buffer> an array of 32 Byte hashed Buffer elements that will make up the tree
ordered bool determines if the elements preserve ordering as we walk up the tree to generate proofs
Source:

Methods

_getProofPair(index, level) → {Buffer}

Internal _getProofPair - returns the adjacent object in the level when walking the tree to generate a proof
Parameters:
Name Type Description
index int
level Array.<Buffer>
Source:
Returns:
or Null
Type
Buffer

generateHashTree()

Internal generateHashTree - a side-effect driven function that generates the levels of the merkle tree; there are log2(n) levels
Source:

generateProof(hashedElement) → {Array.<Buffer>}

generateProof - generates a merkle tree proof for the given hashed element
Parameters:
Name Type Description
hashedElement Buffer a 32 Byte Buffer of the hashed element
Source:
Returns:
Type
Array.<Buffer>

getRoot() → {Buffer}

getRoot - returns the 32 byte merkle root element or 0 buffer otherewise
Source:
Returns:
Type
Buffer

sumLevel()

Internal sumLevell
Source:

verify(proof, hashedElement) → {bool}

Internal verify - returns true or false if the proof and hashedElement equate to the current merkleTree's root
Parameters:
Name Type Description
proof Array.<Buffer> an array of bytes32 elements that represent the merkle proof
hashedElement Buffer hashed bytes32 leaf element to prove exists in the tree
Source:
Returns:
Type
bool