UNPKG

sbtc-bridge-lib

Version:

Library for sBTC Bridge web client and API apps

78 lines (77 loc) 3.81 kB
import { TxMinedParameters } from '../types/sbtc_types.js'; /** * * @param txIdNormal * @param txHex * @param block * @returns */ export declare function getParametersForProof(txIdNormal: string, txHex: string, block: any): TxMinedParameters; export declare function headerHex(block: any): string; export declare const doubleSha: (valueToBeHashed: string) => Uint8Array; export declare const hashPairReverse: (a: string, b: string) => string; export declare const hashPair: (a: string, b: string) => string; /** * If the hashes length is not even, then it copies the last hash and adds it to the * end of the array, so it can be hashed with itself. * @param {Array<string>} hashes */ export declare function ensureEven(hashes: Array<string>): void; /** * Finds the index of the hash in the leaf hash list of the Merkle tree * and verifies if it's a left or right child by checking if its index is * even or odd. If the index is even, then it's a left child, if it's odd, * then it's a right child. * @param {string} hash * @param {Array<Array<string>>} merkleTree * @returns {string} direction */ export declare function getLeafNodeDirectionInMerkleTree(hash: string, merkleTree: Array<Array<string>>): "left" | "right"; /** * Generates the Merkle root of the hashes passed through the parameter. * Recursively concatenates pair of hashes and calculates each sha256 hash of the * concatenated hashes until only one hash is left, which is the Merkle root, and returns it. * @param {Array<string>} hashes * @returns merkleRoot */ export declare function generateMerkleRoot(hashes: Array<string>): any; /** * Creates a Merkle tree, recursively, from the provided hashes, represented * with an array of arrays of hashes/nodes. Where each array in the array, or hash list, * is a tree level with all the hashes/nodes in that level. * In the array at position tree[0] (the first array of hashes) there are * all the original hashes. * In the array at position tree[1] there are the combined pair or sha256 hashes of the * hashes in the position tree[0], and so on. * In the last position (tree[tree.length - 1]) there is only one hash, which is the * root of the tree, or Merkle root. * @param {Array<string>} hashes * @returns {Array<Array<string>>} merkleTree */ export declare function generateMerkleTree(hashes: Array<string>): string[][]; /** * Generates the Merkle proof by first creating the Merkle tree, * and then finding the hash index in the tree and calculating if it's a * left or right child (since the hashes are calculated in pairs, * hthe dash at index 0 would be a left child, the hash at index 1 would be a right child. * Even indices are left children, odd indices are right children), * then it finds the sibling node (the one needed to concatenate and hash it with the child node) * and adds it to the proof, with its direction (left or right) * then it calculates the position of the next node in the next level, by * dividing the child index by 2, so this new index can be used in the next iteration of the * loop, along with the level. * If we check the result of this representation of the Merkle tree, we notice that * The first level has all the hashes, an even number of hashes. * All the levels have an even number of hashes, except the last one (since is the * Merkle root) * The next level have half or less hashes than the previous level, which allows us * to find the hash associated with the index of a previous hash in the next level in constant time. * Then we simply return this Merkle proof. * @param {string} hash * @param {Array<string>} hashes * @returns {Array<node>} merkleProof */ export declare function generateMerkleProof(hash: string, hashes: Array<string>): { hash: string; direction: string; }[] | null;