Hyperledger Fabric 从 Node.js SDK 2.2 版本获取用户私钥
我正在构建一个 Node.js 应用程序,该应用程序使用与区块链网络交互的 Hyperledger Fabric SDK 2.2 版本。
我想要实现的是在检索钱包的身份后检索从 Node.js 应用程序发出请求的用户的私钥。
// 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);
}
所以,我从文档中看到了上面检索私钥的示例。我成功检索了身份并获得了真正的 X509 证书类型。
但之后,我无法将身份转换或转换为 X509 身份以检索私钥。
起初,我无法继续,因为行中出现此错误:
const privateKey = (userExists as X509Identity).credentials.privateKey;
错误:
Type assertion expressions can only be used in TypeScript files.
我不是 Node.js 专家,并且我被告知这可能无法“转换”。但我很困惑,因为我在 Hyperledger Fabric Node SDK 的文档中看到了这一点。
有人知道任何解决方案,甚至知道如何继续吗?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我太复杂了!
由于 X509 的接口是从 Identity 接口扩展而来的,因此以下代码可以工作:
比看起来更容易,但也很棘手。感谢您抽出时间!
I was going too complex!
Since interface of X509 is extended from the Identity interface then the following code is working:
Easier than it seems and also tricky. Thanks for your time!