Hyperledger Composer Demystified!

Saif Rehman
Coinmonks
6 min readJan 21, 2018

--

What is Blockchain, and what it means to IBM?

“Blockchain is a shared, immutable ledger for recording the history of transactions. It fosters a new generation of transactional applications that establish trust, accountability and transparency.”

In 3 simple words, we can define blockchain as “Decentralised Immutable Assets

Hyperledger Composer is based on Hyperledger Fabric. Hyperledger fabric is an open source business blockchain created by Linux Foundation. IBM has massively contributed to the Hyperledger project and has accomplished various project based on Hyperledger Fabric. Unlike Ethereum and other Blockchain which are based on the concept of cryptocurrency, IBM has taken a step forward to evolve in an area of private and permission-based business blockchain, where participants know each other. Hyperledger Fabric blockchain is based on “Practical Byzantine Fault Tolerant” protocol. Unlike Ethereum’s PoW (Proof or Work) protocol, Hyperledger Fabric PBFT protocol doesn’t have a concept of mining which makes it much well performant than Ethereum in terms of computation but it lacks scalability which is a trade-off.

Discover and review best Blockchain softwares

Comparison between 3 different blockchain technologies

Building your first Blockchain, creating a mini student grade portal “BlockchainUDC”

What is Hyperledger Fabric ?

Hyperledger Composer simplifies application development on top of the Hyperledger Fabric blockchain infrastructure.

Prerequisites

  1. Install Docker
  2. Install VSCode, and VSCode Hyperledger Composer Plugin
  3. Install required npm node packages

3. Setting and running Hyperledger Fabric

When you start Hyperledger Fabric
when you create a peer admin command with this command ./createPeerAdminCard.sh

Instantiating your Business Blockchain with Composer

  1. Create a new project directory

2. Instantiate a Business network by yo npm plugin

3. Navigate to business network folder and install the dependencies

Project structure of your network
  • You define your models which can be Participant, Asset, Event, and Transaction in .cto file which over here is org.acme.mynetwork.cto
  • You define your blockchain logic of smart contract in logic.js under lib folder

4. Coding your model file

  • Line 8–12: You create a participant that will participate in the blockchain which in this case is the professor. You define your class member with annotation ‘o’ and relationship with ‘ →’
  • Line 15–19: You define your asset which is the grade of the student called assetGrade. This asset has studentID and grade.
  • Line 22–25: You create a smart contract to change student’s final grade

5. Coding your logic.js

  • Here you code the functionality of your smart contract which is your business logic.
  • Line 7 : Declare transaction which is your smart contract instantiated in your model file
  • Line 10–18: Prototypes your transaction function which is your smart contract. Line 11 saves the new grade in your asset model. Line 13 calls the assetregistry method that manages asset stored in the blockchain. Line 14–17 is a callback promise if the contract is successfully accessible with the correct namespace defined. Line 17 updates the asset in the blockchain which is your new grade

Creating the .bna file

Once everything is set let’s create a .bna file which defines your compiled logic of your blockchain

As simple as that :)

Test your Blockchain application on composer playground

What is composer playground?

Hyperledger Composer Playground! In this web sandbox, you can deploy, edit and test business network definitions

Go to https://composer-playground.mybluemix.net to use composer playground

  1. Create Import/Replace button and select Drop Here to Upload or Browse

2. Choose your .bna created from previous command

3. Go to test directory to actually test your business blockchain application

4. Create a new participant, in this case it is professor

5. Create a empty asset which is the student grade

6. Invoke the smart contract to change the grade by clicking on Submit Transaction Button

7. The smart contract will be invoked and the asset will change.

8. You will have history of all transaction made through smart contract in All Transaction button

Deploying the .bna file on the Hyperledger Fabric Blockchain

composer runtime install — card PeerAdmin@hlfv1 — businessNetworkName my-network
composer network start — card PeerAdmin@hlfv1 -A admin -S adminpw -a network.bna -f networkadmin.card
composer card import -f networkadmin.card

Interfacing you blockchain with Restfull CRUD Apis

Fill in your proper details which correct network card name which we recently created

Explore your Restful Apis created by loopback on http://localhost:3000 :)

Awesome!

Now you can create a proper front end application in Angular/React/Vuejs or which even framework of your choice by leveraging these rest apis.

Read more:

  1. Follow me for more: https://www.engineerability.com

Get Best Software Deals Directly In Your Inbox

Conclusion

This tutorial has only covered basic just to get started with Hyperledger Composer based on Hyperledger Fabric by using Hyperledger Composer. We successfully created a end-to-end blockchain that allow the professor to store the grade of the student. By working on top of this tutorial you create a blockchain replica of blackboard. With blockchain the possibilities are endless.

If this was a informative blog, clap if you liked it :) A lot of effort has been put writing this blog.

Click to read today’s top story

--

--