All files / src/utility tscProofToMerklePath.ts

91.66% Statements 22/24
80% Branches 8/10
100% Functions 2/2
90.9% Lines 20/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4857x               57x 3x 3x             57x 3x 3x 57x 57x 57x 57x 57x     57x   57x 57x 3x         3x     3x     57x   3x      
import { MerklePath } from '@bsv/sdk'
 
export interface TscMerkleProofApi {
  height: number
  index: number
  nodes: string[]
}
 
export function convertProofToMerklePath(txid: string, proof: TscMerkleProofApi): MerklePath {
    const blockHeight = proof.height
    const treeHeight = proof.nodes.length
    type Leaf = {
        offset: number
        hash?: string
        txid?: boolean
        duplicate?: boolean
    }
    const path: Leaf[][] = Array(treeHeight).fill(0).map(() => ([]))
    let index = proof.index
    for (let level = 0; level < treeHeight; level++) {
        const node = proof.nodes[level]
        const isOdd = index % 2 === 1
        const offset = isOdd ? index - 1 : index + 1
        const leaf: Leaf = { offset }
        Iif (node === '*' || (level === 0 && node === txid)) {
            leaf.duplicate = true
        } else {
            leaf.hash = node
        }
        path[level].push(leaf)
        if (level === 0) {
            const txidLeaf: Leaf = {
                offset: proof.index,
                hash: txid,
                txid: true,
            }
            Iif (isOdd) {
                path[0].push(txidLeaf)
            } else {
                path[0].unshift(txidLeaf)
            }
        }
        index = index >> 1
    }
    return new MerklePath(blockHeight, path)
}