trezor / python-shamir-mnemonic

MIT License
165 stars 59 forks source link

Sample code for creating hex string #21

Closed kornpow closed 4 years ago

kornpow commented 4 years ago

I am trying to create the hex string to feed in to the cli as the secret.

The way the code does it in default mode:

>>> import os
>>> secret_bytes = os.urandom(128 // 8)
>>> secret_bytes
b'\xb2\x80;i(\x8d\xbe\xc4#\x85\xcbH.r\xc9p'

I know how to do this a few different ways, but they all yield equivalent results as below:

>>> import codecs
>>> codecs.encode(b"how now brown cow","hex")
b'686f77206e6f772062726f776e20636f77'

This always gives this result:

ValueError: The length of the master secret in bytes must be an even number.

Though my result is even also. This is when I looked at the code and found the different form from above.

I know its something simple, but what am I missing?

prusnak commented 4 years ago

1) use secret_bytes.hex() no need to use codecs module 2) error says the length of the master secret in bytes must be an even number, yet you use a secret of 17 bytes which is odd - "how now blue cow" will work

kornpow commented 4 years ago

Thanks for the clarification, it was simple :+1: Just to clarify why it was confusing for me, to hopefully feed back into future documentation:

>>> b'how now brown cow'.hex()
'686f77206e6f772062726f776e20636f77'
>>> b'how now blue cow'.hex()
'686f77206e6f7720626c756520636f77'
>>> len(b'how now blue cow'.hex())
32
>>> len(b'how now brown cow'.hex())
34
>>> len(b'how now blue cow')
16
>>> len(b'how now brown cow')
17

My incorrect phrase, in hex form, looks to be even also.

prusnak commented 4 years ago

I think error wording is pretty spot-on:

The length of the master secret in bytes must be an even number.

If you have an idea of how to make it even more explicit, let us now.