Rework: An Implementation of API Server for Hyperledger Fabric Network (Fabric v2.2)

Introduction

My previous work on implementation of API Server was done on Fabric v1.4 one year ago. Certain changes have been seen since v2.0. Here is a rework on the implementation. I will skip most of the explanation as the overall implementation logic is the same as the previous one. Only the difference is highlighted in this article.

You can always refer to this previous article for more detailed explanation.

Implementation Setup

Here is the setup of our implementation. The overall design remains the same. The only difference in this setup is the use of Test Network instead of First Network. Many sample applications and scripts are rewritten to use Test Network, and so is fabcar. For those who wish to know more about the difference between Test Network and First Network, you can refer to these articles (link, link).

Image for post
Image for post

Similar to our previous setup, we have two hosts setup for this implementation. They are AWS EC2 instances.

Image for post
Image for post

API Server Code in JavaScript

Here I simply reuse the previous apiserver.js with modification to reflect the difference in using Test Network and some change in SDK v2.2.

One major difference is the way to create wallet objects. In v1.4 it was handled by FileSystemWallet class. In v2.2 it is the class Wallets handling wallet in client applications. You can notice the change in the code (line 20, 65, 109 and 152).

Besides, the client applications in fabcar/javascript/ are modified. In v1.4 it was user1 as a directory, and inside the directory we see the private key, public key and certificates. Now In v2.2 it is appUser.id, which is an object file containing the private key and certificate. In terms of functionality they are the same. Our API server code is updated to reflect this change.

Here is the code.

Demonstration Step

On Fabric Host

(screenshots are in black colour background)

Step 1: Bring up Test Network and deploy fabcar chaincode

We use the script to perform this step.

cd fabric-samples/fabcar/
./startFabric.sh

Step 2: Generate user appUser for API server

By default, the fabcar uses SDK to generate a user appUser for client application (API server). we first install the packages. Then we execute the two scripts enrollAdmin.js and registerUser.js.

cd javascript/
npm install
node enrollAdmin.js
node registerUser.js
Image for post
Image for post

Upon success we will see the wallet/ directory and appUser.id is generated.

Image for post
Image for post

For a quick test, we can use appUser to perform a chaincode function for querying all cars. We run node query.js, which performs the queryAllCars function. We will see the ten car records preloaded with initLedger function done in the script.

Image for post
Image for post

On API Server Host

(screenshots are in blue colour background)

Now we move to API Server. We use a Ubuntu 18.04 LTS server.

Step 3: Install NodeJS environment

sudo apt-get update
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_10.x | sudo bash -
sudo apt install -y nodejs
sudo apt-get install build-essential
node -v
npm -v
Image for post
Image for post

Step 4: Copy files from Fabric Host

Create the directory in API Server Host.

mkdir apiserver
cd apiserver
Image for post
Image for post

The files we need on API Server are

  • wallet (identity): fabcar/javascript/wallet/appUser.id
  • node package file: fabcar/javascript/package.json
  • connection profile: test-network/organizations/peerOrganizations/org1.example.com/connection-org1.json

Again we are using localhost as a bridge for my two AWS EC2 instances. For well structure we move the appUser.id to wallet/appUser.id

Here is the result.

Image for post
Image for post

Step 5: Copy apiserver.js to this directory.

Image for post
Image for post

Step 6: Modify connection profile

The connection profile when generated is using localhost. We change it from localhost to IP of Fabric Host (change to yours).

sed -i 's/localhost/34.203.38.3/g' connection-org1.json

Step 7: Update /etc/hosts to reach Fabric Host

Finally we let API Server Host reach Fabric Host by resource names we are using. Here is the result.

Image for post
Image for post

Now configuration is ready.

Step 8: Install packages on API Server Host

npm install
npm install express body-parser --save

Step 9: Run API Server

node apiserver.js

As we have kept the console.log in our code, we will see console.log result in apiserver.js when accessing the APIs.

Testing: on my localhost

(screenshots are in red colour background)

Now the API Server is exposing the API we can access the fabcar application. We follow the same test suite in our previous articles.

Query All Cars

curl http://3.84.237.64:8080/api/queryallcars
Image for post
Image for post

Add a new car and query the new car

curl -d '{"carid":"CAR12","make":"Honda","model":"Accord","colour":"black","owner":"Tom"}' -H "Content-Type: application/json" -X POST http://3.84.237.64:8080/api/addcarcurl http://3.84.237.64:8080/api/query/CAR12
Image for post
Image for post

Change owner for a car and query that car again

curl http://3.84.237.64:8080/api/query/CAR4curl -d '{"owner":"KC"}' -H "Content-Type: application/json" -X PUT http://3.84.237.64:8080/api/changeowner/CAR4curl http://3.84.237.64:8080/api/query/CAR4
Image for post
Image for post

The API Server is working well.

Hope you enjoy reading this.

Written by

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store