bitcoin-sv / ts-sdk

Other
51 stars 13 forks source link

[FEATURE REQUEST] - MerklePath.fromTSC() method #93

Open sirdeggen opened 5 months ago

sirdeggen commented 5 months ago

Summary

Create MerklePath from TSC standard merkle proof using WoC api.

Motivation

Probably a bunch of apps have stored merkle proofs in TSC format and this gives them an opportunity to use that data quickly in construction of BUMP for use in BEEF transactions for SPV.

Description

It's already implemented elsewhere, this issue is to discuss whether incorporation within the MerklePath module here is something other people want or not.

Additional References

Existing implementation which may be copied in if so desired.

sirdeggen commented 3 months ago

I sort of wrote this in beef.ö.network but it requires a lookup so, not really a direct conversion.

async function convertTSCtoBUMP(tsc) {
    const txid = tsc.txOrId
    const header = await woc.getHeader(tsc.target)
    const bump = {}
    bump.blockHeight = header.height
    bump.path = []
    const leafOfInterest = { hash: txid, txid: true, offset: tsc.index }
    tsc.nodes.map((hash, idx) => {
        const offset = tsc.index >> idx ^ 1
        const leaf = { offset }
        if (hash === '*') leaf.duplicate = true
        else leaf.hash = hash
        if (idx === 0) {
            if (tsc.index % 2) bump.path.push([leafOfInterest, leaf])
            else bump.path.push([leaf, leafOfInterest])
        }
        else bump.path.push([leaf])
    })
    const merklePath = new MerklePath(bump.blockHeight, bump.path)
    if (header.merkleroot !== merklePath.computeRoot(txid)) throw new Error('Invalid Merkle Path')
    return merklePath
}

Open to adding this to compat module, but probably assume that you have the full header data to convert rather than looking it up.

sirdeggen commented 3 months ago

Not sure the constructor does the trimming functionality?

tonesnotes commented 1 month ago

It's a good idea. Add an input blockHeight argument and externalize the required lookup. The Babbage wallet proven transaction table schema stores serialized TSC format proof in one column and the height in a second column.