繁体   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

我正在构建一个 Node.js 应用程序,它使用与 blockchain.network 交互的 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 节点 SDK 的文档中看到了这一点。

任何人都知道任何解决方案,甚至是如何继续的提示?

谢谢

我太复杂了!

由于 X509 的接口是从 Identity 接口扩展的,因此以下代码有效:

const privateKey = userExists.credentials.privateKey;

比看起来容易,也很棘手。 谢谢你的时间!

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM