Getting Started With Hyperledger Composer

Varun Raj
Varun Raj, Co-founder and CTO
Getting Started With Hyperledger Composer

What is Hyperledger Composer?

Hyperledger composer is a framework for writing a blockchain network’s chaincode and to deploy the business network in Hyperledger Fabric. Composer comes with a lot of features built in that helps you architect you business network in a clean and efficient way.

Setting Up The Environment

First you’ll install the Hyperledger Fabric framework. We can make use of the fabric-dev-servers for this series. Make sure you’ve already installed Docker.

Now kill all the Docker images.

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

Now run these following commands to download and unzip the fabric-tools folder. This is a script that help you to download and install the Hyperledger Fabric.

Terminal window
mkdir ~/fabric-tools && cd ~/fabric-tools
curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.zip
unzip fabric-dev-servers.zip

After downloading and un-ziping the fabric-tools you can download Fabric and setup the server.

Terminal window
cd ~/fabric-tools
./downloadFabric.sh
./startFabric.sh
./createPeerAdminCard.sh

Now that your Fabric is running and create a peer admin and add it to the network. This admin is the one who will be able to deploy the business network that you build with composer to the Hyperledger Fabric.

Next is the composer-cli tool to do various composer task. It’s on npm and you’ll install it with below command.

Terminal window
npm install -g composer-cli

Also you can install the Yeoman generator for composer with the package generator-hyperledger-composer and you can install it with. Make sure you’ve installed the yo npm package in global scope.

Terminal window
npm install -g generator-hyperledger-composer

Architecture

Since composer is a framework it comes with various components that are put together to create the business network and deploy it. There are several files that are indented to perform and hold some responsibilities in the architecture. I’ll explain the major and the mandatory ones below.

Model File (.cto)

Model file is the place where you’ll be defining all the entities of the business network. There are three major components in this which are the assets, participants, transactions. Each will be having the set of variables and it’s types. You can also built relationships between each entity.

Script File (.js)

This is the place where you’ll be defining 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.

Access Control File (.acl)

When you want to create user roles and define the role based validations you’ll be defining the rules in this file. You’ll be defining the conditions, who can perform what transactions, who can create what asset and more such rules.

Query file (.qry)

In the business network you can aslo perform queries to get the filtered results of assets, participants, and even transactions. It’s more like SQL but like a lite version of it.

Business Network Archive (.bna)

With all the above mentioned files, you’ll be creating an composer archive which will be later deployed as the business network in the hyperledger fabric enviroment.

Creating A Simple Business Network

Since you’ve installed the composer generator you can use that to build the skeleton of the business network and to do this, run the following command.

Terminal window
yo hyperledger-composer:businessnetwork

There will be set of questions that will help you to set up the skeleton. I assume that you’ve given the following as response and will be using them in future reference.

Terminal window
Network name : acme-network
Namespace : org.acme

Once you create it, you’ll be getting the above mentioned files and you’re good to deploy the sample network to Hyperledger Fabric.

First create an archive (.bna) with the following command

Terminal window
composer archive create -t dir -n .

It’ll will create a file named [email protected] in the project’s root folder. You’ll be deploying this in your fabric. To do that first install the runtime and then start the network with this file.

Terminal window
composer runtime install --card PeerAdmin@hlfv1 --businessNetworkName acme-network
composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --archiveFile [email protected] --file networkadmin.card

If you notice, here we’re using the PeerAdmin card that was created initially to deploy the network and also creating an network admin. there is network admin card created which you’ll be using for creating participants.

Before proceeding, make sure that you import the card, ping the server with that card and then export it again so that you can reuse it multiple times.

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

Now your network has been deployed and running in the Hyperledger Fabric. In the upcoming articles I’ll explain each of the components with more depth.

You can also use the playground to create a business network mock and export the bna file at a later time. It’ll be much easier for you that way as you don’t have to redeploy for every change to the network.