Creating a Hyperledger Fabric network from scratch — Part IV SDK and Rest API

0
40

Creating a Hyperledger Fabric network from scratch — Part IV SDK and Rest API

Photo by Alvaro Reyes on Unsplash

This is the last post in the series on how to create a supply chain with Hyperledger fabric from scratch. In this post we will see how to interact with the SDK provided by Hyperledger and register our users on the network, then we will use a Nodejs application, to implement a rest API in express.

Creating users with the SDK

The sdk provided by Hyperldeger (software development kit) can be used for several functions:

  • Network functions: Submit transactions, make querys in the ledger. These functions are exported from the fabric-network package.
  • Client functions: Interact with channels, chaincodes, make querys. monitor events … these functions are exported in the fabric-client package.
  • CA functions: It allows us to perform functions of the certificate entity, such as registering users, generating their certificates, revoking access to a user. This package will be fabric-ca-client.

The complete documentation of this sdk is on the official hyperledger site.

Discover and review best Blockchain softwares

In order to import and make use of these packages, we will create a Nodejs project, as always do with the command:

npm init -y

And we will add the packages that we have commented previously with

npm install fabric-network fabric-client fabric-ca-client -S

Creating users in the netowork

In order for end users to interact with the network, it is necessary to register them in the network, being a permissible network they can only do so through the organizations that compose it, and an administrator user will have to give them access. In this way, we will first register the administrator user of each of the organizations.

To do this we will create a enrollAdmin.js script, which has the following steps:

  • Call js with the name of the organization, in the form node enrollAdmin.js producer
  • Retrieve the connection of this organization.
  • Create a CA client through the package we have installed.
  • Create a wallet to store the administrator’s certificate.
  • Create the identity and save it in the wallet specifying a username and password.

The complete javascript code will be as follows:

After registering the admin, we can already use these identities to generate the users, so we will make another javascript file registerUser.js that will contain the following steps:

  • Call js with the name of the organization, in the form node registerUser.js producer
  • Retrieve the connection of this organization.
  • Connect to the CA through a gateway, in which we use the identity of the admin.
  • Create and save the user’s identity in the wallet.

This code will be stored in a registerUser.js file of the form:

Once we have our identities generated, we can interact with the network, and to make it as simple as possible we will create a Rest API so that it can be consumed by customers.

Generating the API

To generate this api, we will use the express library, which we will install in the same way as the previous packages:

npm install express -S

In order to expose the api, we will create two files, fabricNetwork.js, in charge of creating the connection to the network, and server.js, from where the API methods are exposed.

In the case of fabricNetwork, we will generate a method that will receive as a parameter the connection and wallet that we have previously created, and will connect to the network, and will return the chaincode to which we want to send the transaction. We will export this method so that it can be used from the API paths.

Inside the server.js, we will create the routes that will be accessible to the API, and call the chaincode methods that we have deployed. There will be two types of methods, GET which will not generate a transaction on the network, and will only check the status of the identifier we pass and POST, which will send a transaction to the network and will have to be validated before being stored in the ledger.

The get methods will return the data consulted for the last identifier, and the post will save the transaction in the ledger and return the transactionID generated in it.

In this way, our API would be

If we run the server with node server.jsor create an execution script within the package.json to start the server, our API will be listening and we can interact with the network, so we can save data and consult you with the get methods, to see its state or historical complete with the getHistory.

This repository, with all the steps that we have been creating, is complete in my github and has execution scripts to automate all the steps, so that, just by downloading it and installing the dependencies, the network can be run and expose the api with the one to use in a PoC.

If you like this content, follow me on Twitter to be updated.

Thanks for reading!


Creating a Hyperledger Fabric network from scratch — Part IV SDK and Rest API was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.