简体   繁体   English

Hyperledger Fabric get User Private Key from Node.js SDK 2.2版本

[英]Hyperledger Fabric get User Private Key from Node.js SDK 2.2 version

I am building a Node.js application that uses the Hyperledger Fabric SDK 2.2 version that interacts with the blockchain.network.我正在构建一个 Node.js 应用程序,它使用与 blockchain.network 交互的 Hyperledger Fabric SDK 2.2 版本。

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.我想要实现的是在检索到钱包身份后,检索从 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);

      }

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.我成功地检索了身份并获得了真正为 X509 的证书类型。

But after that, I am unable to cast or convert the identity to X509 Identity in order to retrieve the private key.但在那之后,我无法将身份转换或转换为 X509 身份以检索私钥。

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".我不是 Node.js 的专家,我被告知这可能无法“投射”。 But I am confused since I have seen that in the documentation of the Hyperledger Fabric Node SDK.但我很困惑,因为我在 Hyperledger Fabric 节点 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:由于 X509 的接口是从 Identity 接口扩展的,因此以下代码有效:

const privateKey = userExists.credentials.privateKey;

Easier than it seems and also tricky.比看起来容易,也很棘手。 Thanks for your time!谢谢你的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关于适用于node.js的Hyperledger Fabric SDK的类型定义 - About type definition of Hyperledger Fabric SDK for node.js Hyperledger Fabric Node.js Sdk 出现错误:访问被拒绝 - Hyperledger Fabric Node.js Sdk got error :access denied Hyperledger Fabric Node.js SDK:离线交易签名问题 - Hyperledger Fabric Node.js SDK: Issue with offline transaction signatures Hyperledger Fabric Version 2: How to query Block Header such as data hash, previous hash by using Fabric Node SDK 2.2 - Hyperledger Fabric Version 2: How to query Block Header such as data hash, previous hash by using Fabric Node SDK 2.2 HyperLedger Fabric 获取区块信息 - 使用 node.js - HyperLedger Fabric Get Block Info - using node.js 如何在Hyperledger Fabric中使用Node JS SDK? - how to use node js SDK in hyperledger Fabric? Hyperledger Fabric 节点 SDK - Hyperledger Fabric Node SDK 如何使用node.js SDK和TLS查询Hyperledger Fabric Go Chaincode? - How to query Hyperledger Fabric Go Chaincode by using the node.js SDK and TLS? Hyperledger Fabric 2.0,无法使用 Node.js SDK 访问用户的 Fabtoken - Hyperledger Fabric 2.0, Can't Access Fabtokens of Users Using Node.js SDK 无法从 Hyperledger Fabric 2.2 注册管理员用户“admin” - Failed to enroll admin user "admin" from Hyperledger Fabric 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM