How to import a HD wallet from an extended private key I'd like to export a derived private key from my Electrum HD wallet, and use it as a HD wallet master key in a Mycelium wallet.

I used bx to get the key like this (m/2'/0) (where m is the unencrypted xprv key extracted from default_wallet JSON):

$ cat m | bx hd-private --index 2 --hard \
        | bx hd-to-wif | qrencode -o - | feh -


And scanned the resulting QR code in Mycelium using the "Add Unrelated Account/Scan".

The wallet works, but it's treated as a "single-address" wallet, not a HD wallet. I presumed that the derived xprv can be used as a master key for deriving more keys, since it contains the chaincode. Is that not the case?

Also, the Mycelium "Add Unrelated Account" screen says that


  Scan Bitcoin address, private key or HD-Account.


What format does "HD-Account" mean? Is it WIF?

UPDATE: To make it clear, the key thing I want is to have a HD account in Mycelium, whith a master key derived from my Electrum HD wallet.

The purpose of this is to have only one seed to remember store - the seed of the Electrum wallet. If my phone is lost, or my Mycelium wallet is compromised, I only have to derive it's master key again from my Electrum master key, reconstruct the wallet keys, take the money and run. And since the Mycelium master key is hardened, I don't have to worry about the rest of the tree being compromised, even if the master public key leaks.

Based on answers of Christopher Gurnee and Wizard of Ozzie, I could build a diagram to make it more clear:



UPDATE 2: Updated diagram to match Mycelium's behavior of treating imported extended private keys.
Response to clarified first part

You're pretty close, I suspect you want something simpler like this (and then typing in the xprv you extracted from an Electrum 2.x (unencrypted) wallet file):

bx hd-private --index 2 --hard | qrencode -o - | feh -

In particular, don't include the bx hd-to-wif step, that's probably what's tripping you up.

When you do the import/add-new-account in Mycelium (2.3.0+ required), make sure you're choosing the Advanced option, and then do the scan.

Once you're done, the resulting keys/addresses will be along a path of m/2'/c/i.[1]

If you ever need to do a recovery (i.e. if you lose your phone), you can import the same xprv you imported into Mycelium into a new wallet in Electrum (using the restore wallet option). It may not be documented, but Electrum 2.x will also accept an xprv during a restore, and then it will generate the same c/i path that Mycelium uses for imported xprvs.

I haven't tested any of this though... please test it out before relying on it!



Response to original first part

(this is mostly irrelevant to the clarified question)

To answer the first part of your question:

An Electrum 2.x extended master private key can be directly imported imported into Mycelium for Android (version 2.3.0+), no conversions or path derivations are required. It will result in the same list of addresses being generated in both Electrum 2.x and Mycelium. It turns out you were just overthinking the problem ;-)

This means you can just run qrencode -o - | feh - and paste your extracted xprv directly in.

More details on inter-wallet compatibility can be found here: https://bitcointalk.org/index.php?topic=1000544.0. For your specific case of exporting/importing an xprv, you can look at the third tab in the referenced spreadsheet.

Incidentally, the reason they are compatible isn't obvious. Both use BIP-32, however (as Wizard Of Ozzie already pointed out) Mycelium uses a path of m/44'/0'/a'/c/i, whereas Electrum 2.x uses a path of m/c/i.[1]

What makes them compatible is that Electrum 2.x exports the master key at the root position, however Mycelium assumes that the key was exported at the m/44'/0'/a' position. Mycelium then derives keys by appending an additional c/i path to the imported key, which is the exact same c/i path with Electrum appends to it's root key (m). A diagram might be more clear:

Electrum 2.x:           m/c/i
Mycelium:     m/44'/0'/0'/c/i
                       ^^ key being exported/imported


[1]:
a = account index
c = 0 for external, 1 for internal (change)
i = key/address index
' means hardened