Hyperledger Fabric 从 Node.js SDK 2.2 版本获取用户私钥

发布于 2025-01-14 23:48:10 字数 1618 浏览 3 评论 0原文

我正在构建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏月而来 2025-01-21 23:48:10

我太复杂了!

由于 X509 的接口是从 Identity 接口扩展而来的,因此以下代码可以工作:

const privateKey = userExists.credentials.privateKey;

比看起来更容易,但也很棘手。感谢您抽出时间!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文