How to make a Bitcoin transaction in Ruby using the Chain gem

Note: Since the Consensus Blockchain Conference in September, 2015, when Chain received funding from Citi, the API has been out of service. It was great, and hopefully it comes back even better (and still free).

Chain’s API is a fast and reliable tool for conducting transactions over the Bitcoin network, and this blog post covers how to implement it in Ruby. Because Chain’s Ruby documentation is a bit sparse for the a beginner Bitcoiner, and this blog post will cover how to create a Bitcoin address, how to format user data for the Chain gem, and how to execute a transaction with the Chain gem. Our experience comes from a project made at the Flatiron School which uses Bitcoin to track the ownership and provenance of high end wines. We hope the following will be useful for anyone interested in Bitcoin or making blockchain apps in Ruby.

This tutorial will require two gems, Bitcoin and Chain. Require them in the file that you will be using.

Require the Bitcoin and Chain gems in your file or add them to your Rails Gemfile

Step 1: Create a Bitcoin address and a set of keys

The first step to using Bitcoin in your code is generating an address for your users or for use in functions within your application.

Here we attach the Bitcoin address and its accompanying key information to an Address object

Bitcoin addresses are generated from the public key in a private/public key pair, which is why you must first generate the key pair and then its corresponding address.

You can create as many Bitcoin addresses as you need, which is useful for programs that use addresses and transactions to store user information.

Step 2: Prepare user input for a transaction via the Chain gem

To use the Chain gem, you must register for a developer API at their website and generate a key from your Dashboard page. That key will allow you to establish a connection to Chain, as in the following line of code:

You should add this code to a method like the following. In the following example, we demonstrate making a transaction using params from a Rails form.

Calling this method will execute a Bitcoin transaction if there is sufficient balance in the sending Bitcoin address

Keep in mind that the amount variable is demoninated in Satoshis, or units of 0.00000001 BTC.

Step 3: Check your address balance by parsing API data

Calling the get_balance method on the Chain API will give you useful balance information for an individual Bitcoin address.

You can see that the balance information is returned as a large hash that requires some filtering. Once you’ve gotten the balance information from the nested hash, you can make useful methods that transact based on the balance in your address.

And those are the basics! The Chain gem has other methods and features including blockchain and transaction notifications, which can provide a lot of functionality. Check out the rest of Chain’s Ruby documentation here.

Show your support

Clapping shows how much you appreciated David Perel’s story.