Since learning about the Hyperledger Blockchain, I've been intrigued by how it works and the evolution of solutions offered on the network. When I got the opportunity to develop and deploy a composer network for a smart insurance project, I was excited.
Composer is one of the network solutions offered by Hyperledger alongside other projects like:
- Burrow: Permissionable smart contract machine (EVM)
- Fabric: Permissioned with channel support
- Sawtooth: Permissioned & permissionless support EVM transaction family
- Aries: Infrastructure for peer-to-peer interactions
These are just some of the solutions offered within the Hyperledger project structure. In this post however, I'll be guiding you through creating a Hyperledger Composer Network as I did it for our smart insurance decentralized application SmartInsure.
This guide assumes you're using an Ubuntu operating system; linux 16.04/18.04 LTS (64-Bit).
Installing Pre-Requisites & Development Environment
Before getting started with our pre-requisite installation its important to note the following: avoid installing or running this program as root. Run prereqs-ubuntu.sh as a normal user.
curl -O https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh chmod u+x prereqs-ubuntu.sh
Next run the script - you may be prompted for a password for the execution of the script functions.
./prereqs-ubuntu.sh
Now that our pre-requisites are successfully installed, lets proceed to installing the development tools we will use primarily to create the business network. These include the Command Line Interface (CLI) tools like composer-cli, generator-hyperledger-composer and more.
Note again that you should not use sudo for the following npm commands
- To install CLI tools run:
- To run the REST Server on your machine to expose your business networks as RESTful APIs install this utility:
- The Hypeledger genenrator is a key component for creating application assets:
- Yeoman is a tool for generating applications, which utilises generator-hyperledger-composer:
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
Installing Hyperledger Fabric
With all the development tools installed, the next step is to install the local Hyperledger Fabric runtime to deploy your business networks.
- First, we'll create a directory ~/fabric-dev-servers, get the .tar.gz file that contains the tools to install Hyperledger Fabric:
- With the scripts you just downloaded and extracted, download a local Hyperledger Fabric v1.2 runtime:
mkdir ~/fabric-dev-servers && cd ~/fabric-dev-servers curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.tar.gz tar -xvf fabric-dev-servers.tar.gz
cd ~/fabric-dev-servers export FABRIC_VERSION=hlfv12 ./downloadFabric.sh
With this two easy steps, you've now installed everything required for the typical Developer Environment. To develop and test your blockchain business network Hyperledger composer gives user three options.
- Using a web app, Composer Playground, typically opened on your browsser automatically run on the address http://localhost:8080/login;
- A bespoke REST API based on a business network at http://localhost:3000/explorer
- A skeleton Angular 4 Application running aganist the REST API at http://localhost:4200
With your environment set up, we can now start up a new runtime by running a start script that generates a PeerAdmin card. To do this, run:
cd ~/fabric-dev-servers export FABRIC_VERSION=hlfv12 ./startFabric.sh ./createPeerAdminCard.sh
Since we can't cover all the three options in this single post, I recommend reading the Hyperledger Composer official business guide to learn about the Playground or Angular 4. In this post we will build and deploy a business network and run it on the composer-rest-server.
Building A Business Network
On Composer, creating a business network structure is as simple as using a Yeoman generator that creates a directory containing all the components of a business network. To be the SmartInsure network, I follow these few steps:
- Run the command:
- Enter smartinsure for the network name, and desired information for description, author name, and author email.
- Select Apache-2.0 as the license.
- Select com.smartinsure as the namespace.
- Select No when asked whether to generate an empty network or not.
yo hyperledger-composer:smartinsure(business-network)
Defining The Network Parameters
With the business network successfully created, there is a model (.cto) file which will contain the class definitions for all assets, participants, and transactions in the business network. The skeleton business network also contains an access control (permissions.acl) document with basic access control rules, a script (logic.js) file containing transaction processor functions, and a package.json file containing business network metadata.
Here, you'll get the chance to define and costumize your network parameters. On SmartInsure, our assets include contracts and claims; our participants include customers and vendors and our transactions include createContract, initClaim, queryContract and queryClaim. Once you have customized your network parameters. You're ready for the next step - generating a business network archive.
Generating A Business Network Archive
To do this, we must package our new network into a deployable business network archive (.bna) file. Simply:
- Navigate to the business-network directory.
- From the business-network directory, run the following command:
composer archive create -t dir -n .
Deploy The Business Network
Deploying a business network to the Hyperledger Fabric requires the Hyperledger Composer business network to be installed on the peer, then the business network can be started, and a new participant, identity, and associated card must be created to be the network administrator. Finally, the network administrator business network card must be imported for use, and the network can then be pinged to check it is responding.
- To install the business network, from the business-network directory, run the following command:
- To start the business network, run the following command:
- To import the network administrator identity as a usable business network card, run the following command:
- To check that the business network has been deployed successfully, run the following command to ping the network:
composer network install --card PeerAdmin@hlfv1 --archiveFile business-network@0.0.1.bna
composer network start --networkName business-network --networkVersion 0.0.1 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card
composer card import --file networkadmin.card
composer network ping --card admin@business-network
The composer network ping command requires a business network card to identify the network to ping.
Using The Composer Rest Server
Hyperledger Composer can generate a bespoke REST API based on a business network. For developing a web application, the REST API provides a useful layer of language-neutral abstraction.
- To create the REST API, navigate to the business-network directory and run the following command:
- Enter admin@smartinsure as the card name.
- Select never use namespaces when asked whether to use namespaces in the generated API.
- Select No when asked whether to secure the generated API.
- Select Yes when asked whether to enable event publication.
- Select No when asked whether to enable TLS security.
composer-rest-server
The generated API is connected to the deployed blockchain and business network.
Conclusion
And there you have, you have successfully installed Hyperledger Fabric, created a business network and deployed it. Your network can now make transactions, can be integrated to you web or mobile application through the REST API and offer your customers a decentralized service. Building a cool product, I'd love to hear about it.