UNPKG

merkletreejs

Version:
361 lines (360 loc) 11.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.unmarshalTree = exports.marshalTree = exports.unmarshalProof = exports.marshalProof = exports.unmarshalLeaves = exports.marshalLeaves = exports.treeToString = exports.getOptions = exports.resetTree = exports.getHexLayersFlat = exports.getLayersFlat = exports.getHexLayers = exports.getLayers = exports.getDepth = exports.verifyMultiProof = exports.getHexMultiProof = exports.getMultiProof = exports.getHexProofs = exports.getProofs = exports.updateLeaf = exports.removeLeaf = exports.getLeafIndex = exports.getLeaf = exports.getLeafCount = exports.getHexLeaf = exports.getHexLeaves = exports.getLeaves = exports.verifyProof = exports.getHexProof = exports.getProof = exports.addLeaves = exports.addLeaf = exports.getRoot = exports.getHexRoot = exports.createMerkleTree = void 0; // eslint-disable-next-line no-unused-vars const MerkleTree_1 = require("./MerkleTree"); const sha256_1 = __importDefault(require("crypto-js/sha256")); // Default hash function const defaultHashFn = sha256_1.default; /** * 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 */ function createMerkleTree(leaves, hashFn = defaultHashFn, options = {}) { return new MerkleTree_1.MerkleTree(leaves, hashFn, options); } exports.createMerkleTree = createMerkleTree; /** * Gets the root hash of a Merkle tree as hex string * @param tree - MerkleTree instance * @returns Root hash as hex string */ function getHexRoot(tree) { return tree.getHexRoot(); } exports.getHexRoot = getHexRoot; /** * Gets the root hash of a Merkle tree as Buffer * @param tree - MerkleTree instance * @returns Root hash as Buffer */ function getRoot(tree) { return tree.getRoot(); } exports.getRoot = getRoot; /** * 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 */ function addLeaf(tree, leaf, options = {}) { tree.addLeaf(leaf, options.shouldHash || false); return tree; } exports.addLeaf = addLeaf; /** * 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 */ function addLeaves(tree, leaves, options = {}) { tree.addLeaves(leaves, options.shouldHash || false); return tree; } exports.addLeaves = addLeaves; /** * 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 */ function getProof(tree, leaf, index) { return tree.getProof(leaf, index); } exports.getProof = getProof; /** * 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 */ function getHexProof(tree, leaf, index) { return tree.getHexProof(leaf, index); } exports.getHexProof = getHexProof; /** * 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 */ function verifyProof(proof, leaf, root, hashFn = defaultHashFn, options = {}) { return MerkleTree_1.MerkleTree.verify(proof, leaf, root, hashFn, options); } exports.verifyProof = verifyProof; /** * Gets all leaves from a Merkle tree * @param tree - MerkleTree instance * @returns Array of leaves as Buffers */ function getLeaves(tree) { return tree.getLeaves(); } exports.getLeaves = getLeaves; /** * Gets all leaves from a Merkle tree as hex strings * @param tree - MerkleTree instance * @returns Array of leaves as hex strings */ function getHexLeaves(tree) { return tree.getHexLeaves(); } exports.getHexLeaves = getHexLeaves; /** * Gets a leaf from a Merkle tree as hex string * @param tree - MerkleTree instance * @param index - Leaf index * @returns Leaf as hex string */ function getHexLeaf(tree, index) { return tree.getHexLeaf(index); } exports.getHexLeaf = getHexLeaf; /** * Gets the leaf count * @param tree - MerkleTree instance * @returns Number of leaves */ function getLeafCount(tree) { return tree.getLeafCount(); } exports.getLeafCount = getLeafCount; /** * Gets a specific leaf by index * @param tree - MerkleTree instance * @param index - Leaf index * @returns Leaf as Buffer */ function getLeaf(tree, index) { return tree.getLeaf(index); } exports.getLeaf = getLeaf; /** * Gets the index of a specific leaf * @param tree - MerkleTree instance * @param leaf - Target leaf * @returns Leaf index or -1 if not found */ function getLeafIndex(tree, leaf) { return tree.getLeafIndex(leaf); } exports.getLeafIndex = getLeafIndex; /** * Removes a leaf by index * @param tree - MerkleTree instance * @param index - Leaf index to remove * @returns Removed leaf as Buffer */ function removeLeaf(tree, index) { return tree.removeLeaf(index); } exports.removeLeaf = removeLeaf; /** * 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 */ function updateLeaf(tree, index, value, options = {}) { tree.updateLeaf(index, value, options.shouldHash || false); } exports.updateLeaf = updateLeaf; /** * Gets all proofs for all leaves * @param tree - MerkleTree instance * @returns Array of all proofs */ function getProofs(tree) { return tree.getProofs(); } exports.getProofs = getProofs; /** * Gets all proofs for all leaves as hex strings * @param tree - MerkleTree instance * @returns Array of all proofs as hex strings */ function getHexProofs(tree) { return tree.getHexProofs(); } exports.getHexProofs = getHexProofs; /** * Gets multiproof for multiple indices * @param tree - MerkleTree instance * @param indices - Array of leaf indices * @returns Multiproof as array of Buffers */ function getMultiProof(tree, indices) { return tree.getMultiProof(indices); } exports.getMultiProof = getMultiProof; /** * 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 */ function getHexMultiProof(tree, indices) { return tree.getHexMultiProof(tree.getLayersFlat(), indices); } exports.getHexMultiProof = getHexMultiProof; /** * 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 */ function verifyMultiProof(root, proofIndices, proofLeaves, leavesCount, proof, hashFn = defaultHashFn, options = {}) { const tree = new MerkleTree_1.MerkleTree([], hashFn, options); return tree.verifyMultiProof(root, proofIndices, proofLeaves, leavesCount, proof); } exports.verifyMultiProof = verifyMultiProof; // Note: getProofFlags function removed due to compilation issue with the underlying method // The getProofFlags method exists in the source but is not being compiled correctly /** * Gets the tree depth * @param tree - MerkleTree instance * @returns Tree depth (number of layers - 1) */ function getDepth(tree) { return tree.getDepth(); } exports.getDepth = getDepth; /** * Gets all layers of the tree * @param tree - MerkleTree instance * @returns Array of layers as Buffers */ function getLayers(tree) { return tree.getLayers(); } exports.getLayers = getLayers; /** * Gets all layers of the tree as hex strings * @param tree - MerkleTree instance * @returns Array of layers as hex strings */ function getHexLayers(tree) { return tree.getHexLayers(); } exports.getHexLayers = getHexLayers; /** * Gets flattened layers * @param tree - MerkleTree instance * @returns Flattened array of all nodes */ function getLayersFlat(tree) { return tree.getLayersFlat(); } exports.getLayersFlat = getLayersFlat; /** * Gets flattened layers as hex strings * @param tree - MerkleTree instance * @returns Flattened array of all nodes as hex strings */ function getHexLayersFlat(tree) { return tree.getHexLayersFlat(); } exports.getHexLayersFlat = getHexLayersFlat; /** * Resets the tree by clearing all leaves and layers * @param tree - MerkleTree instance */ function resetTree(tree) { tree.resetTree(); } exports.resetTree = resetTree; /** * Gets tree options * @param tree - MerkleTree instance * @returns Tree options object */ function getOptions(tree) { return tree.getOptions(); } exports.getOptions = getOptions; /** * Converts tree to string representation * @param tree - MerkleTree instance * @returns String representation of the tree */ function treeToString(tree) { return tree.toString(); } exports.treeToString = treeToString; /** * Marshals leaves to JSON string * @param leaves - Array of leaves * @returns JSON string representation */ function marshalLeaves(leaves) { return MerkleTree_1.MerkleTree.marshalLeaves(leaves); } exports.marshalLeaves = marshalLeaves; /** * Unmarshals leaves from JSON string * @param jsonStr - JSON string or object * @returns Array of leaves as Buffers */ function unmarshalLeaves(jsonStr) { return MerkleTree_1.MerkleTree.unmarshalLeaves(jsonStr); } exports.unmarshalLeaves = unmarshalLeaves; /** * Marshals proof to JSON string * @param proof - Proof array * @returns JSON string representation */ function marshalProof(proof) { return MerkleTree_1.MerkleTree.marshalProof(proof); } exports.marshalProof = marshalProof; /** * Unmarshals proof from JSON string * @param jsonStr - JSON string or object * @returns Proof array */ function unmarshalProof(jsonStr) { return MerkleTree_1.MerkleTree.unmarshalProof(jsonStr); } exports.unmarshalProof = unmarshalProof; /** * Marshals entire tree to JSON string * @param tree - MerkleTree instance * @returns JSON string representation of the tree */ function marshalTree(tree) { return MerkleTree_1.MerkleTree.marshalTree(tree); } exports.marshalTree = marshalTree; /** * Unmarshals tree from JSON string * @param jsonStr - JSON string or object * @param hashFn - Hash function * @param options - Tree options * @returns MerkleTree instance */ function unmarshalTree(jsonStr, hashFn = defaultHashFn, options = {}) { return MerkleTree_1.MerkleTree.unmarshalTree(jsonStr, hashFn, options); } exports.unmarshalTree = unmarshalTree;