Hyperledger from Scratch-Part 1

Coding in Hyperledger Composer Playground

Alex Devassy
Coinmonks
Published in
7 min readJan 27, 2018

--

Over View

In this tutorial we will create and deploy a business network from scratch, which defines functions to make users, their accounts, and transfer money from one account to another.

Prerequisites

Docker, NPM, Node JS.

Wait don’t be scared, as we wish to start from the basis itself, so for the first let us only approach by writing Model, Script and Access Control files on Hyperledger Composer Playground.

Glossary

Composer Playground: To incorporate the various abstractions in the blocks using hyperledger may become a tedious job for beginners, so this is where playground comes to the rescue. Composer playground is an extensive, open development tool set and framework which makes it easier for developers to develop blockchain applications.

Files: A project hosted in Composer playground, requires three files, namely Model, Script and Access Control. The model file as the name suggests, is present to define the domain model for the blockchain network, in here we define things like, participants, assets, transactions, events, concepts. Script file basically is written to describe the business logic. Access Control is present to enlist the permissions for different users.

So lets start:

Step 1: Click on the following link

you will be directed to a homepage looking like below, we will start by defining a business network from scratch so click on deploy a new business network icon.

Preface: Composer playground presents you with a default business network, so that you can familiarize with the platform. By clicking get started you will be redirected to that business network.

Step 2: Give a name for your business network. For demonstration purposes we have named our network as “testnetwork” and in the template section select empty-business-network template. Now click on deploy button

Step 3: Click on “Add a file” and add model and script files.

Step 4: Click on Model File

Inside the model file copy paste the below code.

/**
* New model file
*/

namespace org.acme.model
asset account identified by accntId {
o String accntId
o Double balance
}

participant user identified by userId {
o String userId
o String firstName
→ account acnt
}

transaction SampleTransaction {
o Double amount
→ account fromaccnt
→ account toaccnt
} // Note: → is actually “- ->”, without space.

Line 1: namespace org.acme.model, defines the unique names which could be used to identify resources in your business network. Example, in above code “user” resource can be identified using the namespace as follows,

org.acme.sample.user

For a clearer understanding refer to the Script file, enlisted below.

Line 2: defines a resource called “account”. Asset in common language refers to “an item of property owned by a person or company, regarded as having value and available to meet debts, commitments, or legacies”, similarly when we define an asset here, we specify a property by the aid of which we can uniquely identify the defined asset. In our example it is, “o String accntId”.

In an asset definition we can also include an optional abstract declaration, this can be useful to avoid redundancy of code.

participant user identified by userId

Here we are in to defining a participant. In common language a participant, is a person who takes part into something, here we are defining the participants of the network, for example users, admin, owner of assets and others.

Similar to “account”, participant can also have attributes such as, userId to uniquely identify each participant and more.

The line “ → account acnt”, defines a unidirectional relationship from “user” participant to “account” asset, that is an account can be uniquely identified by a user which means each user is related to an account.

Next to transfer money from user’s account to another user’s account we have to define a transaction. In our case we are defining a transaction called “SampleTransaction” for transferring money b/w user accounts . Point to note here is that, we do not specify the logic involved behind the transaction in model file.

The attributes given in transaction definition is taken as parameters for the transaction. We can see this later when we test our business network in testing.

Step 5: Looking into the Script file.

Script file defines the logic of our business model. In hyperledger it is known as chain code.

Copy paste the below listed code into your script file

/**
* New script file
*/
/**
* Sample transaction function.
* @param {org.acme.model.SampleTransaction} sampletransfer
* @transaction
*/
function sampletransfer(sampletransfer){
sampletransfer.fromaccnt.balance -=sampletransfer.amount;
sampletransfer.toaccnt.balance +=sampletransfer.amount;
return getAssetRegistry(‘org.acme.model.account’)
.then(function(accountRegistery){
return accountRegistery.updateAll([sampletransfer.fromaccnt,sampletransfer.toaccnt]);
});

}

The above code enlists a simple transaction function, which transfers money from one user account to another.

First lets test the files to achieve a better understanding of the full concept. So let’s start….

Start by updating the business network. To update press the update icon,

Once the update is achieved, go to the test tab.

Now, in the test tab first you have to create user. To do so click on user, click on “Create New Participant”,

A screen like below will be visible

Type in the “userId” as U1 instead of number present, change the “firstName” as per your choice, and in “acnt”, just change the value after “#”, to “A1”, and click Create New tab. You at least need to make two users to carry out a transaction, so in similar fashion as above define one more user.

Create account for each user respectively,

Go to accounts, then create account, here first for user U1,

Put “accntId” as A1, and give a balance amount, in our case we have taken it as 330. Finally just click CreateNew.

Repeat it for user U2 as well.

You land on a screen similar to the one below

On click on submit transaction, you get something like below

Here specify, “amount”, amount that needs to be transferred. In “fromaccnt”, change the value after # to account id of sending account, and in “toaccnt”, again change the value after # to account id of beneficiary account. On clicking submit, you will be redirected to account page, and on this page, you can see the transfer of funds from one account to another.

Now coming back to the Script file, point to note first is that we are using the same namespace,

@param {org.acme.model.SampleTransaction} sampletransfer

this enhances the fact that we can carry out the transactions.

Next we are accessing two objects of account class, that is via “fromaccnt.balance” we access the account balance of sender, and via “toaccnt.balance” we access the account beneficiary, and make the required changes in their respective balances, that is deduct from sender and add to beneficiary.

Now the next lines are present to commit these changes to the global database.

return getAssetRegistry(‘org.acme.model.account’)
.then(function(accountRegistery){
return accountRegistery.updateAll([sampletransfer.fromaccnt,sampletransfer.toaccnt]);
});

line 1 returns all the account assets in A1, A2, a javascript promise. Then the updateAll method updates the registry. In our case it updates A1, A2.

Step 6:

Looking into Access Control file. The access control file, as defined before is to control which kind of users can access, what kind of stuff, like providing super user permissions, only allowing owner to update his account and much more.

To keep our example easy we have not used any access file. By using an access control though this will become suitable for a real life scenario.

With this we arrive at the end of our first article in the series, hope it gives you something to start with, and do lookout for more releasing soon…..

Do share your valuable comments and suggestions with me and if you like then do follow for more..

You can find part-2 of this series here.

Thank you…Peace!!!

Get Best Software Deals Directly In Your Inbox

--

--

Alex Devassy
Coinmonks

Penetration tester | speaks Blockchain, SAP, Red Teaming, Salesforce and Machine Learning