merkletreejs
Version:
Construct Merkle Trees and verify proofs
256 lines (255 loc) • 8.92 kB
TypeScript
/// <reference types="node" />
import { MerkleTree, Options } from './MerkleTree';
export declare type Leaf = Buffer | string | number | BigInt;
export declare type LeafData = Buffer;
export declare type HashFunction = (data: any) => Buffer;
export declare type Proof = {
position: 'left' | 'right';
data: Buffer;
}[];
export declare type HexProof = string[];
/**
* Creates a Merkle tree from an array of leaves
* @param leaves - Array of leaves (strings, buffers, objects, etc.)
* @param hashFn - Optional hash function (defaults to SHA256)
* @param options - Merkle tree options
* @returns MerkleTree instance
*/
export declare function createMerkleTree(leaves: LeafData[], hashFn?: HashFunction, options?: Options): MerkleTree;
/**
* Gets the root hash of a Merkle tree as hex string
* @param tree - MerkleTree instance
* @returns Root hash as hex string
*/
export declare function getHexRoot(tree: MerkleTree): string;
/**
* Gets the root hash of a Merkle tree as Buffer
* @param tree - MerkleTree instance
* @returns Root hash as Buffer
*/
export declare function getRoot(tree: MerkleTree): Buffer;
/**
* Adds a leaf to an existing Merkle tree
* @param tree - MerkleTree instance
* @param leaf - Leaf to add
* @param options - Options object with shouldHash property
* @returns Updated MerkleTree instance
*/
export declare function addLeaf(tree: MerkleTree, leaf: LeafData, options?: {
shouldHash?: boolean;
}): MerkleTree;
/**
* Adds multiple leaves to an existing Merkle tree
* @param tree - MerkleTree instance
* @param leaves - Array of leaves to add
* @param options - Options object with shouldHash property
* @returns Updated MerkleTree instance
*/
export declare function addLeaves(tree: MerkleTree, leaves: LeafData[], options?: {
shouldHash?: boolean;
}): MerkleTree;
/**
* Gets a proof for a specific leaf
* @param tree - MerkleTree instance
* @param leaf - Target leaf
* @param index - Optional leaf index (for duplicate leaves)
* @returns Proof as array of objects with position and data
*/
export declare function getProof(tree: MerkleTree, leaf: LeafData, index?: number): Proof;
/**
* Gets a proof for a specific leaf as hex strings
* @param tree - MerkleTree instance
* @param leaf - Target leaf
* @param index - Optional leaf index (for duplicate leaves)
* @returns Proof as array of hex strings
*/
export declare function getHexProof(tree: MerkleTree, leaf: LeafData, index?: number): HexProof;
/**
* Verifies a proof against a root and target leaf
* @param proof - Proof array
* @param leaf - Target leaf
* @param root - Merkle root
* @param hashFn - Hash function used to create the tree
* @param options - Options used to create the tree
* @returns Boolean indicating if proof is valid
*/
export declare function verifyProof(proof: Proof | HexProof, leaf: LeafData, root: string | Buffer, hashFn?: HashFunction, options?: Options): boolean;
/**
* Gets all leaves from a Merkle tree
* @param tree - MerkleTree instance
* @returns Array of leaves as Buffers
*/
export declare function getLeaves(tree: MerkleTree): Buffer[];
/**
* Gets all leaves from a Merkle tree as hex strings
* @param tree - MerkleTree instance
* @returns Array of leaves as hex strings
*/
export declare function getHexLeaves(tree: MerkleTree): string[];
/**
* Gets a leaf from a Merkle tree as hex string
* @param tree - MerkleTree instance
* @param index - Leaf index
* @returns Leaf as hex string
*/
export declare function getHexLeaf(tree: MerkleTree, index: number): string;
/**
* Gets the leaf count
* @param tree - MerkleTree instance
* @returns Number of leaves
*/
export declare function getLeafCount(tree: MerkleTree): number;
/**
* Gets a specific leaf by index
* @param tree - MerkleTree instance
* @param index - Leaf index
* @returns Leaf as Buffer
*/
export declare function getLeaf(tree: MerkleTree, index: number): Buffer;
/**
* Gets the index of a specific leaf
* @param tree - MerkleTree instance
* @param leaf - Target leaf
* @returns Leaf index or -1 if not found
*/
export declare function getLeafIndex(tree: MerkleTree, leaf: LeafData): number;
/**
* Removes a leaf by index
* @param tree - MerkleTree instance
* @param index - Leaf index to remove
* @returns Removed leaf as Buffer
*/
export declare function removeLeaf(tree: MerkleTree, index: number): Buffer;
/**
* Updates a leaf at a specific index
* @param tree - MerkleTree instance
* @param index - Leaf index to update
* @param value - New leaf value
* @param options - Options object with shouldHash property
*/
export declare function updateLeaf(tree: MerkleTree, index: number, value: LeafData, options?: {
shouldHash?: boolean;
}): void;
/**
* Gets all proofs for all leaves
* @param tree - MerkleTree instance
* @returns Array of all proofs
*/
export declare function getProofs(tree: MerkleTree): Proof[];
/**
* Gets all proofs for all leaves as hex strings
* @param tree - MerkleTree instance
* @returns Array of all proofs as hex strings
*/
export declare function getHexProofs(tree: MerkleTree): string[];
/**
* Gets multiproof for multiple indices
* @param tree - MerkleTree instance
* @param indices - Array of leaf indices
* @returns Multiproof as array of Buffers
*/
export declare function getMultiProof(tree: MerkleTree, indices: number[]): Buffer[];
/**
* Gets multiproof for multiple indices as hex strings
* @param tree - MerkleTree instance
* @param indices - Array of leaf indices
* @returns Multiproof as array of hex strings
*/
export declare function getHexMultiProof(tree: MerkleTree, indices: number[]): string[];
/**
* Verifies a multiproof
* @param root - Merkle root
* @param proofIndices - Leaf indices for proof
* @param proofLeaves - Leaf values at indices
* @param leavesCount - Total number of leaves
* @param proof - Multiproof
* @param hashFn - Hash function
* @param options - Tree options
* @returns Boolean indicating if multiproof is valid
*/
export declare function verifyMultiProof(root: string | Buffer, proofIndices: number[], proofLeaves: LeafData[], leavesCount: number, proof: Buffer[] | string[], hashFn?: HashFunction, options?: Options): boolean;
/**
* Gets the tree depth
* @param tree - MerkleTree instance
* @returns Tree depth (number of layers - 1)
*/
export declare function getDepth(tree: MerkleTree): number;
/**
* Gets all layers of the tree
* @param tree - MerkleTree instance
* @returns Array of layers as Buffers
*/
export declare function getLayers(tree: MerkleTree): Buffer[][];
/**
* Gets all layers of the tree as hex strings
* @param tree - MerkleTree instance
* @returns Array of layers as hex strings
*/
export declare function getHexLayers(tree: MerkleTree): string[][];
/**
* Gets flattened layers
* @param tree - MerkleTree instance
* @returns Flattened array of all nodes
*/
export declare function getLayersFlat(tree: MerkleTree): Buffer[];
/**
* Gets flattened layers as hex strings
* @param tree - MerkleTree instance
* @returns Flattened array of all nodes as hex strings
*/
export declare function getHexLayersFlat(tree: MerkleTree): string[];
/**
* Resets the tree by clearing all leaves and layers
* @param tree - MerkleTree instance
*/
export declare function resetTree(tree: MerkleTree): void;
/**
* Gets tree options
* @param tree - MerkleTree instance
* @returns Tree options object
*/
export declare function getOptions(tree: MerkleTree): Options;
/**
* Converts tree to string representation
* @param tree - MerkleTree instance
* @returns String representation of the tree
*/
export declare function treeToString(tree: MerkleTree): string;
/**
* Marshals leaves to JSON string
* @param leaves - Array of leaves
* @returns JSON string representation
*/
export declare function marshalLeaves(leaves: LeafData[]): string;
/**
* Unmarshals leaves from JSON string
* @param jsonStr - JSON string or object
* @returns Array of leaves as Buffers
*/
export declare function unmarshalLeaves(jsonStr: string | object): Buffer[];
/**
* Marshals proof to JSON string
* @param proof - Proof array
* @returns JSON string representation
*/
export declare function marshalProof(proof: Proof | HexProof): string;
/**
* Unmarshals proof from JSON string
* @param jsonStr - JSON string or object
* @returns Proof array
*/
export declare function unmarshalProof(jsonStr: string | object): Proof;
/**
* Marshals entire tree to JSON string
* @param tree - MerkleTree instance
* @returns JSON string representation of the tree
*/
export declare function marshalTree(tree: MerkleTree): string;
/**
* Unmarshals tree from JSON string
* @param jsonStr - JSON string or object
* @param hashFn - Hash function
* @param options - Tree options
* @returns MerkleTree instance
*/
export declare function unmarshalTree(jsonStr: string | object, hashFn?: HashFunction, options?: Options): MerkleTree;