Before I jump in any further into this article, I will assume that you’re familiar with setting up the network with multiple organisations and also assume that there is a network already running on your machine. (If not please follow our previous articles in completing them.)
It’s also great if you have prior experience in the following,
JavaScript OOP concepts
Javascript Promise
Server Routing and HTTP Methods.
ExpressJS
Step 1: Setting up the project
As the first step in any project, we will be creating the folder and the file structures.
The packages fabric-ca-client & fabric-client are the ones which help us to interact with the Fabric network and express is to create the web server for RESTFul API and finally body-parser to parse the data passed in the request body.
Step 2: Create Connection Profile and Crypto Config
After setting up we need to create a common connection profile that has information about the current organization’s peers, orderer and CA so that the client can communicate to corresponding services.
I’ve created a file under Config/ConnectionProfile.yml
The important thing to look at here is the credentials store that the application will be using to store the keys and certificates.
Step 3. Create Script for Generating Admin Crypto Materials.
In order to submit a transaction from the client, you need to set user content in the SDK. However, before that we need to enroll and get the certificates for the admin of the organization. Here’s a simple script to do that, ./enrollAdmin.js
There’s also a file called ./Config/FabricClient that extends the functions of FabricClient SDK to provide enhanced features.
Now you can run the above script to generate an admin certificate and that will be stored in the crypto-store mentioned in the ConnectionProfile.yml
node enrollAdmin.js
The corresponding result should look like this,
In the above script, I’ve extended the base client and created a function to submit a transaction and query the data to clean it after retrieval. These will be used for future purposes.
Step 4: Creating Basic Endpoints
Now that all our basic things are ready, let’s start with an endpoint to submit a transaction called sell.
Step 5. Build a Model Class
We’ll be creating a model class that will work like a library function to perform a set of application related actions that will be used by each route.
Here in the above code, you will notice that we’re again using the same fabricClient as previously used. Also, we have a function that submits the sell transaction proposal to the system.
Here the model takes the userName in the constructor and sets it as the context for the current instance of the client. In our case, it’s the admin who will be signing this transaction.
Step 6: Bridging the library and the server endpoints
Once we’ve created the library as well the server endpoints, let’s call the library from the server function as below,
If you notice, the above code used admin as the username to interact with the network. If you have multiple users you can call the network with the corresponding username provided the certificates are present in the store. In my next article, I’ll explain how to handle user management and session management for a multi-user scenario.