enrollAdmin.js - Enroll Admin to ca.example.com

Q

What is the admin enrollment Node.js program enrollAdmin.js?

✍: FYIcenter.com

A

Admin enrollment Node.js program enrollAdmin.js enrolls an user "admin" to ca.example.com.

1. View the enrollAdmin.js source code:

$ cp ~/hyperledger-binaries/fabric-samples/fabcar/javascript

$ more enrollAdmin.js
/*
 * 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');

const ccpPath = path.resolve(__dirname, '..', '..', 'basic-network', 'connection.json');
const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);

async function main() {
    try {

        // Create a new CA client for interacting with the CA.
        const caURL = ccp.certificateAuthorities['ca.example.com'].url;
        const ca = new FabricCAServices(caURL);

        // Create a new file system based wallet for managing identities.
        const walletPath = path.join(process.cwd(), 'wallet');
        const wallet = new FileSystemWallet(walletPath);
        console.log(`Wallet path: ${walletPath}`);

        // Check to see if we've already enrolled the admin user.
        const adminExists = await wallet.exists('admin');
        if (adminExists) {
            console.log('An identity for the admin user "admin" already exists in the wallet');
            return;
        }

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

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

main();

As you can see, the script enrolls a user ID with a secret password with ca.example.com. Then it receives the public certificate and the private key from ca.example.com and save them in a directory call wallet.

2. Run it:

$ node enrollAdmin
Wallet path: /home/fyicenter/hyperledger-binaries/fabric-samples/fabcar/javascript/wallet
Successfully enrolled admin user "admin" and imported it into the wallet

$ ls -l wallet/admin/
-rw-rw-r-- 1 fyicenter 246 Apr  1 01:40 2844345e545c1957df94a6362cedabc81682...-priv
-rw-rw-r-- 1 fyicenter 182 Apr  1 01:40 2844345e545c1957df94a6362cedabc81682...-pub
-rw-rw-r-- 1 fyicenter 986 Apr  1 01:40 admin

 

registerUser.js - Register User to ca.example.com

Reinstall Node.js Required Modules

WYFA (Writing Your First Application)

⇑⇑ Hyperledger Tutorials

2020-01-04, 1952🔥, 0💬