MR.QUESTION
MR.QUESTION

Reputation: 359

Hyperledger Fabric Import identity in InMemoryWallet

I'm trying to import identities of already enrolled users in InMemoryWallet. The main idea is to import users from external source when application needs it (like DB which stores all necessary information like certificates and private keys for each user) to avoid storing files in FS. And load admin identity when my application started. But have no luck with this.

// 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: While executing I'm getting this error: (node:85462) UnhandledPromiseRejectionWarning: Error: fabric-ca request register failed with errors [[{"code":20,"message":"Authentication failure"}]]

But when I'm doing the same from FileSystemWallet everything working as expected....

Upvotes: 0

Views: 326

Answers (2)

MR.QUESTION
MR.QUESTION

Reputation: 359

The problem was in certificate and private key format. The should be used 'as is'. With all generated line breaks.

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

Upvotes: 0

david_k
david_k

Reputation: 5868

The problem is this line here

wallet.import(adminName, adminIdentity)

it's an async method so you should do

await wallet.import(adminName, adminIdentity)

Upvotes: 1

Related Questions