{{announcement.body}}
{{announcement.title}}

Deploy a Hyperledger Fabric v2 Web App Using the Node.js SDK

DZone 's Guide to

Deploy a Hyperledger Fabric v2 Web App Using the Node.js SDK

Develop a web app that works with Hyperledger Fabric v2

· Open Source Zone ·
Free Resource

Introduction

One of the main highlights of Hyperledger Fabric v2 is the concept of decentralized governance for smart contracts. It introduces peer chaincode lifecycle, which focuses on improving chaincode management between multiple organizations. Using the new lifecycle, installation, and upgrading of chaincodes requires approval from multiple organizations before the chaincode can be interacted with.

The latest fabric-sdk-node update also removed the fabric-client library, which was mainly responsible for handling administrative tasks such as chaincode and channel management on the network. These tasks now instead have to be carried out through the peer CLI.

This article uses the Freedom Dividend chaincode as an example to show you how to build a complete Hyperledger Fabric v2 application using the new peer chaincode lifecycle together with the fabric-sdk-node and a network spun up with the Chainstack platform.

The main difference from developing a Hyperledger Fabric v1.4 application is the integration of the peer lifecycle commands into the application flow as illustrated in the diagram below.

Application architecture

Prerequisites

Overview

  1. Set up a Hyperledger Fabric network with Chainstack.
  2. Set up the environment.
  3. Set up the back end application.
  4. Set up the front end client.
  5. Install the chaincode.
  6. Interact with the installed chaincode through the front end.
  7. Upgrade the chaincode.
  8. Interact with the upgraded chaincode through the front end.

1. Set Up a Hyperledger Fabric Network With Chainstack

  1. Create a consortium project with Chainstack. See Create a project.
  2. Deploy a Hyperledger Fabric network. See Deploy a consortium network.

2. Set Up the Environment

Clone the freedomDividend repository on your local machine:

Shell


Install the Hyperledger Fabric v2 binaries using the downloadPeerBinary.sh script in the repository:

macOS:

Shell


Linux:

Shell


The script will download a set of Hyperledger Fabric v2 binaries that you can execute to interact with the network.

Set up the repository using the TLS and MSP certificates together with the peer and orderer details from your Hyperledger Fabric v2 network deployed with Chainstack.

Freedom Dividend Chaincode

Click on the network details to retrieve the orderer details.

Getting orderer details

From the network details pop-up:

  • Replace the ORDERER_ADDRESS in the server/.env.template with the orderer name.
  • Export the orderer TLS certificate to the repository’s webapp/certs directory.
  • Export the connection profile to the repository’s webapp/certs directory.

Next, access the node details for the peer you want to use to interact with the network.

Node details

From the node details page:

  • Replace CORE_PEER_ADDRESS in the server/.env.template with the peer name.
  • Replace CORE_LOCAL_MSPID in the server/.env.template with the MSP ID.
  • Export and unzip the organization MSP certificates to the repository’s webapp/certs directory.

Your directory structure should look like the following:

Plain Text


Your .env file should look like the following:

Properties files

3. Set Up the Backend Application

In the cloned repository, install the required dependencies:

Shell


Start the back end server:

Shell


You should see the following output in the terminal, which indicates that the wallet and gateway have been successfully initiated using the Hyperledger Fabric v2 SDK library together with the values you set in the .env file:

Plain Text


You should also note that a wallet with the label user01 has been created in the directory webapp/server/fabric/wallets/; this is the wallet identity that Hyperledger Fabric v2 SDK uses to authenticate the user with the network.

4. Set Up the Frontend Client

In the cloned repository, install the required dependencies:

Shell


Compile and build the front end client:

Shell


Head on over to http://localhost:4000 in your browser, and you should see the following:

Initial output

Initial output

5. Install the Chaincode

The cloned repository includes the bash script that automates the process of installing or upgrading the chaincode with the peer lifecycle process.

Install the Freedom Dividend chaincode with the following command:

Shell


You should see the following on the terminal to indicate that the chaincode has been successfully installed on the network:

Shell


The command will execute the following flow:

Application workflow

Application workflow

The chaincode.sh bash script uses the data from the .env file to communicate with the network using the peer binary. The script executes the entire flow from packaging the raw chaincode JavaScript to committing it to the network.

Note that we are using one organization in this example. If there are multiple parties involved and the endorsement policy requires more than one endorser, the chaincode package will have to be approved by others before it can be committed to the network.

Go to http://localhost:4000. You should see the freedomDividend chaincode contract with version 1.0 and sequence 1:

Installed chaincode packages

Installed chaincode packages

Retrieving chaincodes on the fabric-node-sdk@1.4 was as simple as calling the queryInstalledChaincodes function. But this function, which is part of the Hyperledger Fabric SDK, does not work with the new peer chaincode lifecycle (at the time of writing). So the chaincode details have to be retrieved using the peer CLI.

6. Interact With the Installed Chaincode Through the Frontend

You can now interact with the installed chaincode. To read more about the chaincode and the arguments, see the chaincode tutorial at Chainstack Docs.

Access the chaincode interface by clicking on the chaincode label — freedomDividend1.0. You will see the following:

Chaincode transacations dashboard

Chaincode transacations dashboard

A dynamic set of forms are automatically generated using the contract.evaluateTransaction('org.hyperledger.fabric:GetMetadata') function based on the chaincode you are accessing. In this example, we are using the Freedom Dividend chaincode which includes three transactions:

  • OptIn
  • OptOut
  • QuerySSN

Note that this will only work if your chaincode uses the fabric-contract-api.

Execute the QuerySSN function with a random value; we will use 123-456-789 as an example. You should see an error as shown below. This error indicates that there is no record on the ledger with SSNID 123-456-789.

No ledger with ID

No ledger with ID

Next, execute the OptIn function with a random value; we will use 123-456-789 with a random statement as an example.

Next, execute the QuerySSN function with 123-456-789 again. You should see the following:

Executing query

Executing query

7. Upgrade the Chaincode

Now create a more descriptive metadata to override the one inferred from the source code to provide more meaningful parameters for the generated forms.

Retrieve the current metadata with the following API call in your browser, http://localhost:4000/api/v1/chaincode/freedomDividendContract.

You should see the following:

JSON


Copy the JSON schema and create a metadata.json file the repository directory contract/contract-metadata. Your contract directory structure should look like this:

Plain Text


Replace:

  • Arg 0 with ssnId
  • Argument 0 with SSN ID
  • Arg 1 with description
  • Argument 1 with description

Next, upgrade the Freedom Dividend chaincode with the following command:

Plain Text


The command will automatically increase CHAINCODE_VERSION in the .env file by 0.1 and CHAINCODE_SEQUENCE by 1.

You should see the following on the terminal to indicate that chaincode has been successfully upgraded on the network:

Shell


The command will execute the following flow:

Command execution workflow

Command execution workflow

By using a different CHAINCODE_SEQUENCE and CHAINCODE_VERSION number, the peer lifecycle command initiates an upgrade process to override the existing Freedom Dividend chaincode with the new changes.

8. Interact With the Upgraded Chaincode Through the Frontend

Access the chaincode interface by clicking on the chaincode label — freedomDividend1.1.

Installed chaincode packages

Installed chaincode packages

You should see the changes made previously are reflected in the automatically generated forms:

freedomDividend final output

freedomDividend final output

Conclusion

The concept of integrating CLI commands as a part of the application can be challenging to understand. Hopefully this Freedom Dividend application helps you understand the process of building a Hyperledger Fabric v2 application.

Topics:
blockchain ,hyperledger fabric ,nodejs ,open source ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}