DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • How To Use the Node Docker Official Image
  • Keep Your Application Secrets Secret
  • Simplifying A/B/n Testing of Backend Services
  • How To Validate Names Using Java

Trending

  • Integration Isn’t a Task — It’s an Architectural Discipline
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Metrics at a Glance for Production Clusters
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  1. DZone
  2. Data Engineering
  3. Databases
  4. Deploy a Hyperledger Fabric v2 Web App Using the Node.js SDK

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

Develop a web app that works with Hyperledger Fabric v2

By 
Thomas Lee user avatar
Thomas Lee
·
May. 13, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
9.7K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • A Chainstack account.
  • Node.js 12.13.1 or higher.
  • NPM 6 or higher.
  • nodemon.

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
xxxxxxxxxx
1
 
1
git clone git@github.com:chainstack/freedom-dividend-chaincode.git 


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

macOS:

Shell
xxxxxxxxxx
1
 
1
sudo bash downloadPeerBinary.sh


Linux:

Shell
xxxxxxxxxx
1
 
1
sudo bash downloadPeerBinary.sh linux


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
xxxxxxxxxx
1
 
1
freedom-dividend-chaincode
2
|__ contract
3
|__ webapp
4
| |__ certs
5
|    |__ RG-837-380-MSP
6
|    |__ connection-profile.json
7
|    |__ nd-433-899-254-cert.pem


Your .env file should look like the following:

Properties files
xxxxxxxxxx
1
21
 
1
AS_LOCALHOST=false
2
FABRIC_CFG_PATH=/etc/hyperledger/config
3
ORDERER_CA=/nd-433-889-254-cert.pem
4
ORDERER_ADDRESS=nd-433-889-254.rg-837-380.int.chainstack.com:7050
5
6
# Chaincode
7
CHANNEL_ID=defaultchannel
8
CHAINCODE_NAME=freedomDividendContract
9
CHAINCODE_VERSION=1
10
CHAINCODE_SEQUENCE=1
11
12
# Admin
13
ADMIN_CERT=/RG-837-380-MSP/users/Admin@rg-837-380.int.chainstack.com/msp/admincerts/Admin@rg-837-380.int.chainstack.com-cert.pem
14
ADMIN_PRIVATE_KEY= /RG-837-380-MSP/users/Admin@rg-837-380.int.chainstack.com/msp/keystore/priv_sk
15
16
# Peer
17
CORE_PEER_TLS_ENABLED=true
18
CORE_PEER_ADDRESS=nd-577-609-393.rg-837-380.int.chainstack.com:7051
19
CORE_PEER_MSPCONFIGPATH=/RG-837-380-MSP/users/Admin@rg-837-380.int.chainstack.com/msp/
20
CORE_PEER_LOCALMSPID=RG-837-380-MSP
21
CORE_PEER_TLS_ROOTCERT_FILE=/RG-837-380-MSP/peers/nd-577-609-393.rg-837-380.int.chainstack.com/tls/ca.crt

3. Set Up the Backend Application

In the cloned repository, install the required dependencies:

Shell
xxxxxxxxxx
1
 
1
cd webapp/server
2
npm i


Start the back end server:

Shell
xxxxxxxxxx
1
 
1
npm run start


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
xxxxxxxxxx
1
 
1
Setting up fabric wallet and gateway...
2
Server running at http://localhost:4000/
3
==========AS_LOCALHOST: false==========
4
Set up complete!


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
xxxxxxxxxx
1
 
1
cd webapp/client
2
npm i


Compile and build the front end client:

Shell
xxxxxxxxxx
1
 
1
npm run build


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
xxxxxxxxxx
1
 
1
node webapp/server/cli/peer install 


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

Shell
xxxxxxxxxx
1
15
 
1
$ node webapp/server/cli/peer install
2
executing cli - install command
3
PACKAGE_ID: freedomDividend1:b5dc7b4864baf9909295ffc177bd6da448c6000907f9e879f5d2270f9a7593e3
4
Chaincode definition for chaincode 'freedomDividend', version '1', sequence '1' on channel 'defaultchannel' approval status by org:
5
RG-837-380-MSP: true
6
Committed chaincode definitions on channel 'defaultchannel':
7
Name: freedomDividend, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
8
Name: freedomDividend, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
9
Name: freedomDividendContract, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
10
Name: fabcar, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
11

          
12
2020-04-20 02:01:30.874 +08 [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nRfreedomDividend1:b5dc7b4864baf9909295ffc177bd6da448c6000907f9e879f5d2270f9a7593e3\022\021freedomDividend1" >
13
2020-04-20 02:01:30.874 +08 [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: freedomDividend1:b5dc7b4864baf9909295ffc177bd6da448c6000907f9e879f5d2270f9a7593e3
14
2020-04-20 02:01:33.333 +08 [chaincodeCmd] ClientWait -> INFO 001 txid [6633ee972019298183eb18489bfb56d5b7b13abf1211cea4ff88cdac7540d3d2] committed with status (VALID) at
15
2020-04-20 02:01:35.715 +08 [chaincodeCmd] ClientWait -> INFO 001 txid [ab6e47c549ca543e344f6d12b00c3bf16c3fb83e3093762d3a905af3c30973bc] committed with status (VALID) at nd-577-609-393.rg-837-380.int.chainstack.com:7051


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
xxxxxxxxxx
1
92
 
1
{
2
  "$schema": "https://fabric-shim.github.io/master/contract-schema.json",
3
  "contracts": {
4
    "FreedomDividendContract": {
5
      "name": "FreedomDividendContract",
6
      "contractInstance": {
7
        "name": "FreedomDividendContract",
8
        "default": true
9
      },
10
      "transactions": [
11
        {
12
          "name": "optIn",
13
          "tags": [
14
            "submitTx"
15
          ],
16
          "parameters": [
17
            {
18
              "name": "arg0",
19
              "description": "Argument 0",
20
              "schema": {
21
                "type": "string"
22
              }
23
            },
24
            {
25
              "name": "arg1",
26
              "description": "Argument 1",
27
              "schema": {
28
                "type": "string"
29
              }
30
            }
31
          ]
32
        },
33
        {
34
          "name": "optOut",
35
          "tags": [
36
            "submitTx"
37
          ],
38
          "parameters": [
39
            {
40
              "name": "arg0",
41
              "description": "Argument 0",
42
              "schema": {
43
                "type": "string"
44
              }
45
            }
46
          ]
47
        },
48
        {
49
          "name": "querySSN",
50
          "tags": [
51
            "submitTx"
52
          ],
53
          "parameters": [
54
            {
55
              "name": "arg0",
56
              "description": "Argument 0",
57
              "schema": {
58
                "type": "string"
59
              }
60
            }
61
          ]
62
        }
63
      ],
64
      "info": {
65
        "title": "",
66
        "version": ""
67
      }
68
    },
69
    "org.hyperledger.fabric": {
70
      "name": "org.hyperledger.fabric",
71
      "contractInstance": {
72
        "name": "org.hyperledger.fabric"
73
      },
74
      "transactions": [
75
        {
76
          "name": "GetMetadata"
77
        }
78
      ],
79
      "info": {
80
        "title": "",
81
        "version": ""
82
      }
83
    }
84
  },
85
  "info": {
86
    "version": "1.0.2",
87
    "title": "freedomDividendContract"
88
  },
89
  "components": {
90
    "schemas": {}
91
  }
92
}


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
xxxxxxxxxx
1
 
1
freedom-dividend-chaincode
2
|__ contract
3
| |__ contract-metadata
4
|    |__ metadata.json


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
xxxxxxxxxx
1
 
1
node webapp/server/cli/peer upgrade 


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
xxxxxxxxxx
1
14
 
1
$ node webapp/server/cli/peer upgrade
2
executing cli - upgrade command
3
PACKAGE_ID: freedomDividend1.1:5176145e7db42a2459ccb23145212c481faa2e6a60011e6476f7bedf1d58b652
4
Chaincode definition for chaincode 'freedomDividendA', version '1.1', sequence '2' on channel 'defaultchannel' approval status by org:
5
RG-837-380-MSP: true
6
Committed chaincode definitions on channel 'defaultchannel':Name: fabcar, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
7
Name: freedomDividend, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
8
Name: freedomDividendA, Version: 1.1, Sequence: 2, Endorsement Plugin: escc, Validation Plugin: vscc
9
Name: freedomDividendContract, Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc
10

          
11
2020-04-20 02:42:35.647 +08 [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nTfreedomDividend1.1:5176145e7db42a2459ccb23145212c481faa2e6a60011e6476f7bedf1d58b652\022\023freedomDividend1.1" >
12
2020-04-20 02:42:35.648 +08 [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: freedomDividend1.1:5176145e7db42a2459ccb23145212c481faa2e6a60011e6476f7bedf1d58b652
13
2020-04-20 02:42:38.068 +08 [chaincodeCmd] ClientWait -> INFO 001 txid [cdef7a33a27a51b26592112a4bb878f764d38641010830a9fc6f4ed3b324f87f] committed with status (VALID) at
14
2020-04-20 02:42:40.563 +08 [chaincodeCmd] ClientWait -> INFO 001 txid [eca032257e82cc2a53abc6a9475012de33ee098064afbf6bd8b2757aa4cb87d7] committed with status (VALID) at nd-577-609-393.rg-837-380.int.chainstack.com:7051


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.

Software development kit Network Node.js application Plain text Repository (version control) shell Command (computing) app

Opinions expressed by DZone contributors are their own.

Related

  • How To Use the Node Docker Official Image
  • Keep Your Application Secrets Secret
  • Simplifying A/B/n Testing of Backend Services
  • How To Validate Names Using Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!