bitcoinjs / bitcoinjs-lib

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

[Question] Are CSV examples using segwit? #1604

Closed educob closed 4 years ago

educob commented 4 years ago

Hi.

Is the address generated by const bitcoin.payments.p2sh({ redeem: { output: complexCsvOutput( ... segwit? If not what would be the code?

Is it possible to use segwit when usingnew bitcoin.Transaction(); or only with new bitcoin.Psbt()?

In this line: .finalizeInput(0, csvGetFinalScripts) I don't see where the function parameters are passed. Could you please explain?

In the return in function csvGetFinalScripts, where is payment.inputdefined?

Thanks.

junderw commented 4 years ago

Is the address generated by const bitcoin.payments.p2sh({ redeem: { output: complexCsvOutput( ... segwit?

No. There is no p2wsh or p2wpkh anywhere, so it is not segwit.

If not what would be the code?

change p2sh with p2wsh for native segwit and wrap p2wsh in p2sh for backwards compatibility.

Is it possible to use segwit when using new bitcoin.Transaction(); or only with new bitcoin.Psbt()?

Transaction is literally a bitcoin transaction. Anything is possible. You just need to code the logic for adding whatever inputs / witnesses / outputs you need.

In this line: .finalizeInput(0, csvGetFinalScripts) I don't see where the function parameters are passed. Could you please explain?

They are passed in here:

https://github.com/bitcoinjs/bitcoinjs-lib/blob/7622c58365e09ff26002b56d4a4e7fd13251c949/ts_src/psbt.ts#L330-L337

In the return in function csvGetFinalScripts, where is payment.input defined?

The Payment API is a lazy-loading generator that takes inputs and generates outputs based on info it has already been passed.

ie. If I make a p2pkh payment with only output I can read the payment.address attribute and as soon as I read it the getter will grab the pubkeyhash out of the output and base58check encode it, cache it, then return it. So by "using" the input, it will throw an error if it doesn't have enough info to generate the input. Example:

https://github.com/bitcoinjs/bitcoinjs-lib/blob/7622c58365e09ff26002b56d4a4e7fd13251c949/ts_src/payments/p2pkh.ts#L77-L81