Develop Application with Hyperledger Fabric through SDK


Develop Application with Hyperledger Fabric through SDK

Recipe ID: hsts-r30


Self-paced training

We offer blockchain introduction, Hyperledger for system admin, Ethereum, Solidity, Corda R3, Hyperledger for developers, blockchain cybersecurity and more classes in self-paced video format starting at $60. Click here to learn more and register. For complete self-paced blockchain training, visit our Complete Blockchain Development Training page.


Recipe Overview

Hyperledger focuses primarily on building distributed ledger solutions for permissioned blockchains and consortium networks. It is gaining popularity among large technology companies. Financial players have undoubtedly given blockchain technology substantial exposure as well.
At its core, Hyperledger is an umbrella project of modular open source frameworks and tools for building and experimenting with blockchains. Hyperledger refers to its design as the greenhouse for enterprise blockchains. It aims to be an incubator for developing practical applications and business solutions with blockchain technology.
Hyperledger Fabric is an open source enterprise-grade platform that leverages a highly-modular and configurable architecture. Hyperledger Fabric is optimized for a broad range of industry use cases, including the finance, banking, healthcare, insurance, and public sectors, as well as supply chains and digital asset management.
For those who are not familiar with Hyperledger project Intro to Hyperledger Family and Hyperledger Blockchain Ecosystem, Hyperledger Design Philosophy and Framework Architecture, The Survey of Hyperledger Fabric Architecture and Components for Blockchain Developers and Overview of Building Blockchain Smart Contracts in Hyperledger articles are strongly recommended.
Hyperledger Fabric supports Smart Contact development in general-purpose programming languages, such as JavaScriptJava, Go, and Node.js. Hyperledger Fabric is also operating under a governance model to build trust between participants on a shared network.
We have written two sets of tutorials to explore Hyperledger Fabric in depth. First set covered the following six recipes:
It started with installing Hyperledger Fabric on an AWS EC2 virtual machine, setting up the first Hyperledger Fabric network and working with Hyperledger Fabric Command Line Interface or CLI. We learned the following:

We moved on to show you how to Add New Network to a Channel, Use CouchDB as a State Database for Hyperledger Fabric, and Create a Smart Contract and then Deploy it into the Blockchain.
In short, in the previous recipes, we learned about how to set up and configure Hyperledger Fabric. We explored its key components, including channels, Membership Service Providers (MSPs), the ordering service, and Fabric Certificate Authority (CA).
The second set, we will show you how to build a simple device asset management DApp. It consists of 6 recipes as follows:

In summary, in the second set of recipes, we are going to build a simple device asset management DApp. We will exploit this example by writing chaincode implemented by various programming languages and we'll also build, test, and deploy our DApp.

IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.

In our previous recipes, we used the fabric-samples prebuilt devmode environment to deploy and test our asset manager chaincode using chaincode-docker-devmode. In this recipe, we will write UI code to interact with chaincode from blockchain. We will also build a Fabric CA container to improve the security of our application. Fortunately, fabric-sample provides the basic-network template, which can help us set up a basic Fabric network
We first need to write a Fabric script to start the Fabric network and deploy the chaincode in this network. Using the CLI container, we then install and instantiate the chaincode. The authorized client with the identity of the wallet will be able to interact with the Fabric chaincode in the network.

Creating and Executing startFabric.sh

Follow these steps:

 

Here is the folder structure we have created so far:
Hyperledger Fabric and blockchain application

 

The startFabric.sh file contains the following commands:

export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
CC_RUNTIME_LANGUAGE=golang
CC_SRC_PATH=github.com/assetmgr

rm -rf ./hfc-key-store

./start.sh

The script calls the start.sh file in the fabric-samples section of the basic-network project to bring up the Fabric network. This will start the orderer, couchdb, CLI, peer, and ca container. Then, we issue the CLI command to install and instantiate our assetmgr chaincode:

bring up cli cntainer to install, instantiate, invoke chaincode docker-compose -f ./docker-compose.yml up -d cli

 

docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer /crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp " cli peer chaincode install -n assetmgr -v 1.0 -p "$CC_SRC_PATH" -l "$CC_RUNTIME_LANGUAGE"
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer /crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp

 

After that, we set up our chaincode in a basic network environment. In the next step, we invoke our chaincode from CLI:
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e

"CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer /crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp

 

Once the script file is created, it is ready to execute the script. First, we should make sure there aren't any other Docker containers still running. Here are the commands to clean up the Docker environment:
~/fabric-samples/first-network$ ./byfn.sh down ~/fabric-samples/first-network$ docker rm -f $(docker ps -aq) ~/fabric-samples/first-network$ docker network prune

Now, we can run the script.sh file:

~/itasset/client$ ./startFabric.sh
Here is the result after executing the script.sh file:
Hyperledger fabric application

 

As we can see from the result, the Fabric CA, client, peer, orderer, and counchdb containers are running. The assetmgr chaincode is installed and instantiated in the blockchain. After executing the invoke order chaincode command, the orgAsset status is changed to ORDER.

Setting up a Client Project

Next, it is time for us to write client-side code to trigger our chaincode. As we discussed, when the client sends a request to the Fabric network to query or invoke the chaincode, these requests need to be authorized. In Fabric release 1.4, we can create a wallet by enrolling the user and importing the identity into the wallet. The client application can interact with a smart contract in blockchain by utilizing the fabric-ca-client and fabric-network APIs with the authorized wallet.

Let's create a Node.js app:
1. Navigate to the ~/itasset/client/webapp folder, issue the npm init command, and fill up the related project information. This will create a basic node application:

ubuntu@ip-172-31-9-54:~/itasset/client/webapp$ npm init package name: (webapp) assetmgr

version: (1.0.0)

description: hyperledger cookbook fabric

entry point: (index.js)

2. Install the default npm libraries. This includes the express.js, ejs, fabric-ca-client, and fabric-network libraries, shown as follows:

npm install

npm install express -save

~/itasset/client/webapp$ npm i fabric-ca-client@1.4.0

~/itasset/client/webapp$ npm i fabric-network@1.4.0

~/itasset/client/webapp$ npm install ejs

3. We need to copy three files (connection.json, enrollAdmin.js, and registerUser.js) from the fabric-samples/fabcar project to our project:

cp ~/fabric-samples/basic-network/connection.json .

cp /home/ubuntu/fabric-samples/fabcar/javascript/enrollAdmin.js . cp /home/ubuntu/fabric-samples/fabcar/javascript/registerUser.js . Create empty wallet folder by issue below command:
mkdir wallet

At this step, our files and folders should look as follows:

Hyperledger Fabric application with Node JS

 

4. Since we copied enrollAdmin.js and registerUser.js from the fabric-samples/fabcar folder, we also need to update the file path defined in the enrollAdmin.js and registerUser.js files. Update path.resolve() in the enrollAdmin.js and registerUser.js files, as follows:

const ccpPath = path.resolve(__dirname, 'connection.json');

The connection.js file we copied from basic-network is in the same folder as
enrollAdmin.js and registerUser.js.

5. Let's create a wallet to enroll the admin and the user onto our Fabric network. To do this, issue the following command:

~/itasset/client/webapp$ node enrollAdmin.js ~/itasset/client/webapp$ node registerUser.js

This will create a wallet for the admin and the users. The following screenshot shows the wallet structure for admin and user1:

Hyperledger Fabric

 

The following recipes are excellent resources for installing other Hyperledger tools:

Blockchain Developer Guide- How to Install and work with Hyperledger Sawtooth
Blockchain Hyperledger Composer Business Network Modeling and Environment Setup
Blockchain Developer Guide- How to Install Hyperledger Burrow on AWS
Blockchain Developer Guide- How to Install Hyperledger Iroha on AWS
Blockchain Developer Guide- How to Install Hyperledger Indy and Indy CLI on AWS
Blockchain Developer Guide- How to Install Hyperledger Seth and Docker on AWS

To conclude this recipe, we like to recommend our Blockchain Hyperledger Development in 30 hours course to those interested in pursuing a blockchain development career. Indeed, as of this writing, Hyperledger Foundation offers the following two Hyperledger certifications: The Certified Hyperledger Fabric Administrator (CHFA) and The Certified Hyperledger Sawtooth Administrator (CHSA), both of which are highly regarded in the industry. Hyperledger Foundation is in the process of creating Hyperledger Developer certification program, which may be released in early or middle of 2020. In short, by taking our hands-on online Hyperledger class, you would be able to obtain CHFA certification.

This recipe is written by Brian Wu who is our senior Hyperledger instructor in Washington DC. His Hyperledger Cookbook with 40+ hands-on recipes is highly recommended.

Related Training Courses

Hands-on Node.JS, MongoDB and Express.js Training
Advance JavaScript, jQuery Using JSON and Ajax
Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs
Learn Blockchain Hyperledger Development & Get Certified in 30 Hrs
Become Blockchain Certified Security Architect in 30 hours
Blockchain Certified Solution Architect in 30 hours
Introduction to Python Programming
Object Oriented Programming with UML


Private and Custom Tutoring

We provide private tutoring classes online and offline (at our DC site or your preferred location) with custom curriculum for almost all of our classes for $50 per hour online or $75 per hour in DC. Give us a call or submit our private tutoring registration form to discuss your needs.


View Other Classes!