UNPKG

merkle-tree-lib

Version:

Merkle Tree implementation with BIP340 tagged hash support.

107 lines (106 loc) 3.46 kB
import { HashStrategy } from '../hash/HashStrategy'; import { MerkleProof } from '../proof/MerkleProof'; /** * Default tag constants for hashing */ export declare const DEFAULT_TAGS: { LEAF: string; BRANCH: string; }; /** * MerkleTree - Implementation of a Merkle tree data structure * * A Merkle tree is a binary tree of hashes where leaf nodes contain * hashes of data blocks, and non-leaf nodes contain hashes of their children. */ export declare class MerkleTree { /** Original data leaves */ private readonly leaves; /** Hash of the leaves */ private readonly leafHashes; /** Tree structure - array of arrays, each inner array is a level in the tree */ private readonly levels; /** Strategy for hashing leaf nodes */ private readonly leafHashStrategy; /** Strategy for hashing branch nodes */ private readonly branchHashStrategy; /** * Create a new Merkle tree * * @param data - Array of data elements to include in the tree * @param leafHashStrategy - Strategy to use for hashing leaves (default: TaggedSha256 with 'MERKLE_LEAF' tag) * @param branchHashStrategy - Strategy to use for hashing branches (default: TaggedSha256 with 'MERKLE_BRANCH' tag) */ constructor(data: string[], leafHashStrategy?: HashStrategy, branchHashStrategy?: HashStrategy); /** * Build the Merkle tree from leaf hashes * * @param leafHashes - Array of leaf node hashes * @returns 2D array representing tree levels */ private buildTree; /** * Get the Merkle root hash * * @returns The root hash as a Buffer */ getRoot(): Buffer; /** * Get the Merkle root hash as a hex string * * @returns The root hash as a hex string */ getRootHex(): string; /** * Generate a Merkle proof for a specific leaf * * @param index - Index of the leaf in the original data array * @returns A MerkleProof object * @throws Error if the index is out of bounds */ generateProof(index: number): MerkleProof; /** * Get the number of leaves in the tree * * @returns The count of leaf nodes */ getLeafCount(): number; /** * Get the original data leaf at the specified index * * @param index - Index in the original data array * @returns The original data string * @throws Error if the index is out of bounds */ getLeaf(index: number): string; /** * Get the hash of the leaf at the specified index * * @param index - Index in the original data array * @returns The leaf hash as a Buffer * @throws Error if the index is out of bounds */ getLeafHash(index: number): Buffer; /** * Check if the tree contains a specific leaf value * * @param data - The leaf data to check * @returns The index of the leaf if found, -1 otherwise */ findLeaf(data: string): number; /** * Export the tree structure for visualization or debugging * * @returns 2D array of hex strings representing the tree */ exportTree(): string[][]; /** * Update a leaf and recompute affected tree nodes * * @param index - Index of the leaf to update * @param newData - New data for the leaf * @returns The new root hash * @throws Error if the index is out of bounds */ updateLeaf(index: number, newData: string): Buffer; }