UNPKG

@kevincharm/sparse-merkle-tree

Version:

Sparse Merkle Tree implementation in Solidity with accompanying JS library

66 lines (65 loc) 2.18 kB
import { SparseMerkleTreeKVOptions, Proof, UpdateProof } from './SparseMerkleTree'; export declare const SMT_DEPTH = 256; export declare const ZERO = 0n; /** * SparseMerkleTreeKV * SMT-backed key-value database. */ export declare class SparseMerkleTreeKV { db: Map<bigint, [bigint, bigint, bigint?]>; private _root; private _hashFn; private _deserialiserFn; private _serialiserFn; constructor(options?: SparseMerkleTreeKVOptions); hash(...inputs: bigint[]): bigint; get root(): string; /** * Get a proof of membership (or non-membership) of a key * * @param key Key * @returns {Proof} Proof of membership (or non-membership) */ get(key: string): Proof; /** * Verify membership of a leaf using a Merkle proof. * * @param leaf Leaf = H(key, value, 1) * @param index Index = H(key), i.e. path of leaf in the Merkle tree * @param enables 256-bitstring signifying which siblings are non-zero in the path * @param siblings Non-zero sibling subtree hashes * @returns {boolean} true if the proof is valid for this tree */ verifyProof(leaf: string, index: bigint, enables: bigint, siblings: string[]): boolean; /** * Walk down the tree to determine whether a key exists in the database. * * @param key * @returns true if key exists */ exists(key: bigint): boolean; /** * Insert a (key,value) into the database. Throws if key already exists. * * @param key Key * @param value Value * @returns {Proof} Membership of previous (key,value) leaf */ insert(key: string, value: string): UpdateProof; /** * Update a value belonging to an existing key. Throws if key does not exist. * * @param key Key * @param value New value * @returns {Proof} Membership of previous (key,value) leaf */ update(key: string, value: string): UpdateProof; /** * Update a value at key, regardless of whether it already exists or not. * * @param key Key * @param value Value * @returns {Proof} Membership of previous (key,value) leaf */ private upsert; }