簡體   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