ofek / bit

Bitcoin made easy.
https://ofek.dev/bit/
MIT License
1.25k stars 216 forks source link

Import key using mnemonic #100

Open jkugler opened 4 years ago

jkugler commented 4 years ago

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?

bjarnemagnussen commented 4 years ago

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

jkugler commented 4 years ago

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

bjarnemagnussen commented 4 years ago

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?

jkugler commented 4 years ago

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

sbellem commented 4 years ago

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

Please let me know!

ofek commented 4 years ago

Yes please 😄

ofek commented 4 years ago

@bjarnemagnussen Can you show what you had?

bjarnemagnussen commented 4 years ago

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:

sbellem commented 4 years ago

Thanks @bjarnemagnussen!

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

More precisely:

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:

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.

ofek commented 4 years ago

That sounds logical indeed, thanks! WDYT @bjarnemagnussen?

bjarnemagnussen commented 4 years ago

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.

jkugler commented 4 years ago

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.

bjarnemagnussen commented 4 years ago

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.

ofek commented 4 years ago

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

jkugler commented 4 years ago

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

R4FKEN commented 4 years ago

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/

Johnnyevans32 commented 2 years ago

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

Johnnyevans32 commented 2 years ago

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