Options
All
  • Public
  • Public/Protected
  • All
Menu

Merkle tree

Hierarchy

  • MerkleTree

Index

Constructors

  • new MerkleTree(levels: string | number, elements?: BigNumberish[], __namedParameters?: { hashFunction: (a: any, b: any) => BigNumber; zeroElement: string }): MerkleTree
  • Constructor

    Parameters

    • levels: string | number

      Number of levels in the tree

    • elements: BigNumberish[] = []
    • __namedParameters: { hashFunction: (a: any, b: any) => BigNumber; zeroElement: string } = {}
      • hashFunction: (a: any, b: any) => BigNumber
          • (a: any, b: any): BigNumber
          • Parameters

            • a: any
            • b: any

            Returns BigNumber

      • zeroElement: string

    Returns MerkleTree

Properties

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

Type declaration

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

      • left: BigNumberish
      • right: BigNumberish

      Returns BigNumberish

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

Methods

  • _rebuild(): void
  • bulkInsert(elements: any): void
  • elements(): BigNumberish[]
  • getIndexByElement(element: any): number
  • indexOf(element: any, comparator?: any): number
  • Find an element in the tree

    Parameters

    • element: any

      An element to find

    • Optional comparator: any

      A function that checks leaf value equality

    Returns number

    Index if element is found, otherwise -1

  • insert(element: any): void
  • number_of_elements(): number
  • path(index: number): { element: BigNumberish; merkleRoot: BigNumberish; pathElements: any[]; pathIndices: any[] }
  • Get merkle path to a leaf

    Parameters

    • index: number

      Leaf index to generate path for

    Returns { element: BigNumberish; merkleRoot: BigNumberish; pathElements: any[]; pathIndices: any[] }

    An object containing adjacent elements and left-right index

    • element: BigNumberish
    • merkleRoot: BigNumberish
    • pathElements: any[]
    • pathIndices: any[]
  • root(): BigNumberish
  • serialize(): { _layers: BigNumberish[][]; _zeros: BigNumberish[]; levels: number }
  • 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: BigNumberish[][]; _zeros: BigNumberish[]; levels: number }

    • _layers: BigNumberish[][]
    • _zeros: BigNumberish[]
    • levels: number
  • update(index: number, element: any): void
  • zeros(): BigNumberish[]
  • createTreeWithRoot(levels: number, leaves: string[], targetRoot: string): MerkleTree
  • 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.

    Parameters

    • 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 MerkleTree

  • deserialize(data: any, hashFunction: any): any
  • 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

    Parameters

    • data: any
    • hashFunction: any

    Returns any

Generated using TypeDoc