From edd17e7a680bcbfe81f2966d005df83fc1105970 Mon Sep 17 00:00:00 2001 From: NIKHIL E GUPTA Date: Wed, 25 Mar 2020 16:44:14 -0400 Subject: [PATCH] Add enrollUser files to commercial paper Signed-off-by: NIKHIL E GUPTA --- commercial-paper/network-starter.sh | 10 +++- .../digibank/application/addToWallet.js | 8 +-- .../organization/digibank/application/buy.js | 2 +- .../digibank/application/enrollUser.js | 54 +++++++++++++++++++ .../digibank/application/package.json | 2 +- .../magnetocorp/application/addToWallet.js | 4 +- .../magnetocorp/application/enrollUser.js | 54 +++++++++++++++++++ .../magnetocorp/application/issue.js | 2 +- .../magnetocorp/application/package.json | 2 +- 9 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 commercial-paper/organization/digibank/application/enrollUser.js create mode 100644 commercial-paper/organization/magnetocorp/application/enrollUser.js diff --git a/commercial-paper/network-starter.sh b/commercial-paper/network-starter.sh index d35716455b..c2fe682ec0 100755 --- a/commercial-paper/network-starter.sh +++ b/commercial-paper/network-starter.sh @@ -20,11 +20,17 @@ cd "${DIR}/../test-network/" docker kill cliDigiBank cliMagnetoCorp logspout || true ./network.sh down -./network.sh up createChannel -s couchdb -i 2.0.0 +./network.sh up createChannel -ca -s couchdb -# Copy the connection profiles so they are in the correct organizations. +# Copy the connection profiles so they are in the correct organizations. cp "${DIR}/../test-network/organizations/peerOrganizations/org1.example.com/connection-org1.yaml" "${DIR}/organization/digibank/gateway/" cp "${DIR}/../test-network/organizations/peerOrganizations/org2.example.com/connection-org2.yaml" "${DIR}/organization/magnetocorp/gateway/" +cp ${DIR}/../test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/* ${DIR}/../test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/signcerts/User1@org1.example.com-cert.pem +cp ${DIR}/../test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/* ${DIR}/../test-network/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com/msp/keystore/priv_sk + +cp ${DIR}/../test-network/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/signcerts/* ${DIR}/../test-network/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/signcerts/User1@org2.example.com-cert.pem +cp ${DIR}/../test-network/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/keystore/* ${DIR}/../test-network/organizations/peerOrganizations/org2.example.com/users/User1@org2.example.com/msp/keystore/priv_sk + echo Suggest that you monitor the docker containers by running echo "./organization/magnetocorp/configuration/cli/monitordocker.sh net_test" diff --git a/commercial-paper/organization/digibank/application/addToWallet.js b/commercial-paper/organization/digibank/application/addToWallet.js index 0829a3dad4..40ef75ad7d 100644 --- a/commercial-paper/organization/digibank/application/addToWallet.js +++ b/commercial-paper/organization/digibank/application/addToWallet.js @@ -20,13 +20,13 @@ async function main() { const wallet = await Wallets.newFileSystemWallet('../identity/user/balaji/wallet'); // Identity to credentials to be stored in the wallet - const credPath = path.join(fixtures, '/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com'); - const certificate = fs.readFileSync(path.join(credPath, '/msp/signcerts/Admin@org1.example.com-cert.pem')).toString(); + const credPath = path.join(fixtures, '/organizations/peerOrganizations/org1.example.com/users/User1@org1.example.com'); + const certificate = fs.readFileSync(path.join(credPath, '/msp/signcerts/User1@org1.example.com-cert.pem')).toString(); const privateKey = fs.readFileSync(path.join(credPath, '/msp/keystore/priv_sk')).toString(); // Load credentials into wallet const identityLabel = 'balaji'; - + const identity = { credentials: { certificate, @@ -50,4 +50,4 @@ main().then(() => { console.log(e); console.log(e.stack); process.exit(-1); -}); \ No newline at end of file +}); diff --git a/commercial-paper/organization/digibank/application/buy.js b/commercial-paper/organization/digibank/application/buy.js index 7908f1582d..3b0f25428d 100644 --- a/commercial-paper/organization/digibank/application/buy.js +++ b/commercial-paper/organization/digibank/application/buy.js @@ -100,4 +100,4 @@ main().then(() => { console.log(e.stack); process.exit(-1); -}); \ No newline at end of file +}); diff --git a/commercial-paper/organization/digibank/application/enrollUser.js b/commercial-paper/organization/digibank/application/enrollUser.js new file mode 100644 index 0000000000..e5f4e95e89 --- /dev/null +++ b/commercial-paper/organization/digibank/application/enrollUser.js @@ -0,0 +1,54 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict'; + +const FabricCAServices = require('fabric-ca-client'); +const { Wallets } = require('fabric-network'); +const fs = require('fs'); +const yaml = require('js-yaml'); +const path = require('path'); + +async function main() { + try { + // load the network configuration + let connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/connection-org1.yaml', 'utf8')); + + // Create a new CA client for interacting with the CA. + const caInfo = connectionProfile.certificateAuthorities['ca.org1.example.com']; + const caTLSCACerts = caInfo.tlsCACerts.pem; + const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName); + + // Create a new file system based wallet for managing identities. + const walletPath = path.join(process.cwd(), '../identity/user/balaji/wallet'); + const wallet = await Wallets.newFileSystemWallet(walletPath); + console.log(`Wallet path: ${walletPath}`); + + // Check to see if we've already enrolled the admin user. + const userExists = await wallet.get('balaji'); + if (userExists) { + console.log('An identity for the client user "balaji" already exists in the wallet'); + return; + } + + // Enroll the admin user, and import the new identity into the wallet. + const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: 'user1pw' }); + const x509Identity = { + credentials: { + certificate: enrollment.certificate, + privateKey: enrollment.key.toBytes(), + }, + mspId: 'Org1MSP', + type: 'X.509', + }; + await wallet.put('balaji', x509Identity); + console.log('Successfully enrolled client user "balaji" and imported it into the wallet'); + + } catch (error) { + console.error(`Failed to enroll client user "balaji": ${error}`); + process.exit(1); + } +} + +main(); diff --git a/commercial-paper/organization/digibank/application/package.json b/commercial-paper/organization/digibank/application/package.json index 27eeef9d0f..2bc80f12b5 100644 --- a/commercial-paper/organization/digibank/application/package.json +++ b/commercial-paper/organization/digibank/application/package.json @@ -11,7 +11,7 @@ "license": "Apache-2.0", "dependencies": { "fabric-network": "beta", - "fabric-client": "beta", + "fabric-ca-client": "beta", "js-yaml": "^3.12.0" }, "devDependencies": { diff --git a/commercial-paper/organization/magnetocorp/application/addToWallet.js b/commercial-paper/organization/magnetocorp/application/addToWallet.js index 9de29e68a2..40ec799d3c 100644 --- a/commercial-paper/organization/magnetocorp/application/addToWallet.js +++ b/commercial-paper/organization/magnetocorp/application/addToWallet.js @@ -35,7 +35,7 @@ async function main() { type: 'X.509' } - + await wallet.put(identityLabel,identity); } catch (error) { @@ -50,4 +50,4 @@ main().then(() => { console.log(e); console.log(e.stack); process.exit(-1); -}); \ No newline at end of file +}); diff --git a/commercial-paper/organization/magnetocorp/application/enrollUser.js b/commercial-paper/organization/magnetocorp/application/enrollUser.js new file mode 100644 index 0000000000..d661d7eb04 --- /dev/null +++ b/commercial-paper/organization/magnetocorp/application/enrollUser.js @@ -0,0 +1,54 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict'; + +const FabricCAServices = require('fabric-ca-client'); +const { Wallets } = require('fabric-network'); +const fs = require('fs'); +const yaml = require('js-yaml'); +const path = require('path'); + +async function main() { + try { + // load the network configuration + let connectionProfile = yaml.safeLoad(fs.readFileSync('../gateway/connection-org2.yaml', 'utf8')); + + // Create a new CA client for interacting with the CA. + const caInfo = connectionProfile.certificateAuthorities['ca.org2.example.com']; + const caTLSCACerts = caInfo.tlsCACerts.pem; + const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName); + + // Create a new file system based wallet for managing identities. + const walletPath = path.join(process.cwd(), '../identity/user/isabella/wallet'); + const wallet = await Wallets.newFileSystemWallet(walletPath); + console.log(`Wallet path: ${walletPath}`); + + // Check to see if we've already enrolled the admin user. + const userExists = await wallet.get('isabella'); + if (userExists) { + console.log('An identity for the client user "user1" already exists in the wallet'); + return; + } + + // Enroll the admin user, and import the new identity into the wallet. + const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: 'user1pw' }); + const x509Identity = { + credentials: { + certificate: enrollment.certificate, + privateKey: enrollment.key.toBytes(), + }, + mspId: 'Org2MSP', + type: 'X.509', + }; + await wallet.put('isabella', x509Identity); + console.log('Successfully enrolled client user "isabella" and imported it into the wallet'); + + } catch (error) { + console.error(`Failed to enroll client user "isabella": ${error}`); + process.exit(1); + } +} + +main(); diff --git a/commercial-paper/organization/magnetocorp/application/issue.js b/commercial-paper/organization/magnetocorp/application/issue.js index 04db65eab6..fe6e7c4ee5 100644 --- a/commercial-paper/organization/magnetocorp/application/issue.js +++ b/commercial-paper/organization/magnetocorp/application/issue.js @@ -98,4 +98,4 @@ main().then(() => { console.log(e.stack); process.exit(-1); -}); \ No newline at end of file +}); diff --git a/commercial-paper/organization/magnetocorp/application/package.json b/commercial-paper/organization/magnetocorp/application/package.json index 3e412473cd..0b29a9c1cb 100644 --- a/commercial-paper/organization/magnetocorp/application/package.json +++ b/commercial-paper/organization/magnetocorp/application/package.json @@ -11,7 +11,7 @@ "license": "Apache-2.0", "dependencies": { "fabric-network": "beta", - "fabric-client": "beta", + "fabric-ca-client": "beta", "js-yaml": "^3.12.0" }, "devDependencies": {