Wallets

如何使用 eth-lightwallet“無種子集”從密鑰庫中提取私鑰

  • February 3, 2018

我在 NodeJS 伺服器上使用 eth-lightwallet。當我嘗試從密鑰庫中獲取私鑰時,我得到了堆棧跟踪。它正在標記這一行“keystore.generateNewAddress(pwDerivedKey);” 我看不出它是如何相關的。當我註釋掉“account.prv_key = keystore.exportPrivateKey(sendingAddr, pwDerivedKey);”行時 它似乎完美無缺。難道我做錯了什麼?錯誤是拋出“沒有種子集”但是當我在調試模式下查看時,種子肯定是設置的。我是 Node 新手,我懷疑它與 Node 的非同步特性有關。我在下麵包含了程式碼和堆棧跟踪。謝謝你。

lightwallet.keystore.deriveKeyFromPassword('mypassword',
   function (err, pwDerivedKey) {
     var seed = lightwallet.keystore.generateRandomSeed();
     var keystore = new lightwallet.keystore(seed, pwDerivedKey);

     keystore.generateNewAddress(pwDerivedKey);
     account.address = keystore.getAddresses()[0];
     account.prv_key = keystore.exportPrivateKey(sendingAddr, pwDerivedKey);
   })



/Users/username/WebstormProjects/eth-project/nodejs-server/node_modules/eth-lightwallet/lib/keystore.js:389
   throw new Error('KeyStore.generateNewAddress: No seed set');
   ^

Error: KeyStore.generateNewAddress: No seed set
   at KeyStore.generateNewAddress (/Users/username/WebstormProjects/eth-project/nodejs-server/node_modules/eth-lightwallet/lib/keystore.js:389:11)
   at /Users/username/WebstormProjects/eth-project/nodejs-server/controllers/ActorService.js:39:20
   at cb (/Users/username/WebstormProjects/eth-project/nodejs-server/node_modules/eth-lightwallet/lib/keystore.js:459:7)
   at /Users/username/WebstormProjects/eth-project/nodejs-server/node_modules/eth-lightwallet/node_modules/scrypt-async/scrypt-async.js:474:9
   at Immediate._onImmediate (/Users/username/WebstormProjects/eth-project/nodejs-server/node_modules/eth-lightwallet/node_modules/scrypt-async/scrypt-async.js:433:11)
   at processImmediate [as _immediateCallback] (timers.js:383:17)

您的處理程序函式可能有一些錯誤阻止處理程序正確執行。您可以檢查是否在您的處理程序中設置了“err”並列印它以查看它到底是什麼錯誤。對我來說,如果我執行您的確切程式碼,我必須在程式碼執行之前刪除對“帳戶”的引用並將“sendingAddr”更改為密鑰庫的第一個地址。確保在您的程式碼上下文中定義了這兩個變數。這是對我有用的程式碼:

lightwallet.keystore.deriveKeyFromPassword('mypassword',
function (err, pwDerivedKey) {
 var seed = lightwallet.keystore.generateRandomSeed();
 var keystore = new lightwallet.keystore(seed, pwDerivedKey);

 keystore.generateNewAddress(pwDerivedKey);
 var address = keystore.getAddresses()[0];
 var prv_key = keystore.exportPrivateKey(address, pwDerivedKey);
 console.log('address and key: ', address, prv_key);
})

引用自:https://ethereum.stackexchange.com/questions/3985