/// /** * Class containing a Merkle tree structure and its related operations. */ export default class MerkleTree { /** * Stores the Merkle tree root node once finalize() is called. */ private merkleTreeRootNode?; /** * Hashing function usinged by this Merkle tree. */ private hash; /** * Intermediate structure for storing different sizes of balanced Merkle trees that will eventually form one final tree. * [0] stores tree of 1 leaf, [1] stores tree of 2 leaves, [2] -> 4 leaves, [3] -> 8 leaves and so on. */ private subtrees; /** * A map such that given a value, the corresponding leaf hash node is located. */ private valueToMerkleNodeMap; /** * Creates a MerkleTree. * @param values values to be added to the Merkle tree. * @param customHashFunction Optional custom hash function. SHA256 is used if not specified. */ static create(values: Buffer[], customHashFunction?: (value?: Buffer) => Buffer): MerkleTree; /** * Gets the Merkle tree root hash. */ readonly rootHash: Buffer; /** * Create a Merkle receipt for the given value. * @returns Merkle receipt in the format specified by the Sidetree protocol. */ receipt(value: Buffer): IMerkleReceiptEntry[]; /** * Proves that the given receipt is valid for the given value and Merkle root. * If receipt is not given, the the hash of the valude must directly equal to the given Merkle root. * @param receipt Merkle receipt in the format specified by the Sidetree protocol. * @param customHashFunction Optional custom hash function. SHA256 is used if not specified. */ static prove(value: Buffer, merkleRoot: Buffer, receipt: IMerkleReceiptEntry[], customHashFunction?: (value?: Buffer) => Buffer): boolean; /** * Creates a MerkleTree. * @param values values to be added to the Merkle tree. * @param customHashFunction Optional custom hash function. SHA256 is used if not specified. */ private constructor(); /** * Adds a value to the current list of balanced Merkles subtrees in 'this.subtrees * such that it always maintains the smallest number of balanced trees. * The list of balanced Merkle subtrees will be combined to form the final Merkle tree when finalize() is called. * Also adds the value to valueToMerkleNodeMap such that the corresponding leaf hash node can be located quickly. */ private add; /** * Combines the list of balanced Merkle subtrees to form the final Merkle tree. */ private finalize; /** * Creates a parent Merkle tree node given two child nodes. */ private createParent; } /** * Represents an entry of many inside a Merkle receipt. */ interface IMerkleReceiptEntry { side: string; hash: Buffer; } export { IMerkleReceiptEntry };