diff --git a/asset-transfer-basic/application-javascript/app.js b/asset-transfer-basic/application-javascript/app.js new file mode 100644 index 00000000..d6eeaa83 --- /dev/null +++ b/asset-transfer-basic/application-javascript/app.js @@ -0,0 +1,131 @@ +/* + * Copyright IBM Corp. All Rights Reserved. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict'; + +const { Gateway, Wallets } = require('fabric-network'); +const path = require('path'); +const fs = require('fs'); +const registerUser = require('./registerUser'); +const enrollAdmin = require('./enrollAdmin'); + +const myChannel = 'mychannel'; +const myChaincodeName = 'basic'; + +function prettyJSONString(inputString) { + return JSON.stringify(JSON.parse(inputString),null,2); +} + +// pre-requisites: +// fabric-sample test-network setup with two peers and an ordering service, +// the companion chaincode is deployed, approved and committed on the channel mychannel +async function main() { + try { + // load the network configuration + const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json'); + const fileExists = fs.existsSync(ccpPath); + if (!fileExists) { + throw new Error(`no such file or directory: ${ccpPath}`); + } + const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8')); + + // Create a new file system based wallet for managing identities. + const walletPath = path.join(__dirname, 'wallet'); + const wallet = await Wallets.newFileSystemWallet(walletPath); + console.log(`Wallet path: ${walletPath}`); + + + // Steps: + // Note: Steps 1 & 2 need to done only once in an app-server per blockchain network + // 1. register & enroll admin user with CA, stores admin identity in local wallet + enrollAdmin.EnrollAdminUser(); + + // 2. register & enroll application user with CA, which is used as client identify to make chaincode calls, stores app user identity in local wallet + registerUser.RegisterAppUser(); + + // Check to see if app user exist in wallet. + const identity = await wallet.get(registerUser.ApplicationUserId); + if (!identity) { + console.log('An identity for the user does not exist in the wallet: '+ registerUser.ApplicationUserId); + return; + } + + //3. Prepare to call chaincode using fabric javascript node sdk + // Create a new gateway for connecting to our peer node. + const gateway = new Gateway(); + await gateway.connect(ccp, { wallet, identity: registerUser.ApplicationUserId, discovery: { enabled: true, asLocalhost: true } }); + try { + // Get the network (channel) our contract is deployed to. + const network = await gateway.getNetwork(myChannel); + + // Get the contract from the network. + const contract = network.getContract(myChaincodeName); + + //4. Init a set of asset data on the channel using chaincode 'InitLedger' + console.log('Submit Transaction: InitLedger creates the initial set of assets on the ledger.'); + await contract.submitTransaction('InitLedger'); + + //5. *** Some example transactions are listed below *** + + // GetAllAssets returns all the current assets on the ledger + let result = await contract.evaluateTransaction('GetAllAssets'); + console.log('Evaluate Transaction: GetAllAssets, result: ' + prettyJSONString(result.toString()) ); + + console.log('\n***********************'); + console.log('Submit Transaction: CreateAsset asset13'); + //CreateAsset creates an asset with ID asset13, color yellow, owner Tom, size 5 and appraizedValue of 1300 + await contract.submitTransaction('CreateAsset', 'asset13', 'yellow', 5, 'Tom', 1300); + + console.log('Evaluate Transaction: ReadAsset asset13'); + // ReadAsset returns an asset with given assetID + result = await contract.evaluateTransaction('ReadAsset', 'asset13'); + console.log(' result: ' + prettyJSONString(result.toString()) ); + + console.log('\n***********************'); + console.log('Evaluate Transaction: AssetExists asset1'); + // AssetExists returns 'true' if an asset with given assetID exist + result = await contract.evaluateTransaction('AssetExists', 'asset1'); + console.log(' result: ' + prettyJSONString(result.toString()) ); + + console.log('Submit Transaction: UpdateAsset asset1, new AppraisedValue : 350'); + // UpdateAsset updates an existing asset with new properties. Same args as CreateAsset + await contract.submitTransaction('UpdateAsset', 'asset1', 'blue', 5, 'Tomoko', 350); + + console.log('Evaluate Transaction: ReadAsset asset1'); + result = await contract.evaluateTransaction('ReadAsset', 'asset1'); + console.log(' result: ' + prettyJSONString(result.toString()) ); + + try { + console.log('\nSubmit Transaction: UpdateAsset asset70'); + //Non existing asset asset70 should throw Error + await contract.submitTransaction('UpdateAsset', 'asset70', 'blue', 5, 'Tomoko', 300); + } + catch (error) { + let errMsg = 'Expected an error on UpdateAsset of non-existing Asset. '; + console.log(errMsg + error); + } + console.log('\n***********************'); + + console.log('Submit Transaction: TransferAsset asset1 from owner Tomoko > owner Tom'); + // TransferAsset transfers an asset with given ID to new owner Tom + await contract.submitTransaction('TransferAsset', 'asset1', 'Tom'); + + console.log('Evaluate Transaction: ReadAsset asset1'); + result = await contract.evaluateTransaction('ReadAsset', 'asset1'); + console.log(' result: ' + prettyJSONString(result.toString()) ); + + } finally { + // Disconnect from the gateway peer when all work for this client identity is complete + gateway.disconnect(); + } + } catch (error) { + console.error(`Failed to evaluate transaction: ${error}`); + process.exit(1); + } +} + + +main(); diff --git a/asset-transfer-basic/application-javascript/enrollAdmin.js b/asset-transfer-basic/application-javascript/enrollAdmin.js index 85d36a0b..79a794e6 100644 --- a/asset-transfer-basic/application-javascript/enrollAdmin.js +++ b/asset-transfer-basic/application-javascript/enrollAdmin.js @@ -10,8 +10,11 @@ const FabricCAServices = require('fabric-ca-client'); const { Wallets } = require('fabric-network'); const fs = require('fs'); const path = require('path'); +const adminUserId = 'admin'; +const adminUserPasswd = 'adminpw'; +const walletPath = path.join(__dirname, 'wallet'); -async function main() { +async function enrollAdminUser() { try { // load the network configuration const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json'); @@ -26,20 +29,18 @@ async function main() { 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(__dirname, 'wallet'); + // Create a new wallet : Note that wallet can be resfor managing identities. const wallet = await Wallets.newFileSystemWallet(walletPath); - console.log(`Wallet path: ${walletPath}`); // Check to see if we've already enrolled the admin user. - const identity = await wallet.get('admin'); + const identity = await wallet.get(adminUserId); if (identity) { - console.log('An identity for the admin user "admin" already exists in the wallet'); + console.log('An identity for the admin user already exists in the wallet'); return; } // Enroll the admin user, and import the new identity into the wallet. - const enrollment = await ca.enroll({ enrollmentID: 'admin', enrollmentSecret: 'adminpw' }); + const enrollment = await ca.enroll({ enrollmentID: adminUserId, enrollmentSecret: adminUserPasswd }); const x509Identity = { credentials: { certificate: enrollment.certificate, @@ -48,13 +49,14 @@ async function main() { mspId: 'Org1MSP', type: 'X.509', }; - await wallet.put('admin', x509Identity); - console.log('Successfully enrolled admin user "admin" and imported it into the wallet'); + await wallet.put(adminUserId, x509Identity); + console.log('Successfully enrolled admin user and imported it into the wallet'); } catch (error) { - console.error(`Failed to enroll admin user "admin": ${error}`); + console.error(`Failed to enroll admin user : ${error}`); process.exit(1); } } -main(); +exports.AdminUserId = adminUserId; +exports.EnrollAdminUser = enrollAdminUser; diff --git a/asset-transfer-basic/application-javascript/invoke.js b/asset-transfer-basic/application-javascript/invoke.js deleted file mode 100644 index 1092e230..00000000 --- a/asset-transfer-basic/application-javascript/invoke.js +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright IBM Corp. All Rights Reserved. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict'; - -const { Gateway, Wallets } = require('fabric-network'); -const fs = require('fs'); -const path = require('path'); - -async function main() { - try { - // load the network configuration - const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json'); - const fileExists = fs.existsSync(ccpPath); - if (!fileExists) { - throw new Error(`no such file or directory: ${ccpPath}`); - } - let ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8')); - - // Create a new file system based wallet for managing identities. - const walletPath = path.join(__dirname, 'wallet'); - const wallet = await Wallets.newFileSystemWallet(walletPath); - console.log(`Wallet path: ${walletPath}`); - - // Check to see if we've already enrolled the user. - const identity = await wallet.get('appUser'); - if (!identity) { - 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: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); - - // Get the contract from the network. - const contract = network.getContract('basic'); - - // Submit the specified transaction. (several example transactions are listed below) - // createAsset creates an asset with ID asset1, color yellow, owner Dave, size 5 and appraizedValue of 130 requires 6 arguments. - // ex: ('createAsset', 'asset1', 'yellow', 'Dave', 5, 1300) - // transferAsset transfers an asset with ID asset1 to new owner Tom - requires 2 arguments. - // ex: ('transferAsset', 'asset1', 'Tom') - await contract.submitTransaction('createAsset', 'asset13', 'yellow', 5, 'Tom', 1300); - console.log('Transaction has been submitted'); - - // Disconnect from the gateway. - await gateway.disconnect(); - - } catch (error) { - console.error(`Failed to submit transaction: ${error}`); - process.exit(1); - } -} - -main(); diff --git a/asset-transfer-basic/application-javascript/query.js b/asset-transfer-basic/application-javascript/query.js deleted file mode 100644 index 9c9adff0..00000000 --- a/asset-transfer-basic/application-javascript/query.js +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright IBM Corp. All Rights Reserved. - * - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict'; - -const { Gateway, Wallets } = require('fabric-network'); -const path = require('path'); -const fs = require('fs'); - - -async function main() { - try { - // load the network configuration - const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json'); - const fileExists = fs.existsSync(ccpPath); - if (!fileExists) { - throw new Error(`no such file or directory: ${ccpPath}`); - } - const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8')); - - // Create a new file system based wallet for managing identities. - const walletPath = path.join(__dirname, 'wallet'); - const wallet = await Wallets.newFileSystemWallet(walletPath); - console.log(`Wallet path: ${walletPath}`); - - // Check to see if we've already enrolled the user. - const identity = await wallet.get('appUser'); - if (!identity) { - 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: 'appUser', discovery: { enabled: true, asLocalhost: true } }); - - // Get the network (channel) our contract is deployed to. - const network = await gateway.getNetwork('mychannel'); - - // Get the contract from the network. - const contract = network.getContract('basic'); - - // Evaluate the specified transaction. (several example transactions are listed below) - // getAsset returns an asset with given assetID asset4 - requires 1 argument. - // ex: ('getAsset', 'asset4') - // getAllAssets returns all assets in the world state - requires no arguments. - // ex: ('getAllAssets') - const result = await contract.evaluateTransaction('getAllAssets'); - console.log(`Transaction has been evaluated, result is: ${result.toString()}`); - - // Disconnect from the gateway. - await gateway.disconnect(); - - } catch (error) { - console.error(`Failed to evaluate transaction: ${error}`); - process.exit(1); - } -} - -main(); diff --git a/asset-transfer-basic/application-javascript/registerUser.js b/asset-transfer-basic/application-javascript/registerUser.js index 8320b1bd..16c14535 100644 --- a/asset-transfer-basic/application-javascript/registerUser.js +++ b/asset-transfer-basic/application-javascript/registerUser.js @@ -10,8 +10,12 @@ const { Wallets } = require('fabric-network'); const FabricCAServices = require('fabric-ca-client'); const fs = require('fs'); const path = require('path'); +const enrollAdmin = require('./enrollAdmin'); +const caChaincodeUserRole = 'client'; +const applicationUserId = 'appUser'; +const walletPath = path.join(__dirname, 'wallet'); -async function main() { +async function registerAppUser() { try { // load the network configuration const ccpPath = path.resolve(__dirname, '..', '..', 'test-network', 'organizations', 'peerOrganizations', 'org1.example.com', 'connection-org1.json'); @@ -25,38 +29,37 @@ async function main() { const caURL = ccp.certificateAuthorities['ca.org1.example.com'].url; const ca = new FabricCAServices(caURL); - // Create a new file system based wallet for managing identities. - const walletPath = path.join(__dirname, 'wallet'); + // Create a new file system based wallet for managing identities. ; const wallet = await Wallets.newFileSystemWallet(walletPath); - console.log(`Wallet path: ${walletPath}`); // Check to see if we've already enrolled the user. - const userIdentity = await wallet.get('appUser'); + const userIdentity = await wallet.get(applicationUserId); if (userIdentity) { - console.log('An identity for the user "appUser" already exists in the wallet'); + console.log('An identity for the user '+applicationUserId+' already exists in the wallet'); return; } // Check to see if we've already enrolled the admin user. - const adminIdentity = await wallet.get('admin'); + const adminIdentity = await wallet.get(enrollAdmin.AdminUserId); if (!adminIdentity) { - console.log('An identity for the admin user "admin" does not exist in the wallet'); + console.log('An identity for the admin user does not exist in the wallet'); console.log('Run the enrollAdmin.js application before retrying'); return; } // build a user object for authenticating with the CA const provider = wallet.getProviderRegistry().getProvider(adminIdentity.type); - const adminUser = await provider.getUserContext(adminIdentity, 'admin'); + const adminUser = await provider.getUserContext(adminIdentity, enrollAdmin.AdminUserId); // Register the user, enroll the user, and import the new identity into the wallet. + // if affiliation is specified by client, the affiliation value must be configured in CA const secret = await ca.register({ affiliation: 'org1.department1', - enrollmentID: 'appUser', - role: 'client' + enrollmentID: applicationUserId, + role: caChaincodeUserRole }, adminUser); const enrollment = await ca.enroll({ - enrollmentID: 'appUser', + enrollmentID: applicationUserId, enrollmentSecret: secret }); const x509Identity = { @@ -67,13 +70,14 @@ async function main() { mspId: 'Org1MSP', type: 'X.509', }; - await wallet.put('appUser', x509Identity); - console.log('Successfully registered and enrolled admin user "appUser" and imported it into the wallet'); + await wallet.put(applicationUserId, x509Identity); + console.log('Successfully registered and enrolled user '+applicationUserId +' and imported it into the wallet'); } catch (error) { - console.error(`Failed to register user "appUser": ${error}`); + console.error(`Failed to register user : ${error}`); process.exit(1); } } -main(); +exports.ApplicationUserId = applicationUserId; +exports.RegisterAppUser = registerAppUser;