Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MerkleTree

The Merkle Tree provides you with a way to audit a vector of data by comparing the hashes of each datum. It does this by building a layer using your initial hashes, and then takes a hash sum of two hashes until it reaches the last hash, effectively creating a smaller layer on top.

Note that since the Merkle Tree is a binary tree, whenever a layer has an odd amount of nodes, it must duplicate the odd node and add the hash to itself.

export
class

MerkleTree

Hierarchy

  • MerkleTree

Index

Constructors

Private constructor

Properties

Private _hashes

_hashes: string[] = []

Internally stores the hashes of all of the data that was passed in.

see

{MerkleTree#addNode}

see

{MerkleTree#addNodes}

type

{string[]}

memberof

MerkleTree

Private _isDirty

_isDirty: boolean = true

Internally stores the state of whether something has been added to the tree since the last time it was updated or not.

see

{MerkleTree#isDirty}

type

{boolean}

memberof

MerkleTree

Private _rootHash

_rootHash: string = ""

Internally stores the root hash. Should only be accessible through

#computeRootHash.

see

{MerkleTree#computeRootHash}

type

{string}

memberof

MerkleTree

Accessors

isDirty

  • get isDirty(): boolean
  • Use this to check if a node has been added to the tree between now and the last time #computeRootHash was called.

    readonly
    type

    {boolean}

    memberof

    MerkleTree

    Returns boolean

length

  • get length(): number
  • The count of each node in the base of the tree.

    readonly
    type

    {number}

    memberof

    MerkleTree

    Returns number

Methods

Private _addNode

  • _addNode(data: any): Promise<number>
  • Parameters

    • data: any

    Returns Promise<number>

addNode

  • addNode(data: any): Promise<number>
  • memberof

    MerkleTree

    Parameters

    • data: any

      Any form of data can be passed here. It will be stringified, hashed, and only store the hash.

    Returns Promise<number>

    A promise containing the new length of the base of the Merkle Tree.

addNodes

  • addNodes(data: any[]): Promise<number>
  • Adds an array of data to the hash nodes.

    see

    {MerkleTree#addNode}

    memberof

    MerkleTree

    Parameters

    • data: any[]

      An array of any data.

    Returns Promise<number>

    A promise containing the new length of the base of the Merkle Tree.

compareWith

  • compareWith(thatTree: MerkleTree): Promise<boolean>
  • Use this to compare the root hash of this tree with another tree.

    example
    async function foo() {
        const data = [1, 2, 3];
        const tree1 = await MerkleTree.createWith(data);
        const tree2 = await MerkleTree.createWith(data);
        return await tree1.compareWith(tree2);
    }
    memberof

    MerkleTree

    Parameters

    Returns Promise<boolean>

computeRootHash

  • computeRootHash(): Promise<string>
  • Constructs a Merkle Tree based off of the hashes found in the endmost descendant nodes (the hashes that came from your data). It then builds all of the intermediate nodes of the tree until it reaches the root, at which it will then return the root hash which is comprised of the sum of all of the descendant node hashes.

    Note: This will return the previous hash which is cached in the tree as long as nothing has been added to the tree since the last time #computeRootHash was called.

    Diagram of the Merkle Tree result:

              H(7)           where H(x) is a hashing function
            /      \         that takes any data as it's input,
        H(5)        H(6)     H(5) = H(1) + H(2),
       /    \      /    \    H(6) = H(3) + H(4),
     H(1)   H(2) H(3)   H(4) H(7) = H(5) + H(6).
    memberof

    MerkleTree

    Returns Promise<string>

Static Private buildLayer

  • buildLayer(hashes: string[]): Promise<string[]>
  • Used internally to convert a layer of hashes in a Merkle Tree to the next smaller layer. This is where the most of the Merkle Tree algorithm resides.

    static
    memberof

    MerkleTree

    Parameters

    • hashes: string[]

    Returns Promise<string[]>

Static create

  • Use this to get a new instance of a MerkleTree.

    static
    memberof

    MerkleTree

    Returns MerkleTree

Static createWith

  • Create a new instance of a MerkleTree with predefined data.

    static
    memberof

    MerkleTree

    Parameters

    • data: any[]

      An array of any data.

    Returns Promise<MerkleTree>

    A Merkle Tree containing the hashes of the data that was passed in.

Generated using TypeDoc