15

I want to add some additional information to my specific transaction, so that I can send the additional information to blockchain.

Murch
  • 75,206
  • 34
  • 186
  • 622
Rasela Don
  • 339
  • 1
  • 3
  • 8

1 Answers1

12

Note that you should only do this, putting extra data into the block chain, if it is really necessary. The block chain has to be stored by every full node, so try not to take up all our hard drive space with unnecessary stuff whenever possible.

With that said, if you do want to add extra data to your transaction, then add an additional output to the transaction, for which the scriptPubKey has the following form:

OP_RETURN {80 bytes of whatever data you want}

This transaction output is automatically un-spendable, and so will not be kept in the UTXO set. The other UTXOs from your transaction will still be safe.

For more info:

This will embed extra information into your transaction. Be aware, though, that the software that made the transaction and the software that reads the transaction have to be compatible in order for the user to be able to read the given information when the transaction is received.

You can see an example of a transaction that used OP_RETURN to encode extra information here: https://blockchain.info/tx/6dfb16dd580698242bcfd8e433d557ed8c642272a368894de27292a8844a4e75?show_adv=true.

Nick ODell
  • 29,396
  • 11
  • 72
  • 130
morsecoder
  • 14,168
  • 2
  • 42
  • 94
  • 2
    Note that receivers (other outputs of your tx), or people in general, won't automatically see this information. People would have to know about your particular tx and specifically decode it and view its raw contents, in order to be aware of your info. – RocketNuts Oct 15 '14 at 14:39
  • Thanks, @RocketNuts, I will edit my answer to include the information you gave. – morsecoder Oct 15 '14 at 15:17
  • Thank you very much for your reply. I'm sure this is the right way to do it, but I'm having some trouble with including data into the transaction object with OP_RETURN. I really appreciate you if you could help me with this. – Rasela Don Oct 16 '14 at 06:21
  • 1
    Where are you getting stuck? Can you tell me what you've tried? – morsecoder Oct 16 '14 at 12:34
  • std::vector data; *script << OP_DUP << OP_HASH160 << keyID << OP_EQUALVERIFY << OP_CHECKSIG << OP_RETURN << data;

    I just send the required data like this. I could successfully make the transaction, but it doesn't go to the block chain. I could find the type of the transaction as follows. "type": "nonstandard"

    – Rasela Don Oct 17 '14 at 08:40
  • 2
    You're doing it wrong, look again at the example transaction linked in the answer, the third output is the OP_RETURN and the entire output script is "OP_RETURN 68656c6c6f20776f726c64" (decoded: jhello world). There is no OP_DUP, OP_HASH160, keyID, OP_EQUALVERIFY or OP_CHECKSIG in an OP_RETURN output script. It's an unspendable output that contains the OP_RETURN opcode and up to 40 bytes of arbitrary data, nothing more or less, it's a separate output from any pay to pubkey hash or pay to script hash outputs in the same transaction. – stevenh512 Nov 09 '14 at 19:00
  • 3
    I think the size changed in 2015 from 40 to 80. Right? – Leandro Apr 23 '17 at 15:26
  • @Leandro Fixed. – Nick ODell Apr 23 '17 at 22:57
  • 1
    @Leandro Yes the size changed to 80 bytes – Erhard Dinhobl Jul 10 '18 at 11:25