Deep-Dive into FabCar: A Complete Application Example on Hyperledger Fabric (Part 3)

7. Understand FabCar Client Application

Client Application in General

In Hyperledger Fabric, Client Application is the point external world interacting with the Fabric network and the deployed chaincode. This interaction is done through Software Development Kit (SDK). Hyperledger Fabric offers SDK for various programming languages. The officially released SDK are Java and Node SDK.

FabCar is using Node SDK

As FabCar Client Application is written in JavaScript, the Node SDK is being used.

FabCar Client Application

FabCar Client Application is inside fabric-samples/fabcar/ directory. The latest release 1.4 the FabCar codes are reworked in JavaScript. We see three folders: javascript/, typescript/ and javascript-low-level/. The demonstration here is using javascript/. If you are interested in pre 1.4 code, you can use javascript-low-level/. If you compare them you can see the large improvement on JavaScript coding.

User Enrollment into Fabric Network

The two codes enrollAdmin.js and registerUser.js are responsible for admin and user enrollment on the Fabric network before we can interact the chaincode. The result is key pair and certificates for admin and for user. In 1.4 they are stored in wallet/ directory locally.

  1. Load the required modules from fabric-ca-client and fabric-network.
  2. Retrieve the detail about the Basic Network deployment. It is used for obtaining the access point of Fabric CA (line 20–21).
  3. Check whether “admin” is already inside the wallet directory. If so, no further action is needed.
  4. Enroll the admin user to the Fabric CA with enrollment ID and Secret (aligned with what is defined in Fabric CA, see ca.example.com service in the docker-compose file basic-network/docker-composer.yml).
  5. The result of enrollment is key pair and certificate. The result is then stored in wallet/admin/.
  1. Load the required modules from fabric-network.
  2. Retrieve the detail about the Basic Network deployment.
  3. Check whether user1 is already enrolled. If so no further action is needed.
  4. Check whether admin is here. If admin is not yet enrolled, prompt for enrollAdmin.js and no further action is needed.
  5. Create a new gateway connecting to peer, with connection detail from Basic Network deployment.
  6. Register the user1 with admin created in previous part. The result is a secret.
  7. Now we can enroll the user1 with the secret.
  8. The result of enrollment is key pair and certificate for user1. The result is then stored in wallet/user1/.

Invoking functions

The fabric-samples come with two JavaScript file: query.js and invoke.js. From the name we know that they are for the two category of chaincode invoke.

  1. Load the required modules from fabric-network.
  2. Retrieve the detail about the Basic Network deployment.
  3. Check whether user1 is already enrolled (in wallet). If not, prompt for registerUser.js and no further action is needed.
  4. Create a new gateway connecting to peer, with connection detail from Basic Network deployment.
  5. Use the gateway to retrieve the channel (network) mychannel and chaincode (contract) fabcar.
  6. Use contract API evaluateTransaction() with the arguments required for query.
  7. The result is shown in string.
Use evaluateTransaction() to make query
  1. Load the required modules from fabric-network.
  2. Retrieve the detail about the Basic Network deployment.
  3. Check whether user1 is already enrolled (in wallet). If not, prompt for registerUser.js and no further action is needed.
  4. Create a new gateway connecting to peer, with connection detail from Basic Network deployment.
  5. Use the gateway to retrieve the channel (network) mychannel and chaincode (contract) fabcar.
  6. Use contract API submitTransaction() with the arguments required for query.
  7. Disconnect the gateway once transaction is processed.
Use submitTransaction() to invoke transactions that will update ledger

Demo of Client Application to the Deployed Chaincode

Finally, we will use the Client Application to interact with the chaincode.

  1. Use query.js to inspect CAR5
  2. Use invoke.js to change car owner of CAR5
  3. Use query.js again to inspect CAR5, and see if the owner is correctly changed
Modify the arguments to query a specific Car ID
Modify the arguments to change car owner
Car owner changed.

--

--

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