Recently, I was ask how easy it was to send a message in the blockchain. In Bitcoin, there are transactions, with input (coins that will be spent) & outputs (coins locking script), and in outputs, you can create null data transactions that are not spendable outputs (as they are pruned by Bitcoin Core), that embeds up to 83 bytes of data.
Using bitcoin-cli, creating such a raw transaction is quite easy. Let’s take a look at it.
You’ll need to:
Find out an unspect transaction output to spent;
Build a new transaction, using that unspent output as input, and your target address to send the coins (removing the fees) to create a first output,
While creating this transaction, add a null data output as a second output;
Sign that transaction;
Send it to the network.
Finding out an unspent output
There are a lot in the blockchain, but unfortunatelly, you’ll need one you can spend. Let’s find one using listunspent:
This one will do it.
Creating our transaction
Let’s prepare our data. I want to write in the blockchain the following string: Testing null data transaction for my weblog.. It is a 44 (0x2c) bytes strings and its hex representation is:
We will have to send 54657374696e67206e756c6c2064617461207472616e73616374696f6e20666f72206d79207765626c6f672e0a (the hex representation of the string).
The output I’ll use can be use to spend 0.1625 coins. We need to take care of fees, and as our transaction will be ~140 bytes, I’ll use a fee of 280 satoshis (~2 satoshis/byte). I’ll be able to recover 0.1625-(280/100000000) = .16249720.
Now, to build our transaction, we need to use createrawtransaction:
We can take a look at our transaction using decodetx
Signing & sending
Next 2 steps are simpler. Signing will just take as an input the raw transaction, creating the signed transaction with the signrawtransaction rpc call. And finally, last step will be to inject the transaction in the network.
Last step is about to send our signed transaction, using sendrawtransaction:
And we’re done !
Checking
Blocks explorer can give you information about your transaction, if it is confirmed or not, and show you the data send to the chain.
You can also use gettransaction and decoderawtransaction to check confirmations on the transaction and data sent:
In a next article, I hope to redo the same thing, but programmatically using python or golang!