Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Seed.random().as_string() throws TrytesDecodeError #81

Closed
jinnerbichler opened this issue Oct 15, 2017 · 2 comments
Closed

Seed.random().as_string() throws TrytesDecodeError #81

jinnerbichler opened this issue Oct 15, 2017 · 2 comments
Assignees

Comments

@jinnerbichler
Copy link
Contributor

jinnerbichler commented Oct 15, 2017

Executing Seed.random().as_string() and Seed.random().as_bytes() raises TrytesDecodeError.

Traceback (most recent call last):
  File "/Users/jinnerbi/.pyenv/versions/working_env/lib/python3.5/site-packages/iota/codecs.py", line 147, in decode
    + (self.index[second] * len(self.index))
ValueError: byte must be in range(0, 256)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/jinnerbi/Development/_projects/iota_mail/iota_mail/wallet/api/iota_api.py", line 55, in <module>
    Seed.random().as_string()
  File "/Users/jinnerbi/.pyenv/versions/working_env/lib/python3.5/site-packages/iota/types.py", line 529, in as_string
    return decode(trytes, 'trytes', errors).decode('utf-8', errors)
  File "/Users/jinnerbi/.pyenv/versions/working_env/lib/python3.5/site-packages/iota/codecs.py", line 165, in decode
    'input': input,
iota.codecs.TrytesDecodeError: 'trytes' codec can't decode trytes BT at position 4-5: ordinal not in range(255)

The calculation in lines https://github.com/iotaledger/iota.lib.py/blob/master/iota/codecs.py#L145-L148 exceeds the valid range for bytes.

@todofixthis
Copy link
Contributor

todofixthis commented Oct 16, 2017

Hey @jinnerbichler that is expected behaviour, due to the way PyOTA currently handles the tits -> bytes conversion process.

It's similar in concept to doing this:

In [1]: from secrets import token_bytes

In [2]: token_bytes(81)
Out[2]: b'U\x00h\xd2\xb0\xa6\x05@\xabf\xbbN\xea\xce\xaf\xc9#\xabt\x17\xe4\x0c\x87\xbb\xf2i\xdc\xde\xe6\x12P\x9a\x10\x8flR\x00\xdf=8\xdf\xecv\x1f\xc0\xe34^\xee\x87\x1e\xe53\xc1\x93\x06\xd9\xa2\xfb\x96\xfd\x8d\xfd\x89\xec\xcf\xf4vc\xeb\x0f\x10\xe9\x0b\xd3wTM1\xd1z'

In [3]: token_bytes(81).decode('utf-8')
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-3-240c978e079d> in <module>()
----> 1 token_bytes(81).decode('utf-8')

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 2: invalid start byte

You can take a random sequence of bytes and try to decode them using UTF-8, but it's very unlikely to yield a coherent result (and if it does, that's only by coincidence).

Similarly, creating a random sequence of trytes and trying to decode the result into character data will not work, except in rare cases due to sheer coincidence.

If you are trying to get the raw trytes from the seed, simply cast the Seed object as a string:

In [1]: from iota.crypto.types import Seed

In [2]: from six import text_type

In [3]: text_type(Seed.random())
Out[3]: 'KOBSFUWFPLSAXRRXVMGPSONMLQHNGXSTXYBRZALDOAMCGHESGEXRAXOSMKOUQ9ETAOA9VDAMDIKUMCPSV'

(I admit; the naming convention of some of the methods in TryteString can be a tad confusing; there may be alternate ways we can approach this; I'm open to ideas!)

For more info about converting between trits <-> bytes, see #62

@todofixthis
Copy link
Contributor

I'm going to close this issue and create a related one — I think as_string desperately needs a rename, and once #62 is implemented, all of this conversion stuff will make a whole lot more sense.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants