.js - Sign in to CA Server


How to write a Node.js script to sign in to the CA Server?



Hyperledger Fabric network is permission based server application. All client applications must use an authenticated user identity to access the network.

User authentication is controlled by the CA (Certificate Authority) server using PKI technology.

Here is sample Node.js script, sign-in.js, that takes a user ID and password and tries to sign in to the CA server.

/* Copyright (c)
'use strict';
const FabricCAServices = require('fabric-ca-client');

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

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

async function main() {
   try {
      // Create a new CA client for interacting with the CA.
      const caURL = ccp.certificateAuthorities[''].url;
      const ca = new FabricCAServices(caURL);

      // Enroll the user and dump the output
      const enrollment = await ca.enroll({ enrollmentID: user, enrollmentSecret: pass });
      console.log("User (%s) authenticated!", user);
   } catch (error) {
      console.error("Failed to authenticate user %s", user);


To run this application script on your client system, you need to make sure that the WYFA (Write Your First Application) network is up and running on your server system.

This script also needs a copy of connection.json which contains IP addresses and port numbers of the WYFA network services. You may need to review them and update them accordingly.

$ cp ../../basic-network/connection.json .
$ more connection.json

    "orderers": {
        "": {
            "url": "grpc://localhost:7050"
    "peers": {
        "": {
            "url": "grpc://localhost:7051"
    "certificateAuthorities": {
        "": {
            "url": "http://localhost:7054",
            "caName": ""

Now try with the default admin user and password pre-defined in the WYFA network:

$ node sign-in.js admin adminpw

   { type: 'EC',
     isPrivate: true,
     isPublic: false,
     curveName: 'secp256r1',
      { name: 'secp256r1',
        keylen: 256,
        curve: [Object],
        G: [Object],
        n: [Object],
        h: [Object],
        oid: undefined,
        info: undefined },
     prvKeyHex: '09424cecea10a8b52364dc32cedbcfce8bb34e100c2a617ef6cd2c73919e5b17',
     pubKeyHex: '0411cfe977bc08a07bbabe88554ef546c98565762819c73e633e453fa76...' } }

User (admin) authenticated!

As you can see, the enroll() method is really an authentication method. After you are authenticated, issues you a key and certificate, using EC (Elliptic Curve) cryotography technology on the "secp256r1" curve.

You can also try it again with a wrong password:

$ node sign-in.js admin junk

error: [FabricCAClientService.js]: Failed to enroll admin, error:%o \
   message=Enrollment failed with errors [[{"code":20,"message":"Authentication failure"}]], 
Failed to authenticate user admin


wallet-import.js - Import Keys to Wallet

FabricCAServices Node.js Class

Interfaces to Communicate with Ledger Peer

⇑⇑ Hyperledger Tutorials

2019-11-08, 1641🔥, 0💬