OP_RETURN Bitcoin transaction with Ruby and Electrum
Feb 5, 2015
This post is a basic introduction to the OP_RETURNOp Code , which can be used to store data in the Bitcoin Blockchain. Doing this requires an intermediate understanding of the Bitcoin protocol and some familiarity with the Ruby programming language
Bitcoin is primarily known as a digital currency; however, it is much more than just a type of ‘digital money’. Bitcoin transactions can also be used to store small amounts of extra data in the blockchain - allowing developers to build distributed applications and protocols on top of Bitcoin, such as Counterparty and Mastercoin. These Bitcoin 2.0 protocols have the potential to enable decentralized asset exchange, securities issuance, betting and contract settlements - all on the Blockchain.
Storing extra data in the Bitcoin Blockchain is controversial. Some people see it as ‘dirtying’ the blockchain, but it is part of the protocol nonetheless and allows developers to build interesting technologies.
We will walk through how to create a bitcoin transaction that includes an extra “secret message”. This message will then be stored in the blockchain when we propagate the transaction. To follow along you must have some familiarity with Bitcoin and the Ruby programming language. You must also own some bitcoin. We will use the Electrum Bitcoin wallet and the bitcoin-ruby library to generate this transaction.
WARNING: Do not do this if you hold significant funds in your Electrum wallet. This is for fun/development only. If you make a mistake you can lose your wallet funds.
Let’s start by opening up the Electrum Console and getting a list of our unspent transaction outputs:
Here is an example of one of my unspent outputs (I changed some of the values for privacy). All we need from this is the prevout_hash and prevout_n of the output we want to spend. You should only use an output with a small “value” amount.
This is the output we will use as the input to our new transaction. In order for this transaction to be valid, we need to sign it with the private key of the address of the output we are using (1AjbRh…) to prove that we own it. In order to find this key, type:
The resulting array shows the private key. Anybody with this string can spend the bitcoin at the corresponding address. Keep it safe!
We also need the JSON representation of the transaction with hash prevout_hash. To find this you can go to http://webbtc.com/tx/YOUR_PREVOUT_HASH.json. Paste this JSON data in a file named tx.json. Here is an example.
Now we have all of the information we need to generate the transaction. We will use the bitcoin-ruby gem to do this.
Here is the code (put in same directory as tx.json):
When run, this code will return the JSON and hex versions of our signed transactions:
Hex Encoded Transaction:
We’ve done it! You can see our secret message encoded after OP_RETURN in the second output. OP_RETURN is an “Op Code” in the Bitcoin scripting language that tells the script interpreter to ignore everything after it. You are allowed to store up to 80 bytes of extra data in an OP_RETURN output. You can read more about this and other features of the Bitcoin scripting language here.
Lastly, we need to propagate this transaction into the bitcoin network. There are many ways to do that. The easiest way is to use http://webbtc.com/relay_tx. To verify that the transaction propagated, you can go to https://insight.bitpay.com /tx/YOUR_TX_HASH. It may take some time for it to appear.
It should look something like this:
From here you can do anything you want, including creating your own protocol on top of the Blockchain utilizing this embedded data. One of the downsides to this, however is that such a protocol cannot support SPV (I think). This is something I will spend some more time thinking about.