Merkle tree


  • MerkleTree


  • Constructor


    • levels: string | number

      Number of levels in the tree

    • elements: BigNumberish[] = []

      BigNumberish[] of initial elements

    • options: {
          hashFunction: undefined | ((left: BigNumberish, right: BigNumberish) => BigNumber);
          zeroElement: undefined | BigNumberish;
      } = {}

      Object with the following properties: hashFunction - Function used to hash 2 leaves zeroElement - Value for non-existent leaves

      • hashFunction: undefined | ((left: BigNumberish, right: BigNumberish) => BigNumber)
      • zeroElement: undefined | BigNumberish

    Returns MerkleTree


_hash: ((left: BigNumberish, right: BigNumberish) => BigNumber)

Type declaration

    • (left: BigNumberish, right: BigNumberish): BigNumber
    • Parameters

      • left: BigNumberish
      • right: BigNumberish

      Returns BigNumber

_layers: BigNumber[][]
_zeros: BigNumber[]
capacity: number
levels: number
zeroElement: BigNumber


  • Insert multiple elements into the tree.


    • elements: BigNumberish[]

      Elements to insert

    Returns void

  • Find an element in the tree


    number - Index if element is found, otherwise -1


    • element: BigNumberish

      An element to find

    Returns number

  • Insert new element into the tree


    • element: BigNumberish

      Element to insert

    Returns void

  • Get merkle path to a leaf


    pathElements: Object[], pathIndex: number[] - An object containing adjacent elements and left-right index


    • index: number

      Leaf index to generate path for

    Returns MerkleProof

  • Serialize entire tree state including intermediate layers into a plain object Deserializing it back will not require to recompute any hashes Elements are not converted to a plain type, this is responsibility of the caller

    Returns {
        _layers: BigNumber[][];
        _zeros: BigNumber[];
        levels: number;

    • _layers: BigNumber[][]
    • _zeros: BigNumber[]
    • levels: number
  • Change an element in the tree


    • index: number

      Index of element to change

    • element: BigNumberish

      Updated element value

    Returns void

  • This function calculates the desired index given the pathIndices


    • pathIndices: number[]

      an array of (0, 1) values representing (left, right) selection of nodes for each level in the merkle tree. The leaves level of the tree is at index 0 and the root of the tree is at index 'levels'

    Returns number

  • Create a merkle tree with the target root by inserting the given leaves one-by-one. If the root matches after an insertion, return the tree. Else, return undefined.


    MerkleTree | undefined


    • levels: number
    • leaves: string[]

      An array of ordered leaves to be inserted in a merkle tree

    • targetRoot: string

      The root that the caller is trying to build a tree against

    Returns undefined | MerkleTree

  • Deserialize data into a MerkleTree instance Make sure to provide the same hashFunction as was used in the source tree, otherwise the tree state will be invalid


    • data: any
    • hashFunction: any

    Returns any

Generated using TypeDoc