Skip to content

Commit

Permalink
[FAB-17268] Move fabcar sample to test network (#103)
Browse files Browse the repository at this point in the history
Signed-off-by: NIKHIL E GUPTA <negupta@us.ibm.com>

Co-authored-by: NIKHIL E GUPTA <negupta@us.ibm.com>
  • Loading branch information
nikhil550 and NIKHIL E GUPTA authored Mar 30, 2020
1 parent 9397788 commit 87600bd
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 261 deletions.
4 changes: 2 additions & 2 deletions fabcar/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fabcar-java</groupId>
<artifactId>fabcar-java</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -53,4 +53,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
5 changes: 2 additions & 3 deletions fabcar/java/src/main/java/org/example/ClientApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ public static void main(String[] args) throws Exception {
// Load a file system based wallet for managing identities.
Path walletPath = Paths.get("wallet");
Wallet wallet = Wallet.createFileSystemWallet(walletPath);

// load a CCP
Path networkConfigPath = Paths.get("..", "..", "first-network", "connection-org1.yaml");
Path networkConfigPath = Paths.get("..", "..", "test-network", "organizations", "peerOrganizations", "org1.example.com", "connection-org1.yaml");

Gateway.Builder builder = Gateway.createBuilder();
builder.identity(wallet, "user1").networkConfig(networkConfigPath).discovery(true);
builder.identity(wallet, "appUser").networkConfig(networkConfigPath).discovery(true);

// create a gateway connection
try (Gateway gateway = builder.connect()) {
Expand Down
2 changes: 1 addition & 1 deletion fabcar/java/src/main/java/org/example/EnrollAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void main(String[] args) throws Exception {
// Create a CA client for interacting with the CA.
Properties props = new Properties();
props.put("pemFile",
"../../first-network/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
"../../test-network/organizations/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
props.put("allowAllHostNames", "true");
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
Expand Down
16 changes: 8 additions & 8 deletions fabcar/java/src/main/java/org/example/RegisterUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) throws Exception {
// Create a CA client for interacting with the CA.
Properties props = new Properties();
props.put("pemFile",
"../../first-network/crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
"../../test-network/organizations/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem");
props.put("allowAllHostNames", "true");
HFCAClient caClient = HFCAClient.createNewInstance("https://localhost:7054", props);
CryptoSuite cryptoSuite = CryptoSuiteFactory.getDefault().getCryptoSuite();
Expand All @@ -39,9 +39,9 @@ public static void main(String[] args) throws Exception {
Wallet wallet = Wallet.createFileSystemWallet(Paths.get("wallet"));

// Check to see if we've already enrolled the user.
boolean userExists = wallet.exists("user1");
boolean userExists = wallet.exists("appUser");
if (userExists) {
System.out.println("An identity for the user \"user1\" already exists in the wallet");
System.out.println("An identity for the user \"appUser\" already exists in the wallet");
return;
}

Expand Down Expand Up @@ -98,14 +98,14 @@ public String getMspId() {
};

// Register the user, enroll the user, and import the new identity into the wallet.
RegistrationRequest registrationRequest = new RegistrationRequest("user1");
RegistrationRequest registrationRequest = new RegistrationRequest("appUser");
registrationRequest.setAffiliation("org1.department1");
registrationRequest.setEnrollmentID("user1");
registrationRequest.setEnrollmentID("appUser");
String enrollmentSecret = caClient.register(registrationRequest, admin);
Enrollment enrollment = caClient.enroll("user1", enrollmentSecret);
Enrollment enrollment = caClient.enroll("appUser", enrollmentSecret);
Identity user = Identity.createIdentity("Org1MSP", enrollment.getCert(), enrollment.getKey());
wallet.put("user1", user);
System.out.println("Successfully enrolled user \"user1\" and imported it into the wallet");
wallet.put("appUser", user);
System.out.println("Successfully enrolled user \"appUser\" and imported it into the wallet");
}

}
2 changes: 1 addition & 1 deletion fabcar/javascript/enrollAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new CA client for interacting with the CA.
Expand Down
10 changes: 5 additions & 5 deletions fabcar/javascript/invoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
let ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new file system based wallet for managing identities.
Expand All @@ -20,16 +20,16 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const identity = await wallet.get('user1');
const identity = await wallet.get('appUser');
if (!identity) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}

// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
Expand All @@ -39,7 +39,7 @@ async function main() {

// Submit the specified transaction.
// createCar transaction - requires 5 argument, ex: ('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR10', 'Dave')
// changeCarOwner transaction - requires 2 args , ex: ('changeCarOwner', 'CAR12', 'Dave')
await contract.submitTransaction('createCar', 'CAR12', 'Honda', 'Accord', 'Black', 'Tom');
console.log('Transaction has been submitted');

Expand Down
8 changes: 4 additions & 4 deletions fabcar/javascript/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fs = require('fs');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new file system based wallet for managing identities.
Expand All @@ -21,16 +21,16 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const identity = await wallet.get('user1');
const identity = await wallet.get('appUser');
if (!identity) {
console.log('An identity for the user "user1" does not exist in the wallet');
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('Run the registerUser.js application before retrying');
return;
}

// Create a new gateway for connecting to our peer node.
const gateway = new Gateway();
await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: true, asLocalhost: true } });
await gateway.connect(ccp, { wallet, identity: 'appUser', discovery: { enabled: true, asLocalhost: true } });

// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
Expand Down
16 changes: 8 additions & 8 deletions fabcar/javascript/registerUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const path = require('path');
async function main() {
try {
// load the network configuration
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));

// Create a new CA client for interacting with the CA.
Expand All @@ -25,9 +25,9 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);

// Check to see if we've already enrolled the user.
const userIdentity = await wallet.get('user1');
const userIdentity = await wallet.get('appUser');
if (userIdentity) {
console.log('An identity for the user "user1" already exists in the wallet');
console.log('An identity for the user "appUser" already exists in the wallet');
return;
}

Expand All @@ -46,11 +46,11 @@ async function main() {
// Register the user, enroll the user, and import the new identity into the wallet.
const secret = await ca.register({
affiliation: 'org1.department1',
enrollmentID: 'user1',
enrollmentID: 'appUser',
role: 'client'
}, adminUser);
const enrollment = await ca.enroll({
enrollmentID: 'user1',
enrollmentID: 'appUser',
enrollmentSecret: secret
});
const x509Identity = {
Expand All @@ -61,11 +61,11 @@ async function main() {
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('user1', x509Identity);
console.log('Successfully registered and enrolled admin user "user1" and imported it into the wallet');
await wallet.put('appUser', x509Identity);
console.log('Successfully registered and enrolled admin user "appUser" and imported it into the wallet');

} catch (error) {
console.error(`Failed to register user "user1": ${error}`);
console.error(`Failed to register user "appUser": ${error}`);
process.exit(1);
}
}
Expand Down
18 changes: 18 additions & 0 deletions fabcar/networkDown.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error
set -ex

# Bring the test network down
pushd ../test-network
./network.sh down
popd

# clean out any old identites in the wallets
rm -rf javascript/wallet/*
rm -rf java/wallet/*
rm -rf typescript/wallet/*
Loading

0 comments on commit 87600bd

Please sign in to comment.