使用节点SDK在超账本结构中注册和登记时面临错误。

问题描述 投票:0回答:2

我需要帮助,伙计们,我正在用2个Orgs开发定制的HLF网络,Fabric网络与cli完美地工作,当我想与node sdk连接时,就会出现错误。我正在开发自定义HLF网络与2Orgs.The Fabric网络是完美的工作与Cli,当我想连接与node sdk,然后错误来了。我成功地注册管理员,但未能注册用户和面临错误,而注册和注册用户1。你可以看到这个错误ca集装箱日志.文件是提到下面的感谢提前

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
            }
        }
    }
}
node.js hyperledger-fabric hyperledger-fabric-ca
2个回答
0
投票

你的错误msg说,用户1已经在ca服务器上注册了,所以你必须重新启动网络,或者你必须从ca服务器上删除该身份。我建议你重启网络并删除钱包文件夹中的所有内容,然后就可以了。或者换个身份试试,比如用户


0
投票

好像你已经注册了 "user1"。试着删除您注册时生成的本地文件,或者您应该动态地设置您的用户注册。

© www.soinside.com 2019 - 2024. All rights reserved.