IBM/raft-fabric-sample

Error trying add new user

Closed this issue · 5 comments

Hi @horeaporutiu ,

I tried to run the registerUser.js and I got an error because the connection.yaml is not a json file. So I modified the code, to solve this issue, but now I'm getting the following error:

Failed to register user userError: Common connection profile is missing this client's organization and certificate authority

The problem is occurring during the gateway connection, when the connection file information is being used.

My question is: Do I have to create a new certificate to the new user or this is not the real error cause?

It follows the registerUser code.

Thanks in advance.

`

const { FileSystemWallet, Gateway, X509WalletMixin } = require('fabric-network');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

// capture network variables from config.json
const configPath = path.join(process.cwd(), 'config.json');
const configJSON = fs.readFileSync(configPath, 'utf8');
const config = JSON.parse(configJSON);
var connection_file = config.connection_file;
var appAdmin = config.appAdmin;
var orgMSPID = config.orgMSPID;
var userName = config.userName;
var gatewayDiscovery = config.gatewayDiscovery;

const ccpPath = path.join(process.cwd(), connection_file);
let fileContents = fs.readFileSync(ccpPath, 'utf8');
let connectionFile = yaml.safeLoad(fileContents);

async function main() {
    try {

        // 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 user.
        const userExists = await wallet.exists(userName);
        if (userExists) {
            console.log('An identity for the user "user1" already exists in the wallet');
            return;
        }

        // Check to see if we've already enrolled the admin user.
        const adminExists = await wallet.exists(appAdmin);
        if (!adminExists) {
            console.log('An identity for the admin user "admin" does not exist in the wallet');
            console.log('Run the enrollAdmin.js application before retrying');
            return;
        }

        // Create a new gateway for connecting to our peer node.
        const gateway = new Gateway();
        await gateway.connect(connectionFile, { wallet, identity: appAdmin, discovery: gatewayDiscovery });

        // 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 secret = await ca.register({ affiliation: 'org1.department1', enrollmentID: userName, role: 'client' }, adminIdentity);
        const enrollment = await ca.enroll({ enrollmentID: userName, enrollmentSecret: secret });
        const userIdentity = X509WalletMixin.createIdentity(orgMSPID, enrollment.certificate, enrollment.key.toBytes());
        wallet.import(userName, userIdentity);
        console.log('Successfully registered and enrolled admin user ' + userName + ' and imported it into the wallet');

    } catch (error) {
        console.error('Failed to register user ' + userName + error);
        process.exit(1);
    }
}

main();

`

so i haven't tested this pattern for register user @guilhermelionzo

I will take out that part of the code. Usually what happens is that you enroll an admin for an org, and use reference that admin when you register other users for that org

that wasn't really a part of this code pattern, since i mostly just wanted to show raft, and creating a five ordering nodes

closing due to inactivity

Can this issue be addressed?, i am facing the exact issue while trying to register a user. What could be the issue?. should we updtae the connection profile?. I see all things are correct. Any insight will be highly helpful.

Ok, found the solution, we need to pass a caname in getCertificateAuthority() function, which solved the issue.