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

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:

主站蜘蛛池模板: 广河县| 太仓市| 遂平县| 庄河市| 西安市| 贡山| 柏乡县| 阳山县| 鄄城县| 青冈县| 凤庆县| 揭阳市| 乌鲁木齐市| 太和县| 定兴县| 疏勒县| 张家界市| 区。| 独山县| 彭州市| 奉新县| 乌拉特前旗| 丰顺县| 宁陵县| 孝义市| 淳化县| 耒阳市| 绍兴县| 河间市| 道孚县| 明溪县| 蒲城县| 雷州市| 万盛区| 义乌市| 昭苏县| 铁力市| 高碑店市| 得荣县| 阜新| 宁波市|