Hyperledger Cookbook
上QQ阅读APP看书,第一时间看更新

Setting up a client project

Next, it is time for us to write client-side code to trigger our chaincode. As we discussed, when the client sends a request to the Fabric network to query or invoke the chaincode, these requests need to be authorized. In Fabric release 1.4, we can create a wallet by enrolling the user and importing the identity into the wallet. The client application can interact with a smart contract in blockchain by utilizing the fabric-ca-client and fabric-network APIs with the authorized wallet.

Let's create a Node.js app:

  1. Navigate to the ~/itasset/client/webapp folder, issue the npm init command, and fill up the related project information. This will create a basic node application:
ubuntu@ip-172-31-9-54:~/itasset/client/webapp$ npm init
package name: (webapp) assetmgr
version: (1.0.0)
description: hyperledger cookbook fabric
entry point: (index.js)
  1. Install the default npm libraries. This includes the express.js, ejs, fabric-ca-client, and fabric-network libraries, shown as follows:
npm install
npm install express -save
~/itasset/client/webapp$ npm i fabric-ca-client@1.4.0
~/itasset/client/webapp$ npm i fabric-network@1.4.0
~/itasset/client/webapp$ npm install ejs
  1. We need to copy three files (connection.json, enrollAdmin.js, and registerUser.js) from the fabric-samples/fabcar project to our project:
cp ~/fabric-samples/basic-network/connection.json .
cp /home/ubuntu/fabric-samples/fabcar/javascript/enrollAdmin.js .
cp /home/ubuntu/fabric-samples/fabcar/javascript/registerUser.js .
Create empty wallet folder by issue below command:
mkdir wallet

At this step, our files and folders should look as follows:

  1. Since we copied enrollAdmin.js and registerUser.js from the fabric-samples/fabcar folder, we also need to update the file path defined in the enrollAdmin.js and registerUser.js files. Update path.resolve() in the enrollAdmin.js and registerUser.js files, as follows:
const ccpPath = path.resolve(__dirname, 'connection.json');

The connection.js file we copied from basic-network is in the same folder as enrollAdmin.js and registerUser.js.

  1. Let's create a wallet to enroll the admin and the user onto our Fabric network. To do this, issue the following command:
~/itasset/client/webapp$ node enrollAdmin.js
~/itasset/client/webapp$ node registerUser.js

This will create a wallet for the admin and the users. The following screenshot shows the wallet structure for admin and user1: