Web3j – Fixing IndexOutOfBoundsException for BIP-39 Compatible Ethereum Wallet Generation

androidweb3j

Trying to configure android app to use a master seed phrase instead of individual private key generation.

Using the new(ish) generatebip39wallet results in an indexoutofboundsexception.

Offending code:

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

Log file:

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

So I'm not too sure what's going on. Can anyone help

EDIT: The error is coming from populateWordList in mnemonicUtils.java

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();
    }
}

Is this error trying to say that there are too many words to load into a single index. The file which contains them "en-mnemonic-word-list.txt" has over 2000 words in it's index. Curious if anyone has solved this problem or if it's solvable without reducing security.

Best Answer

I also ran into this problem. It seems that the problem is that the file path with the mnemonic words is incorrectly parsed. The solution to this problem is to slightly change the existing implementation of the 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;
}

It works for me on Android and pure Java.

Related Topic