Creating a utility to enroll the admin user

We will need to create a Node.js utility that can be run to register a new admin user for Bank A and a new admin user BankB. This admin user will then be used to register the other users. Let's look at the steps to create this utility:

  1. Create a new file, enrollAdmin-BankA.jsin the fabric-samples/bankchain directory.
  2. Open the file in a code editor, and start writing the code. We start by importing all the required dependencies, as follows:
/*
* SPDX-License-Identifier: Apache-2.0
*/

'use strict';

const FabricCAServices = require('fabric-ca-client');
const { FileSystemWallet, X509WalletMixin } = require('fabric-network');
const fs = require('fs');
const path = require('path');
  • The fabric-ca-client software development kit (SDK) is used to configure identities and user groups in CAs for Bank A and Bank B.
  • We parse the connection-banka.json file for the network connection profile for banka
  • The file is generated as an artifact when we bring the Bankchain network online.
  • The connection profile values are stored in the ccp object after parsing, as shown in the following code block:
const ccpPath = path.resolve(__dirname, 'connection-banka.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);
  1. Next, we create a new ca object that points to the CA for Bank A, as follows:
main();
async function main() {
try {
const caInfo = ccp.certificateAuthorities['ca.banka.example.com'];
const caTLSCACerts = caInfo.tlsCACerts.pem;
const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);

Next, we configure the walletPath object. The walletPath object points to the location of the wallet, where the keys for the admin and the user will be stored. If there is no existing wallet at the location, a new wallet is created.

The name of the wallet for Bank A is wallet-BankA, as shown in the following code block:


const walletPath = path.join(process.cwd(), 'wallet-BankA');
const wallet = new FileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);

An if statement checks if the admin user is already registered and the identity exists in the wallet, as follows:

const adminExists = await wallet.exists('admin');
if (adminExists) {
console.log('An identity for the admin user "admin" already exists in the wallet');
return;
}

Next, we call ca.enroll, to register the admin user with the password adminpw.

A new key pair is generated for the identity and added to the MSP for Bank A (bankaMSP).

Lastly, we import the certificate file and private key for the admin user, and store it in our wallet, by running the following code:

const enrollment = await ca.enroll({ enrollmentID: 'admin', enrollmentSecret: 'adminpw' });
const identity = X509WalletMixin.createIdentity('bankaMSP', enrollment.certificate, enrollment.key.toBytes());
await wallet.import('admin', identity);
console.log('Successfully enrolled admin user "admin" and imported it into the wallet');

A catch block catches any errors during execution, as follows:

catch (error) {
console.error(`Failed to enroll admin user "admin": ${error}`);
process.exit(1);
}
}

That brings us to the end of the utility.

..................Content has been hidden....................

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