Launch your own blockchain with Hyperledger Composer

in ark •  6 years ago  (edited)

hyperledger_composer_logo_color.png

In this post I'll show you how to install Hyperledger Composer and create your own blockchain (business network in terms of Hyperledger). Hyperledger Composer greatly simplifies work with Hyperledger Fabric - a permissioned blockchain. Hyperledger Composer facilitates building your own blockchains via:

  • Modeling Language
  • Reusable POCs
  • Data Integration

Setup Server

Refer to "Cloud Server Setup" section here

Install Hyperledger Composer

Create and switch to a not root user:

adduser ark
usermod -a -G sudo ark
su - ark


Install prerequisites:

curl -O https://hyperledger.github.io/composer/latest/prereqs-ubuntu.sh
chmod u+x prereqs-ubuntu.sh
./prereqs-ubuntu.sh
source .bashrc


Install dev tools:

npm install -g [email protected]
npm install -g [email protected]
npm install -g [email protected]
npm install -g yo
npm install -g [email protected]


Install Hyperledger Fabric:

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


Start fabric:

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


Open 8080 port:

sudo ufw allow 8080

Create Your Blockchain


Start Composer Playground:

composer-playground


Access playground in your browser at http://your-ip:8080 and click "Deploy a new business network":

Screen Shot 2018-10-18 at 1.47.17 PM.png

Hyperledger Composer provides a nice selection of business network templates which you can start with and build on top of. We are going to build a simple insurance network from an empty template. Enter "ark-insurance-network" as your network name:

Screen Shot 2018-10-18 at 1.54.03 PM.png

Select empty business network:

Screen Shot 2018-10-18 at 1.54.24 PM.png

Enter ID and Secret:

Screen Shot 2018-10-18 at 1.54.47 PM.png

And click "Deploy":

Screen Shot 2018-10-18 at 1.54.53 PM.png

Behind the scenes, Composer launches a new docker image to server your newly created network:

Screen Shot 2018-10-18 at 2.24.46 PM.png

Once deployed, you will see a new connection card with the name of your network. Click "Connect now"

models/model.cto file descibes busineness model of your network. For the purpose of our Insurance Network, we'll have Subscriber, InsurancePolicy, PolicyPaymentTransaction, and PolicyPaymentEvent. Open models/model.cto in the left nav and then copy paste the below code into the editor window:

namespace io.arklabs

participant Subscriber identified by subscriberId {
  o String subscriberId
  o String firstName
  o String lastName
  o String email
}

asset InsurancePolicy identified by policyId {
  o String policyId
  --> Subscriber subscriber
  o String amountPaid
  o String arkTransaction
}

transaction PolicyPaymentTransaction {
  --> InsurancePolicy policy
  o String amountPaid
  o String arkTransaction
}

event PolicyPaymentEvent {
  --> InsurancePolicy policy
  o String amountPaid
  o String arkTransaction
}


Once that is done, lets add logic which will be operating with the above defined model. Select "Add a file":

Screen Shot 2018-10-18 at 2.31.48 PM.png

and choose "Script File (.js)":

Screen Shot 2018-10-18 at 2.33.25 PM.png

Then copy and paste the below code:


'use strict';

/**
 * Sample insurance payment transaction
 * @param {io.arklabs.PolicyPaymentTransaction} policyPaymentTransaction
 * @transaction
 */
async function policyPayment(tx) {
    tx.policy.amountPaid = tx.amountPaid;
    tx.policy.arkTransaction = tx.arkTransaction;

    const policyRegistry = await getAssetRegistry('io.arklabs.InsurancePolicy');    
    await policyRegistry.update(tx.policy);

    // Emit an event for the modified asset.
    let event = getFactory().newEvent('io.arklabs', 'PolicyPaymentEvent');
    event.policy = tx.policy;
    event.amountPaid = tx.amountPaid;
    event.arkTransaction = tx.arkTransaction;
    emit(event);
}


We are almost set. As the last step before we can play with our new blockchain we need to set permissions. Open permissions.acl and copy paste the below code:

rule EverybodyCanReadEverything {
    description: "Allow all participants read access to all resources"
    participant: "io.arklabs.Subscriber"
    operation: READ
    resource: "io.arklabs.*"
    action: ALLOW
}

rule EverybodyCanSubmitTransactions {
    description: "Allow all participants to submit transactions"
    participant: "io.arklabs.Subscriber"
    operation: CREATE
    resource: "io.arklabs.PolicyPaymentTransaction"
    action: ALLOW
}

rule OwnerHasFullAccessToTheirAssets {
    description: "Allow all participants full access to their assets"
    participant(p): "io.arklabs.Subscriber"
    operation: ALL
    resource(r): "io.arklabs.InsurancePolicy"
    condition: (r.owner.getIdentifier() === p.getIdentifier())
    action: ALLOW
}

rule SystemACL {
  description:  "System ACL to permit all access"
  participant: "org.hyperledger.composer.system.Participant"
  operation: ALL
  resource: "org.hyperledger.composer.system.**"
  action: ALLOW
}

rule NetworkAdminUser {
    description: "Grant business network administrators full access to user resources"
    participant: "org.hyperledger.composer.system.NetworkAdmin"
    operation: ALL
    resource: "**"
    action: ALLOW
}

rule NetworkAdminSystem {
    description: "Grant business network administrators full access to system resources"
    participant: "org.hyperledger.composer.system.NetworkAdmin"
    operation: ALL
    resource: "org.hyperledger.composer.system.**"
    action: ALLOW
}


Click "Deploy changes":

Screen Shot 2018-10-18 at 2.39.52 PM.png

Leave defaults and click "Upgrade":

Screen Shot 2018-10-18 at 2.41.00 PM.png

Try Your Blockchain in Action!

Select "Test" tab in the top panel:

Screen Shot 2018-10-18 at 2.43.25 PM.png

First, let's create a Subscriber participant. Make sure "Subscriber" is selected on the left menu, and then click "Create New Participant". Fill in the data and click "Create New":

Screen Shot 2018-10-18 at 2.46.11 PM.png

Then, we need to create an InsurancePolicy asset. Select "InsurancePolicy" on the left menu, and then click "Create New Asset". Fill in the form and hit "Create New":

Screen Shot 2018-10-18 at 2.55.42 PM.png

Finally, let the subscriber pay for his insurance policy. Click "Submit Transaction", fill in payment details, and click "Submit":

Screen Shot 2018-10-18 at 2.58.44 PM.png

Once transaction goes through, you'll see it in the list of "All Transactions":

Screen Shot 2018-10-18 at 2.59.45 PM.png

Transaction details can be seen via "view record" link next to transaction:

Screen Shot 2018-10-18 at 2.59.57 PM.png

You can see how this payment transaction has updated our insurance policy asset with amount paid:

Screen Shot 2018-10-18 at 2.59.27 PM.png

And that's how you submit a transaction on your own blockchain! Now, most likely you'll need to access your blockchain programmatically. And, Hyperledger Composer really simplifies your life by providing out of the box REST API server.

Launch REST API Server for your Blockchain

In terminal run "composer-rest-server" and specify connection details:

Screen Shot 2018-10-18 at 3.17.58 PM.png

Now you can access Swagger's interface exposing RESTful API methods for your blockchain:

Screen Shot 2018-10-18 at 3.21.02 PM.png

In summary,

as you have seen, Hyperledger Composer makes launching your own private blockchain really straightforward. We have launched a sample insurance blockchain, created participant, asset and submitted a transaction. In addition, we launched RESTful API server. And all that was with little to no coding!

Resources

Hyperledger Fabric: https://www.hyperledger.org/projects/fabric
Hyperledger Composer: https://hyperledger.github.io/composer/latest/
Swagger: https://swagger.io/

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @vadymus! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Support SteemitBoard's project! Vote for its witness and get one more award!