在 Hyperledger fabric v 2.0 中注册用户时出错
Error registering user in Hyperledger fabric v 2.0
我正在尝试 hyperledger fabric 示例 (v2.0)。我的基本网络 运行 很好,我可以注册管理员。然而,当我尝试注册用户时,它抛出了一个错误,如 "Failed to register user "user1": TypeError: gateway.getClient is not a function"
请在下面找到我正在尝试的代码示例
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userIdentity = await wallet.get('user1');
if (userIdentity) {
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 adminIdentity = await wallet.get('admin');
if (!adminIdentity) {
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: false, asLocalhost: true } });
// Get the CA client object from the gateway for interacting with the CA.
const client = gateway.getClient();
const ca = client.getCertificateAuthority();
const adminUser = await client.getUserContext('admin', false);
// 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' }, adminUser);
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: secret });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('user1', x509Identity);
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();
注意:我能够通过容器中的 cli 调用和查询 fabcar 示例,并且它工作正常。当我试图通过 SDK 实现相同时,它抛出错误“无法注册用户 "user1":TypeError:gateway.getClient 不是函数”。任何线索将不胜感激。
您需要从其官方存储库中克隆 registerUser.js 文件的新副本。他们对存储库中的该文件进行了一些修改。因此,只需从他们的官方存储库中获取 registerUser.js 文件的新副本即可。
我正在尝试 hyperledger fabric 示例 (v2.0)。我的基本网络 运行 很好,我可以注册管理员。然而,当我尝试注册用户时,它抛出了一个错误,如 "Failed to register user "user1": TypeError: gateway.getClient is not a function"
请在下面找到我正在尝试的代码示例
'use strict';
const { Gateway, Wallets } = require('fabric-network');
const path = require('path');
const ccpPath = path.resolve(__dirname, '..', '..', 'first-network', 'connection-org1.json');
async function main() {
try {
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
console.log(`Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userIdentity = await wallet.get('user1');
if (userIdentity) {
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 adminIdentity = await wallet.get('admin');
if (!adminIdentity) {
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: false, asLocalhost: true } });
// Get the CA client object from the gateway for interacting with the CA.
const client = gateway.getClient();
const ca = client.getCertificateAuthority();
const adminUser = await client.getUserContext('admin', false);
// 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' }, adminUser);
const enrollment = await ca.enroll({ enrollmentID: 'user1', enrollmentSecret: secret });
const x509Identity = {
credentials: {
certificate: enrollment.certificate,
privateKey: enrollment.key.toBytes(),
},
mspId: 'Org1MSP',
type: 'X.509',
};
await wallet.put('user1', x509Identity);
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();
注意:我能够通过容器中的 cli 调用和查询 fabcar 示例,并且它工作正常。当我试图通过 SDK 实现相同时,它抛出错误“无法注册用户 "user1":TypeError:gateway.getClient 不是函数”。任何线索将不胜感激。
您需要从其官方存储库中克隆 registerUser.js 文件的新副本。他们对存储库中的该文件进行了一些修改。因此,只需从他们的官方存储库中获取 registerUser.js 文件的新副本即可。