InMemoryWallet 中的 Hyperledger Fabric 导入身份

Hyperledger Fabric Import identity in InMemoryWallet

我正在尝试在 InMemoryWallet 中导入已注册用户的身份。主要思想是在应用程序需要时从外部源导入用户(如 DB,它存储所有必要的信息,如每个用户的证书和私钥)以避免将文件存储在 FS 中。并在我的应用程序启动时加载 admin 身份。但是运气不好。

// Here I'm using data from wallet files which was created after enroll of admin user
const cert = '-----BEGIN CERTIFICATE-----certificatestring-----END CERTIFICATE-----'
const pk = '-----BEGIN PRIVATE KEY-----privatekeystring-----END PRIVATE KEY-----'
const adminName = 'admin'

adminIdentity = X509WalletMixin.createIdentity('Org1MSP', cert, pk);
wallet = new InMemoryWallet()

await wallet.import(adminName, adminIdentity)

const adminExists = await wallet.exists(adminName);
console.log('adminExists', adminExists) --> Exists!!!
await gateway.connect(ccpPath, { wallet, identity: adminName, discovery: { enabled: true, asLocalhost: true } });
ca = gateway.getClient().getCertificateAuthority();
adminIdentity = gateway.getCurrentIdentity();

// Trying to create User
const userId = 'someId'
const secret = await ca.register({ affiliation: 'org1.department1', enrollmentID: userId, role: 'client' }, adminIdentity);
const enrollment = await ca.enroll({ enrollmentID: userId, enrollmentSecret: secret });
const userIdentity = X509WalletMixin.createIdentity('Org1MSP', enrollment.certificate, enrollment.key.toBytes());
await wallet.import(userId, userIdentity);

UPD:执行时出现此错误: (node:85462) UnhandledPromiseRejectionWarning: Error: fabric-ca request register failed with errors [[{"code":20,"message":"Authentication failure"}]]

但是当我从 FileSystemWallet 做同样的事情时,一切都按预期工作....

问题出在这行

wallet.import(adminName, adminIdentity)

这是一个异步方法,所以你应该这样做

await wallet.import(adminName, adminIdentity)

问题出在证书和私钥格式上。应该使用 'as is'。 所有生成的换行符。

-----BEGIN CERTIFICATE-----\nMIICAjCCAaigAwIBAgIUBrQKwHNcJLbF52MaYWh29/9UJRgwCgYIKoZIzj0EAwIw\nc...;
...LSdWpObOxeh\r\n-----END PRIVATE KEY-----\r\n;"