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

OP_RETURN in output #38

Closed skyrover7 closed 1 month ago

skyrover7 commented 5 months ago

@cmdruid was trying to implement the runes specification on my existing codebase used for inscriptions.

I feel like this should be a straightforward task but for whatever reason once my txn is broadcast the block explorers show UNKNOWN rather than the OP_RETURN. runes protocol aside, i should be able to use an OP_RETURN with any data following it, right?

Here is some sample code

var payload = hexToBytes( '148dde9d011427' );
var rune   = [ 'OP_RETURN', 'OP_13', payload ]
var runestone = Script.encode(rune);

i then add runestone as the scriptpubkey of a 0 value txn output. the txn signs and broadcasts no problem.

here are some examples on testnet that shown UNKNOWN

It should look something like the OP_RETURN here.

any thoughts appreciated!

skyrover7 commented 5 months ago

yea so i didn't need to script.encode, just directly put the script in the txn and it worked!

cmdruid commented 5 months ago

Yeah the transaction encoder should also handle encoding any scripts that you have in the transaction, so you don't have to encode them separately.

skyrover7 commented 5 months ago

for sure, which is super helpful, i just assumed it needed to be explicitly encoded because that is the flow for ordinals where the script is built and encoded with the Tap class

melvincarvalho commented 4 months ago

yea so i didn't need to script.encode, just directly put the script in the txn and it worked!

would be great if you could paste the full code snippet, so others who stumble upon this, like me, can use

cmdruid commented 4 months ago

I haven't done any runes stuff yet, but if you want to add an OP_RETURN output to your transaction, it should look something like this:

const vout = [{
  value        : 0,
  scriptPubKey : [ 'OP_RETURN', 'hex_data_goes_here' ]
}],

FYI the runes protocol stuffs data in the OP_RETURN output, but that data references other outputs in the transaction by their index, so you have to make sure that your outputs are ordered properly. OP_RETURN outputs typically are the last output in the tx.

Let me know if you have any further questions about it.

melvincarvalho commented 4 months ago

Thanks I did figure it out, but it took some trial and error. That snippet will help anyone searching the repo for OP_RETURN. Works great, thanks for the library!