bitcoinjs / bitcoinjs-lib

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

How to set witness data in PSBT #2175

Closed hanakannzashi closed 1 month ago

hanakannzashi commented 1 month ago

I want to spend a P2WSH UTXO which needs two signatures. Alice sign the PSBT, then send partial signed PSBT to Bob, Bob sign the PSBT. Now If I want to build a transaction, I need to hack the PSBT

const transaction = (psbt as any).__CACHE._TX

then build witness data with

const witness = buildWitnessData(
  psbt.data.inputs[0].partialSig[0].signature,
  psbt.data.inputs[0].partialSig[1].signature
)

then set witness data with

transaction.setWitness(0, witness)

Is there any PSBT API to do this?

junderw commented 1 month ago

https://github.com/bitcoinjs/bitcoinjs-lib/blob/32e08aa57f6a023e995d8c4f0c9fbdc5f11d1fa0/test/integration/csv.spec.ts#L145-L160

Here's an example of using a simple CSV script.

It has an example of using a custom finalizer.

Please let me know if you have any other questions, but for now this answers your question; yes, there is an API to do that.

junderw commented 1 month ago

If you'd like to post your locking script I could write an example and add it to the integration tests for you.