Bitcoin Forum

Bitcoin => Electrum => Topic started by: Coding Enthusiast on November 06, 2020, 07:54:15 AM



Title: Looking for derivation paths used for different Electrum mnemonic types
Post by: Coding Enthusiast on November 06, 2020, 07:54:15 AM
I just added the recovery option for Electrum mnemonics to The FinderOuter and even though the derivation path could be set manually but it would be simpler if it were set automatically based on mnemonic type selected (or the address type entered).
https://i.imgur.com/1uT42sA.jpg

However I couldn't find what derivation path is used for each mnemonic type above (eg. m/0 for standard) and need help.
I'd appreciate it if you could also include the link to code where it is defined.


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: HCP on November 06, 2020, 08:05:40 AM
I think the code you're looking for is here: https://github.com/spesmilo/electrum/blob/1c07777e135d28fffa157019f90ccdaa002b614e/electrum/base_wizard.py#L407

You can see how during the "setup wizard", it is setting up the various derivation paths for each script_type, for each wallet type. The functions being called to "create" the derivation paths are in the bip32.py (normalize_bip32_derivation() (https://github.com/spesmilo/electrum/blob/1c07777e135d28fffa157019f90ccdaa002b614e/electrum/bip32.py#L367)) and keystore.py (purpose48_derivation() (https://github.com/spesmilo/electrum/blob/1c07777e135d28fffa157019f90ccdaa002b614e/electrum/keystore.py#L972) & bip44_derivation() (https://github.com/spesmilo/electrum/blob/1c07777e135d28fffa157019f90ccdaa002b614e/electrum/keystore.py#L966)) code.


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: Coding Enthusiast on November 06, 2020, 08:26:42 AM
The derivation_and_script_type_dialog seems to be called when the wallet is being created using a hardware wallet not from an Electrum seed phrase. That is why path for eg. 'standard', 'legacy (p2pkh)' is being set to m/44'/0'/0' and I know for a fact that it is m/0 for Electrum mnemonics.


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: HCP on November 07, 2020, 01:10:25 AM
The derivation_and_script_type_dialog seems to be called when the wallet is being created using a hardware wallet not from an Electrum seed phrase.
Oh yes, of course... it also uses it when restoring from a BIP39 mnemonic.

Most likely this is because Electrum mnemonics are "versioned"... so the script type is encoded in the mnemonic itself. Electrum reads that, then defaults to using the encoded script_type and appropriate derivation path automatically.

I've noted that the derivation path is stored in the "keystore" within the wallet file as well:
Code: (Native Segwit Electrum wallet)
    "keystore": {
        "derivation": "m/0'",
        "pw_hash_version": 1,
        "root_fingerprint": "da996cc9",
        "seed": "naive goddess they empty super bone friend visit segment person act palm",
        "type": "bip32",
        "xprv": "zprvAaAs87fpU4gB4oxt9ALBFa6rFj5g4ti1GH71qHHVt8hEcj1N7Ct5CmX1nFB94FewoH5Pmy9dCmRjkTCWzYfsNowk6angDwgdKug8QbMLddf",
        "xpub": "zpub6oADXdCiJSEUHJ3MFBsBci3aokvAUMRrdW2cdfh7SUEDVXLWekCKkZqVdVhW2jXUGr5umrchPNxwfdaQjxkc854SGvaG5Qd8rifoCrGQAnx"
    },


Code: (Legacy Electrum wallet)
    "keystore": {
        "derivation": "m",
        "pw_hash_version": 1,
        "root_fingerprint": "65751984",
        "seed": "comfort history avocado badge genre uphold rely evolve soccer beef defy spoon",
        "type": "bip32",
        "xprv": "xprv9s21ZrQH143K4VH6GX2JjtPu9DtUQ5BJF1VUf59Eexcaez3W5QtTRm4YeKMwL1iP9WUkAgRmw63Ls4nwHjPdTiv6sAU1LqzFCHxKJ2dATSw",
        "xpub": "xpub661MyMwAqRbcGyMZNYZK72LdhFixoXu9cER5TTYrDJ9ZXnNecxChyZP2VaconGXsfh6Vof1vn2qFD5GU1Wgi2VkNxU344HNgSsRQLLiFv8b"
    },


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: NotATether on November 07, 2020, 05:11:43 AM
When someone clicks on the “Restore from seed” option and inputs a BIP39 seed phrase, that’ll restore from derivation paths m/44'/0'/0' for legacy, m/49'/0'/0' for segwit, and m/84'/0'/0' for native segwit, so there are actually three more paths to account for if you’re not just supporting Electrum mnemonics.


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: Coding Enthusiast on November 07, 2020, 12:04:45 PM
I dug around in the code again today and I believe this is where the paths are set:
https://github.com/spesmilo/electrum/blob/1c07777e135d28fffa157019f90ccdaa002b614e/electrum/keystore.py#L984-L1003


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: HCP on November 07, 2020, 12:10:13 PM
Looks good... I would just add that the actual address derivation path most likely has another /0 on the end of those ;)

Might pay to experiment a little with some Electrum seeds and this Electrum compatible fork of Ian Coleman's BIP39 tool: https://github.com/FarCanary/ElectrumSeedTester


Title: Re: Looking for derivation paths used for different Electrum mnemonic types
Post by: Chikito on November 09, 2020, 12:01:18 AM
When someone clicks on the “Restore from seed” option and inputs a BIP39 seed phrase, that’ll restore from derivation paths m/44'/0'/0' for legacy, m/49'/0'/0' for segwit, and m/84'/0'/0' for native segwit, so there are actually three more paths to account for if you’re not just supporting Electrum mnemonics.
Electrum 4.0.4 already had Detect Existing Accounts button this function for Scanning Common Paths without choosing it manually.