From 5422c40451d8ceb25a6ec9ff39473017bf52d951 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 27 Feb 2018 16:30:32 +0100 Subject: [PATCH] start dropping py2 compatibility code --- trezorlib/ckd_public.py | 11 ++--------- trezorlib/client.py | 24 ++++++------------------ trezorlib/tools.py | 15 ++------------- 3 files changed, 10 insertions(+), 40 deletions(-) diff --git a/trezorlib/ckd_public.py b/trezorlib/ckd_public.py index 1aee54e..9b18788 100644 --- a/trezorlib/ckd_public.py +++ b/trezorlib/ckd_public.py @@ -32,20 +32,13 @@ from . import messages as proto PRIME_DERIVATION_FLAG = 0x80000000 -if sys.version_info < (3,): - def byteindex(data, index): - return ord(data[index]) -else: - def byteindex(data, index): - return data[index] - def point_to_pubkey(point): order = SECP256k1.order x_str = number_to_string(point.x(), order) y_str = number_to_string(point.y(), order) vk = x_str + y_str - return struct.pack('B', (byteindex(vk, 63) & 1) + 2) + vk[0:32] # To compressed key + return struct.pack('B', (vk[63] & 1) + 2) + vk[0:32] # To compressed key def sec_to_public_pair(pubkey): @@ -152,7 +145,7 @@ def deserialize(xpub): node.chain_code = data[13:45] key = data[45:-4] - if byteindex(key, 0) == 0: + if key[0] == 0: node.private_key = key[1:] else: node.public_key = key diff --git a/trezorlib/client.py b/trezorlib/client.py index 6f3e6a6..ec77cea 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -38,15 +38,9 @@ from .coins import coins_slip44 from .debuglink import DebugLink from .protobuf import MessageType -# Python2 vs Python3 -try: - input = raw_input -except NameError: - pass - if sys.version_info.major < 3: - warnings.warn("Trezorlib will stop supporting Python2 in next versions.", DeprecationWarning) + raise Exception("Trezorlib does not support Python 2 anymore.") # try: # from PIL import Image @@ -194,17 +188,11 @@ def session(f): def normalize_nfc(txt): - if sys.version_info[0] < 3: - if isinstance(txt, unicode): - return unicodedata.normalize('NFC', txt) - if isinstance(txt, str): - return unicodedata.normalize('NFC', txt.decode('utf-8')) - else: - if isinstance(txt, bytes): - return unicodedata.normalize('NFC', txt.decode('utf-8')) - if isinstance(txt, str): - return unicodedata.normalize('NFC', txt) - raise ValueError('unicode/str or bytes/str expected') + if isinstance(txt, bytes): + return unicodedata.normalize('NFC', txt.decode('utf-8')) + if isinstance(txt, str): + return unicodedata.normalize('NFC', txt) + raise ValueError('expected str or bytes argument') class BaseClient(object): diff --git a/trezorlib/tools.py b/trezorlib/tools.py index 4ff3b50..5ea9bee 100644 --- a/trezorlib/tools.py +++ b/trezorlib/tools.py @@ -22,17 +22,6 @@ import binascii import struct import sys -if sys.version_info < (3,): - def byteindex(data, index): - return ord(data[index]) - - def iterbytes(data): - return (ord(char) for char in data) -else: - def byteindex(data, index): - return data[index] - iterbytes = iter - def Hash(data): return hashlib.sha256(hashlib.sha256(data).digest()).digest() @@ -52,8 +41,8 @@ def hash_160_to_bc_address(h160, address_type): def compress_pubkey(public_key): - if byteindex(public_key, 0) == 4: - return bytes((byteindex(public_key, 64) & 1) + 2) + public_key[1:33] + if public_key[0] == 4: + return bytes((public_key[64] & 1) + 2) + public_key[1:33] raise ValueError("Pubkey is already compressed")