merkle-tree-lib
Version:
Merkle Tree implementation with BIP340 tagged hash support.
84 lines (83 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MerkleProof = exports.ProofDirection = void 0;
/**
* Direction enum for which side the sibling is on in a Merkle proof
*/
var ProofDirection;
(function (ProofDirection) {
ProofDirection[ProofDirection["LEFT"] = 0] = "LEFT";
ProofDirection[ProofDirection["RIGHT"] = 1] = "RIGHT";
})(ProofDirection || (exports.ProofDirection = ProofDirection = {}));
/**
* MerkleProof - Class representing a Merkle proof for a specific leaf
*/
class MerkleProof {
/**
* Create a new Merkle proof
*
* @param leafData - The original data for the leaf
* @param leafIndex - The index of the leaf in the original data array
* @param elements - Array of proof elements
* @param rootHash - The Merkle root hash
*/
constructor(leafData, leafIndex, elements, rootHash) {
this.leafData = leafData;
this.leafIndex = leafIndex;
this.elements = elements;
this.rootHash = rootHash;
}
/**
* Get the leaf data
* @returns The original leaf data
*/
getLeafData() {
return this.leafData;
}
/**
* Get the leaf index
* @returns The index of the leaf in the original tree
*/
getLeafIndex() {
return this.leafIndex;
}
/**
* Get the proof elements
* @returns Array of proof elements
*/
getElements() {
return this.elements;
}
/**
* Get the Merkle root hash
* @returns The root hash
*/
getRootHash() {
return this.rootHash;
}
/**
* Convert a proof from the library format to our standard format
*
* @param libProof - Proof from merkle-tree-lib's old format
* @returns MerkleProofElement array
*/
static fromLibraryFormat(libProof) {
return libProof.map(item => ({
siblingHash: item.siblingHash,
direction: item.side
}));
}
/**
* Convert a standard proof to a format suitable for API responses
*
* @param proof - MerkleProofElement array
* @returns Array of [hashHex, direction] tuples
*/
static toApiFormat(proof) {
return proof.map(item => {
const hashHex = item.siblingHash.toString('hex');
return [hashHex, item.direction];
});
}
}
exports.MerkleProof = MerkleProof;