1

How would I restore a bitcoin wallet using the Electrum command line on Linux with a BIP39 mnemonic and custom derivation path?

I need to use m/0' for legacy and m/1' for native segwit?

I cannot find anything in the documentation about BIP39 mnemonics or using a custom derivation path without using the GUI interface of Electrum.

Thanks!

1

1 Answer 1

1

You can't do it from the console, but you can do it through the python API. Stuff like this is done in their test scripts all the time.

Look at the test_bip39_seed_bip84_native_segwit in their github repo for some hints

Another alternative would be to convert your bip39 seed to an xprv and use the restore command line option with the newly minted xprv.

Here's some sample code to convert your mnemonic to an xprv. Obviously change the derivation path and mnemonic seed:

#!/usr/bin/env python3
from mnemonic import Mnemonic
from pycoin.symbols.btc import network as btc

code = ("abandon abandon abandon abandon abandon abandon" + 
        " abandon abandon abandon abandon abandon about")
path = '84H/0H/0H'
mnemo = Mnemonic("english")

xprv = btc.keys.bip32_seed(mnemo.to_seed(code)
           ).subkey_for_path(path).hwif(as_private=True)

print("Issue the Electrum commands")
print(f"\tconvert_xkey {xprv} p2wpkh-p2sh")
print(f"\tconvert_xkey {xprv} p2wpkh")

Do keep in mind that Electrum imports are assumed to be at the Account Extended Key level. This means that importing an xprv at the derivation path m/5' level (random example) will have you a first receive address of m/5'/0/0 and your first change address of m/5'/1/0. Only way around this behavior would require importing the WIF keys one at a time. A simple change to the pycoin code, but there is no simple xprv way to do it.

2
  • Thx for the answer but would I be able to set derivation path or choose legacy or native segwit from the console when restoring the xprv?
    – super.yang
    Sep 23, 2020 at 20:27
  • @super.yang, yes. Basically you will need to write some python with pycoin or mnemonic to go from mnemonic+derivation -> xprv, then you can use the electrum command convert_xkey to convert the xprv to yprv, or zprv. I'll dig up some code and update the comment, but there is no path I can see that won't involve python
    – Dan
    Sep 23, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.