Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FAB-16147] Update Commercial Paper to work with v2 #98

Merged
merged 1 commit into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ci/azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,25 @@ jobs:
- template: install-deps.yml
- template: install-fabric.yml
- template: fabcar-typescript.yml
- job: commercialpaper_javascript
displayName: CommercialPaper (JavaScript)
pool:
vmImage: ubuntu-18.04
dependsOn: []
timeoutInMinutes: 60
steps:
- template: install-deps.yml
- template: install-fabric.yml
- template: commercialpaper-javascript.yml
- job: commercialpaper_java
displayName: CommercialPaper (Java)
pool:
vmImage: ubuntu-18.04
dependsOn: []
timeoutInMinutes: 60
steps:
- template: install-deps.yml
- template: install-fabric.yml
- template: commercialpaper-java.yml


46 changes: 46 additions & 0 deletions ci/commercialpaper-java.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# SPDX-License-Identifier: Apache-2.0
#

steps:
- script: bash start.sh
workingDirectory: basic-network
displayName: Start Fabric
- script: |
./gradlew build
workingDirectory: commercial-paper/organization/digibank/contract-java
displayName: Build Java Contract
- script: |
docker-compose -f docker-compose.yml up -d cliDigiBank

docker exec cliDigiBank peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/contract-java/build/libs --label cp_0
docker exec cliDigiBank peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliDigiBank peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec cliDigiBank peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec cliDigiBank peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec cliDigiBank peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent

workingDirectory: commercial-paper/organization/digibank/configuration/cli
displayName: Setup Commercial Paper contract

- script: retry -- npm install
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Install Magnetocorp application
- script: |
set -ex
node addToWallet.js
node issue.js
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Magnetocorp issue paper

- script: retry -- npm install
workingDirectory: commercial-paper/organization/digibank/application
displayName: Install Digibank application
- script: |
set -ex
node addToWallet.js
node buy.js
node redeem.js
workingDirectory: commercial-paper/organization/digibank/application
displayName: Digibank issue paper
42 changes: 42 additions & 0 deletions ci/commercialpaper-javascript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# SPDX-License-Identifier: Apache-2.0
#

steps:
- script: bash start.sh
workingDirectory: basic-network
displayName: Start Fabric
- script: |
docker-compose -f docker-compose.yml up -d cliMagnetoCorp

docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent

workingDirectory: commercial-paper/organization/magnetocorp/configuration/cli
displayName: Setup Commercial Paper contract

- script: retry -- npm install
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Install Magnetocorp application
- script: |
set -ex
node addToWallet.js
node issue.js
workingDirectory: commercial-paper/organization/magnetocorp/application
displayName: Magnetocorp issue paper

- script: retry -- npm install
workingDirectory: commercial-paper/organization/digibank/application
displayName: Install Digibank application
- script: |
set -ex
node addToWallet.js
node buy.js
node redeem.js
workingDirectory: commercial-paper/organization/digibank/application
displayName: Digibank issue paper
41 changes: 41 additions & 0 deletions ci/getDockerImages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash -e
set -o pipefail

echo "======== PULL DOCKER IMAGES ========"
##########################################################
REPO_URL=hyperledger-fabric.jfrog.io
ORG_NAME="fabric"

VERSION=2.0.0
ARCH="amd64"
: ${STABLE_VERSION:=$VERSION-stable}
STABLE_TAG=$ARCH-$STABLE_VERSION
MASTER_TAG=$ARCH-stable

echo "---------> STABLE_VERSION:" $STABLE_VERSION

dockerTag() {
for IMAGES in baseos peer orderer ca tools orderer ccenv javaenv nodeenv; do
echo "Images: $IMAGES"
echo
docker pull $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG
if [ $? != 0 ]; then
echo "FAILED: Docker Pull Failed on $IMAGES"
exit 1
fi
docker tag $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG hyperledger/$ORG_NAME-$IMAGES
docker tag $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG hyperledger/$ORG_NAME-$IMAGES:latest
docker tag $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG hyperledger/$ORG_NAME-$IMAGES:$ARCH-$VERSION-stable
docker tag $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG hyperledger/$ORG_NAME-$IMAGES:$ARCH-stable
docker tag $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG hyperledger/$ORG_NAME-$IMAGES:$VERSION

echo "Deleting docker images: $IMAGES"
docker rmi -f $REPO_URL/$ORG_NAME-$IMAGES:$STABLE_TAG
done
}

dockerTag

echo
docker images
echo
10 changes: 1 addition & 9 deletions ci/install-fabric.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,5 @@ steps:
cd /usr/local
sudo tar xzvf /tmp/hyperledger-fabric-ca-latest-linux-amd64.latest-SNAPSHOT.tar.gz
displayName: Download Fabric CA CLI
- script: |
set -ex
for i in baseos ca ccenv javaenv nodeenv peer orderer tools; do
docker pull nexus3.hyperledger.org:10001/hyperledger/fabric-$i:amd64-2.0.0-stable
docker tag nexus3.hyperledger.org:10001/hyperledger/fabric-$i:amd64-2.0.0-stable hyperledger/fabric-$i:amd64-2.0.0-stable
docker tag nexus3.hyperledger.org:10001/hyperledger/fabric-$i:amd64-2.0.0-stable hyperledger/fabric-$i:amd64-2.0.0
docker tag nexus3.hyperledger.org:10001/hyperledger/fabric-$i:amd64-2.0.0-stable hyperledger/fabric-$i:2.0.0
docker tag nexus3.hyperledger.org:10001/hyperledger/fabric-$i:amd64-2.0.0-stable hyperledger/fabric-$i:latest
done
- script: bash ci/getDockerImages.sh
displayName: Pull Fabric Docker images
1 change: 1 addition & 0 deletions commercial-paper/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cp.tar.gz
15 changes: 10 additions & 5 deletions commercial-paper/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# Commercial Paper Tutorial

This folder contains the code for an introductory tutorial to Smart Contract development. It is based around the scenario of Commercial Paper.
The full tutorial, including full scenario details and line by line code walkthroughs is in the [Hyperledger Fabric documentation](https://hyperledger-fabric.readthedocs.io/en/release-1.4/tutorial/commercial_paper.html).
The full tutorial, including full scenario details and line by line code walkthroughs is in the [Hyperledger Fabric documentation](https://hyperledger-fabric.readthedocs.io/en/latest/tutorial/commercial_paper.html).

## Scenario

In this tutorial two organizations, MagnetoCorp and DigiBank, trade commercial paper with each other using PaperNet, a Hyperledger Fabric blockchain network.

Once you’ve set up a basic network, you’ll act as Isabella, an employee of MagnetoCorp, who will issue a commercial paper on its behalf. You’ll then switch hats to take the role of Balaji, an employee of DigiBank, who will buy this commercial paper, hold it for a period of time, and then redeem it with MagnetoCorp for a small profit.

![](https://hyperledger-fabric.readthedocs.io/en/release-1.4/_images/commercial_paper.diagram.1.png)
![](https://hyperledger-fabric.readthedocs.io/en/latest/_images/commercial_paper.diagram.1.png)

## Quick Start

Expand All @@ -34,7 +34,7 @@ You are strongly advised to read the full tutorial to get information about the
You will need a machine with the following

- Docker and docker-compose installed
- Node.js v8 if you want to run JavaScript client applications
- Node.js v12 if you want to run JavaScript client applications
- Java v8 if you want to run Java client applications
- Maven to build the Java applications

Expand Down Expand Up @@ -68,9 +68,14 @@ This will start a docker container for Fabric CLI commands, and put you in the c
**For a JavaScript Contract:**

```
docker exec cliMagnetoCorp peer chaincode install -n papercontract -v 0 -p /opt/gopath/src/github.com/contract -l node
docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent

docker exec cliMagnetoCorp peer chaincode instantiate -n papercontract -v 0 -l node -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' -C mychannel -P "AND ('Org1MSP.member')"
```

**For a Java Contract:**
Expand Down
59 changes: 59 additions & 0 deletions commercial-paper/cp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
#
# SPDX-License-Identifier: Apache-2.0
#
set -ex

function _exit(){
printf "Exiting:%s\n" "$1"
exit -1
}

# Where am I?
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"

## Use this to remove anything existing on the basic network before starting
# docker kill $(docker network inspect net_basic --format '{{json .Containers}}' | jq -r 'keys[]') && docker rm $(docker ps -aq)

## Start the Fabric Network
cd "${DIR}/basic-network"
. ./start.sh

docker ps

## Run as MagnetoCorp
# cd "${DIR}/commercial-paper/organization/magnetocorp/configuration/cli"
# docker-compose -f docker-compose.yml up -d cliMagnetoCorp

# docker exec cliMagnetoCorp peer lifecycle chaincode package cp.tar.gz --lang node --path /opt/gopath/src/github.com/contract --label cp_0
# docker exec cliMagnetoCorp peer lifecycle chaincode install cp.tar.gz
# export PACKAGE_ID=$(docker exec cliMagnetoCorp peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

# docker exec cliMagnetoCorp peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
# docker exec cliMagnetoCorp peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
# docker exec cliMagnetoCorp peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent


cd "${DIR}/commercial-paper/organization/digibank/configuration/cli"
docker-compose -f docker-compose.yml up -d cliDigiBank
CLI_CONTAINER=cliDigiBank
docker exec ${CLI_CONTAINER} peer lifecycle chaincode package cp.tar.gz --lang java --path /opt/gopath/src/github.com/contract-java/build/libs --label cp_0
docker exec ${CLI_CONTAINER} peer lifecycle chaincode install cp.tar.gz
export PACKAGE_ID=$(docker exec ${CLI_CONTAINER} peer lifecycle chaincode queryinstalled 2>&1 | awk -F "[, ]+" '/Label: /{print $3}')

docker exec ${CLI_CONTAINER} peer lifecycle chaincode approveformyorg --channelID mychannel --name papercontract -v 0 --package-id $PACKAGE_ID --sequence 1 --signature-policy "AND ('Org1MSP.member')"
docker exec ${CLI_CONTAINER} peer lifecycle chaincode commit -o orderer.example.com:7050 --channelID mychannel --name papercontract -v 0 --sequence 1 --waitForEvent --signature-policy "AND ('Org1MSP.member')"
docker exec ${CLI_CONTAINER} peer chaincode invoke -o orderer.example.com:7050 --channelID mychannel --name papercontract -c '{"Args":["org.papernet.commercialpaper:instantiate"]}' --waitForEvent



cd "${DIR}/commercial-paper/organization/magnetocorp/application"
npm install
node addToWallet.js
node issue.js

cd "${DIR}/commercial-paper/organization/digibank/application"
npm install
node addToWallet.js
node buy.js
node redeem.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static String makeKey(String[] keyParts) {
}

public static String[] splitKey(String key) {
System.out.println("Splittin gkey " + key + " " + java.util.Arrays.asList(key.split(":")));
System.out.println("splitting key " + key + " " + java.util.Arrays.asList(key.split(":")));
return key.split(":");
}

Expand Down
26 changes: 17 additions & 9 deletions commercial-paper/organization/digibank/application/addToWallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,37 @@

// Bring key classes into scope, most importantly Fabric SDK network class
const fs = require('fs');
const { FileSystemWallet, X509WalletMixin } = require('fabric-network');
const { Wallets } = require('fabric-network');
const path = require('path');

const fixtures = path.resolve(__dirname, '../../../../basic-network');

// A wallet stores a collection of identities
const wallet = new FileSystemWallet('../identity/user/balaji/wallet');

async function main() {

// Main try/catch block
try {

// A wallet stores a collection of identities
const wallet = await Wallets.newFileSystemWallet('../identity/user/balaji/wallet');

// Identity to credentials to be stored in the wallet
const credPath = path.join(fixtures, '/crypto-config/peerOrganizations/org1.example.com/users/[email protected]');
const cert = fs.readFileSync(path.join(credPath, '/msp/signcerts/[email protected]')).toString();
const key = fs.readFileSync(path.join(credPath, '/msp/keystore/cd96d5260ad4757551ed4a5a991e62130f8008a0bf996e4e4b84cd097a747fec_sk')).toString();
const certificate = fs.readFileSync(path.join(credPath, '/msp/signcerts/[email protected]')).toString();
const privateKey = fs.readFileSync(path.join(credPath, '/msp/keystore/5ba12183ab07014ba831f9a79cf51fe7e6f62cdebe6193f070445243aedddee9_sk')).toString();

// Load credentials into wallet
const identityLabel = '[email protected]';
const identity = X509WalletMixin.createIdentity('Org1MSP', cert, key);

await wallet.import(identityLabel, identity);

const identity = {
credentials: {
certificate,
privateKey
},
mspId: 'Org1MSP',
type: 'X.509'
}

await wallet.put(identityLabel, identity);

} catch (error) {
console.log(`Error adding to wallet. ${error}`);
Expand Down
8 changes: 5 additions & 3 deletions commercial-paper/organization/digibank/application/buy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ SPDX-License-Identifier: Apache-2.0
// Bring key classes into scope, most importantly Fabric SDK network class
const fs = require('fs');
const yaml = require('js-yaml');
const { FileSystemWallet, Gateway } = require('fabric-network');
const { Wallets, Gateway } = require('fabric-network');
const CommercialPaper = require('../../magnetocorp/contract/lib/paper.js');

// A wallet stores a collection of identities for use
const wallet = new FileSystemWallet('../identity/user/balaji/wallet');

// Main program function
async function main () {

// A wallet stores a collection of identities for use
const wallet = await Wallets.newFileSystemWallet('../identity/user/balaji/wallet');


// A gateway defines the peers used to access Fabric networks
const gateway = new Gateway();

Expand Down
Loading