Author

Topic: [Q] How to add payload to bitcoin transactions? (Read 186 times)

newbie
Activity: 4
Merit: 1
hey thank you very much for this comprehensive answer! Cheesy
full member
Activity: 198
Merit: 130
Some random software engineer
You want to send a transaction with a null data output in it, as documented at https://bitcoin.org/en/developer-guide#null-data
Currently, the bitcoin protocol allows a length 83 bytes, including the mandatory OP_RETURN in it.

AFAIK, you can't do that within the GUI (well, you can using the debug window), and you just have to use the use the createrawtransaction/signrawtransaction/sendrawtransaction to achieve that.

First, make sure you have unspent UTXO to send your coins. I'll be using the testnet to make in my example:

Code:
$ alias btc-test="bitcoin-cli -testnet"
$ btc-test listunspent
[
  {
    "txid": "177057101201ebf8a179f88b4062b5811c75e670d9771fb6d6483c62440b7106",
    "vout": 0,
    "address": "2NAE4ATn2J39o6RCz5eLcC28s8q7uGwsjxM",
    "account": "",
    "redeemScript": "0014b4fb6c09924a8345888378b44c5a267536b0da8e",
    "scriptPubKey": "a914ba41857257b590385a85a1d52ac8661d998fb04887",
    "amount": 0.16250000,
    "confirmations": 1,
    "spendable": true,
    "solvable": true,
    "safe": true
  }
]

Then, you create your new raw transaction, using this unspent output. In this transaction, I'll send the coins back to the same address (minus fees - 0.0005; I'm freaky generous on this one!), and send the "hello subezi"-md5sum hashed message:

Code:
$ echo "hello subezi" | md5sum
66901913e0cdea467afd40ef844823df

$ btc-test createrawtransaction "[{\"txid\":\"177057101201ebf8a179f88b4062b5811c75e670d9771fb6d6483c62440b7106\",\"vout\":0}]" "{\"2NAE4ATn2J39o6RCz5eLcC28s8q7uGwsjxM\":0.162,\"data\":\"66901913e0cdea467afd40ef844823df\"}"
020000000106710b44623c48d6b61f77d970e6751c81b562408bf879a1f8eb0112105770170000000000ffffffff024031f7000000000017a914ba41857257b590385a85a1d52ac8661d998fb048870000000000000000126a1066901913e0cdea467afd40ef844823df00000000

You can check with "decoderawtransaction" the transaction back:

Code:
$ TX=020000000106710b44623c48d6b61f77d970e6751c81b562408bf879a1f8eb0112105770170000000000ffffffff024031f7000000000017a914ba41857257b590385a85a1d52ac8661d998fb048870000000000000000126a1066901913e0cdea467afd40ef844823df00000000
$ btc-test decoderawtransaction $TX
{
  "txid": "5114489a3f2fb3523d1fb45abfaa10cd9d59217971ad6ec82ca9042eee187d8d",
  "hash": "5114489a3f2fb3523d1fb45abfaa10cd9d59217971ad6ec82ca9042eee187d8d",
  "version": 2,
  "size": 110,
  "vsize": 110,
  "locktime": 0,
  "vin": [
    {
      "txid": "177057101201ebf8a179f88b4062b5811c75e670d9771fb6d6483c62440b7106",
      "vout": 0,
      "scriptSig": {
        "asm": "",
        "hex": ""
      },
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0.16200000,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_HASH160 ba41857257b590385a85a1d52ac8661d998fb048 OP_EQUAL",
        "hex": "a914ba41857257b590385a85a1d52ac8661d998fb04887",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "2NAE4ATn2J39o6RCz5eLcC28s8q7uGwsjxM"
        ]
      }
    },
    {
      "value": 0.00000000,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_RETURN 66901913e0cdea467afd40ef844823df",
        "hex": "6a1066901913e0cdea467afd40ef844823df",
        "type": "nulldata"
      }
    }
  ]
}

Next step is to sign the transaction, using signrawtransaction:

Code:
$ btc-test signrawtransaction $TX
{
  "hex": "0200000000010106710b44623c48d6b61f77d970e6751c81b562408bf879a1f8eb0112105770170000000017160014b4fb6c09924a8345888378b44c5a267536b0da8effffffff024031f7000000000017a914ba41857257b590385a85a1d52ac8661d998fb048870000000000000000126a1066901913e0cdea467afd40ef844823df02483045022100c31561e63c0bc0f023dd05008b7c25e48079ae36bc6942b971eb49080ca605f502206cf6743c986b0d1e2507deb9147728e006a895a2b6bbe477bd61eacb54574305012103029926e88dabdcdc6e24702d4f4b6209438166d23200f604b3604cdfa1aa06ac00000000",
  "complete": true
}

And finally, last step, to send this signed transaction to the network:

Code:
$ export SIGNED=0200000000010106710b44623c48d6b61f77d970e6751c81b562408bf879a1f8eb0112105770170000000017160014b4fb6c09924a8345888378b44c5a267536b0da8effffffff024031f7000000000017a914ba41857257b590385a85a1d52ac8661d998fb048870000000000000000126a1066901913e0cdea467afd40ef844823df02483045022100c31561e63c0bc0f023dd05008b7c25e48079ae36bc6942b971eb49080ca605f502206cf6743c986b0d1e2507deb9147728e006a895a2b6bbe477bd61eacb54574305012103029926e88dabdcdc6e24702d4f4b6209438166d23200f604b3604cdfa1aa06ac00000000

$ btc-test sendrawtransaction $SIGNED
259fbbba743bb54a116b114b1faed137f9f005e478c80759d446c4f03e06824a

And you can check that this transaction has stored in the blockchain your initial message, using any good block explorer, simply like this https://testnet.blockchain.info/tx/259fbbba743bb54a116b114b1faed137f9f005e478c80759d446c4f03e06824a?show_adv=true

Code:
$ curl -s https://testnet.blockchain.info/rawtx/259fbbba743bb54a116b114b1faed137f9f005e478c80759d446c4f03e06824a|grep 66901913e0cdea467afd40ef844823df
         "script":"6a1066901913e0cdea467afd40ef844823df"

Have fun sending custom data in the blockchain!

newbie
Activity: 4
Merit: 1
Hi All,

Could anybody point me to a good tutorial?

I would like to know how to add data (txt) to a bitcoin tx.
I read somewhere that 80 byte would be doable but I don't know if this is correct.

Any help is greatly appreciated.

Thx & cheers,

Subezi
Jump to: