Creating Application in Hyperledger

ABHISHEK KUMAR
6 min readMar 18, 2018

--

HyperLedger Fabric is a business Blockchain project hosted by Linux Foundation. It is a platform for distributed ledger solutions, underpinned by a modular architecture delivering high degrees of confidentiality, resiliency, flexibility and scalability. It is designed to support plug-gable implementations of different components, and accommodate the complexity and intricacies that exist across the economic ecosystem.

We are going to build a digital bank using Hyperledger Composer. It will have customers and accounts. At the end of it, you’ll be able to transfer funds and record all transactions on blockchain. We’ll expose a RESTful API for the same, so that even a person who has no clue what blockchain is can make a beautiful user interface (UI) around it. We’ll also create this application’s UI in Angular.

Lets Start:

you need to ensure that the machine you’re using is equipped with the required configurations. You may need to download certain prerequisites and set up a basic dev environment. Below are the links to do that. Follow those steps before starting to develop an application, otherwise you’ll definitely run into stupid errors.

Install prerequisites : https://hyperledger.github.io/composer/installing/installing-prereqs.html

Install development Environments: https://hyperledger.github.io/composer/installing/development-tools.html

now go to fabric-tools folder and run ./downloadFabric.sh it will take time after downloading this run ./startFabric.sh it will also take time after this run ./createPeerAdminCard.sh it will create business card.

Now that your machine is all set, we can start coding

Step 1: Outline your Business Network

Our Business Network Definition (BND) consists of the data model, transaction logic, and access control rules. The data model and access control rules are coded in domain specific language (which is very simple to catch up with). The transaction logic will be coded in JavaScript.

To create a BND, we need to create a suitable project structure on disk. We will create a skeleton business network using Yeoman. To create a project structure, open your terminal and run the following command:

$ yo hyperledger-composer

This will shoot out a series of questions as follows. You’ll be required to use your arrow keys to navigate through the answers.

Open this project in your favorite text editor. I’m using Visual Code. This is what the file structure will look like:

Delete the contents of test/logic.js. We won’t be using it at the moment.

Step 2.1: Coding out our Business Network (models/test.cto)

First, we’ll define models/test.cto. It contains the class definitions for all assets, participants, and transactions in the business network. This file is written in https://hyperledger.github.io/composer/reference/cto_language.html .


namespace test
asset Account identified by accountId {
o String accountId
--> Customer owner
o Double balance
}
participant Customer identified by customerId {
o String customerId
o String firstName
o String lastName
}
transaction AccountTransfer {
--> Account from
--> Account to
o Double amount
}

Account is an asset which is uniquely identified with accountId. Each account is linked with Customer who is the owner of the account. Account has a property of balance which indicates how much money the account holds at any moment.

Customer is a participant which is uniquely identified with customerId. Each Customer has firstName and lastName.

AccountTransfer is a transaction that can occur to and from an Account. And how much money is to be transferred is stored in amount.

Step 2.2: Coding out the Business Network (lib/logic.js)

In this file, we’ll add transaction logic in JavaScript.

/**
* Sample transaction
* @param {test.AccountTransfer} accountTransfer
* @transaction
*/
function accountTransfer(accountTransfer) {
if (accountTransfer.from.balance < accountTransfer.to.balance) {
throw new Error ("Insufficient funds");
}
accountTransfer.from.balance -= accountTransfer.amount;
accountTransfer.to.balance += accountTransfer.amount;
return getAssetRegistry('test.Account')
.then (function (assetRegistry) {
return assetRegistry.update(accountTransfer.from);
})
.then (function () {
return getAssetRegistry('test.Account');
})
.then(function (assetRegistry) {
return assetRegistry.update(accountTransfer.to);
});
}

@param {test.AccountTransfer} accountTransfer is the decorator we put at the top of the file to link the transaction with our JavaScript function. Then we validate if the account where funds are has enough money. Otherwise, an error will be thrown. Then we perform basic addition and subtraction on the account’s balance.

At this point, the most important step is to update this on the blockchain. To do this we call getAssetRegistry API of our assets which is Account. Then we update the retrieved assetRegistry for both the account doling out the funds and the account receiving the funds.

Step 3: Generate the Business Network Archive (BNA)

Now that the business network has been defined, it must be packaged into a deployable business network archive (.bna) file.

Step 3.1: Navigate into the test-bank app in your terminal.

Step 3.2: Run the following command:

$ composer archive create -t dir -n . 

This creates a .bna file in the test-bank folder.

Step 4.1: Install composer runtime

$ composer runtime install --card PeerAdmin@hlfv1 --businessNetworkName test-bank

Step 4.2: Deploy the business network

$ composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --archiveFile test-bank@0.0.1.bna --file networkadmin.card

Step 4.3: Import the network administrator identity as a usable business network card

$ composer card import --file networkadmin.card

Step 4.4: To check that the business network has been deployed successfully,run the following command to ping the network:

$ composer network ping --card admin@test-bank

STEP 5: Expose a RESTful API

To create a RESTful API from your command line, run the following command:

$ composer-rest-server

This will shoot a lot of questions.

Now point your browser to http://localhost:3000/explorer. You’ll see your beautiful blockchain API.

First, let’s add a customer named Abhishek Kumar:

We get a 200 response code.

Now we’ll add customer named Satish Kumar in a similar way.

To check if you’ve added them correctly, GET them.Now let’s add two Customers.

next add test account details:

We get a 200 response code.

Let’s check if the balance is updated by getting the account information.

Step 6: Angular Front End

To create Angular scaffolding automatically, run the following command in the test-bank folder:

$ yo

files will look like

now start npm it will open on http://localhost:4200

you will see below screen

Now go to Assets in the top right corner and click on Account.

These are the exact accounts we created.

So now you can play around with this.

You have your front end and your backend ready!

All transactions that happen on localhost:3000 are reflected on localhost:4200 and vice versa. And this is all on blockchain.

--

--

ABHISHEK KUMAR

DevOps/Cloud | 2x AWS Certified | 1x Terraform Certified | 1x CKAD Certified | Gitlab