Getting Started with Hyperledger Composer

Ferry Djaja
Coinmonks
7 min readSep 4, 2018

--

On the first post, I would like to run through the installation process of Hyperledger Composer on Ubuntu 16.0.4 LTS. On the part two, we will create a simple SAP Mobile App that consumer the REST API of Hyperledger Composer.

Discover and review best Blockchain softwares

What is Hyperledger Composer?

According to their website: Hyperledger Composer is a set of collaboration tools for building blockchain business networks that make it simple and fast for business owners and developers to create smart contracts and blockchain applications to solve business problems. Built with JavaScript, leveraging modern tools including node.js, npm, CLI and popular editors, Composer offers business-centric abstractions as well as sample apps with easy to test devops processes to create robust blockchain solutions that drive alignment across business requirements with technical development.

https://www.hyperledger.org/projects/composer

Hyperledger Composer Installation

At your home directory, install the prerequisites by running the following script:

cd $HOME
curl -O -k https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh
chmod u+x prereqs-ubuntu.sh

run the command to install the:

sudo apt-get install -y software-properties-common

Once it’s completed without any error, you will get this message:

Install Required Components

npm install -g composer-cli@0.20
npm install -g composer-rest-server@0.20
npm install -g generator-hyperledger-composer@0.20
npm install -g yo

Install Playground

npm install -g composer-playground@0.20

Install Hyperledger Fabric

Create a directory fabric-dev-servers at your home directory.

mkdir ~/fabric-dev-servers && cd ~/fabric-dev-servers

And run curl command to download the tool and unzip it.

curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.tar.gztar -xvf fabric-dev-servers.tar.gz

Download the runtime:

export FABRIC_VERSION=hlfv12
./downloadFabric.sh

Running on the First Time

cd ~/fabric-dev-servers
export FABRIC_VERSION=hlfv12
./startFabric.sh
./createPeerAdminCard.sh

To stop, run this command:

~/fabric-dev-servers/stopFabric.sh

Start the Playground

To start, run the following command:

composer-playground

It will open the browser automatically and redirect to http://localhost:8080/login

Create New Business Network with Command Line

Create a new folder at your home folder, name it (for example) blockchainDocker. Go to that folder and run this command:

yo hyperledger-composer:businessnetwork

Fill the Business network name: tutorial-network, Namespace: org.basic.server and the rest is up to you.

Upon successful, you will see the new folder tutorial-network was created.

Go to the folder tutorial-network/lib and update the logic.js with the following content:

Update org.basic.server.cto in tutorial-network/models with the following content:

We will leave permissions.acl file as it is.

Generate a Business Network Archive

After we defined the business network, it must be packaged into a deployable business network archive (.bna) file. Go to the tutorial-network folder and run this command.

composer archive create -t dir -n .

Deploying Business Network Archive

Run the following command to install the business network from tutorial-network directory:

composer network install --card PeerAdmin@hlfv1 --archiveFile tutorial-network@0.0.1.bna

To start the business network, run the following command:

composer network start --networkName tutorial-network --networkVersion 0.0.1 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card

To import the network administrator identity as a usable business network card, run the following command:

composer card import --file networkadmin.card

Once you have imported, you will see the card in the Playground: http://localhost:8080/login

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

composer network ping --card admin@tutorial-network

Test the Network

We are going to test the network by populating the prerequisite information for the asset DataCenterId. The Participant Personnel in this example is not being used. Once we have the DataCenterID populated, we can submit the transaction against the particular DataCenterID with the server parameters like how many memory installed, how many CPU installed, etc.

  • Go to Test tab.
  • Populate the prerequisite information. Under Participants, select Personnel and click New Participant.
  • Once you created, you will see the registry entry under Participants.
  • Create the Asset DataCenterId (for example DC1).
  • Submit a transaction to a particular Data Center, for this example is the Data Center Id DC1 that we just created.
  • If we submit the transaction to the Data Center Id which is not exist, it will throw an error message. In this example below, the Data Center Id DC2 has not been defined and we will get an error message.
  • Once you have successfully submitted the transaction, you will see the server parameter information is populated under the Data Center Id you have submitted (in this case is DC1).
  • To see the log of transactions, select All Transactions. Click view record to see all the detail log transactions.

REST Server

The last thing that we want to build is the REST server. With this, we can expose the Assets, Participants and Transactions that we have created so it can be consumed with the client app like web or mobile app.

We will create a docker container for this purpose with the following steps:

  • Install the LoopBack connector for MongoDB:
npm install -g loopback-connector-mongodb
  • Make sure you can ping the tutorial-network. Run this command:
composer network ping -c admin@tutorial-network
  • Start an instance of docker called mongo. This instance will be used to persist all information regarding authenticated users and their wallets for the REST server.
docker run -d --name mongo --network composer_default -p 27017:27017 mongo
  • Create a new file called Dockerfile under new folder with the following contents:
FROM hyperledger/composer-rest-server
RUN npm install --production loopback-connector-mongodb passport-github && \
npm cache clean --force && \
ln -s node_modules .node_modules
  • Run this command:
docker build -t myorg/my-composer-rest-server .
  • Create a new file envvars.txt on the same folder with this content:
COMPOSER_CARD=admin@tutorial-network
COMPOSER_NAMESPACES=never
COMPOSER_AUTHENTICATION=true
COMPOSER_MULTIUSER=false
COMPOSER_PROVIDERS='{
"github": {
"provider": "github",
"module": "passport-github",
"clientID": "REPLACE_WITH_CLIENT_ID",
"clientSecret": "REPLACE_WITH_CLIENT_SECRET",
"authPath": "/auth/github",
"callbackURL": "/auth/github/callback",
"successRedirect": "/",
"failureRedirect": "/"
}
}'
COMPOSER_DATASOURCES='{
"db": {
"name": "db",
"connector": "mongodb",
"host": "mongo"
}
}'
  • Run this command to load it to the environment variable:
source envvars.txt
  • We need to update the localhost string on the ~/.composer/cards/admin@tutorial-network/connection.json.
sed -e ‘s/localhost:7051/peer0.org1.example.com:7051/’ -e ‘s/localhost:7053/peer0.org1.example.com:7053/’ -e ‘s/localhost:7054/ca.org1.example.com:7054/’ -e ‘s/localhost:7050/orderer.example.com:7050/’ < $HOME/.composer/cards/admin@tutorial-network/connection.json > /tmp/connection.json && cp -p /tmp/connection.json $HOME/.composer/cards/admin@tutorial-network/
  • run the docker:
docker run \
-d \
-e COMPOSER_CARD=${COMPOSER_CARD} \
-e COMPOSER_NAMESPACES=${COMPOSER_NAMESPACES} \
-e COMPOSER_AUTHENTICATION=${COMPOSER_AUTHENTICATION} \
-e COMPOSER_MULTIUSER=${COMPOSER_MULTIUSER} \
-e COMPOSER_PROVIDERS="${COMPOSER_PROVIDERS}" \
-e COMPOSER_DATASOURCES="${COMPOSER_DATASOURCES}" \
-v ~/.composer:/home/composer/.composer \
--name rest \
--network composer_default \
-p 3000:3000 \
myorg/my-composer-rest-server
  • Check if the server is running without error, you will see the message as per below screenshot.
docker logs -f rest
  • If no error, you will see the page below with the access token information.
  • Select Server : A transaction named Server and click Post. Populate with the below data and click Try it out!.
{
“$class”: “org.basic.server.Server”,
“Active_Memory”: 4,
“HW_Model”: “Dell”,
“Installed_CPUs”: 2,
“Installed_RAM”: 4,
“Server_Model”: “Model A”,
“datacenterid”: “DC1”
}
  • Now select Get and click Try it out!. You will see the data that you have posted.

Stay tuned for part 2 where we will build a SAP mobile app (SAPUI5) that consumed the REST API. Thanks for reading my post.

Get Best Software Deals Directly In Your Inbox

--

--