register-user.js - Register New User

Q

How to write a Node.js script to Register New Users to ca.example.com?

✍: FYIcenter.com

A

If you are an administrator on ca.example.com, you can register other new users to ca.example.com.

Here is sample Node.js script, register-user.js, that uses your "admin" identity from the local wallet to register a new user to on ca.example.com.

/* Copyright (c) FYIcenter.com
 */
'use strict';
const FabricCAServices = require('fabric-ca-client');
const { FileSystemWallet, X509WalletMixin, Gateway } = require('fabric-network');

// Build the connection setting object
const fs = require('fs');
const ccp = JSON.parse(fs.readFileSync("connection.json", 'utf8'));

// Get user name from the command line
const user = process.argv[2];

async function main() {
   try {
      // Open local wallet
      const wallet = new FileSystemWallet('wallet');
  
      // Create a new gateway for connecting to our peer node.
      const gateway = new Gateway();
      await gateway.connect(ccp, { wallet, identity: 'admin', discovery: { enabled: false } });
      
      // Get the CA client object from the gateway for interacting with the CA.
      const ca = gateway.getClient().getCertificateAuthority();
      const adminIdentity = gateway.getCurrentIdentity();

      // Register the user, enroll the user, and import the new identity into the wallet.
      const pass = await ca.register({ enrollmentID: user }, adminIdentity);
      console.log("Temporary password received: %s", pass);

      // Enroll the user
      const enrollment = await ca.enroll({ enrollmentID: user, enrollmentSecret: pass });

      // Remove old keys
      if (await wallet.exists(user)) {
         await wallet.delete(user);
      };

      // Import new keys to wallet
      const identity = X509WalletMixin.createIdentity('Org1MSP', 
      enrollment.certificate, enrollment.key.toBytes());
      wallet.import(user, identity);
    
      console.log("User (%s) registered!", user);
   } catch (error) {
      console.error(`Something wrong: ${error}`); 
      process.exit(1);
   }
}

main();

To run this application script on your client system, you need finish the last tutorial to enroll with your admin user and password, and import the certificate and the key info the local wallet.

Now try to register a new user to ca.example.com

$ node register-user.js alice

Temporary password received: JGtVfGJRGcGZ
User (alice) registered!

Note that password generated by ca.example.com for the new user is temporary password, you cannot use it to enroll (sign in) again with the sample password.

You need to save the certificate and key of the new user in the local wallet and use them later to communicate with the Fabric network.

$ ls -l wallet/alice

-rw-rw-r-- 1 fyicenter  246 Apr  1 01:03 7091c900c71b08e2e221fe88fdbd1383072...-priv
-rw-rw-r-- 1 fyicenter  182 Apr  1 01:03 7091c900c71b08e2e221fe88fdbd1383072...-pub
-rw-rw-r-- 1 fyicenter 1110 Apr  1 01:03 alice

User name in ca.example.com must be unique. If you try to register "alice" again, you will get an error:

$ node register-user.js alice

Something wrong: Error: fabric-ca request register failed with errors 
[[{"code":0,"message":"Registration of 'alice' failed: Identity 'alice' is already registered"}]]

 

evaluate-transaction.js - Evaluate Chaincode Transaction

wallet-import.js - Import Keys to Wallet

Interfaces to Communicate with Ledger Peer

⇑⇑ Hyperledger Tutorials

2019-04-22, 348👍, 0💬