Before you go, check out these stories!

0
Hackernoon logoAdding Messages On The Bitcoin Blockchain - A How-To Guide by@bitcoin-in-action

Adding Messages On The Bitcoin Blockchain - A How-To Guide

Author profile picture

@bitcoin-in-actionBitcoin in Action

See on https://bitcoininaction.com

Hey!,

Today Luca asks:

Can I write on a Bitcoin Blockchain? Can I drop down a message?

Yes, Luca of course you can! You can leave a message on a Bitcoin blockchain using a particular op code, called

OP_RETURN
.

Through this operation code, your UTXO became unspendable! You can use at most 80 bytes for your message (160 hexadecimal characters)!

This operation is known as โ€œtimestamping of write informationโ€ because the message will become part of a block with a timestamp as well.

In Action

Can I write on Bitcoin Blockchain?โ€Šโ€”โ€ŠItalian Language ๐Ÿ‡ฎ๐Ÿ‡น

First of all, I get a SegWit address from my testnet node.

$ bitcoin-cli getnewaddress "" "bech32"
tb1qrggdlvezgd4uy9mntz50mpmwd6l4vk9rm4ft3d

I need bitcoins! I need to use a faucet service such as: https://bitcoinfaucet.uo1.net/send.php

After that, I can check my mempool and find my TX ID.

$ bitcoin-cli getrawmempool | grep c5ce66d638f1b8ca702dfb8f7d1da7a6707d9c6497212dc66829c99f69b28b9a

the c5ce66d638f1b8ca702dfb8f7d1da7a6707d9c6497212dc66829c99f69b28b9a is the faucetโ€™s transaction.

When can I use my UTXO?

I can use it after the mining process.

Please recover my UTXO with

listunspent
command.

$ bitcoin-cli listunspent 1 101 '["tb1qrggdlvezgd4uy9mntz50mpmwd6l4vk9rm4ft3d"]' | jq
[
{
"txid": "c5ce66d638f1b8ca702dfb8f7d1da7a6707d9c6497212dc66829c99f69b28b9a",
"vout": 1,
"address": "tb1qrggdlvezgd4uy9mntz50mpmwd6l4vk9rm4ft3d",
"label": "",
"scriptPubKey": "00141a10dfb322436bc2177358a8fd876e6ebf5658a3",
"amount": 0.00100000,
"confirmations": 6,
"spendable": true,
"solvable": true,
"desc": "wpkh([3a46ecca/0'/0'/4']020d12775323bbdaf0cb6e9a2b44ae7a591ef5872364e80e363a93d283c10b9e4f)#kxjva7dw",
"safe": true
}
]

Nice! I need to retrieve the private key, as we know itโ€™s mandatory to sign my transaction.

$ bitcoin-cli dumpprivkey tb1qrggdlvezgd4uy9mntz50mpmwd6l4vk9rm4ft3d
cPHTHs7ERe6jDYiitj9eLVswsX3RpeKMB19eXYjpLb4CkEHd7drq

Now, we are able to create an amazing message, for instance: corsocompleto.bitcoininaction.com.

Yep, we need to use hexadecimal!

$ printf "corsocompleto.bitcoininaction.com" | xxd -ps
636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d

I have the necessary to create the transaction!

Using

help
I can retrieve a lot of information about this method. I want to use it on
createrawtransaction
.

$ bitcoin-cli help createrawtransaction

The receiver is the faucetโ€™s address, I give back bitcoinsย :).

The address is:

2NGZrVvZG92qGYqzTLjCAewvPZ7JE8S8VxE

Finally, I can build my transaction, in the data field I can put my hexadecimal message. Remember that it will create an operation code

OP_RETURN
as we discussed above.

$ bitcoin-cli createrawtransaction '[{"txid":"c5ce66d638f1b8ca702dfb8f7d1da7a6707d9c6497212dc66829c99f69b28b9a","vout":1}]' '[{"2NGZrVvZG92qGYqzTLjCAewvPZ7JE8S8VxE":0.00099000},{"data":"636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d"}]'
02000000019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d00000000

n.b. As you can see I have just one input, for that reason I donโ€™t need of change address because I donโ€™t have any change!

Furthermore, I donโ€™t spend my entire output because I need to pay fees (input-output = fees).

Nice, we created the transaction data!

Yeah!

The next step is to sign it and send it!

We need to use the

signrawtransactionwithkey
method.

$ bitcoin-cli signrawtransactionwithkey 02000000019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d00000000 '["cPHTHs7ERe6jDYiitj9eLVswsX3RpeKMB19eXYjpLb4CkEHd7drq"]' '[{"txid":"c5ce66d638f1b8ca702dfb8f7d1da7a6707d9c6497212dc66829c99f69b28b9a","vout":1,"scriptPubKey":"00141a10dfb322436bc2177358a8fd876e6ebf5658a3","amount":0.00100000}]'

{
"hex": "020000000001019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d0247304402205688399cb5a230f050330e2bc6d04d9864d459f85fec48a0118ca31be9239d530220228d7c04fe9e6eea3690033c01ed222284efaa01b28a9a7cae809bdb32d7ce7a0121020d12775323bbdaf0cb6e9a2b44ae7a591ef5872364e80e363a93d283c10b9e4f00000000",
"complete": true
}

Nice, the signature is valid, now we gonna send the transaction with

sendrawtransaction
method.

$ bitcoin-cli sendrawtransaction 020000000001019a8bb2699fc92968c62d2197649c7d70a6a71d7d8ffb2d70cab8f138d666cec50100000000ffffffff02b88201000000000017a914ffd0dbb44402d5f8f12d9ba5b484a2c1bb47da42870000000000000000236a21636f72736f636f6d706c65746f2e626974636f696e696e616374696f6e2e636f6d0247304402205688399cb5a230f050330e2bc6d04d9864d459f85fec48a0118ca31be9239d530220228d7c04fe9e6eea3690033c01ed222284efaa01b28a9a7cae809bdb32d7ce7a0121020d12775323bbdaf0cb6e9a2b44ae7a591ef5872364e80e363a93d283c10b9e4f00000000

edee419f93521f43259b763ffb42e4b882504534494381b7e18057015a27c548

We obtained the transaction id.

We can use a block explorer (https://tbtc.bitaps.com/) with the transaction id in order to find out the transaction and check its structure.

Our message! Amazing Bitcoin course ๐Ÿš€โ€Šโ€”corsocompleto.bitcoininaction.com

The transaction has been sent properly and now itโ€™s waiting for mining.

It contains two outputs. One of them is for faucetโ€™s address, the other one is an unspendable UTXO, because it contains our message with operation code:

OP_RETURN
.

Through this example, we can now understand which operation code is used to โ€œwriteโ€ on the Bitcoin Blockchain.

You can find the code in our GitHub!

Do you want more?

You rock ๐ŸŽธ!

Check our books, they are available at Amazon and corsobitcoin.com (accept bitcoin)

See you next time! ๐ŸคŸ๐Ÿป

โ€” โ€”

๐ŸŽฅย Bitcoin in Action

โ€”

๐Ÿ™ GitHub:ย https://bit.ly/2Lj3yeY

โ€”

๐Ÿ“–ย Libro Bitcoin dalla teoria alla pratica (Amazon)

๐Ÿ“–ย Libro Bitcoin dalla teoria alla pratica (pagamento in bitcoin)

๐Ÿ“–ย Libro Bitcoin dalla teoria alla pratica (Amazon)

๐Ÿ“–ย Book Bitcoin from theory to practice

โ€”

๐Ÿ“–ย Tascabile Bitcoin 199 domande (Amazon)

๐Ÿ“–ย Tascabile Bitcoin 199 domande (pagamento in bitcoin)

๐Ÿ“–ย Pocket Book Bitcoin 199 questions (Amazon)

๐Ÿ“–ย Pocketย Bookย Bitcoin 199 questions (accept bitcoin)

โ€”

๐ŸŽฅย Video Corso Bitcoin dalla teoria alla pratica

โ€”

โ–บย Twitterย ,ย Facebook,ย Medium,ย Instagram,ย Youtube,ย GitHub

Television isnโ€™t a good idea (Radio Stations)

Email isnโ€™t a good idea (Post offices)

Amazon isnโ€™t a good idea (Retail stores)

Bitcoin isnโ€™t a good idea (Central banks)

Inย cryptoย we trust

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.