Hyperledger Chennai Meetup — July — Workshop on Hyperledger Composer with WaveMaker

Chennai Hyperledger Meetup — July had a good amount of people from various domains and industries. Our main focus on the event was a workshop on Hyperledger Composer. Most of the Blockchain meetups which is hosted in Chennai get good amount of participants who are new to Blockchain. So we wanted to concentrate on introducing the Tools and show a simple example on How to create Blockchain Applications with Hyperledger Composer & Wavemaker.

Before getting started I would like to thank Hyperledger for sending us the awesome USBs. The USBs were given to all the participants in the Meetup.

USBs sent by Hyperledger Foundation

Our main aim of the workshop was to do a commercial property leasing or house leasing application using Hyperledger Composer but we changed that to show an easier one so that it would be easier for new people to understand and start working. We picked the best and easiest one to develop and understand — Which was the Hyperledger Composer Marble Trade example. The reason for taking this example, many people wanting to learn Blockchain are finding it hard to understand the exact usecase on how to implement Blockchain on business networks. Marbles trade gives us good and simple steps to understand logic of how we transfer an asset between users on a business network using Blockchain. Also, we want to show how quickly you will be able to create a PoC with Hyperledger Composer and WaveMaker (Rapid Application Development for Web).

Before getting started users have to install Composer and start Fabric. The exact steps on composer installation and starting fabric are available on the Hyperledger Composer website — this is the best place to get started. But for this workshop i will post it here:

Prerequisites:

Operating Systems: Ubuntu Linux 14.04 / 16.04 LTS (both 64-bit), or Mac OS 10.12
Docker Engine: Version 17.03 or higher
Docker-Compose: Version 1.8 or higher
Node: 6.x (note version 7 is not supported)
npm: 3.10.x
git: 2.9.x
A code editor of your choice, we recommend VSCode.

If you’re running on Ubuntu, you can download the prerequisites using the following commands:

curl -O https://raw.githubusercontent.com/hyperledger/composer-sample-applications/c023b4c28216ac288d30449673185eb057a0dcf1/packages/getting-started/scripts/prereqs-ubuntu.sh

chmod u+x prereqs-ubuntu.sh
./prereqs-ubuntu.sh

Step 1: Installing Hyperledger Composer development tools

The development tools you’ll need can all be installed with npm install -g

  1. To install composer-cli run the following command:
  2. Copynpm install -g composer-cli
  3. The composer-cli contains all the command line operations for developing business networks.
  4. To install generator-hyperledger-composer run the following command: npm install -g generator-hyperledger-composer
  5. The generator-hyperledger-composer is a Yeoman plugin that creates applications for your business network.
  6. To install composer-rest-server run the following command:npm install -g composer-rest-server
  7. The composer-rest-server uses the Hyperledger Composer LoopBack Connector to connect to a business network, extract the models and then present a page containing the REST APIs that have been generated for the model.
  8. To install Yeoman run the following command:npm install -g yo
  9. Yeoman is a tool for generating applications. When combined with the generator-hyperledger-composer component, it can interpret business networks and generate Angular applications based on them. Please note: This will create components only for Assets, for Participants & Transactions we will have to build separately.

Optional development tools

  1. If you want to run the Playground locally, install the composer-playground using the following command install -g composer-playground

Step 2: Starting Hyperledger Fabric & Creating your Business Network

docker kill $(docker ps -q)
docker rm $(docker ps -aq)
docker rmi $(docker images dev-* -q)
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
export FABRIC_VERSION=hlfv1 (Fabric 1.0)
cd ~/fabric-tools
./downloadFabric.sh
./startFabric.sh
./createComposerProfile.sh

After the installation is successful you can start using Composer playground — playground is a tool to pre-test your Blockchain applications before deploying it. For this workshop we will not be testing with composer playground, we will directly create a Composer application and deploy it to Fabric.

To make sure your Fabric is running, please check Docker containers.

Step 1:

Create a directory called marbles-trade

mkdir marbles-trade

cd marbles-trade && npm init

Step 2:

Create two directories for Logic and Model files

mkdir lib

mkdir models

Step 3:

Hyperledger Composer includes an Object-Oriented modeling language that is used to define the domain model for a business network definition. Assets, Participants & Transactions are the main Class definitions available on a Model. Here we define marble as an Asset, Players as Participant and Marble Trade as Transaction.

cd models
vim marbles.cto
namespace org.chennai.marbles
enum MarbleColor {
o RED
o GREEN
o BLUE
o PURPLE
o ORANGE
}
enum MarbleSize {
o SMALL
o MEDIUM
o LARGE
}
asset Marble identified by marbleId {
o String marbleId
o MarbleSize size
o MarbleColor color
--> Player owner
}
participant Player identified by email {
o String email
o String firstName
o String lastName
}
transaction TradeMarble {
--> Marble marble
--> Player newOwner
}

Step 4:

Hyperledger Composer includes a Logic file that is written in Javascript. The file contains the function to represent the Marble ownership trade between two players.

cd lib
vim logic.js

/**
* Trade a marble to a new player
* @param  {org.chennai.marbles.TradeMarble} tradeMarble - the trade marble transaction
* @transaction
*/
function tradeMarble(tradeMarble) {
tradeMarble.marble.owner = tradeMarble.newOwner;
return getAssetRegistry('org.chennai.marbles.Marble')
.then(function (assetRegistry) {
return assetRegistry.update(tradeMarble.marble);
});
}

Step 5:

Hyperledger Composer includes an access control language (ACL) that provides declarative access control over the elements of the domain model. By defining ACL rules you can determine which users/roles are permitted to create, read, update or delete elements in a business network. Here we give allow access to all. Please Note: On this workshop we will not dig into Identity, we will talk more on that offline.

vim permissions.acl

/**
* Sample access control list.
*/
rule Default {
description: "Allow all participants access to all resources"
participant: "ANY"
operation: ALL
resource: "org.chennai.marbles.*"
action: ALLOW
}

Step 6: Create & Deploy .bna

Package up your Business Network (lib/logic.js, models/marbles.cto, permission.acl & Package.json) and export it as an Archive called .bna file (or) Banana File. Which can be used to deploy on Fabric.

composer archive create --archiveFile marbles-trade.bna --sourceType marbles-trade/ --sourceName marbles-trade
Once the .bna file is created without errors
composer network deploy -a marbles-trade.bna -p defaultProfile -i PeerAdmin -s adminpw

Step 7: Start Composer Rest Server

Hyperledger Composer includes a standalone Node.js process that exposes a business network as a REST API. The LoopBack framework is used to generate APIs. This generates APIs for Assets, Participants and Transactions.

composer-rest-server -p hlfv1 -n marbles-trade -i PeerAdmin -s adminpw -N always
Once started open your browser to check http://localhost:3000/explorer

Now that the rest APIs are set, the next step is to create UIs for the Rest APIs. Composer comes with a Yo generator which generates an Angular Application. But this generates components for only Assets, we should separately do for Participants & Transactions. This is where WaveMaker comes in handy, one should know atleast the basics of Angular to work on the generated code. WaveMaker- Rapid Application Development tool is an easy to use tool for quick UI and logics. To use WaveMaker — the only skillset needed is basic Javascript.

For more information and tutorials on Hyperledger Composer check this

Step 3: Create UI for the Business Network using WaveMaker

Step 1: Create an free account with WaveMaker

Step 2: Create a new App with WaveMaker & Select Web

Step 3: Select Template for your Web Application

For our simple usecase i will select Admin-Dashboard

Step 4: Adding REST APIs as services on Wavemaker

Select REST to add any service from our Generated APIs (In this example — we will add GET all Marbles API)

Step 5: Create a Variable to add the results to the Dashboard

Since the REST service gives us a JSON we will not be able to add the total number of values on the dashboard directly. So on success we will have to call Javascript component to get the Data Length.

Now lets run the application and see whether we are able to get the Total No of Marbles.

Likewise follow the same steps for getting the Total Number of Players.

Step 6: Include Form to Add Players and Marbles

Drag & Drop form component from Widgets, this will open the Configure Form dialog box

Add a player and check the dashboard for the count

Likewise try and add for Marbles.

Step 7: Do Transactions (Transfer ownership of Marble from one player to another)

Show all Marbles and Owners:

We select “M103” to be transferred to “test100@pramati.com”

Once the transaction successfully executes, open show all marbles again to see ownership change

This shows you a basic, easy and quick way to create an Admin dashboard or normal web applications for your Blockchain Business Network. One of the main reasons we have used WaveMaker is because it saves a lot of time in the Application front. We can spend more time on the Business network rather than Web Application.

An usecase to Collaborate on:

Though we have used Marbles Trade as an Example, our initial plan was to do a PoC on Commercial Property Leasing. We planned to do the same based on the Whitepaper written by ANZ & Westpac Australia. In coming weeks we will be hosting a community call where interested people can join in working on a PoC to understand and learn more on Hyperledger Fabric & Hyperledger Composer.

Finally thanks to Satheesh & DTCC for hosting the Meetup.

UseFull Links: