简体   繁体   中英

facing error while enroll and register in hyperledger fabric using node SDK

I need help guys. I am developing custom HLF network with 2Orgs.The Fabric network is perfectly working with cli,when i want to connect with node sdk,then error come. i succesfully enrolled the admin but failed to register the user and faces error while enrolling and regitering the user1. you can see the error and ca container logs .Files are mentioned below the Thanks in advance

enrollAdmin.js file

'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 , '..'  , 'connectionprofileOrg1.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 caInfo = ccp.certificateAuthorities['ca.org1.example.com'];
    const caTLSCACerts = caInfo.tlsCACerts.pem;
    const ca = new FabricCAServices(caInfo.url, { trustedRoots: caTLSCACerts, verify: false }, caInfo.caName);

    // 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());
    await 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();

register.js file

'use strict';

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

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

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('user1');
    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('admin');
    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(ccpPath, { wallet, identity: 'admin', discovery: { enabled: true, asLocalhost: true } });

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

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

main();

connectionprofileOrg1.json

{
    "name": "byfn",
    "version": "1.0.0",
    "client": {
        "organization": "Org1",
        "connection": {
            "timeout": {
                "peer": {
                    "endorser": "300"
                }
            }
    }
},
"organizations": {
    "Org1": {
        "mspid": "Org1MSP",
        "peers": [
            "peer0.org1.example.com"
        ],
        "certificateAuthorities": [
            "ca.org1.example.com"
        ]
    }
},
"peers": {
    "peer0.org1.example.com": {
        "url": "grpcs://localhost:7051",
        "tlsCACerts": {
            "path": "crypto-config/peerOrganizations/org1.example.com/tlsca/tlsca.org1.example.com-cert.pem"

        },
        "grpcOptions": {
            "ssl-target-name-override": "peer0.org1.example.com",
            "hostnameOverride": "peer0.org1.example.com"
        }
    }

},
"certificateAuthorities": {
    "ca.org1.example.com": {
        "url": "https://localhost:7054",
        "caName": "ca.example.com",
        "tlsCACerts": {
            "path": "crypto-config/peerOrganizations/org1.example.com/ca/ca.org1.example.com-cert.pem"          
          },
        "httpOptions": {
            "verify": false
            }
        }
    }
}

As your error msg say it the user1 is already registered with ca server so you have to restart the network or you have to remove that identity from ca server. I suggest you restart the network and remove all from the wallet folder then it will work. Or try it with a different identity like user

It seems like you already have "user1" registered. Try to delete the local file generated from your enrollment or you should set your user registration dynamically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM