trezor / python-mnemonic

:snake: Mnemonic code for generating deterministic keys, BIP39
https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
MIT License
837 stars 365 forks source link

Incompatible with Python3 #28

Closed posita closed 8 years ago

posita commented 9 years ago

Currently, this package fails to run with Python 3, due at least to the use of longs in the secretsharing package:

Traceback (most recent call last):
  File "test_mnemonic.py", line 31, in <module>
    from mnemonic import Mnemonic
  File "/.../posita/python-mnemonic/mnemonic/__init__.py", line 2, in <module>
    from .shamir import Shamir
  File "/.../posita/python-mnemonic/mnemonic/shamir.py", line 23, in <module>
    from secretsharing import secret_int_to_points, points_to_secret_int
  File "/.../posita/python-mnemonic/.tox/py34/lib/python3.4/site-packages/secretsharing/__init__.py", line 12, in <module>
    from .sharing import *
  File "/.../posita/python-mnemonic/.tox/py34/lib/python3.4/site-packages/secretsharing/sharing.py", line 14, in <module>
    from .primes import get_large_enough_prime
  File "/.../posita/python-mnemonic/.tox/py34/lib/python3.4/site-packages/secretsharing/primes.py", line 35, in <module>
    STANDARD_PRIMES = calculate_mersenne_primes() + [
  File "/.../posita/python-mnemonic/.tox/py34/lib/python3.4/site-packages/secretsharing/primes.py", line 25, in calculate_mersenne_primes
    prime = long(1)
NameError: name 'long' is not defined
prusnak commented 9 years ago

Fixed this issue in e940cb661b623b9f85fac4e7896171f907927bf7. Another one in 5fdb048b2e077fa4c378127d3c8d1b8628d1c43a.

There is still (at least) one to go:

$ python3 test_mnemonic.py

...

ERROR: test_to_entropy (__main__.MnemonicTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_mnemonic.py", line 222, in test_to_entropy
    self.assertEqual(m.to_entropy(m.to_mnemonic(d).split()), d)
  File "/home/stick/work/trezor/python-mnemonic/mnemonic/mnemonic.py", line 118, in to_entropy
    hashBits = list(itertools.chain.from_iterable(( [ ord(c) & (1 << (7 - i)) != 0 for i in range(8) ] for c in hashBytes )))
  File "/home/stick/work/trezor/python-mnemonic/mnemonic/mnemonic.py", line 118, in <genexpr>
    hashBits = list(itertools.chain.from_iterable(( [ ord(c) & (1 << (7 - i)) != 0 for i in range(8) ] for c in hashBytes )))
  File "/home/stick/work/trezor/python-mnemonic/mnemonic/mnemonic.py", line 118, in <listcomp>
    hashBits = list(itertools.chain.from_iterable(( [ ord(c) & (1 << (7 - i)) != 0 for i in range(8) ] for c in hashBytes )))
TypeError: ord() expected string of length 1, but int found
posita commented 9 years ago

If one wants to avoid introducing dependencies on compatibility toolkits like python-future, I found this helpful.

prusnak commented 8 years ago

One can see build failures here: https://travis-ci.org/trezor/python-mnemonic

I would love to see a Python 3 expert fixing them while not breaking Python 2 and at the same time not introducing a lot of unnecessary changes.

prusnak commented 8 years ago

I fixed all issues in mnemonic code and pushed 0.14 out.

SSSS module is still not ready yet, but as it is unused in any production code I am closing this issue.

You are welcome to help with fixing the issue, though (just run python3 test_shamir.py) to see the errrors.

posita commented 8 years ago

@prusnak, should I open another issue to track SSSS -> Python 3 migration specifically?

prusnak commented 8 years ago

No. Only pull requests are allowed. :-)

prusnak commented 8 years ago

Shamir fixed in https://github.com/trezor/python-mnemonic/commit/f9f7720ab79b07a86e0c10071d56d2a3ed5ab27c

Released as 0.15