Creating a utility to register users

We will create a new registerUser-BankA.js file in the fabric-sample/bankchain directory, as follows:

  1. 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');
  1. 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');
  1. 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}`);
  1. 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;
}
  1. 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;
}
  1. 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 } });
  1. 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();
  1. 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.
  2. 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);
  1. 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);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.195