start dropping py2 compatibility code

This commit is contained in:
matejcik 2018-02-27 16:30:32 +01:00 committed by matejcik
parent e1e419485f
commit 5422c40451
3 changed files with 10 additions and 40 deletions

View File

@ -32,20 +32,13 @@ from . import messages as proto
PRIME_DERIVATION_FLAG = 0x80000000 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): def point_to_pubkey(point):
order = SECP256k1.order order = SECP256k1.order
x_str = number_to_string(point.x(), order) x_str = number_to_string(point.x(), order)
y_str = number_to_string(point.y(), order) y_str = number_to_string(point.y(), order)
vk = x_str + y_str 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): def sec_to_public_pair(pubkey):
@ -152,7 +145,7 @@ def deserialize(xpub):
node.chain_code = data[13:45] node.chain_code = data[13:45]
key = data[45:-4] key = data[45:-4]
if byteindex(key, 0) == 0: if key[0] == 0:
node.private_key = key[1:] node.private_key = key[1:]
else: else:
node.public_key = key node.public_key = key

View File

@ -38,15 +38,9 @@ from .coins import coins_slip44
from .debuglink import DebugLink from .debuglink import DebugLink
from .protobuf import MessageType from .protobuf import MessageType
# Python2 vs Python3
try:
input = raw_input
except NameError:
pass
if sys.version_info.major < 3: 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: # try:
# from PIL import Image # from PIL import Image
@ -194,17 +188,11 @@ def session(f):
def normalize_nfc(txt): def normalize_nfc(txt):
if sys.version_info[0] < 3: if isinstance(txt, bytes):
if isinstance(txt, unicode): return unicodedata.normalize('NFC', txt.decode('utf-8'))
return unicodedata.normalize('NFC', txt) if isinstance(txt, str):
if isinstance(txt, str): return unicodedata.normalize('NFC', txt)
return unicodedata.normalize('NFC', txt.decode('utf-8')) raise ValueError('expected str or bytes argument')
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')
class BaseClient(object): class BaseClient(object):

View File

@ -22,17 +22,6 @@ import binascii
import struct import struct
import sys 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): def Hash(data):
return hashlib.sha256(hashlib.sha256(data).digest()).digest() 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): def compress_pubkey(public_key):
if byteindex(public_key, 0) == 4: if public_key[0] == 4:
return bytes((byteindex(public_key, 64) & 1) + 2) + public_key[1:33] return bytes((public_key[64] & 1) + 2) + public_key[1:33]
raise ValueError("Pubkey is already compressed") raise ValueError("Pubkey is already compressed")