Hyperledger Fabric 2.2 SDK for Node.js (Node-SDK) Tutorial

Ruisiang
3 min readSep 4, 2020

--

Edit: module is now available on npm as fabric-network-simple

Hyperledger Fabric is a common approach when implementing decentralized solutions for commercial scenarios. The following article will mainly focus on calling chaincode apis from remote.

This article assumes that you have basic knowledge about how Hyperledger Fabric works and have a network with chaincode installed up and running.

Node-SDK interacts with the blockchain network with an object called “gateway”. The gateway object establishes connections to peers defined in the configuration, and verifies its identity with a “wallet” object.

Since chaincodes only start interacting with the ledger after its definition is committed to a channel, we’ll need to get the channel and chaincode definition as well. Objects “network” and “contract” are for these two respectively.

Configuration files consist of two main parts, a connection profile for defining blockchain network components, and a wallet file for storing identity information.

connectionprofile.json contains the public information of channels, orgs, orderers, peers, namely, their definition and connection methods. Details about the structure is as follows, with an example here

//connectionprofile.json pseudo-code
{
"name": blockchain-network-name,
"version": self-defined-version-of-connection-profile,
//list of channels that the SDK connects to
"channels": {
//channel block: defines channel members, repeatable
channel-name: {
"orderers": array-of-orderer-names,
"peers": array-of-peer-names
}
//end of channel block
},
//list of orgs that are mentioned in the channel list
"organizations": {
//org block: defines org members, repeatable
organization-name: {
"mspid": mspid-of-org,
"peers": array-of-peer-names,
"certificateAuthorities": array-of-ca-names
}
//end of org block
},
//list of orderers mentioned in the org list
"orderers": {
//orderer block: defines orderer connections, repeatable
orderer-name: {
"url": grpcs-url-of-orderer,
"grpcOptions": {
"ssl-target-name-override": orderer-name
},
"tlsCACerts": {
"path": tlsca-cert-file-location
}
}
//end of orderer block
},
//list of peers mentioned in the org list
"peers": {
//peer block: defines peer connections, repeatable
peer-name: {
"url": grpc-url-of-peer,
"grpcOptions": {
"ssl-target-name-override": peer-name
},
"tlsCACerts": {
"path": tlsca-cert-file-location
}
}
//end of peer block
}
}

wallet.id stores private information of the identity that the SDK uses, including keys and credentials. Details about the structure is as follows, with an example here (in the sample networks signcert can be found in the signcert folder under the user folder, secret key is in the keystore folder under the user folder)

//wallet.id pseudo-code
{"credentials":{"certificate":signcert-of-user-with-linebreaks-replaced-by-\n ,"privateKey":secret-key-of-user-with-linebreaks-replaced-by-\r\n},"mspId":mspid-of-org,"type":"X.509","version":1}

Now we can start working on Node-SDK. There are basically two main parts to do, establishing a connection to the chaincode api and interacting with it. I’ve put together a small module here to simplify the process of interacting with the chaincode api.

Usage (say we have a query function "set", and invoke function "get" that gets/sets a number to a variable on the chaincode):

import fabricService from "./fabric-service";//sets variable to 5
const invokeResult =
await fabricService.invokeChaincode(
'set', [5], {}
);
//gets variable from ledger
const queryResult =
await fabricService.queryChaincode(
'get', []
);
console.log('queryResult'); //prints 5

Full typescript implementation of the module (detailed description in comments)

💌 Get In Touch

For questions, or just to say hi, feel free to reach out to me on Twitter @ruisiang_tw or drop me an email at hi@rs.me.

--

--

Ruisiang

Blockchain and Backend Developer, Privacy Advocate, Crypto Enthusiast. Twitter: ruisiang_tw