Options
All
  • Public
  • Public/Protected
  • All
Menu

Class representing a Merkle tree.

Hierarchy

  • Tree

Index

Constructors

  • new Tree(data: Uint8Array[], hashName?: string, options?: { debug: undefined | boolean; requireBalanced: undefined | boolean }): Tree
  • Create a new Merkle tree.

    example

    Create a new Merkle tree with a SHA-256 hash function, and a binary proof.

    import { Tree } from '@truestamp/tree'
    const data = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
    const tree = new Tree(data, 'sha256')
    const root = tree.root()
    const proof = tree.proof(data[0])
    const verified = tree.verify(root, proof, data[0], 'sha256')
    example

    Create a new Merkle tree with a SHA-256 hash function, and a hex proof.

    import { Tree } from '@truestamp/tree'
    const data = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
    const tree = new Tree(data, 'sha256')
    const root = tree.root()
    const proof = tree.proofHex(data[0])
    const verified = tree.verify(root, proof, data[0], 'sha256')
    example

    Create a new Merkle tree with a SHA-256 hash function, and an object proof.

    import { Tree } from '@truestamp/tree'
    const data = [new Uint8Array([1, 2, 3]), new Uint8Array([4, 5, 6])]
    const tree = new Tree(data, 'sha256')
    const root = tree.root()
    const proof = tree.proofObject(data[0])
    const verified = tree.verify(root, proof, data[0])

    Parameters

    • data: Uint8Array[]

      The array of Uint8Array data values that the tree will be constructed from.

    • hashName: string = 'sha256'

      The hash function that will be used to create the tree. Defaults to 'sha256'.

    • options: { debug: undefined | boolean; requireBalanced: undefined | boolean } = ...

      The options to use when creating the tree.

      • debug: undefined | boolean
      • requireBalanced: undefined | boolean

    Returns Tree

Methods

  • height(): number
  • Get the Merkle tree height, which is the number of hashing layers that resulted after building the tree not including the Merkle root. Should be Math.ceil(Math.log2(data.length)).

    Returns number

    The tree height value.

  • proof(dataItem: Uint8Array): Uint8Array
  • Get the Merkle inclusion proof for specific data in raw form.

    Parameters

    • dataItem: Uint8Array

      The single data item, already added to the tree, to get the proof for.

    Returns Uint8Array

    The Merkle inclusion proof value.

  • proofHex(dataItem: Uint8Array): string
  • Get the Merkle inclusion proof for specified data as a single Hex encoded string.

    Parameters

    • dataItem: Uint8Array

      The single data item, already added to the tree, to get the proof for.

    Returns string

    The Merkle inclusion proof value.

  • proofObject(dataItem: Uint8Array): { h: "sha224" | "sha256" | "sha384" | "sha512" | "sha512_256" | "sha3_224" | "sha3_256" | "sha3_384" | "sha3_512"; p: [number, string][]; v: number }
  • Get the Merkle inclusion proof for specified data as a JavaScript Object with Hex encoded hash values.

    Parameters

    • dataItem: Uint8Array

      The single data item, already added to the tree, to get the proof for.

    Returns { h: "sha224" | "sha256" | "sha384" | "sha512" | "sha512_256" | "sha3_224" | "sha3_256" | "sha3_384" | "sha3_512"; p: [number, string][]; v: number }

    The Merkle inclusion proof value.

    • h: "sha224" | "sha256" | "sha384" | "sha512" | "sha512_256" | "sha3_224" | "sha3_256" | "sha3_384" | "sha3_512"
    • p: [number, string][]
    • v: number
  • root(): Uint8Array
  • verify(root: Uint8Array, proof: string | Uint8Array | { h: "sha224" | "sha256" | "sha384" | "sha512" | "sha512_256" | "sha3_224" | "sha3_256" | "sha3_384" | "sha3_512"; p: [number, string][]; v: number }, data: Uint8Array, hashName?: string): boolean
  • Verify a proof against the Merkle root, data, and hashFunction generated at Tree creation time.

    Parameters

    • root: Uint8Array

      The Merkle root value of a tree.

    • proof: string | Uint8Array | { h: "sha224" | "sha256" | "sha384" | "sha512" | "sha512_256" | "sha3_224" | "sha3_256" | "sha3_384" | "sha3_512"; p: [number, string][]; v: number }

      The Merkle inclusion proof that allows traversal from the data to the root. The proof can be provided in any of the supported encodings.

    • data: Uint8Array

      A single data item, exactly as added to the original tree, that the proof was generated for.

    • Optional hashName: string

      The hash function name, must be the same as that used to create the tree originally. Optional if providing an object encoded proof which already has the hash function name embedded.

    Returns boolean

    A boolean to indicate verification success or failure.