bit icon indicating copy to clipboard operation
bit copied to clipboard

Import key using mnemonic

Open jkugler opened this issue 6 years ago • 18 comments

I've read over the documentation, so please feel free to point out if I've missed something. :)

I'm trying to import a key into bit using the 12-word mnemonic seed, as generated by https://github.com/trezor/python-mnemonic. I get the seed via:

import mnemonic
mobj = mnemonic.Mnemonic("english")
seed = mobj.to_seed("my words go here")

But, I do not see a way to do that. Am I missing something? Or is that functionality missing?

jkugler avatar Feb 18 '20 01:02 jkugler

Unfortunately not supported as it requires BIP-32 to be implemented.

bjarnemagnussen avatar Feb 20 '20 21:02 bjarnemagnussen

Would a PR be accepted? Or is the feature not desired?

jkugler avatar Feb 20 '20 23:02 jkugler

A PR would be fantastic! I started working on implementing it but never found the time to finish it. I could share it with you if you are interested in some collaboration on it?

bjarnemagnussen avatar Feb 20 '20 23:02 bjarnemagnussen

I am a cryptocurrency newbie, but I'd be happy to take a look!

jkugler avatar Feb 20 '20 23:02 jkugler

I'd like to help if I can, and if this is still on the "roadmap"!

Please let me know!

sbellem avatar May 09 '20 05:05 sbellem

Yes please 😄

ofek avatar May 09 '20 15:05 ofek

@bjarnemagnussen Can you show what you had?

ofek avatar May 09 '20 15:05 ofek

I only have some snippets for the BIP32 part of this issue.

I was working on a BIP32 library implementation. It was forked from bip32utils (which now does not exist anymore) and adjusted to work with coincurve directly. It was however never tested thoroughly enough.

I also have some code that uses it with the wallet in its own class. Just let me know if any more input is helpful!

Files:

bjarnemagnussen avatar May 09 '20 15:05 bjarnemagnussen

Thanks @bjarnemagnussen!

Just out of curiosity, does it make sense to draw inspiration from electrum as well?

More precisely:

  • bip32: https://github.com/spesmilo/electrum/blob/master/electrum/bip32.py
  • mnemonic (bip39): https://github.com/spesmilo/electrum/blob/master/electrum/mnemonic.py

BIP 39 vs Electrum Seed Version System

Electrum follows a slightly different approach though with regards to BIP 39: https://electrum.readthedocs.io/en/latest/seedphrase.html?highlight=bip39 so this may not be what is wanted ...

So, just to be sure I understand clearly, the reference implementation mentioned above (https://github.com/trezor/python-mnemonic) would have to be followed right?

In an attempt to answer my own question, it seems to me that the best approach may be to treat electrum-based mnemonic phrases as a separate case that may or may not be supported. https://bitcointalk.org/index.php?topic=4514745.0 provides useful information in addition to electrum's seed version system docs (mentioned above).

So, perhaps, it is best to first support BIP 39 as is, and perhaps later add support for electrum's approach:

  1. Follow the reference implementation as per https://github.com/trezor/python-mnemonic.
  2. Eventually consider adding support for electrum's seed version system.

This spreadsheet may be useful to provide context ... https://onedrive.live.com/view.aspx?resid=584F122BA17116EE!313&app=Excel

image

Also, to be noted from https://bitcointalk.org/index.php?topic=4514745.msg40763254#msg40763254

Electrum wallet itself is also compatible with BIP39 seeds... so while you can't generate a BIP39 seed within Electrum, you can still use a BIP39 seed (generated elsewhere) with Electrum to restore a wallet.

So, if the above is correct:

  • BIP 39 mnemonic phrase into >>> electrum does work
  • Electrum generated mnemonic phrase into >>> BIP 39 supporting wallets does not work

from the electrum code: https://github.com/spesmilo/electrum/blob/b891d3dc8580976887e3bb61be8b504257a949b6/electrum/mnemonic.py#L146-L148

In any case, this would need to be tested. The above just aims to survey possible implementations to look at.

sbellem avatar May 10 '20 01:05 sbellem

That sounds logical indeed, thanks! WDYT @bjarnemagnussen?

ofek avatar May 10 '20 12:05 ofek

I think initially supporting BIP39 is very beneficial as it allows for better interoperability.

Of course later the support for Electrum's own seed system could always be added.

bjarnemagnussen avatar May 10 '20 14:05 bjarnemagnussen

Just make sure that whatever you do, it's native C, or calling in to C libraries. I believe bip32utils was what I used first, and it was very slow at generating keys because it uses pure python libs. I then switched to pycoin and it was at least 10 times faster, if not more. Also, look in to cython. Even without type hinting, it can provide a large speed up.

My cython experience: https://twitter.com/pedahzur_ak/status/1232071487349067776

I just took two #Python modules, applied #Cython to them (made NO other changes to the code, not even type annotations), and my 100 iteration benchmark just went from 11.8 seconds to .8 seconds. Math-heavy code, but still. Order of a magnitude FTW.

jkugler avatar May 11 '20 15:05 jkugler

It would then be interesting to see how bip32utils performs with the changes above that make use of coincurve instead, which is using CFFI as binding to C.

bjarnemagnussen avatar May 11 '20 15:05 bjarnemagnussen

yikes, Electrum's bip32 code is unreadable...

ofek avatar May 11 '20 16:05 ofek

@bjarnemagnussen Certainly worth a try. I think pycoin uses ctypes, so that is one reasons it's so fast.

jkugler avatar May 11 '20 20:05 jkugler

I would very much appreciate mnemonic -> key support. There are two libraries that already have something like that, maybe it's copy-pasteable? :D Unfortunately, I lack the skills... https://pypi.org/project/bitcoin/ https://pypi.org/project/bitcoinlib/

R4FKEN avatar May 30 '20 19:05 R4FKEN

hi guys! still no way to use mnemonic seeds to generate keys?

Johnnyevans32 avatar Aug 04 '22 23:08 Johnnyevans32

what's the next fast cython library for bitcoin that supports bip39 please?

Johnnyevans32 avatar Aug 04 '22 23:08 Johnnyevans32