I am building a Node.js application that uses the Hyperledger Fabric SDK 2.2 version that interacts with the blockchain network.
What I want to achieve, is to retrieve the Private Key of the user making the request from the Node.js application after retrieved the identity of the wallet.
// Create a new file system based wallet for managing identities.
const walletPath = path.join(process.cwd(), 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
logger.info(`API/` + requestType + `: Wallet path: ${walletPath}`);
// Check to see if we've already enrolled the user.
const userExists = await wallet.get(in_username);
if (!userExists) {
logger.info('API/` + requestType + `: An identity for the user: '+in_username+' does not exist in the wallet');
//return 'An identity for the user: "'+in_username+'" does not exist in the wallet';
throw new Error('An identity for the user: "'+in_username+'" does not exist in the wallet');
}
if (userExists && userExists.type === 'X.509') {
console.log("userPrivateKey 1 : " + userExists.type);
const privateKey = (userExists as X509Identity).credentials.privateKey;
console.log("userPrivateKey 1 : " + privateKey);
}
So, I have seen from the documentation the above example of retrieving the private key. I retrieve successfully the identity and get the type of the certificate which is truly X509.
But after that, I am unable to cast or convert the identity to X509 Identity in order to retrieve the private key.
At first I am not able to proceed as this error comes up in the row:
const privateKey = (userExists as X509Identity).credentials.privateKey;
Error:
Type assertion expressions can only be used in TypeScript files.
I am not expert in Node.js and I have been informed that this might not be possible to "cast". But I am confused since I have seen that in the documentation of the Hyperledger Fabric Node SDK.
Anyone knows any solution on that, or even a hint on how to continue?
Thank you
I was going too complex!
Since interface of X509 is extended from the Identity interface then the following code is working:
const privateKey = userExists.credentials.privateKey;
Easier than it seems and also tricky. Thanks for your time!