Build an insurance application with Hyperledger Composer and React.js Part 1

Jack Hickey
Jun 10 · 9 min read

This tutorial covers how to build a simple Hyperledger Fabric network with Hyperledger Composer and React.js as the front-end web application.

The application will be an insurance application that utilizes the blockchain to perform risk analysis in a more efficient and secure manner.

  • Part 1 looks at implementing the blockchain network

Hyperledger Composer

Hyperledger composer is a tool for building blockchain business networks on Hyperledger Fabric. It abstracts a lot of the complexities involved in building the blockchain network.

To install Hyperledger Composer follow the instructions here.

Overview

The system is an insurance application that utilizes the blockchain to perform a more efficient and secure risk analysis. This is achieved as the policyholders carry their data between insurers meaning the policyholders bring their data to the insurer.

There are two participants in the system:

  • Policyholders

The policyholders of the system want to do the following:

  • Add assets

The insurers want to be able to perform the following:

  • View assets open to insurance

Policyholders are individuals looking for insurance or already have insurance for an asset such as a house or a car.

Insurers are the companies offering this insurance to the policyholders. Unlike traditional insurance where the policyholder seeks out the insurance company, the policyholder advertises their needs of insurance to the blockchain network and insurance companies make offers.

Risk analysis will be implemented as chaincode which will automatically run when a policyholder is looking for insurance. All data relating to past claims and assets are owned and carried by the policyholder and does not remain with the insurer. This data will be used to perform a simple assessment of the policyholder and the requirements of their asset to provide a risk analysis score which insurers can use to inform their insurance offers.

As this system will have sensitive data that we do not want everyone to see we will need to implement permissions that control the access to this personal data.

High-Level System Overview

Implementation

If you haven’t already installed Hyperledger Composer and its requirements please follow the instructions here.

You can find all the code here on GitHub.

Let’s start by creating a folder in which to store our application:

mkdir insurance-application && cd insurance-application

Now let’s create a skeleton for our business network using yeoman

yo hyperledger-composer:businessnetwork

Business network name: risk-analysis-tutorial
Description: risk analysis tutorial
Author name: your name
Author email: your email
License: Apache-2.0
Namespace: org.acme.riskanalysis
Do you want to generate an empty template network? Yes

cd risk-analysis-tutorial

Running the command ls should reveal the following folders and files:
- models
- package.json
- permissions.acl
- README.md

So first we want to define the participants of our network who we know are policyholders and insurers. To do this we open the file org.acme.riskanalysis.cto which can be found in the models folder.

Policyholders also want to be able to add assets such as a house or car so let’s define an asset called PrivateAsset and also an enum called AssetType. PrivateAsset has a dependency on a policyholder who will be the person creating the asset. There are only 3 types of asset that can be added to the system which are defined by the enum AssetType.

We will add two more asset types one for making an insurance offer and another making a claim.

Next, we need to define our transactions for the network.

This is what your full model file should look like:

Next up we need to implement our transactions and this will be done as chaincode.

First, you need to create a new directory called lib in the home directory for our Hyperledger composer project where you will see the files such as package.json or if you are in the models directory in your terminal run the following command:

mkdir ../lib/ && cd ../lib/

Inside the lib directory now we are going to create a new file called logic.js which will be our chaincode file.

Let’s implement CreateNewAsset first. You will notice that we use decorators to supply metadata to our functions.

Now that we have this done let’s test it out to see if it works.

First, we’ve got to start Hyperledger Fabric. Find the directory you have downloaded this too and run the scripts as follows:

cd ~/fabric-dev-servers
./startFabric.sh
./createPeerAdminCard.sh

Note you only need to run ./createPeerAdminCard.sh if you have previously not run Hyperledger Fabric or previously you have run ./teardownFabric.sh.

If you run into issues with these commands try running as the wrong node version can cause issues which are not apparent from the error messages thrown.

nvm use 8

Now go back to the risk-analysis-tutorial directory and we will start to deploy version 0.0.1 of our network.

Running this command will generate a business network archive file named risk-analysis-tutorial@0.0.1.bna

composer archive create -t dir -n .

Next, we will install our Composer business network on the Hyperledger Fabric peer we have set up

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

Now we can start our business network which may take a few minutes

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

Then just import the network administrator identity as a usable business network card

composer card import --file networkadmin.card

To test if this has all been successful just ping the network

composer network ping --card admin@risk-analysis-tutorial

To check out the current version of your deployed network run

composer network ping -c admin@risk-analysis-tutorial | grep Business

Hyperledger Composer can generate a bespoke REST API based on a business network. For our network, we just want to keep it simple for now so just run the following command to create our REST API

composer-rest-server -c admin@risk-analysis-tutorial -n never -u true -w true

Now you can test out the REST API by navigating to

http://localhost:3000/explorer

You should be presented with the following screen

Hyperledger Composer REST Server

We need to create some policyholders first if we want to test out our CreateNewAsset function so select Policyholder and then select post and enter the following and then click try it out

{
"$class": "org.acme.riskanalysis.Policyholder",
"id": "joe",
"name": "Joe",
"balance": "50000",
"noClaimsYears": "2"
}
composer network ping -c admin@risk-analysis-tutorial | grep Business

Now create another policyholder, in the same manner, using the following information

{
"$class": "org.acme.riskanalysis.Policyholder",
"id": "mary",
"name": "Mary",
"balance": "50000",
"noClaimsYears": "5"
}

The response to both should have been 200 if not check that error message and look over your code for any potential errors.

Now we are ready to test out CreateNewAsset function. To do this go CreateNewAsset > Post. Let’s create a car for Joe with a value of 2000 and seeking 12 months insurance.

{
"$class": "org.acme.riskanalysis.CreateNewAsset",
"policyholder": "resource:org.acme.riskanalysis.Policyholder#joe",
"assetType": "CAR",
"value": 2000,
"durationInMonths": 12
}

The response code should be 200 if everything worked correctly. If not check that everything is working and note that it is case sensitive.

Now if you go to PrivateAssets > Get then click Try it out you should see the asset that we just created.

Now lets just shut down the REST server by clicking ctrl-c in your terminal and we will finish writing the rest of our transactions.

The logic for the RiskAnalysis transaction is simply just checking the asset type and the number of years the policyholder has no claims and the value of the asset.

To test this out go to your package.json file and change the version number from 0.0.1 to 0.0.2 and then create a new business network archive file

composer archive create --sourceType dir --sourceName . -a risk-analysis-tutorial@0.0.2.bna

install the new file

composer network install --card PeerAdmin@hlfv1 --archiveFile risk-analysis-tutorial@0.0.2.bna

upgrade the network

composer network upgrade -c PeerAdmin@hlfv1 -n risk-analysis-tutorial -V 0.0.2

Check that the network has been upgraded to version: 0.0.2

composer network ping -c admin@risk-analysis-tutorial | grep Business

Then run your Composer REST server again

composer-rest-server -c admin@risk-analysis-tutorial -n never -u true -w true

All your policyholders and assets that you previously created will still be here so no need to set these up again. Let’s get the id of the car that we added previously for Joe. Then go to RiskAnalysis > Post and enter in the following replacing ASSETID with your asset ID.

{
"$class": "org.acme.riskanalysis.RiskAnalysis",
"privateAsset": "resource:org.acme.riskanalysis.PrivateAsset#ASSETID"
}

Now go back to PrivateAsset > Get and you should see that the risk analysis score has changed from 0 to 3.

Next, let’s implement MakeInsuranceOffer

Now let’s add the ability to AcceptInsuranceOffer

Let’s implement CreateClaim

Notice how this function uses a query. This query has not yet been defined and what it is simply doing is finding the insurance offer for this asset.

So to implement this query create a new file called queries.qry in the risk-analysis-tutorial directory and add the following lines to the file.

We just need one more function and that is the ability to process a claim.

Now lets test this all out but first let’s update the package.json file to version 0.0.3. Then run the following set of commands.

composer archive create --sourceType dir --sourceName . -a risk-analysis-tutorial@0.0.3.bna
composer network install --card PeerAdmin@hlfv1 --archiveFile risk-analysis-tutorial@0.0.3.bna
composer network upgrade -c PeerAdmin@hlfv1 -n risk-analysis-tutorial -V 0.0.3

Now let’s run the composer REST API server again

composer-rest-server -c admin@risk-analysis-tutorial -n never -u true -w true

So let’s begin by creating an insurance company

{
"$class": "org.acme.riskanalysis.InsuranceCompany",
"id": "awesomeInsurance",
"name": "The Awesome Insurance Company",
"balance": "0.0",
"insuranceContracts": "0"
}

Now you can make your first insurance offer. Make sure to replace the asset ID.

{
"$class": "org.acme.riskanalysis.MakeInsuranceOffer",
"policyholder": "resource:org.acme.riskanalysis.Policyholder#joe",
"insuranceCompany": "resource:org.acme.riskanalysis.InsuranceCompany#awesomeInsurance",
"privateAsset": "resource:org.acme.riskanalysis.PrivateAsset#joe549963",
"monthlyCost": 100
}

Now go to InsuranceOffer > GET and you should see your newly created insurance offer with the status pending. Note the ID for the next step.

Now using the ID of the insurance offer lets test out accepting this insurance offer. Go to AcceptInsuranceOffer > POST and use the following replacing the offer ID with yours.

{
"$class": "org.acme.riskanalysis.AcceptInsuranceOffer",
"offer": "resource:org.acme.riskanalysis.InsuranceOffer#OFFERID"
}

You should now notice if you go to InsuranceOffer > GET and click on Try it out! you should notice the status of the insurance offer is now accepted if everything worked like follows.

Now that Joe’s asset is insured lets test out making a claim. CreateClaim > POST and enter the following replacing the private asset ID with yours.

{
"$class": "org.acme.riskanalysis.CreateClaim",
"privateAsset": "resource:org.acme.riskanalysis.PrivateAsset#PRIVATEASSETID",
"policyholder": "resource:org.acme.riskanalysis.Policyholder#joe",
"description": "Cracked Window",
"claimValue": 800
}

If all has worked out you should now be able to GET that claim.

Finally, we can test out the ability to process this claim. Go to ProcessClaim > POST and remember to change the claim ID.

{
"$class": "org.acme.riskanalysis.ProcessClaim",
"claim": "resource:org.acme.riskanalysis.Claim#CLAIMID",
"status": "approved"
}

Now when you go to Claim > GET you should see your claim is approved similarly to the following.

In the next part, we will look at integrating our Hyperledger Fabric network with a React.js front-end web application.

Part 2 can be found here.

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com

Jack Hickey

Written by

A software engineer with a passion for all things decentralized. Check out my LinkedIn: linkedin.com/in/jack-hickey/

Coinmonks

Coinmonks

Coinmonks is a technology-focused publication embracing decentralize technologies. We are Non-profit and education is our core value. Learn, Build and thrive. Our other project— https://coinmonks.com, https://cryptofi.co, https://coincodecap.com