Tested with latest chaincode, Improved comments

Signed-off-by: Sijo Cherian <sijo@ibm.com>
This commit is contained in:
Sijo Cherian 2020-07-12 18:01:43 -04:00
parent 311d391586
commit 0ccec02bc2
3 changed files with 32 additions and 33 deletions

View file

@ -9,8 +9,8 @@
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const fs = require('fs');
const registerUser = require('./registerUser');
const enrollAdmin = require('./enrollAdmin');
const registerUser = require('./registerUser');
const enrollAdmin = require('./enrollAdmin');
const myChannel = 'mychannel';
const myChaincodeName = 'basic';
@ -19,8 +19,9 @@ function prettyJSONString(inputString) {
return JSON.stringify(JSON.parse(inputString),null,2);
}
//pre-requisite: fabric-sample test-network setup with two peers and an ordering service,
// the companion chaincode is deployed, approved and committed on the channel mychannel
// 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
@ -37,14 +38,15 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);
//Steps
//1. register & enroll admin user
// 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, which is used as client identify to make chaincode calls
// 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 enrolled the app user.
// 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);
@ -63,22 +65,22 @@ async function main() {
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.');
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
//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
// ReadAsset returns an asset with given assetID
result = await contract.evaluateTransaction('ReadAsset', 'asset13');
console.log(' result: ' + prettyJSONString(result.toString()) );
@ -89,33 +91,32 @@ async function main() {
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
// 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
//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('Evaluate Transaction: ReadAsset asset1');
result = await contract.evaluateTransaction('ReadAsset', 'asset1');
console.log(' result: ' + prettyJSONString(result.toString()) );
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
// 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();

View file

@ -12,6 +12,7 @@ const fs = require('fs');
const path = require('path');
const adminUserId = 'admin';
const adminUserPasswd = 'adminpw';
const walletPath = path.join(__dirname, 'wallet');
async function enrollAdminUser() {
try {
@ -28,10 +29,8 @@ async function enrollAdminUser() {
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(adminUserId);

View file

@ -13,6 +13,7 @@ const path = require('path');
const enrollAdmin = require('./enrollAdmin');
const caChaincodeUserRole = 'client';
const applicationUserId = 'appUser';
const walletPath = path.join(__dirname, 'wallet');
async function registerAppUser() {
try {
@ -28,10 +29,8 @@ async function registerAppUser() {
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(applicationUserId);
@ -72,7 +71,7 @@ async function registerAppUser() {
type: 'X.509',
};
await wallet.put(applicationUserId, x509Identity);
console.log('Successfully registered and enrolled user '+applicationUserId +'" and imported it into the wallet');
console.log('Successfully registered and enrolled user '+applicationUserId +' and imported it into the wallet');
} catch (error) {
console.error(`Failed to register user : ${error}`);