Skip to content
This repository has been archived by the owner on Jun 5, 2019. It is now read-only.

Commit

Permalink
use implicit encoding in encode/decode
Browse files Browse the repository at this point in the history
  • Loading branch information
prusnak committed Sep 6, 2018
1 parent 21326d3 commit 434f279
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tools/encfs_aes_getpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def main():

data = {'label': label,
'bip32_path': bip32_path,
'password_encrypted_hex': binascii.hexlify(passw_encrypted).decode('ascii')}
'password_encrypted_hex': binascii.hexlify(passw_encrypted).decode()}

json.dump(data, open(passw_file, 'w'))

Expand Down
2 changes: 1 addition & 1 deletion trezorctl
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def sign_tx(connect, coin):
)
if txapi.bip115:
prev_output = txapi.get_tx(
binascii.hexlify(prev_hash).decode("utf-8")
binascii.hexlify(prev_hash).decode()
).bin_outputs[prev_index]
prev_blockhash = prev_output.block_hash
prev_blockheight = prev_output.block_height
Expand Down
4 changes: 2 additions & 2 deletions trezorlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def getch():
# skip special keys: read the scancode and repeat
msvcrt.getch()
continue
return key.decode("latin1")
return key.decode()

This comment has been minimized.

Copy link
@matejcik

matejcik Sep 6, 2018

Contributor

I suspect this one is wrong; latin1 can decode bytes 0-255, unlike ascii or the default utf-8. We would have to test on Windows whether this breaks something.

This comment has been minimized.

Copy link
@prusnak

prusnak Sep 6, 2018

Author Member

In that case I think it's much better to throw an exception than to rely on obscure behavior of latin1 encoding.

This comment has been minimized.

Copy link
@matejcik

matejcik Sep 6, 2018

Contributor

I mean, there's nothing "obscure" about this behavior, that's how 8bit encodings work. But sure, we can first check if <128 and only then return implicitly-decoded key. This code should not throw exceptions, it's reading user's keyboard input. Throwing an exception when you type "á" is not what we want.

This comment has been minimized.

Copy link
@prusnak

prusnak Sep 6, 2018

Author Member

To me, skipping invalid character seems much more dangerous than throwing an error.

Anyway, the only place where getch is used is in the Recovery. I suggest to drop the getch completely and use click.getchar(echo=False) instead.

This comment has been minimized.

Copy link
@matejcik

matejcik Sep 6, 2018

Contributor

reasonable. of course, client.py doesn't import click, but this code doesn't really belong there anyway and i'm planning to have it moved out

This comment has been minimized.

Copy link
@matejcik

matejcik Sep 6, 2018

Contributor

This comment has been minimized.

Copy link
@matejcik

matejcik Sep 12, 2018

Contributor

funny story: click.getchar has the same problem! pallets/click#1108

This comment has been minimized.

Copy link
@prusnak

prusnak via email Sep 25, 2018

Author Member


def get_buttonrequest_value(code):
Expand Down Expand Up @@ -499,7 +499,7 @@ def _prepare_sign_tx(self, inputs, outputs):
raise RuntimeError("TX_API not defined")

prev_tx = self.tx_api.get_tx(
binascii.hexlify(inp.prev_hash).decode("utf-8")
binascii.hexlify(inp.prev_hash).decode()
)
txes[inp.prev_hash] = prev_tx

Expand Down
2 changes: 1 addition & 1 deletion trezorlib/debuglink.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def load_device_by_mnemonic(
mnemonic = Mnemonic.normalize_string(mnemonic)

# Convert mnemonic to ASCII stream
mnemonic = mnemonic.encode("utf-8")
mnemonic = mnemonic.encode()

m = Mnemonic("english")

Expand Down
2 changes: 1 addition & 1 deletion trezorlib/firmware.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def validate_firmware(filename):
header_bytes = FirmwareHeader.build(stripped_header)
digest = pyblake2.blake2s(header_bytes).digest()

print("Fingerprint: {}".format(binascii.hexlify(digest).decode("ascii")))
print("Fingerprint: {}".format(binascii.hexlify(digest).decode()))

global_pk = cosi.combine_keys(
vendor.pubkeys[i] for i in range(8) if header.sigmask & (1 << i)
Expand Down
4 changes: 2 additions & 2 deletions trezorlib/protobuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def dump_message(writer, msg):

elif ftype is UnicodeType:
if not isinstance(svalue, bytes):
svalue = svalue.encode("utf-8")
svalue = svalue.encode()

dump_uvarint(writer, len(svalue))
writer.write(svalue)
Expand Down Expand Up @@ -388,7 +388,7 @@ def pformat_value(value: Any, indent: int) -> str:
if mostly_printable(value):
output = repr(value)
else:
output = "0x" + binascii.hexlify(value).decode("ascii")
output = "0x" + binascii.hexlify(value).decode()
return "{} bytes {}{}".format(length, output, suffix)

return repr(value)
Expand Down
2 changes: 1 addition & 1 deletion trezorlib/stellar.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def address_from_public_key(pk_bytes):
# checksum
final_bytes.extend(struct.pack("<H", _crc16_checksum(final_bytes)))

return str(base64.b32encode(final_bytes), "utf-8")
return base64.b32encode(final_bytes).decode()


def address_to_public_key(address_str):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_cardano_get_public_key(self, path, public_key, chain_code):

key = get_public_key(self.client, parse_path(path))

assert hexlify(key.node.public_key).decode("utf8") == public_key
assert hexlify(key.node.chain_code).decode("utf8") == chain_code
assert hexlify(key.node.public_key).decode() == public_key
assert hexlify(key.node.chain_code).decode() == chain_code
assert key.xpub == public_key + chain_code
assert key.root_hd_passphrase == root_hd_passphrase
4 changes: 2 additions & 2 deletions trezorlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def normalize_nfc(txt):
This seems to be bitcoin-qt standard of doing things.
"""
if isinstance(txt, bytes):
txt = txt.decode("utf-8")
return unicodedata.normalize("NFC", txt).encode("utf-8")
txt = txt.decode()
return unicodedata.normalize("NFC", txt).encode()


class CallException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion vendor/trezor-common

0 comments on commit 434f279

Please sign in to comment.