官术网_书友最值得收藏!

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:

主站蜘蛛池模板: 青浦区| 新竹县| 读书| 金秀| 濮阳县| 咸丰县| 伊金霍洛旗| 黑河市| 施甸县| 湛江市| 大兴区| 临沂市| 仪陇县| 宝清县| 洱源县| 四子王旗| 双柏县| 龙泉市| 乡城县| 屯门区| 枞阳县| 武宣县| 利辛县| 商河县| 曲麻莱县| 广平县| 唐海县| 大名县| 秦皇岛市| 大厂| 抚宁县| 房产| 阿克苏市| 双柏县| 桂林市| 溆浦县| 利辛县| 巴楚县| 台东市| 赫章县| 渑池县|