Giter VIP home page Giter VIP logo

ffa6995 / fabric-digital-product-passport Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 297.2 MB

Hyperledger Fabric Digital Product Passport

TypeScript 0.06% Dockerfile 0.01% Shell 0.36% HTML 6.81% Go 86.60% Fortran 0.01% C 0.28% Assembly 5.71% JavaScript 0.04% C++ 0.01% Batchfile 0.02% Makefile 0.01% Awk 0.01% Perl 0.07% Python 0.03%
blockchain circular-economy circularity digital-product-passport hyperledger-fabric permissioned-blockchain

fabric-digital-product-passport's Introduction

Hyperledger Fabric Digital Product Passport

Getting started with the Fabric Digital Product Passport

To use the Fabric Digital Product Passport, you need to download the Fabric Docker images and the Fabric CLI tools. First, make sure that you have installed all of the Fabric prerequisites. You can then follow the instructions to Install the Fabric Samples, Binaries, and Docker Images in the Fabric documentation. In addition to downloading the Fabric images and tool binaries, the Fabric samples will also be cloned to your local machine.

First get the install script:

curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh && chmod +x install-fabric.sh

Then pull the docker containers and the binaries

./install-fabric.sh docker binary

Default fabric version is always the latest version, but it can also be set by running this command (for example for version 2.5.0 - which is the version this project was developed):

./install-fabric.sh --fabric-version 2.5.0 binary

Test network

The Fabric test network in the test network folder provides a Docker Compose based test network with two Organization peers and an ordering service node. You can use it on your local machine to run the samples listed below. You can also use it to deploy and test your own Fabric chaincodes and applications. To get started, see the test network tutorial.

Digital Product Passport Prototype

Run the test network

To test the application you first have to run the test network. Instructions are also in the test network tutorial.

cd test-network

With the network.sh script the network can be started.

./network.sh down

to remove any container or artifact from previous runs

./network.sh up

to bring up the network together with its peers and orderer nodes.

Then a channel has to be created for the communication between the organizations.

./network.sh createChannel

this creates a channel with the name 'mychannel'

After that you have to install the chaincode on the channel and define where the privata data collection defintion is located:

./network.sh deployCC -ccn private -ccp ../product-passport/chaincode-typescript -ccl typescript -ccep "OR('Org1MSP.peer','Org2MSP.peer')" -cccg ./collections_config.json

Interacting with the network and chaincode

To interact with the test network and chaincode the peer CLI can be used. To use it the binaries have to be set as well as the fabric cfg path to point to the core.yaml

export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/

When operating as Organisation 1 the environment variables can be set to the following:

Environment variables for Org1

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Environment variables for Org2

When operating as Organisation 2 the environment variables can be set to the following

export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051

To initialize the initial data call the InitLedger function which registers some sample products and materials to the blockchain.

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"InitLedger","Args":[]}'

Depending on which function you want to call the function field has to be set with the function name you want to call.

Read Material Of Product

A function with arguments is called like in the following example. The example shows the ReadMaterialsOfProduct function which takes the ID of the product as argument. The argument is passed in quotes to the "Args":[]" of the function call. In this case "product1":

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"ReadMaterialsOfProduct","Args":["product1"]}'

If there is an error the response will write the error together with the error message in the terminal. For instance, here the caller does not have enough permissions to request private material data:

Error: endorsement failure during invoke. response: status:500 message:"Access denied. Caller does not have permission to read materials of product product1" 

If succesful the response will look something like this, where instead of the hashes, the whole material data is displayed:

2023-07-05 15:24:13.461 CEST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200 payload:"[\"{\\\"docType\\\":\\\"material\\\",\\\"ID\\\":\\\"material1\\\",\\\"MaterialName\\\":\\\"Material 1\\\",\\\"Producer\\\":\\\"Producer 1\\\",\\\"AppraisedValue\\\":100,\\\"Seller\\\":\\\"Producer 1\\\",\\\"Recycled\\\":false}\",\"{\\\"docType\\\":\\\"material\\\",\\\"ID\\\":\\\"material2\\\",\\\"MaterialName\\\":\\\"Material 2\\\",\\\"Producer\\\":\\\"Producer 2\\\",\\\"AppraisedValue\\\":200,\\\"Seller\\\":\\\"Producer 2\\\",\\\"Recycled\\\":false}\"]" 

Request Access to Materials of Product

To request acces to the materials of a product the RequestAccess function can be called. This function requests access for the caller. The function can be called like the following:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"RequestAccess","Args":["product1"]}'

The Manufacturer of the product, which is Org1 in this example, can then either approve or reject the request by calling the approveAccessForRequestID function. In this example the request for product1 for Org2MSP is approved by setting the approval to true:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"approveAccessForRequestID","Args":["product1", "Org2MSP", "true"]}'

Recycle Product

When a product is recycled the RecycleAndOffer function can be called to mark a product as Recycled and to register the materials as new Recycled Material objects on the blockchain.

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"RecycleAndOffer","Args":["product1"]}'

After the recycling process the recycled materials get registered on the blockchain as Recycled materials, that material producers can query for and then buy back with the given information:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n private --peerAddresses localhost:7051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt" --peerAddresses localhost:9051 --tlsRootCertFiles "${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt" -c '{"function":"GetMaterialInformation","Args":["material1RECYCLED"]}'

The response shows, who is the Seller of the recycled procut:

Chaincode invoke successful. result: status:200 payload:"{\"AppraisedValue\":100,\"ID\":\"material1RECYCLED\",\"MaterialName\":\"Material 1\",\"Producer\":\"Producer 1\",\"Recycled\":true,\"Seller\":\"Org2MSP\",\"docType\":\"material\"}" 

License

Hyperledger Project source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file. Hyperledger Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0), available at http://creativecommons.org/licenses/by/4.0/.

fabric-digital-product-passport's People

Contributors

ffa6995 avatar

Watchers

 avatar

Forkers

won21kr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.