使用节点 SDK 在 hyperledger fabric 中注册和注册时遇到错误

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

我需要帮助。我正在使用 2Orgs.The 开发自定义 HLF 网络 Fabric 网络与 cli 完美配合,当我想连接节点 sdk 时,出现错误。我成功注册了管理员,但未能注册用户,并且在注册和注册用户 1 时遇到错误。you can see the error and ca container logs。文件如下所述 提前致谢

enrollAdmin.js 文件

'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 文件

'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
            }
        }
    }
}

正如您的错误消息所说,user1 已经在 ca 服务器上注册,因此您必须重新启动网络,或者您必须从 ca 服务器中删除该身份。我建议您重新启动网络并从钱包文件夹中删除所有内容,然后它将起作用。或者用不同的身份试试,比如 user

您似乎已经 "user1" 注册了。尝试删除注册生成的本地文件,或者您应该动态设置用户注册