UNPKG

merkle-tree-lib

Version:

Merkle Tree implementation with BIP340 tagged hash support.

45 lines (44 loc) 1.81 kB
/** * MerkleTree class: builds a binary Merkle tree using tagged hashing (BIP-0340). * It can compute the Merkle root and generate proofs for given leaves. */ export declare class MerkleTree { private leafHashes; private levels; root: Buffer; private tagHashLeaf; private tagHashBranch; /** * Initialize the Merkle tree with an array of data (strings) and hashing tags. * @param leavesData - Array of strings representing leaf data. * @param leafTag - Tag for hashing leaves (context-specific tag). Defaults to "Bitcoin_Transaction". * @param branchTag - Tag for hashing internal nodes. Defaults to the same as leafTag. */ constructor(leavesData: string[], leafTag?: string, branchTag?: string); /** Compute SHA256 hash of the tag (as UTF-8), used for tagged hashing. */ private computeTagHash; /** * Compute a tagged hash: SHA256( tagHash || tagHash || message ). * @param tagHash - Buffer of 32-byte hash of the tag. * @param msg - Message bytes to hash. * @returns 32-byte SHA256 digest. */ private taggedHash; /** Build the Merkle tree levels and root from the leaf hashes. */ private buildTree; /** * Get the Merkle root as a hex string. * @returns Hexadecimal string of the Merkle root. */ getHexRoot(): string; /** * Generate the Merkle proof for the leaf at the given index. * @param index - Index of the target leaf in the original input array. * @returns An array of proof elements, each a tuple [siblingHash (Buffer), side (0 or 1)]. * side = 0 indicates the sibling is the left node, side = 1 indicates the sibling is the right node. */ getProof(index: number): { siblingHash: Buffer; side: 0 | 1; }[]; }