bitcoinjs / bitcoinjs-lib

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

Examples (integration tests) - creating TXs with complex (CSV/CLTV/MSIG) P2SH outputs #1660

Closed 7-of-9 closed 3 years ago

7-of-9 commented 3 years ago

Hi all,

As best as I can understand, all the tests in csv.spec.ts and cltv.spec.ts relate to spending complex P2SH outputs, i.e. consuming them as inputs...

Would be really useful to see in the inverse of this: consuming regular P2SH(P2WPKH) inputs and outputing complex P2SH(CCSV||CLTV) outputs. Sorry if I've missed this and if they do exist. Even a single example would help a lot.

Cheers, & Happy New Year!!

junderw commented 3 years ago

I'm not following. Why is the following insufficient?

bitcoinjs.payments.p2sh({ redeem: { output: script }})

7-of-9 commented 3 years ago

Hi @junderw - yes thank you, I presume it is pretty much as straightforward as that.

I'm trying to achieve the inverse of the integration test can create (and broadcast via 3PBP) a Transaction where Bob and Charles can send (complex CHECKSEQUENCEVERIFY)... my test code so far looks like (pretty much as you have it above, I think):

// add P2SH(CSV) output - new
const sequence1 = bip68.encode({ blocks: 2 }); // 2 blocks from now
const sequence2 = bip68.encode({ blocks: 5 }); // 5 blocks from now
const p2shOutput = bitcoin.payments.p2sh({
      redeem: {
        output: complexCsvOutput( // locking script
           alice, bob, charles, dave, sequence1, sequence2),
      },
      network: TESTNET,
    });        
   pstx.addOutput({
      script: p2shOutput.output,
       value: 4242
})

I'll keep hacking away until I have something that I can broadcast.

I guess I'm just saying that from a flow/education perspective, an integration test that first walked through the creation and broadcast of a NP2WPK => P2SH(CSV) TX, then followed by the existing P2SH(CSV) => NP2WPK TX tests, would make a lot of sense IMO (maybe also chaining them together would be good/useful).

junderw commented 3 years ago

What you have there is correct.

The logic for getting such a result can be deduced by reading the payments tests, or even just reading the integration tests and figuring out how payments work.

Then you can just say, "oh, all I need to do is add .output to the payment instead of .input..."

There comes a point where "everything possible with bitcoin should be explicitly written as its own example" is not useful, and just promotes "copy pasting the example without thinking critically about it or how it works"...