fabric-samples/asset-transfer-private-data/application-javascript/caUtils.js
Sijo Cherian e90554f4f8
JS App workflow for private-data asset transfer, Chaincode fixes (#252)
* Adding JS App workflow, Bugfixes in Go Chaincode

Initial impl JS App workflow, Added 2 asset sample, with transfer & delete
Bugfixes: Bug fix in ReadTransferAgreement
Improved logging in Go Chaincode, Improved Chaincode error handling, checks for execution on orgs peer, bug fix, lint issues
use addDiscoveryInterest to scope policy, instead of setEndorsingOrganizations

Signed-off-by: Sijo Cherian <sijo@ibm.com>

* added js app to CI pipeline , and linting matrix

Signed-off-by: Sijo Cherian <sijo@ibm.com>

Co-authored-by: Sijo Cherian <sijo@ibm.com>
2020-08-06 14:23:11 -04:00

97 lines
No EOL
3.4 KiB
JavaScript

/*
* Copyright IBM Corp. All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const adminUserId = 'admin';
const adminUserPasswd = 'adminpw';
const org1UserId = 'appUser1';
const org2UserId = 'appUser2';
const caChaincodeUserRole = 'client';
async function registerOrgUser(appUserId, mspId, wallet, caService) {
try {
// Check to see if we've already enrolled the user.
const userIdentity = await wallet.get(appUserId);
if (userIdentity) {
console.log('An identity for the user ' + appUserId + ' already exists in the wallet');
return;
}
// Check to see if we've already enrolled the admin user.
const adminIdentity = await wallet.get(adminUserId);
if (!adminIdentity) {
console.log('An identity for the admin user does not exist in the wallet');
console.log('Call enrollAdmin for admin user enroll 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, 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 caService.register({
affiliation: 'org2.department1',
enrollmentID: appUserId,
role: caChaincodeUserRole
}, adminUser);
const enrollment = await caService.enroll({
enrollmentID: appUserId,
enrollmentSecret: secret
});
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: mspId,
type: 'X.509',
};
await wallet.put(appUserId, x509Identity);
console.log('Successfully registered and enrolled user ' + appUserId + ' and imported it into the wallet');
} catch (error) {
console.error(`Failed to register user : ${error}`);
process.exit(1);
}
}
async function enrollOrgAdminUser(mspId, wallet, caService) {
try {
// Check to see if we've already enrolled the admin user.
const identity = await wallet.get(adminUserId);
if (identity) {
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 caService.enroll({ enrollmentID: adminUserId, enrollmentSecret: adminUserPasswd });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: mspId,
type: 'X.509',
};
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 : ${error}`);
process.exit(1);
}
}
exports.AdminUserId = adminUserId;
exports.Org1UserId = org1UserId;
exports.Org2UserId = org2UserId;
exports.RegisterOrgUser = registerOrgUser;
exports.EnrollOrgAdminUser = enrollOrgAdminUser;