diff --git a/trezorlib/tools.py b/trezorlib/tools.py index 9f874d4b5c..4ff3b50e86 100644 --- a/trezorlib/tools.py +++ b/trezorlib/tools.py @@ -22,6 +22,17 @@ 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() @@ -41,8 +52,8 @@ def hash_160_to_bc_address(h160, address_type): def compress_pubkey(public_key): - if public_key[0] == 4: - return bytes(public_key[64] & 1 + 2) + public_key[1:33] + if byteindex(public_key, 0) == 4: + return bytes((byteindex(public_key, 64) & 1) + 2) + public_key[1:33] raise ValueError("Pubkey is already compressed") @@ -62,7 +73,7 @@ def b58encode(v): """ encode v, which is a string of bytes, to base58.""" long_value = 0 - for c in v: + for c in iterbytes(v): long_value = long_value * 256 + c result = '' @@ -75,7 +86,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 v: + for c in iterbytes(v): if c == 0: nPad += 1 else: