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 note—this 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:

1

2

3

4

5

$npm install-gcomposer-cli

$npm install-gcomposer-rest-server

$npm install-gcomposer-playground

$npm install-gyo

$npm install-ggenerator-hyperledger-composer

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 thing—you’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:

1

$yo hyperledger-composer:businessnetwork

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

Welcome tothe business network generator

?Business network name:tardis

?Description:Key asset management system forDr Who

?Author name:

?Author email:

?License:Apache-2.0

?Namespace:com.gallifrey.tardis

create index.js

create package.json

create README.md

create models/com.gallifrey.tardis.cto

create.eslintrc.yml

create test/logic.js

create lib/logic.js

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

namespacecom.gallifrey.tardis

participantCompanionidentifiedbycompanionId{

oStringcompanionId

oStringname

}

assetKeyidentifiedbykeyId{

oStringkeyId

oStringowner

}

transactionTransferKey{

--&gt;Companion companion

--&gt;Key key

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

'use strict';

varNETWORK_NAME='com.gallifrey.tardis';

varKEY_ASSET='Key';

/**

* Transfer ownership of a TARDIS key to a fello

* @param {com.gallifrey.tardis.TransferKey} transferKey

* @transaction

*/

functiononTransferKey(transferKey){

varkey=transferKey.key;

varkeyFQDN=[NETWORK_NAME,KEY_ASSET].join('.');

returngetAssetRegistry(keyFQDN)

.then(function(registry){

key.owner=transferKey.companion;

returnregistry.update(key);

});

}

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:

1

2

3

$cd~/fabric-tools/

$./startFabric.sh# This will kickstart the fabric servers.

$./createPeerAdminCard.sh# This will create a peer admin user

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:

1

$composer archive create-tdir-n.

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

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:

1

$composer card import--file networkadmin.card

And just to check that everything is up and running:

1

$composer network ping--card admin@tardis

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:

1

$composer-playground-p5000

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:

1

$composer-rest-server-cadmin@tardis-nnever-p9000

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.

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.