Restore BIP39 mnemonic in Electrum using command line and custom derivation path?

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.


Answers 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)
    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.

Related Questions