Hyperledger Fabric Developer Tutorial Part 1: Make Your Own Blockchain

Recommended snack and song:

Sliced bananas and strawberries with granola all mixed in a bowl of natural yogurt while listening to Royal Blood.

When you hear about blockchain, you normally think of a publicly available ledger, on a worldwide distributed network of peers that have an agreement strategy to include new blocks (or transactions) on the blockchain. This is OK for most applications, but what happens when you’d like to take advantage of the blockchain technology, but don’t want your data to be scattered around in the world? What happens when there’s sensitive data that, if made public, could potentially hurt your business interests?

Enter Hyperledger Fabric: an open-source blockchain framework implementation hosted by The Linux Foundation, which you can use to host your very own and very private blockchain. You can read more about it on their website, and perhaps even watch this intro video, which makes it a bit easier to understand what the motivation behind the entire project is.

tl;dr: Go clone this repo and follow the README instructions to get this running.

This is what we’re gonna cover:

  • Installing the prerequisites.
  • Using the yeoman generator to get a basic business network definition project.
  • Coding a simple solution with it.
  • Deploying locally.
  • Looking at some follow-up steps you might want to consider.

Just a quick notethis first part of the Hyperledger Tutorial series is all about getting started building business logic and having it deployed onto your own blockchain. Later blog posts in this series will take a deeper dive into the concepts that really make Hyperledger very powerful.

Installing the Prerequisites

First, make sure you have NodeJS installed (go with LTS, that’s the safest choice). If you already have, a good security measure is making sure you can do sudoless npm global installs (in case you’re using Linux or OSX).

Now, run these commands:

You’ll also need to download and install Docker, which you can do by going to the Docker install page and downloading the Docker Community Edition for your OS.

Once you have Docker running on your system, it’s time to run a few bash script commands:

This will create a directory in ~/fabric-tools, download a few provision scripts and start downloading the Hyperledger Fabric Docker containers.

One other thingyou’d benefit a lot from using VSCode to code this project up. This can be done by installing the Hyperledger Composer extension.

Generating the Business Network Definition Project

We’re going to build some small asset management software for Dr. Who so he/she can track who owns a key and even track the creation of new keys.

First, run the yeoman generator:

…which will ask the following questions (answer them as shown below):

tardis-generation_dmczec

Perfect! Now we have to create our models to represent our network, which will be super simple; we just need a Key asset and a Companion. Also, we need to create a TransferKey transaction to transfer the keys around. Open up the models/com.gallifrey.tardis.cto file and replace its contents with:

As for the logic of the TransferKey transaction, replace the contents of lib/logic.js with:

Quick note: These JS logic files inside the lib folder are NOT NodeJS modules and currently don’t support ES6 syntax. I asked and this was their answer.

Awesome, we’ve coded our first official Business Network Definition project! It’s time to deploy it locally to our Hyperledger Fabric servers:

starting-fabric

The peer admin user takes care of deploying the business network definitions to the peers. For the purposes of this tutorial, we’re gonna have one peer admin card to rule all the peers.

Build Code for Deployment

Once you have your business network definition done, and you’ve made sure your servers are up and operational, we can proceed with building our BNA (business network archive) file by navigating the console to your project’s folder and running:

Then we need to install our business network as a runtime for the peer:

Next up, we need to create a network admin card and deploy our previously generated .bna file:

Make sure you change the parameters accordingly; for instance, the name of the .bna file may change depending on the version of the project in the package.json file.

Now we need to import the generated networkadmin.card card file by typing:

And just to check that everything is up and running:

deploying-business-network

For us to take a look at the final result of what we’ve accomplished, there’s a tool we’ve installed called Composer Playground, which we can launch by doing:

In there, you can play around with your data; you can also launch a small REST server for you to play around with the data, which is built on top of Loopback:

Now you have a complete blockchain-based business network using Hyperledger running on your computer using Docker! How cool is that? For the next parts of this tutorial, we’re going to take a deep dive into more complex concepts that will give you a better perspective on how to apply this technology in your applications.

 

Subscribe to our Blog

Daniel Prado
Daniel Prado
Daniel Prado is a Web Development Practice Lead for Gorilla Logic. He previously taught frontend technologies and data structures at some of Costa Rica’s most prestigious universities. Daniel enjoys open water swimming, go-cart racing, and listen to music - especially 80s heavy metal bands.

Deliver off-the-chart results.

WordPress Video Lightbox Plugin