Build Business Blockchain using SCP , Hyperledger Composer and SCP Cloud Connector
Welcome to my blog , this blog is about how we can build Business Blockchain application on Hyperledger fabric using Hyperledger Composer framework and generate rest server which can be connected easily with SAP Cloud Platform using Classic SAP Cloud Connector and finally how seamlessly a application which runs on SAP Cloud Platform can interact with those API. Here i have taken a very simple example “Asset Transfer Between Participants” .
Asset Transfer Between Participants – In This Business Network we will have multiple participants and assets (It can be anything, E.G A mobile is a asset and it is holding by participant A) and Asset can be transferred to any participant in the Business Network.
As a Prerequisite you should know how business blockchain works , what is the difference between Business Blockchain and Crypto currency like Bitcoin , Ethereum etc. In business blockchain we don’t deal with Proof of Work so mining is not require so don’t need high computation power also. In Below Picture you will get some basic difference between Crypto currency blockchain and Hyperledger fabric.
Before going forward i would like to give little bit introduction about Hyperledger fabric and Hyperledger Composer or Fabric’s Composer.
What is Hyperledger fabric ?
Hyperledger Fabric is a blockchain framework implementation and one of the Hyperledger projects hosted by The Linux Foundation. Intended as a foundation for developing applications or solutions with a modular architecture, Hyperledger Fabric allows components, such as consensus and membership services, to be plug-and-play. Hyperledger Fabric leverages container technology to host smart contracts called “Chaincode” that comprise the application logic of the system. Hyperledger Fabric was initially contributed by Digital Asset and IBM, as a result of the first hackathon. Most Importantly SAP Cloud Platform Blockchain as a service (BAAS) is also based on Hyperledger fabric.
What is Hyperledger Composer ?
To Build this demo i have used On-premise Hyperledger fabric which is Installed and running on Ubuntu 16.04 and on top of that i have installed Hyperledger Composer and Yoman . After building and deploying Business Blockchain (.bna) application into fabric i have generated composer rest api which i have connected with SAP Cloud Platform with help of Classic Sap Cloud Connector then built one Sap ui5 application using Web IDE and deployed the ui5 application into SAP Cloud Platform.
Click here you will get cool guide to setup your Hyperledger fabric and Composer dev environment for Linux and Mac both. Before setting up the Dev environment as a prerequisite you have to Download Prerequisite.
Lets get into stuff .
Step 1: First we have to create business network using yoman which will be creating intial structure of application (.cto , logic.js ) . In .cto file we define our asset , participant and transaction . In logic.js file we define our chaincode or smart contract or business logic for each transaction . In .acl (we have to create permission.acl file explicitly inside root project folder ) file we define security permission for each object.
Give all the details like name , description , license , author etc after that it will generate basic template of your business application.
Step 2: We have to define business network , mainly we need to model our .cto file as per our business model , in logic.js we have to put logic as per our need and permission.acl file we have to define . I have used Visual Studio Code IDE for defining .cto , logic.js and permission.acl file as it supports fabric’s composer plugin.
To make it super duper simple our business network will consist one transaction which will be triggered when asset transfer will be happened between participant .
Below picture describe the model file , we have user participant type , comodity asset type and a transaction called trade.
Here is our Chaincode / Smart contract /Business Logic for transaction trade , which just change the asset owner for particular asset.
I kept my permission.acl file also very simple everything is permitted . this permission.acl file we have to create explicitly .
Once we are done with this three file we have to generate deployable .bna file .
Step3 : To generate deployable .bna file from the project root directory use command in terminal .
composer archive create -t dir -n .
Upon successful command it will create .bna archive file inside root project folder.
Now before deploying we have to make sure our Hyperledger fabric is running , admin card is created and Chaincode is instatiated.
Step 4: Start Hyperledger fabric if its not running .
go to fabric-tools folder and run the shell script startFabric.sh.
Upon Successful start you will get screen like below .
Now If card is not present we have to create business network card by running shell script createPeerAdminCard.sh.
Now we have to install composer runtime for this business network here we instantiate our chaincode also implicitly .
composer runtime install --card PeerAdmin@hlfv1 --businessNetworkName devaucnw
Now we can deploy our deployable .bna file into Hyperledger fabric.
Step 5: To deploy the business network, from the businessnetwork directory, run the following command:
composer network start --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw --archiveFile email@example.com --file networkadmin.card
Step 6: Our Deployment is successful , now we have to import the network administrator identity as a usable business network card, to do that run the following command:
composer card import --file networkadmin.card
To check that the business network has been deployed successfully, run the following command to ping the network:
composer network ping --card admin@devaucnw
Step 7: Generate composer rest server for this Business network (devaucnw) so we can easily integrate with our ui5 application.
To generate composer rest server type following commands
Give all the details as below
Our Fabric and Composer part is ready now we have to configure composer rest api with Sap Cloud Connector. As i am running linux inside my vmware workstation player i can access api explorer from my primary os by addressing http://192.168.29.128:3000/explorer/ in url, and base Api path will be for me http://192.168.29.128:3000/api/.
Below is my Cloud Connector Configuration.
Lets Look at my destination in Sap Cloud Platform Connectivity Service.
Whole Project is hosted On Github .
Now lets test the Deployed Application on Sap Cloud Platform.
Lets Create Participants and Assets by clicking on Asset and Participants tile.
Same way we can create Asset , here for example i have created a asset One Plus 6T mobile and Kandarp S is owner for this asset.
Now we will go back to Dashboard and perform a transaction where asset will be transferred to Nitin
After Successful trade asset owner should be changed , so lets go back to asset list and check the owner.
I hope you enjoyed the coolest integration between Sap Cloud Platform and On-Premise Hyperledger Fabric . New Idea , suggestion , comments are always welcome which will definitely encourage me to write more story on Blockchain and Sap Cloud Platform.