Merkle tree

Hierarchy

  • MerkleTree

Constructors

  • Parameters

    • levels: number
    • poseidonHash2: any
    • elements: string[] = []
    • __namedParameters: {
          zeroElement: undefined | string;
      } = {}
      • zeroElement: undefined | string

    Returns MerkleTree

Properties

_hash: any
_layers: string[][]
_zeros: string[]
capacity: number
levels: number

Constructor

Param

Number of levels in the tree

Param

Initial elements

Param

Param

Function used to hash 2 leaves

Param

Value for non-existent leaves

zeroElement: string

Methods

  • Insert multiple elements into the tree. Tree will be fully rebuilt during this operation.

    Parameters

    • elements: string[]

      Elements to insert

    Returns void

  • Find an element in the tree

    Parameters

    • element: string

      An element to find

    • comparator: null | Function = null

      A function that checks leaf value equality

    Returns number

    Index if element is found, otherwise -1

  • Insert new element into the tree

    Parameters

    • element: string

      Element to insert

    Returns void

  • Get merkle path to a leaf

    Parameters

    • index: number

      Leaf index to generate path for

    Returns {
        pathElements: string[];
        pathIndices: number[];
    }

    An object containing adjacent elements and left-right index

    • pathElements: string[]
    • pathIndices: 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: string[][];
        _zeros: string[];
        levels: number;
    }

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

    Parameters

    • index: number

      Index of element to change

    • element: string

      Updated element value

    Returns void

  • 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: Function

    Returns any

Generated using TypeDoc