We will create a new registerUser-BankA.js file in the fabric-sample/bankchain directory, as follows:
- Open the file in a code editor, and let's start writing the code. We start by importing all the required dependencies, like this:
/*
* SPDX-License-Identifier: Apache-2.0
*/
'use strict';
const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network');
const path = require('path');
- Next, we set the ccpPath object to the location of the network connection profile for Bank A, like this:
const ccpPath = path.resolve(__dirname, 'connection-banka.json');
- We set the walletPath object to the wallet we created in the previous section, like this:
main();
async function main() {
try {
const walletPath = path.join(process.cwd(), 'wallet-BankA');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
- Next, we check to see if the user is already enrolled in the wallet, by running the following code:
const userExists = await wallet.exists('user1');
if (userExists) {
console.log('An identity for the user "user1" already exists in the wallet');
return;
}
- Next, we check if the admin identity exists in the wallet, by running the following code:
const adminExists = await wallet.exists('admin');
if (!adminExists) {
console.log('An identity for the admin user "admin" does not exist in the wallet');
console.log('Run the enrollAdmin-BankA.js application before retrying');
return;
}
- We create a new gateway to connect to the peer node. We connect using the admin user we created earlier, like this:
const gateway = new Gateway();
await gateway.connect(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } });
- We also create the ca client object for interacting with the CA for the Bank A organization, as follows:
const ca = gateway.getClient().getCertificateAuthority();
const adminIdentity = gateway.getCurrentIdentity();
- Next, we register a new affiliation with the CA. An affiliation is like an intermediate certificate. Typically, it is a department or group, or sub-organization within the organization. In our case, we use department1.
- We create a new affiliation, department1, for our banka organization, and register it with the CA. We use the admin identity to submit the request, like this:
let affiliationService = ca.newAffiliationService();
let affiliation = 'banka.department1'
await affiliationService.create({
name: affiliation,
force: true}, adminIdentity);
- Next, we register a new user, user1, for the department1 affiliation in the banka organization.
The user1 user is registered with the CA, and their certificate and private key are imported to the wallet. A success message is printed on the console, as shown in the following code block:
const secret = await ca.register({ affiliation: 'banka.department1', enrollmentID: 'user1', role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity('bankaMSP', enrollment.certificate, enrollment.key.toBytes());
await wallet.import('user1', userIdentity);
console.log('Successfully registered and enrolled user "user1" and imported it into the wallet');
A catch block catches any errors during execution, like this:
catch (error) {
console.error(`Failed to register user "user1": ${error}`);
process.exit(1);
}