bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.68k stars 2.1k forks source link

Examples for Script Spend Taproot Tx #1814

Closed Sudz24 closed 2 years ago

Sudz24 commented 2 years ago

Hii,

The current samples under Taproot shows how to create and use a Key Spend Tx. Can you provide some example that works with Script Spend Tx as well? Thanks a lot.

motorina0 commented 2 years ago

The full taproot functionality still under review (not merged yet). You can find it in this PR: https://github.com/bitcoinjs/bitcoinjs-lib/pull/1742

Sudz24 commented 2 years ago

@motorina0 Thanks for the PR link, been going through the additions.

For the example here - https://gist.github.com/motorina0/e6bb1b32b3d5d2fc80e1bd63d5efea4f, I am not able to find the buildTapscriptFinalizer you imported.

You have said - "for the script-path spend a custom finalizer (FinalScriptsFunc) is required when a Taproot input is finalized. See test/psbt.utils.ts for such an example." But I can't find this file. Can you please point me towards the example.

junderw commented 2 years ago

Try looking at the PR branch:

https://github.com/bitcoincoretech/bitcoinjs-lib/blob/p2tr-v1/test/integration/taproot.spec.ts

Sudz24 commented 2 years ago

Thank you @junderw

However, I am facing the following error while trying to create a P2TR Address : Error: OP_1 5b6aed341b3959beb61e47738cc2e7e1ed721589ef38c2b657aafcf2a611b070 has no matching Address

Here is the snippet I tried: `

const seed1 = await bip39.mnemonicToSeed(seedPhrase1);

const BTC_TESTNET = bitcoin.networks.testnet;

const root1 = bip32.fromSeed(seed1, BTC_TESTNET)
const output = createKeySpendOutput(root1.publicKey);
const address = bitcoin.address.fromOutputScript(output, BTC_TESTNET);

console.log(address)`

Can you pls guide me on what's going wrong here?

motorina0 commented 2 years ago

The PR is still WIP, this might not be the final version:

Sudz24 commented 2 years ago

Ahh yess! Now it works.

Thanks for your time.

Sudz24 commented 2 years ago

@motorina0 From the code examples, I observe that we require the scriptTree structure and the leafSript (for redeem) to be able to spend the balance. So does it mean I have to store the scriptTree somewhere to be able to use it later while spending?

And also, if you could take a scriptTree from the sample code and explain the way its structured, it would be of great help! The ASMs you have included other than the leafScript - ' OP_CHECKSIG', over here what are these ? I appreciate your efforts in solving the queries.

motorina0 commented 2 years ago

does it mean I have to store the scriptTree somewhere to be able to use it later while spending?

motorina0 commented 2 years ago

And also, if you could take a scriptTree from the sample code and explain the way its structured

The details about how the scriptTree is built can be found here:

Sudz24 commented 2 years ago

Hi @motorina0 I noticed that P2TR Script Spend using OP_SEQUENCEVERIFY works without a problem. but using OP_CHECKLOCKTIMEVERIFY, the hex broadcast to spend the locked funds sometimes throw the error 'Non-Final' on mempool.space . Have you come across this before, could you help me understand this error?

junderw commented 2 years ago

Error 'Non-Final' means that you are not beyond the locktime.

If nLockTime says "Next tuesday" and you try to broadcast it. It will not propagate.