UNPKG

@variablesoftware/ts-merkle

Version:

🌳🔗🛡️ A TypeScript library for creating and verifying Merkle trees.

45 lines (44 loc) 1.73 kB
/** * A single Merkle proof node: sibling hash + its position. * @property sibling The sibling node's hash * @property position The sibling's position relative to the current node ('left' or 'right') */ export type ProofNode = { sibling: Uint8Array; position: 'left' | 'right'; }; /** * Options for Merkle tree operations. */ export type MerkleOptions = { /** If true, sort each pair before hashing (default: true) */ sort?: boolean; /** If true, duplicate the last odd node to make pairs (default: true) */ pad?: boolean; }; /** * Compute the Merkle root for a list of leaves. * @param leaves Array of leaf hashes (Uint8Array) * @param options Options for sorting and padding * @returns The Merkle root hash as a Uint8Array * @throws If leaves array is empty */ export declare function computeMerkleRoot(leaves: Uint8Array[], options?: MerkleOptions): Uint8Array; /** * Generate a Merkle proof for a given leaf index. * @param leaves Array of leaf hashes (Uint8Array) * @param index Index of the leaf to prove * @param options Options for sorting and padding * @returns Array of ProofNode objects * @throws If index is out of bounds */ export declare function computeMerkleProof(leaves: Uint8Array[], index: number, options?: MerkleOptions): ProofNode[]; /** * Verify that a given leaf + proof yields the expected root. * @param leaf The leaf hash * @param proof Array of sibling nodes (ProofNode[]) * @param root The expected Merkle root * @param options Options for sorting * @returns `true` if the proof is valid, else `false` */ export declare function verifyMerkleProof(leaf: Uint8Array, proof: ProofNode[], root: Uint8Array, options?: Pick<MerkleOptions, 'sort'>): boolean;