Build Your First Blockchain Application with Hyperledger Fabric & Composer


Everyone is talking about Blockchain now—both in terms of cryptocurrency and how the new technology can reform industries. Supply chains, digital asset management and inventory tools will benefit largely from blockchain technology.

In this article I’ll explain how anyone can build their own blockchain application with a popular framework using Hyperledger Fabric and Composer.

Getting Started

Before getting started, I highly recommend that you:

  1. Have a good knowledge of JavaScript (especially <ahref=>NodeJS)
  2. Understand the basics of server management
  3. Have experience managing Docker Instances

Setting Up Hyperledger Fabric

As the zero step, we need to set up the Hyperledger Fabric network upon which our Composer application will be running. In this example, we’ll be using a boilerplate setup for Fabric which can be cloned from their GitHub repo.

Step 1:
In order to start with a fresh setup, kill all existing Docker instances and remove the images by running the following commands in your Terminal:

 docker kill $(docker ps -q)
  docker rm $(docker ps -aq)
  docker rmi $(docker images dev-* -q)

Step 2:
Now, create a folder named fabric-tools. Then, clone the fabric-dev-server inside it and unzip it. In this example I’m cloning the repo in the home folder, but you can do this where it’s required.

 mkdir ~/fabric-tools && cd ~/fabric-tools

  curl -O


When you unzip it, you will find shell scripts to help you manage your Fabric servers. You can download all the Fabric images and start the server with the following commands:

 cd ~/fabric-tools

Now if you execute docker ps, you can see these images running. There will be one Orderer, one CA, and two peers.

With this, our infrastructure setup is complete—and next up is to build a Composer application that will be acting as the chain code (smart contracts) for our blockchain network. Chain code is basically where you would write your business logic.

Setting Up Hyperledger Composer

Hyperledger Composer can easily be set up. All you need to do is install a CLI client which helps you to manage your network.

 npm install -g composer-cli

Now let’s install the generator-hyperledger-composer package which we can use to generate a basic network for rapid development.

 npm install -g generator-hyperledger-composer

Once you install these, we are good to go!

Create a Simple Network with Composer

You can create a simple boilerplate network with the Yo generator that was installed earlier. To do this, execute the following command:

 yo hyperledger-composer:businessnetwork

There will be a set of questions in the process to set up the network, such as network name and namespaces, among other configurations. In this article, I have used the following as the config:

> Business network name: acme-network
> Description: ACME A blockchain based trading network
> Author name: Bob
> Author email: [email protected]
> License: Apache-2.0
> Namespace: org.acme

Once you run this, there will be a new folder created named acme-network, with the following files:

With this network we will have an asset and update the values associated with it.

Since Composer is a framework, it comes with various components to help you create a business network and deploy it. There are several files that perform and hold responsibilities in the architecture. Let’s look at the major and mandatory ones below.

Model File (.cto)
The Model file is the place to define all the entities in the business network. It contains three major components, which are the assets, participants, and transactions. Each has a set of variables and its types. You can also build relationships between entities.

 namespace org.acme
  participant User identified by email {
    o String email

  asset SampleAsset identified by assetId {
    o String assetId
    o String value

  transaction ChangeAssetValue {
    o String newValue
    --> Asset relatedAsset

Script File (.js)
This is the place to define the transaction processor functions. It’s the business logic of the blockchain application where you’ll be defining what kind of operations can be done in the network. It’s written in JavaScript.

 'use strict';
   * Write your transction processor functions here

   * Sample transaction
   * @param {org.acme.ChangeAssetValue} changeAssetValue
   * @transaction
  function onChangeAssetValue(changeAssetValue) {
      var assetRegistry;
      var id = changeAssetValue.relatedAsset.assetId;
      return getAssetRegistry('org.acme.SampleAsset')
          .then(function(ar) {
              assetRegistry = ar;
              return assetRegistry.get(id);
          .then(function(asset) {
              asset.value = changeAssetValue.newValue;
              return assetRegistry.update(asset);

Access Control File (.acl)
All information pertaining to user roles and role-based validations is defined here. You can define the conditions that govern who can perform what transactions, who can create what asset, and more.

Query File (.qry)
This file contains the queries to be made on the business network. You can get the filtered results of assets, participants, and even transactions. It’s almost like a lite version of SQL.

Business Network Archive (.bna)
All the files mentioned above are used to create a Composer archive which will be used to deploy the business network in the Hyperledger Fabric environment.

Running the Business Network
In order to deploy the network, you need to create an archive and then deploy it with the CLI command:

 composer archive create -t dir -n .

This command creates a new file in the root directory called [email protected].
Next, you need to install a runtime in the Hyperledger Fabric network.

composer runtime install –card PeerAdmin@hlfv1 –businessNetworkName acme-network

And finally, start the network with the created archive:

 composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --archiveFile [email protected] --file networkadmin.card

It will generate a new .card file called networkadmin.card which is used for the creation of users, and other future admin functions. Import the card and ping the network once to generate the certificates and private key.

 composer card import --file networkadmin.card
  composer network ping --card admin@trucerts-network
  composer card export --file networkadmin.card -n admin@trucerts-network

Adding Participants
Participants are the end users who submit the transactions. You can create participants with the following commands:

 composer participant add  -d '{ "$class": "org.trucerts.User", "email": "[email protected]", }' -c admin@acme-network
And create a card file using,
  composer identity issue -f admin.card -a 'resource:org.acme.User#1' -c admin@acme-network -u admin -x
  composer card import -f admin.card
  composer network ping -c admin@acme-network
  composer card export -f admin.card -n admin@acme-network

Submitting Transactions

Lastly, a transaction needs to be performed to create or edit an asset in the network. You can use the CLI to perform transactions as follows:

Create a New Asset

 composer transaction submit -c admin.card -d '{ "$class": "org.acme.SampleAsset", "assetId": "assetId:1", "owner": "resource:org.acme.sample.SampleParticipant#Toby", "value": "100" }'

Change an Asset Value

 composer transaction submit -c admin.card -d '{ "$class": "org.acme.ChangeAssetValue", "asset": "resource:org.acme.sample.SampleAsset#assetId:1", "newValue": "200"}'

These transactions will be submitted as the newly created user since we’re using [email protected]’s card to perform the transaction.

Swaathi Kakarla is the co-founder and CTO at Skcript She enjoys talking and writing about code efficiency, performance and startups. In her free time she finds solace in yoga, bicycling and contributing to open source. Swaathi is a regular contributor at Fixate IO.


Leave a Comment

Your email address will not be published. Required fields are marked *

Skip to toolbar