cmdruid / tapscript

A humble library for working with Tapscript and Bitcoin Transactions.
https://www.npmjs.com/package/@cmdcode/tapscript
Creative Commons Zero v1.0 Universal
188 stars 49 forks source link

In the examples, where is the scriptPubKey address comming from? #35

Closed ChristianOConnor closed 5 months ago

ChristianOConnor commented 5 months ago

This seemingly random address appears in several places but I will use the inscribe.test.ts file as an example https://github.com/cmdruid/tapscript/blob/master/test/example/taproot/inscribe.test.ts. Notice this code:

const txdata = Tx.create({
      vin  : [{
        // Use the txid of the funding transaction used to send the sats.
        txid: 'b8ed81aca92cd85458966de90bc0ab03409a321758c09e46090988b783459a4d',
        // Specify the index value of the output that you are going to spend from.
        vout: 0,
        // Also include the value and script of that ouput.
        prevout: {
          // Feel free to change this if you sent a different amount.
          value: 100_000,
          // This is what our address looks like in script form.
          scriptPubKey: [ 'OP_1', tpubkey ]
        },
      }],
      vout : [{
        // We are leaving behind 1000 sats as a fee to the miners.
        value: 99_000,
        // This is the new script that we are locking our funds to.
        scriptPubKey: Address.toScriptPubKey('bcrt1q6zpf4gefu4ckuud3pjch563nm7x27u4ruahz3y')
      }]
    })

See scriptPubKey: Address.toScriptPubKey('bcrt1q6zpf4gefu4ckuud3pjch563nm7x27u4ruahz3y')... Why is bcrt1q6zpf4gefu4ckuud3pjch563nm7x27u4ruahz3y being used as the script that is locking the funds? Is this just an address chosen at random or is it derived from another piece of code that I'm missing?

Does it just mean that address where the UTXO is being spent chosen at random?

cmdruid commented 5 months ago

The input is spending a random utxo from my test wallet. You will want to replace it with a utxo from your own test wallet.

Similarly, the output is going to a random address generated from my test wallet. You will want to replace it with an address from your own wallet.

I am using the wallet built into in Bitcoin Core in order to manage my utxos and addresses for testing.

I hope that makes sense. Let me know if you have further questions about it.