Broadcasting an OP_RETURN transaction on the Bitcoin blockchain using bitcoinjs-lib
The Bitcoin protocol allows adding metadata to Bitcoin transactions. This is done by adding another output with an OP_RETURN operator. The OP_RETURN operator signals the Bitcoin nodes that this output is not vital for Bitcoin transactions and will never be spent, so that they aren’t obligated to store it.
The steps for putting a transaction containing meta data are as follows:
Get a Bitcoin address with a positive balance
Create an output Bitcoin address that will receive the transaction
Broadcast the transaction to the Bitcoin network
How much data can you put on each transaction?
Every transaction can have up to one OP_RETURN output according to the Bitcoin protocol. Each OP_RETURN output can contain up to 80 bytes of data, which is equivalent to 80 ASCII characters or 160 hexadecimal symbols.
Step 1 - Get a bitcoin address with a positive balance
If you already have a bitcoin address with a positive balance, your can skip to Step 2. If not, you can first create a new key:
var inputKey = bitcoin.ECKey.makeRandom(); //print your private key (in WIF format) console.log(key.toWIF()); //print your key's public address console.log(key.pub.getAddress().toString());
You can now go ahead and send some satoshis to this address from your Bitcoin wallet. You can use a Bitcoin adress QR code generator to simplify the process.
Step 2- Create an output address
You can repeat the steps from above to create another key that will server as the ouput of our transaction
Step 3 - Building the transaction
var bitcoin = require ('bitcoinjs-lib'); var tx = new bitcoin.TransactionBuilder(); var transactionId, //a transaction that has the unspent output unspentOutputId, //the serial id of the unspent output in the transaction inputKey, //the key that corresponds to the unspent output address outputKey,//the key that corresponds to the recipient of the transaction amount; //the amount to be transferred - should be the entire unspent output minus a reasonable fee //Creating the OP_RETURN script var data = new Buffer(”Your message here”); var dataScript = bitcoin.scripts.nullDataOutput(data); //Building the transaction tx.addInput(transaction_id, unused_output); tx.addOutput(dataScript, nspentOutputId); tx.addOutput(outputKey.pub.getAddress().toString(), amount); tx.sign(0, inputKey); //Printing the signed transaction console.log(tx.build().toHex());
Step 4 -Broadcasting the transaction
There are many ways to broadcast your transaction. For example, you could use your own bitcoind using the JSON-RPC API. But easiest, is to use an online service to post the transactions. We'll demonstrate how to use chain.com.
First, you'll need to sign up on chain.com and generate your API token. Then, you need to install the chain.com package from npm.
npm install -S chain-node
Then, in your code you can broadcast the transaction as follows:
Chain = require('chain-node'); chain = new Chain({ keyId: 'your_key', keySecret: 'your_secret', blockChain: 'bitcoin'}); chain.sendTransaction(tx.build().toHex(), function(err, res){ //do something with the repsonse });
If you have any questions, or need any help developing your Bitcoin application, contact me at weiss.itamar at gmail com.