IBM Code Pattern: http://ibm.biz/blockchainpattern050918
Sign up to the IBM Cloud: http://ibm.biz/blockchain050918
IBM and Maersk Are Creating a New Blockchain Company
IBM and Danish shipping giant Maersk are teaming up to form a new company whose aim is to commercialize blockchain technology—the nifty, shared accounting ledgers first made famous by the cryptocurrency Bitcoin.
IBM and Stellar Are Launching Blockchain Banking Across Multiple Countries
https://www.hyperledger.org/projects/composer
https://www.hyperledger.org/projects/fabric
Hyperledger Fabric Documentation
Hyperledger, an open source collaborative effort to advance cross-industry blockchain technologies, is hosted by The Linux Foundation®.
IBM Design Thinking Field Guide
Zero to Blockchain An IBM Redbooks course
Writing Your First Application
In Hyperledger Composer: save data model as a .bna file.
In this journey, we capture the regulatory compliance logic for the Food Supplier Verification Program in a smart contract deployed on a business network.
This business network defines:
Participants:
Supplier
Importer
Retailer
Regulator
Assets:
ProductListingContract
Transactions:
createProductListing
transferListing
checkProducts
updateExemptedList
Initially, the supplier will transfer the food products to an importer who verifies whether the supplier, country, and food type all match with the correct identifiers. At port of entry, the supplier is checked against a list of known suppliers in a database (managed by the regulator). If the supplier is of type exempt, then the products are then transferred to the retailer. If the supplier is of type non-exempt, then the products are checked against a list of known food products in the database (managed by the regulator). If the food is exempt product then transfer it to the retailer. If the food is a non-exempt product, the importer must conduct a harms analysis (either independently, or using a third-party). The supplier provides the harms analysis report to the regulator. The regulator reviews compliance attestation and transfers the products to the retailer.
The createProductListing
function is called when an createProductListing
transaction is submitted. The logic allows a supplier to create a ProductListingContract
asset.
The transferListing
function is called when a transferListing
transaction is submitted by the owner of ProductListingContract
. It is submitted either by Supplier
to transfer ProductListingContract
to Importer
or by Importer
to transfer ProductListingContract
to Retailer
when the exempt check for the products is completed.
The checkProducts
function is called when a checkProducts
transaction is submitted by the Supplier
to perform the exempt check for the products present in the ProductListingContract
. The status of ProductListingContract
contract will change to CHECKCOMPLETED
if all all the products are exempted else the status will change to HAZARDANALYSISCHECKREQ
. HAZARDANALYSISCHECKREQ
means the Supplier
needs to provide Hazard Analysis report for the products. After submitting the report Supplier
performs the checkProducts
transaction to complete the exempt check for the products.
The updateExemptedList
function is called when a updateExemptedList
transaction is submitted by the Regulator
to update the list of exempted Orgs ids and Product ids.
Audience level : Intermediate Developers
If you have an IBM cloud lite account, you can also use the Starter Plan for 30 days to do this pattern.
- Hyperledger Fabric
- Hyperledger Composer
- Blockchain
- Containers
- Cloud
- Install Hyperledger Composer development tools
- Configure and start Hyperledger Fabric network
- Generate the Business Network Archive file
- Deploy the Business Network Archive using Composer Playground
- (Alternative method) Deploy the Business Network Archive on Hyperledger Composer running locally
- Generate the Business Network Archive (BNA)
- Deploy the Business Network Archive using Composer Playground
- Deploy the Business Network Archive on Hyperledger Composer running locally
To check that the structure of the files is valid, you can now generate a Business Network Archive (BNA) file for your business network definition. The BNA file is the deployable unit -- a file that can be deployed to the Composer runtime for execution.
Use the following command to generate the network archive:
npm install
You should see the following output:
Creating Business Network Archive
Looking for package.json of Business Network Definition
Input directory: /Users/ishan/Documents/git-demo/BlockchainPublicRegulationFabric-Food
Found:
Description: Sample food supplier verification network
Name: food-supply
Identifier: food-supply@0.0.1
Written Business Network Definition Archive file to
Output file: ./dist/food-supply.bna
Command succeeded
The composer archive create
command has created a file called food-supply.bna
in the dist
folder.
You can test the business network definition against the embedded runtime that stores the state of 'the blockchain' in-memory in a Node.js process. From your project working directory, open the file test/foodTest.js and run the following command:
npm test
You should see the following output :
> food-supply@0.0.1 test /Users/ishan/Documents/demo/BlockchainPublicRegulationFabric-Food
> mocha --recursive
#composer.food.supply
✓ Transfer ProductListing to Importer (67ms)
✓ Exempt Check for ProductListing (98ms)
✓ Transfer ProductListing to Retailer (95ms)
3 passing (2s)
Open Composer Playground, by default the Basic Sample Network is imported.
If you have previously used Playground, be sure to clear your browser local storage by running localStorage.clear()
in your browser Console.
Now import the food-supply.bna
file and click on deploy button.
You can also setup Composer Playground locally.
You will see the following:
To test your Business Network Definition, first click on the Test tab:
In the Supplier
participant registry, create a new participant. Make sure you click on the Supplier
tab on the far left-hand side first and click on Create New Participant
button.
{
"$class": "composer.food.supply.Supplier",
"supplierId": "supplierA",
"countryId": "UK",
"orgId": "ACME"
}
Similarly create retailer, regulator, importer participants by selecting the respective tabs.
{
"$class": "composer.food.supply.Retailer",
"retailerId": "retailerA",
"products": []
}
{
"$class": "composer.food.supply.Regulator",
"regulatorId": "regulatorA",
"location": "SF",
"exemptedOrgIds": ["ACME","XYZ CORP"],
"exemptedProductIds": ["prodA","prodB"]
}
{
"$class": "composer.food.supply.Importer",
"importerId": "importerA"
}
Now we are ready to add Access Control. Do this by first clicking on the admin
tab to issue new ids to the participants and add the ids to the wallet.
Please follow the instructions as shown in the images below:
Click on Issue New Id
button to create new Ids.
Click on Add to my Wallet
link to add the newly generated Id to the Wallet
.
Select the Supplier id
from Wallet tab
tab. Now click on the test tab
to perform createProductListing
and transferListing
transactions.
Now click on Submit Transaction
button and select createProductListing
transaction from the dropdown, to create a product listing for the list of products. products
array element contains information about the productid
and quantity
separated by ,
.
{
"$class": "composer.food.supply.createProductListing",
"products": ["prodA,5","prodB,2"],
"user": "resource:composer.food.supply.Supplier#supplierA"
}
After executing the transaction successfully, productListing
will be created in ProductListingContract
registry.
Similarly, submit a transferListing
transaction to transfer the productListing to Importer
.
ProductListingContractID
is the id of the ProductListingContract copied from theProductListingContract
registry.
{
"$class": "composer.food.supply.transferListing",
"ownerType": "supplier",
"newOwner": "resource:composer.food.supply.Importer#importerA",
"productListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"
}
importerA
will be the owner of ProductListingContract
and the status will be EXEMPTCHECKREQ
. Also, productListing will be removed from Supplier
view. Now select the importer
id from the Wallet tab
and submit checkProducts
transaction to perform the exempt check for the products.
{
"$class": "composer.food.supply.checkProducts",
"regulator": "resource:composer.food.supply.Regulator#regulatorA",
"productListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"
}
Successful execution of transaction will change the status of productListing to CHECKCOMPLETED
. Now perform transferListing
transaction to transfer the products to retailer.
{
"$class": "composer.food.supply.transferListing",
"ownerType": "importer",
"newOwner": "resource:composer.food.supply.Retailer#retailerA",
"productListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"
}
The transaction will the change the owner of ProductListingContract
and update the list of products in Retailer
registry. Select the Retailer
id from the Wallet tab
and view the updated registries.
You can also use the default
System user
to perform all the actions as we have a rule inpermissions.acl
to permit all accessSystem user
.
Please start the local Fabric using the instructions.
Now change directory to the dist
folder containing food-supply.bna
file and type:
cd dist
composer network install --card PeerAdmin@hlfv1 --archiveFile food-supply.bna
composer network start --networkName food-supply --networkVersion 0.0.1 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card
composer card import --file networkadmin.card
You can verify that the network has been deployed by typing:
composer network ping --card admin@food-supply
You should see the the output as follows:
The connection to the network was successfully tested: food-supply
Business network version: 0.0.1
Composer runtime version: 0.19.4
participant: org.hyperledger.composer.system.NetworkAdmin#admin
identity: org.hyperledger.composer.system.Identity#c1d5b4919bb775fef75f407050c7fa292ce79d7f978703e974cb8f19404d100c
Command succeeded
To create the REST API we need to launch the composer-rest-server
and tell it how to connect to our deployed business network.
Now launch the server by changing directory to the BlockchainPublicRegulationFabric-Food
folder and type:
cd ..
composer-rest-server
Answer the questions posed at startup. These allow the composer-rest-server to connect to Hyperledger Fabric and configure how the REST API is generated.
- Enter
admin@food-supply
as the card name. - Select
never use namespaces
when asked whether to use namespaces in the generated API. - Select
No
when asked whether to secure the generated API. - Select
Yes
when asked whether to enable event publication. - Select
No
when asked whether to enable TLS security.
Test REST API
If the composer-rest-server started successfully you should see these two lines are output:
Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer
Open a web browser and navigate to http://localhost:3000/explorer
You should see the LoopBack API Explorer, allowing you to inspect and test the generated REST API. Follow the instructions to test Business Network Definition as mentioned above in the composer section.