Search code examples
hyperledger-fabrichyperledger-fabric-caibm-blockchain

Getting error as : Error: Client.createUser parameter 'opts mspid' is required


I am trying to use the IBM blockchain. I have created the setup on IBM blockchain, now I am trying to register the user though node-SDK from on the IBM blockchain, however, I am getting an issue when I am trying to register the app-admin to IBM blockchain.

Please help me to find out the issue or let me know where I am doing wrong.

Connection file.

{
    "name": "mychannel",
    "description": "Network on IBP v2",
    "version": "1.0.0",
    "client": {
        "organization": "org1msp"
    },
    "organizations": {
        "org1msp": {
            "mspid": "org1msp",
            "peers": [
                "n77513f-peer0org1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051"
            ],
            "certificateAuthorities": [
                "n77513f-org1ca.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7054"
            ]
        },
        "org2MSP": {
            "mspid": "org2MSP",
            "peers": [
                "n77513f-peer0org2.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051"
            ]
        }
    },
    "orderers": {
        "n77513f-ordererorg1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050": {
            "url": "grpcs://n77513f-ordererorg1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050",
            "tlsCACerts": {
            }
        },
        "n77513f-ordererorg2.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050": {
            "url": "grpcs://n77513f-ordererorg2.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050",
            "tlsCACerts": {
            }
        },
        "n77513f-ordererorg3.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050": {
            "url": "grpcs://n77513f-ordererorg3.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050",
            "tlsCACerts": {
            }
        },
        "n77513f-ordererorg4.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050": {
            "url": "grpcs://n77513f-ordererorg4.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050",
            "tlsCACerts": {
            }
        },
        "n77513f-ordererorg5.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050": {
            "url": "grpcs://n77513f-ordererorg5.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7050",
            "tlsCACerts": {
            }
        }
    },
    "peers": {
        "n77513f-peer0org1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051": {
            "url": "grpcs://n77513f-peer0org1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051",
            "tlsCACerts": {
            },
            "grpcOptions": {
                "ssl-target-name-override": "n77513f-peer0org1.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud"
            }
        },
        "n77513f-peer0org2.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051": {
            "url": "grpcs://n77513f-peer0org2.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7051",
            "tlsCACerts": {
            },
            "grpcOptions": {
                "ssl-target-name-override": "n77513f-peer0org1retail.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud"
            }
        }
    },
    "certificateAuthorities": {
        "n77513f-org1ca.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7054": {
            "url": "https://n77513f-org1ca.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7054",
            "caName": "ca",
            "tlsCACerts": {
            }
        }
    }
}

my config file.

{
  "connection_file": "connection.json",
  "appAdmin": "admin",
  "appAdminSecret": "org1capw",
  "orgMSPIiD": "dFarmMSP",
  "caName": "n77513f-org1ca.mycluster-108893-93897c9faea121bae05cda32f6fe4dd1-0000.us-south.containers.appdomain.cloud:7054",
  "channelname": "mychannle",
  "username":" ",
  "role":" ",
  "chaincodeName":"dfarmsm",
  "gatewayDiscovery": { "enabled": true, "asLocalhost": false }

}

node.js code for register the admin

'use strict';

const FabricCAServices = require('fabric-ca-client');
const {
    FileSystemWallet,
    Gateway,
    X509WalletMixin
} = require('fabric-network');
const fs = require('fs');
const path = require('path');

var MongoClient = require('mongodb').MongoClient;
var Client = require('node-rest-client').Client;

var client = new Client();


const configPath = path.resolve(__dirname, '..', 'config', 'Config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config1 = JSON.parse(configJSON);

var connection_file = config1.connection_file;
var appAdmin = config1.appAdmin;
var gatewayDiscovery = config1.gatewayDiscovery;
var appAdminSecret = config1.appAdminSecret;
var orgMSPID = config1.orgMSPID;
var caName = config1.caName;

const ccpPath = path.resolve(__dirname, '..', 'config', 'connection.json');

const ccpJSON = fs.readFileSync(ccpPath, 'utf8');
const ccp = JSON.parse(ccpJSON);

let response = {};

class FabricClientRegister {
    constructor() {
         console.log("called constructer");
    }
    async RegisterAdmin() {
        try {


            const setAsyncTimeout = (cb, timeout = 0) => new Promise(resolve => {
                setTimeout(() => {
                    cb();
                    resolve();
                }, timeout);
            });


            // Create a new CA client for interacting with the CA.
            const caURL = ccp.certificateAuthorities[caName].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}`);
            // logger.log(`Wallet path: ${walletPath}`);

            // Check to see if we've already enrolled the admin user.
            const adminExists = await wallet.exists(appAdmin);
            // console.log('here is admin exit')
            // console.log('AdminExits', adminExists);
            if (adminExists) {
                response.data = null;
                response.httpstatus = 400;
                response.message = `An identity for the admin user ${appAdmin} already exists in the wallet`
                // console.log('res', response)
                return response;
            }

            // Enroll the admin user and import the new identity into the wallet.
            const enrollment = await ca.enroll({
                enrollmentID: appAdmin,
                enrollmentSecret: appAdminSecret
            });

            const identity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
            wallet.import(appAdmin, identity);
            // logger.log('msg: Successfully enrolled admin user ' + appAdmin + ' and imported it into the wallet');
            console.log('msg: Successfully enrolled admin user ' + appAdmin + ' and imported it into the wallet');
            response.data = identity;
            response.httpstatus = 200;
            response.message = "admin enrolled successfully"
            return response;


        } catch (error) {
            response.error = error;
            response.httpstatus = 500;
            response.message = "Failed to enroll admin due to above error";
            return response;
        }
    };

Let me know if anything I missing. and thanks for advance.


Solution

  • The value of the mspid you are passing in to createIdentity is blank. In your config you define the msp id as

    "orgMSPIiD": "dFarmMSP",
    

    whereas in your code you try to retrieve it as

    var orgMSPID = config1.orgMSPID;
    

    (Note the difference between iD vs ID)

    so basically the value of orgMSPID will be undefined.

    However it would be useful for the createIdentity to report an error as no mspid is given so I would suggest you raise a jira for the fabric-sdk-node project (FABN) at https://jira.hyperledger.org and suggest that createIdentity should check for a undefined mspid on the input parameters