Web3j

生成兼容 BIP-39 的乙太坊錢包會導致 IndexOutOfBoundsException?

  • November 1, 2018

嘗試將 android 應用程序配置為使用主種子片語而不是單獨的私鑰生成。

使用新的(ish) generatebip39wallet 會導致 indexoutofbounds 異常。

違規程式碼:

Bip39Wallet bip39Wallet = WalletUtils.generateBip39Wallet(seed, getFilesDir());

日誌文件:

03-22 14:08:19.519 31451-32106/example.crypto.keyapp E/AndroidRuntime: FATAL EXCEPTION: IntentService[WalletGenService]
                                                                  Process: example.crypto.keyapp, PID: 31451
                                                                  java.lang.IndexOutOfBoundsException
                                                                      at java.util.Collections$EmptyList.get(Collections.java:102)
                                                                      at org.web3j.crypto.MnemonicUtils.generateMnemonic(MnemonicUtils.java:61)
                                                                      at org.web3j.crypto.WalletUtils.generateBip39Wallet(WalletUtils.java:95)
                                                                      at example.crypto.keyapp.WalletGenService.onHandleIntent(WalletGenService.java:97)
                                                                      at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:168)
                                                                      at android.os.HandlerThread.run(HandlerThread.java:61)
03-22 14:08:19.643 31451-31653/example.crypto.keyapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8c19460

所以我不太確定發生了什麼。誰能幫忙

編輯:錯誤來自 mnemonicUtils.java 中的 populateWordList

private static List<String> populateWordList() {
   URL url = Thread.currentThread().getContextClassLoader()
           .getResource("en-mnemonic-word-list.txt");
   try {
       return readAllLines(url.toURI().getSchemeSpecificPart());
   } catch (Exception e) {
       return Collections.emptyList();
   }
}

這個錯誤是否試圖說有太多單詞要載入到單個索引中。包含它們的文件“en-mnemonic-word-list.txt”的索引中有超過 2000 個單詞。好奇是否有人解決了這個問題,或者是否可以在不降低安全性的情況下解決。

我也遇到了這個問題。問題似乎是帶有助記詞的文件路徑解析不正確。這個問題的解決方案是稍微改變populateWordList的現有實現:

private static List<String> populateWordList() {
   InputStream inputStream = Thread.currentThread().getContextClassLoader()
           .getResourceAsStream("en-mnemonic-word-list.txt");
   try {
       return readAllLines(inputStream);
   } catch (Exception e) {
       return Collections.emptyList();
   }
}


public static List<String> readAllLines(InputStream inputStream) throws IOException {
   BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
   List<String> data = new ArrayList<>();
   for (String line; (line = br.readLine()) != null; ) {
       data.add(line);
   }
   return data;
}

它適用於 Android 和純 Java。

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