Improve asset-transfer-basic javascript application workflow + bugfix in createAsset chaincode arg sequence

Adding a sample application workflow to demonstrate the chaincodes, similar to the WIP PR for basic go-application (https://github.com/hyperledger/fabric-samples/pull/211).
Plan is for app.js to replace query.js & invoke.js, to simplify and document-in-code the app workflow of this sample.
Bugfix: `createAsset` param sequence updated to match the chaincode-go `CreateAsset`.

Signed-off-by: Sijo Cherian <sijo@ibm.com>
This commit is contained in:
Sijo Cherian 2020-07-05 22:35:04 -04:00
parent 2f58fb29c4
commit e388a213dd
8 changed files with 172 additions and 31 deletions

3
.gitignore vendored
View file

@ -11,6 +11,9 @@
.project
# omit Go vendor directories
vendor/
# Dependency/Build/IDE directories
node_modules
.vscode
.gradle
.idea

View file

@ -0,0 +1,129 @@
/*
* 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 = 'assets_transfer_basic'; //'basic';
//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
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
//1. register & enroll admin user
enrollAdmin.EnrollAdminUser();
//2. register & enroll application user, which is used as client identify to make chaincode calls
registerUser.RegisterAppUser();
// Check to see if enrolled the app user.
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', 'Tom', 5, 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', 'Tomoko', 5, 350);
try {
console.log('\nSubmit Transaction: UpdateAsset asset70');
//Non existing asset asset70 should throw Error
await contract.submitTransaction('UpdateAsset', 'asset70', 'blue', 'Tomoko', 5, 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
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);
}
}
function prettyJSONString(inputString) {
return JSON.stringify(JSON.parse(inputString),null,2);
}
main();

View file

@ -10,8 +10,10 @@ 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';
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');
@ -32,14 +34,14 @@ async function main() {
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 +50,15 @@ 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;
//main();

View file

@ -9,7 +9,7 @@
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const fs = require('fs');
const registerUser = require('./registerUser');
async function main() {
try {
@ -27,16 +27,15 @@ async function main() {
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const identity = await wallet.get('appUser');
const identity = await wallet.get(registerUser.ApplicationUserId);
if (!identity) {
console.log('An identity for the user "appUser" does not exist in the wallet');
console.log('An identity for the user does not exist in the wallet: '+ registerUser.ApplicationUserId);
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 } });
await gateway.connect(ccp, { wallet, identity: registerUser.ApplicationUserId, discovery: { enabled: true, asLocalhost: true } });
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');

View file

@ -10,8 +10,11 @@ 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'; o
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');
@ -31,32 +34,33 @@ async function main() {
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 +71,15 @@ 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;
//main();

View file

@ -22,7 +22,7 @@ type Asset struct {
Color string `json:"color"`
Size int `json:"size"`
Owner string `json:"owner"`
AppraisedValue int `json:"appraisedValue"`
AppraisedValue int `json:"appraised_value"`
}
// QueryResult structure used for handling result of query

View file

@ -64,7 +64,7 @@ class AssetTransfer extends Contract {
}
// createAsset issues a new asset to the world state with given details.
async createAsset(ctx, id, color, size, owner, appraisedValue) {
async createAsset(ctx, id, color, owner, size, appraisedValue) {
const asset = {
ID: id,
Color: color,
@ -87,7 +87,7 @@ class AssetTransfer extends Contract {
}
// updateAsset updates an existing asset in the world state with provided parameters.
async updateAsset(ctx, id, color, size, owner, appraisedValue) {
async updateAsset(ctx, id, color, owner, size, appraisedValue) {
const exists = await this.assetExists(ctx, id);
if (!exists) {
throw new Error(`The asset ${id} does not exist`);

View file

@ -61,7 +61,7 @@ export class AssetTransfer extends Contract {
}
// createAsset issues a new asset to the world state with given details.
public async createAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
public async createAsset(ctx: Context, id: string, color: string, owner: string, size: number, appraisedValue: number) {
const asset = {
ID: id,
Color: color,
@ -84,7 +84,7 @@ export class AssetTransfer extends Contract {
}
// updateAsset updates an existing asset in the world state with provided parameters.
public async updateAsset(ctx: Context, id: string, color: string, size: number, owner: string, appraisedValue: number) {
public async updateAsset(ctx: Context, id: string, color: string, owner: string, size: number, appraisedValue: number) {
const exists = await this.assetExists(ctx, id);
if (!exists) {
throw new Error(`The asset ${id} does not exist`);