This code pattern shows how to model a supply-chain network using the IBM Blockchain Platform and is based on a collaboration with Brooklyn Roasting Company. The story, along with the supply-chain documents that were used to model this network, can be found at: https://www.ibm.com/blockchainbean. Note that the 'view the blockchain' button is being migrated''
This repository will not be updated. I will check periodically for pull requests, but do not expect a quick response.
This code pattern is based on a recent proof-of-concept developed in collaboration with a coffee roasting company that was nice enough to let us use their supply-chain documents. The finished product of this code pattern is a cloud-based blockchain API, that any other UI app can call. An example UI app that leverages our blockchain API can be found in the screenshots below.
Each cup has a history based on which batch of coffee was used make the cup, which you can see below. Additionally, you can see other details such as who poured the cup, at what time the cup was poured, which type of beans were used, etc. For example, a sample cup 'NJB123' for the result shown below.
In this code pattern we will create a blockchain app that increases visibility and efficiency in the supply chain of a coffee retailer using IBM Blockchain Platform V2 Beta. We will use different transactions to show different possible actions for the different participants in the supply chain. This sample application will record all transactions on the IBM Blockchain Platform V2 Beta, and enable a coffee retailer to ensure the customer that their coffee is organic and fair-trade. The code pattern can be useful to developers that are looking into learning more about creating applications that integrate supply chains with Hyperledger Fabric.
When the reader has completed this code pattern, they will understand how to:
This pattern assumes you have an IBM Cloud account, VSCode and IBM Blockchain Platform Extension for VSCode installed
To run a local network, you can find steps here.
Git clone this repo onto your computer in the destination of your choice, then go into the web-app folder:
HoreaPorutiu$ git clone https://github.com/horeaporutiu/blockchainbean2.git
Navigate to the web-app
directory:
HoreaPorutiu$ cd blockchainbean2/web-app
Install required dependencies using NPM:
web-app$ npm install --ignore-scripts
Right-click under your folders in your workspace area and then click Add Folder to Workspace and then highlight the
blockchainbean/lib
directory as shown in the picture below, and then click on add:
Next, we have to package the smart contract. Click on the F1 button on your keyboard,
which will bring up the VSCode command palette. From there, navigate and click on Package a Smart Contract Project
.
Next, the extension will ask the following question:
Choose a workspace folder to package
Click on the lib folder - note we do not want to package our client (i.e. our web-app directory).
If all went well, you should see the following.
Note that this .cds
file is extremely important if we want to run
our smart contract on the cloud.
Catalog
. For this code pattern, we can use the Free
cluster, and give it a name. Note, that the IBM Cloud allows one instance of a free cluster and expires after 30 days. The cluster takes around 10-15
minutes to provision, so please be patient!
Catalog
, and give a name.
We will build a network as provided by the IBM Blockchain Platform documentation. This will include creating a channel with a single peer organization with its own MSP and CA (Certificate Authority), and an orderer organization with its own MSP and CA. We will create the respective identities to deploy peers and operate nodes.
Org1 CA
.admin
and Admin Secret of adminpw
.
admin
and an Enroll secret of adminpw
. Use the default value of Org1 CA Admin
for the Identity display name.org1admin
, and Enroll Secret of org1adminpw
. Click Next. Set the Type for this identity as client
and select org1
from the affiliated organizations drop-down list. We will leave the Maximum enrollments and Add Attributes fields blank.peer1
, and Enroll Secret of peer1pw
. Click Next. Set the Type for this identity as peer
and select org1
from the affiliated organizations drop-down list. We will leave the Maximum enrollments and Add Attributes fields blank.
Org1 MSP
and an MSP ID of org1msp
.Org1 CA
as the root CA for the organization.org1admin
and org1adminpw
. Then, give the Identity name, Org1 Admin
.
Peer Org1
.Org1 CA
as your Certificate Authority. Then, give the Enroll ID and Enroll secret for the peer identity that you created for your peer, peer1
, and peer1pw
. Then, select the Administrator Certificate (from MSP), Org1 MSP
, from the drop-down list and click Next.admin
, and TLS Enroll secret, adminpw
, the same values are the Enroll ID and Enroll secret that you gave when creating the CA. Leave the TLS CSR hostname blank.Org1 Admin
.
Orderer CA
.admin
and Admin Secret of adminpw
.
admin
and an Enroll secret of adminpw
. Use the default value of Orderer CA Admin
for the Identity display name.ordereradmin
, and Enroll Secret of ordereradminpw
. Click Next. Set the Type for this identity as client
and select org1
from the affiliated organizations drop-down list. We will leave the Maximum enrollments and Add Attributes fields blank.orderer1
, and Enroll Secret of orderer1pw
. Click Next. Set the Type for this identity as peer
and select org1
from the affiliated organizations drop-down list. We will leave the Maximum enrollments and Add Attributes fields blank.
Orderer MSP
and an MSP ID of orderermsp
.Orderer CA
as the root CA for the organization.ordereradmin
and ordereradminpw
. Then, give the Identity name, Orderer Admin
.
Orderer
.Orderer CA
as your Certificate Authority. Then, give the Enroll ID and Enroll secret for the peer identity that you created for your orderer, orderer1
, and orderer1pw
. Then, select the Administrator Certificate (from MSP), Orderer MSP
, from the drop-down list and click Next.admin
, and TLS Enroll secret, adminpw
, the same values are the Enroll ID and Enroll secret that you gave when creating the CA. Leave the TLS CSR hostname blank.Orderer Admin
.
Org1 MSP
, as this is the MSP that represents the peer's organization org1.
mychannel
.Orderer
from the orderers drop-down list.Org1 MSP (org1msp)
.Org1 Admin
.
Orderer
and click Next.mychannel
and click Next.Peer Org1
.
mychannel
to instantiate the smart contract on. Click Next.org1msp
. Click Next.init
and leave Arguments blank.
Connect with SDK
from the overflow menu on the right side of the row.org1msp
.Org1 CA
.
app-admin
and app-adminpw
.client
as Type and any organization for affiliation. We can pick org1
to be consistent.hf.Registrar.Roles
= *
. This will allow this identity to act as registrar and issues identities for our app. Click Add-attribute.
app-admin
and app-adminpw
.org1msp
.{ enabled: true, asLocalhost: false }
to connect to IBP.the current default setup is to connect to a local fabric instance from VS Code
{
"connection_file": "ibpConnection.json",
"appAdmin": "app-admin",
"appAdminSecret": "app-adminpw",
"orgMSPID": "org1msp",
"caName": "173.193.106.28:32634",
"userName": "user1",
"gatewayDiscovery": { "enabled": true, "asLocalhost": false }
}
First, navigate to the server
directory, and install the node dependencies.
cd server
npm install
⚠️ if you get a grpc error run:
npm rebuild
Run the enrollAdmin.js
script
node enrollAdmin.js
You should see the following in the terminal:
msg: Successfully enrolled admin user app-admin and imported it into the wallet
Run the registerUser.js
script.
node registerUser.js
You should see the following in the terminal:
Successfully registered and enrolled admin user user1 and imported it into the wallet
If you get an error such as this
Error: fabric-ca request register failed with errors [[{"code":0,"message":"Registration of 'user1' failed: Identity 'user1' is already registered"}]]
go ahead and change the user in config.json file, you can use any username you want. Once you save the config.json with a new user, for example:
"userName": "user4",
Run the same script agian.
In a new terminal, open the web-app folder from the room blockchainbean2 directory.
cd web-app
Start the client:
npm start
You can find the app running at http://localhost:8080/explorer If all goes well, you should see something like the picture below:
To get started submitting our first transaction on the network, we can update the ledger
with some of our suppliers info, such as their address, their uniqueId, and their
organization. To do this, first click on GrowerController
. You should see the Controller
expand with the GET/POST methods. Click on the green POST/Grower
button and then Try it out
to
the right of the POST/Grower
button. This will enable you to write in a request body. Go ahead
and copy and paste the following JSON in to the request body. P.S. (I have made all the commands
available in the commands.txt file).
{
"$class": "org.ibm.coffee.Grower",
"isFairTrade": true,
"growerId": "Grower-0201",
"organization": "Ethiopia Gedeb 1 Banko Gotiti GrainPro",
"address": {
"$class": "org.ibm.coffee.Address",
"city": "Gedeb",
"country": "Ethiopia",
"street": "N/A",
"zip": "N/A"
}
}
Then click the blue Execute button under the request body. If all goes well, you should see something similar to the image below:
If all went well, you can now go into your blockchain network, click on the channel, and then
you should see the block height increased, and if you click on the last block, you should
see the latest JSON that we inputted being written to the blockchain. Note: The gif shows
how to get to your channel and transaction overview. Your transaction data should be different -
it should show updating the ledger with the information of Grower-0201
.
The rest of the commands for uploading supply chain documents can be found here.
If you want to keep your application running all the time, you'll want to deploy it to the cloud.
To do this, please follow the guide here:
So yes! Go ahead and run the /POST transactions as shown in these steps, and everything will be stored on the IBM Blockchain Platform. So now, you are officially done with this tutorial. So what did you learn?
So at this point, you know more than me! Hopefully you feel pretty good at this point, and can dive a bit deeper into other, more complex topics, such as how to scale your network, how to optimize performance, etc. But for now, you know all of the basics to run a supply chain network on Hyperledger Fabric, both locally, and on the Cloud.
GREAT JOB! YOU DID IT! :)
And when you create the cool new startup unicorn after learning a bunch from this tutorial, don't forget to give me, or IBM Developer some credit :)
If you are getting errors with your IBM Blockchain VSCode extension, ensure you have all prerequisites installed here: https://github.com/IBM-Blockchain/blockchain-vscode-extension#requirements
This code pattern is licensed under the Apache Software License, Version 2. Separate third-party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the Developer Certificate of Origin, Version 1.1 (DCO) and the Apache Software License, Version 2.