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

Move to Python 3 only code #229

Merged
merged 4 commits into from
Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addons:
- libusb-1.0-0-dev

python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
Expand Down
11 changes: 2 additions & 9 deletions trezorlib/ckd_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,13 @@

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):
Expand Down Expand Up @@ -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
Expand Down
38 changes: 12 additions & 26 deletions trezorlib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@
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
Expand Down Expand Up @@ -194,17 +188,13 @@ def wrapped_f(*args, **kwargs):


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')
'''
Normalize message to NFC and return bytes suitable for protobuf.
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')


class BaseClient(object):
Expand Down Expand Up @@ -614,13 +604,11 @@ def int_to_big_endian(value):
@expect(proto.EthereumMessageSignature)
def ethereum_sign_message(self, n, message):
n = self._convert_prime(n)
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
message = normalize_nfc(message).encode('utf-8')
message = normalize_nfc(message)
return self.call(proto.EthereumSignMessage(address_n=n, message=message))

def ethereum_verify_message(self, address, signature, message):
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
message = normalize_nfc(message).encode('utf-8')
message = normalize_nfc(message)
try:
resp = self.call(proto.EthereumVerifyMessage(address=address, signature=signature, message=message))
except CallException as e:
Expand Down Expand Up @@ -685,8 +673,7 @@ def change_pin(self, remove=False):
@expect(proto.MessageSignature)
def sign_message(self, coin_name, n, message, script_type=proto.InputScriptType.SPENDADDRESS):
n = self._convert_prime(n)
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
message = normalize_nfc(message).encode('utf-8')
message = normalize_nfc(message)
return self.call(proto.SignMessage(coin_name=coin_name, address_n=n, message=message, script_type=script_type))

@expect(proto.SignedIdentity)
Expand Down Expand Up @@ -839,8 +826,7 @@ def mosaic_supply_change_to_proto(mosaic_supply_change):
return self.call(msg)

def verify_message(self, coin_name, address, signature, message):
# Convert message to UTF8 NFC (seems to be a bitcoin-qt standard)
message = normalize_nfc(message).encode('utf-8')
message = normalize_nfc(message)
try:
resp = self.call(proto.VerifyMessage(address=address, signature=signature, message=message, coin_name=coin_name))
except CallException as e:
Expand Down
19 changes: 4 additions & 15 deletions trezorlib/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,6 @@
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()
Expand All @@ -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")


Expand All @@ -73,7 +62,7 @@ def b58encode(v):
""" encode v, which is a string of bytes, to base58."""

long_value = 0
for c in iterbytes(v):
for c in v:
long_value = long_value * 256 + c

result = ''
Expand All @@ -86,7 +75,7 @@ def b58encode(v):
# Bitcoin does a little leading-zero-compression:
# leading 0-bytes in the input become leading-1s
nPad = 0
for c in iterbytes(v):
for c in v:
if c == 0:
nPad += 1
else:
Expand Down