Use Hyperledger Fabric to invoke chaincode while storing results and data in the starter plan

Summary

In this code pattern, you will uncover an existing Hyperledger Composer application and convert it to use Hyperledger Fabric Node SDK along with the IBM Blockchain Starter Plan. Get the code for the Composer application. This app comprises three main parts: members of the auction, the car to be auctioned, and the vehicle listing where members can place bids. After you create an IBM Blockchain cloud service, called the Starter Plan, you use the credentials from the cloud connection profile to connect your Node app to the Starter Plan’s certificate authority (CA). Once the CA gives you your credentials, each transaction (chaincode invocation) is then signed by the user’s credentials, so that it can be easily verified by the other peers on the network.

Inside the blockchain cloud service, you install your chaincode onto the peers and then instantiate it. This populates your auction with three members, one car, and one vehicle listing where offers can be made. Your job is to make offers and sell the car. This is easily done by invoking the chaincode, which checks for errors and ensures that the highest bidder wins. The ledger is then updated to show that the car’s new owner is the highest bidder and that the seller of the car gets the buyer’s highest offer. And of course, all of these transactions are saved as blocks in the IBM Blockchain cloud service.

Description

In this code pattern, you will create a blockchain network that simulates a car auction network.

Intro to certificate authority

The first step before diving into the car-auction logic is to enroll your application with the certificate authority from the IBM Blockchain Starter Plan. To do this, you need to give your application the API endpoints of the CA on the IBM Blockchain Starter plan so your app can interact with the network. The CA will then provide you with certificates that will prove your authenticity to the network, allowing you to transact (invoke chaincode) on the network.

Note: Any calls to the Hyperledger Fabric network will have to be signed with a private key and a properly signed X.509 certificate for verification purposes. Each of the actors within your network (peers, orderers, client apps, admins) will have a digital identity encapsulated in an X.509 certificate. You’ll need certificates for both an admin user and a new user, which you can call user1.

Intro to chaincode

After you have finished generating keys and certificates, you need to install the chaincode on the peers. After the chaincode is installed, you will instantiate it, which will call the chaincode constructor and create blocks on the ledger. This is seen in the initLedger function from the chaincode/carauction.js file. This will create a vehicle, a few members, and a vehicle listing (or a listing on which members can bid). After that, the members make offers for the car, which is actually invoking chaincode under the hood.

Note: When you invoke chaincode, you are making a transaction on the blockchain network. This is extremely important. Chaincode is how you make transactions on the network. When you make an offer, the chaincode checks for two types of errors:

The owner of the car bids on his own car.

The bidder doesn’t have enough money in his account to make the bid.

If both checks are passed, an offer is recorded on the ledger. Once the auction closes, you call the closeBidding transaction as seen in the chaincode/carauction.js file. That gives the car to the highest bidder, transferring funds from the buyer to the seller so the buyer gains ownership of the car.

To ensure that your auction has worked correctly, you can query the ledger at the end to ensure that the car has the correct owner and that the seller has been credited the correct amount in their account.

Finally, you can check the logs of the peers on the IBM Blockchain Starter Plan, and also view the details of the blocks to see how transactions are recorded.

When you have completed this code pattern, you will understand how to:

Interact with the IBM Blockchain Starter Plan

Build a blockchain network using Hyperledger Fabric Node SDK

Inspect and read logs from applications that are connected to the IBM Blockchain Starter Plan

Flow

User downloads IBM Blockchain Starter Plan connection profile and adds the URL/secret for the CA in the Node app.

User enrolls the admin and registers a user with the CA.

User installs, instantiates, and invokes the carauction chaincode on the peer.

Ledger is updated, blocks are added to the Starter Plan service, and the response is sent to the Node app.

Instructions

Find the detailed instructions in the README. These steps show you how to: