mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-25 11:05:10 +00:00
Tested with latest chaincode, Improved comments
Signed-off-by: Sijo Cherian <sijo@ibm.com>
This commit is contained in:
parent
311d391586
commit
0ccec02bc2
3 changed files with 32 additions and 33 deletions
|
|
@ -19,8 +19,9 @@ function prettyJSONString(inputString) {
|
||||||
return JSON.stringify(JSON.parse(inputString),null,2);
|
return JSON.stringify(JSON.parse(inputString),null,2);
|
||||||
}
|
}
|
||||||
|
|
||||||
//pre-requisite: fabric-sample test-network setup with two peers and an ordering service,
|
// pre-requisites:
|
||||||
// the companion chaincode is deployed, approved and committed on the channel mychannel
|
// 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() {
|
async function main() {
|
||||||
try {
|
try {
|
||||||
// load the network configuration
|
// load the network configuration
|
||||||
|
|
@ -37,14 +38,15 @@ async function main() {
|
||||||
console.log(`Wallet path: ${walletPath}`);
|
console.log(`Wallet path: ${walletPath}`);
|
||||||
|
|
||||||
|
|
||||||
//Steps
|
// Steps:
|
||||||
//1. register & enroll admin user
|
// 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();
|
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();
|
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);
|
const identity = await wallet.get(registerUser.ApplicationUserId);
|
||||||
if (!identity) {
|
if (!identity) {
|
||||||
console.log('An identity for the user does not exist in the wallet: '+ registerUser.ApplicationUserId);
|
console.log('An identity for the user does not exist in the wallet: '+ registerUser.ApplicationUserId);
|
||||||
|
|
@ -63,7 +65,7 @@ async function main() {
|
||||||
const contract = network.getContract(myChaincodeName);
|
const contract = network.getContract(myChaincodeName);
|
||||||
|
|
||||||
//4. Init a set of asset data on the channel using chaincode 'InitLedger'
|
//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');
|
await contract.submitTransaction('InitLedger');
|
||||||
|
|
||||||
//5. *** Some example transactions are listed below ***
|
//5. *** Some example transactions are listed below ***
|
||||||
|
|
@ -92,6 +94,10 @@ async function main() {
|
||||||
// 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);
|
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 {
|
try {
|
||||||
console.log('\nSubmit Transaction: UpdateAsset asset70');
|
console.log('\nSubmit Transaction: UpdateAsset asset70');
|
||||||
//Non existing asset asset70 should throw Error
|
//Non existing asset asset70 should throw Error
|
||||||
|
|
@ -101,11 +107,6 @@ async function main() {
|
||||||
let errMsg = 'Expected an error on UpdateAsset of non-existing Asset. ';
|
let errMsg = 'Expected an error on UpdateAsset of non-existing Asset. ';
|
||||||
console.log(errMsg + error);
|
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('\n***********************');
|
||||||
|
|
||||||
console.log('Submit Transaction: TransferAsset asset1 from owner Tomoko > owner Tom');
|
console.log('Submit Transaction: TransferAsset asset1 from owner Tomoko > owner Tom');
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const adminUserId = 'admin';
|
const adminUserId = 'admin';
|
||||||
const adminUserPasswd = 'adminpw';
|
const adminUserPasswd = 'adminpw';
|
||||||
|
const walletPath = path.join(__dirname, 'wallet');
|
||||||
|
|
||||||
async function enrollAdminUser() {
|
async function enrollAdminUser() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -28,10 +29,8 @@ async function enrollAdminUser() {
|
||||||
const caTLSCACerts = caInfo.tlsCACerts.pem;
|
const caTLSCACerts = caInfo.tlsCACerts.pem;
|
||||||
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
|
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);
|
||||||
|
|
||||||
// Create a new file system based wallet for managing identities.
|
// Create a new wallet : Note that wallet can be resfor managing identities.
|
||||||
const walletPath = path.join(__dirname, 'wallet');
|
|
||||||
const wallet = await Wallets.newFileSystemWallet(walletPath);
|
const wallet = await Wallets.newFileSystemWallet(walletPath);
|
||||||
console.log(`Wallet path: ${walletPath}`);
|
|
||||||
|
|
||||||
// Check to see if we've already enrolled the admin user.
|
// Check to see if we've already enrolled the admin user.
|
||||||
const identity = await wallet.get(adminUserId);
|
const identity = await wallet.get(adminUserId);
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ const path = require('path');
|
||||||
const enrollAdmin = require('./enrollAdmin');
|
const enrollAdmin = require('./enrollAdmin');
|
||||||
const caChaincodeUserRole = 'client';
|
const caChaincodeUserRole = 'client';
|
||||||
const applicationUserId = 'appUser';
|
const applicationUserId = 'appUser';
|
||||||
|
const walletPath = path.join(__dirname, 'wallet');
|
||||||
|
|
||||||
async function registerAppUser() {
|
async function registerAppUser() {
|
||||||
try {
|
try {
|
||||||
|
|
@ -28,10 +29,8 @@ async function registerAppUser() {
|
||||||
const caURL = ccp.certificateAuthorities['ca.org1.example.com'].url;
|
const caURL = ccp.certificateAuthorities['ca.org1.example.com'].url;
|
||||||
const ca = new FabricCAServices(caURL);
|
const ca = new FabricCAServices(caURL);
|
||||||
|
|
||||||
// Create a new file system based wallet for managing identities.
|
// Create a new file system based wallet for managing identities. ;
|
||||||
const walletPath = path.join(__dirname, 'wallet');
|
|
||||||
const wallet = await Wallets.newFileSystemWallet(walletPath);
|
const wallet = await Wallets.newFileSystemWallet(walletPath);
|
||||||
console.log(`Wallet path: ${walletPath}`);
|
|
||||||
|
|
||||||
// Check to see if we've already enrolled the user.
|
// Check to see if we've already enrolled the user.
|
||||||
const userIdentity = await wallet.get(applicationUserId);
|
const userIdentity = await wallet.get(applicationUserId);
|
||||||
|
|
@ -72,7 +71,7 @@ async function registerAppUser() {
|
||||||
type: 'X.509',
|
type: 'X.509',
|
||||||
};
|
};
|
||||||
await wallet.put(applicationUserId, x509Identity);
|
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) {
|
} catch (error) {
|
||||||
console.error(`Failed to register user : ${error}`);
|
console.error(`Failed to register user : ${error}`);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue