From 64fadde3e0144480a32fe3b3a9955891f21af3f1 Mon Sep 17 00:00:00 2001 From: slush0 Date: Thu, 5 May 2016 04:57:14 +0200 Subject: [PATCH] Passes first unit tests (against trezor-emu over transport_pipe). WIP: Issue with console printing of protobuf messages (probably bug in protobuf for python3 itself). WIP: Disabled Bridge transport because of json_protobuf incompatibility with python3. WIP: Broken expectations of returned objects in unit tests --- tests/config.py | 2 +- trezorlib/client.py | 17 ++++++++++------- trezorlib/debuglink.py | 2 +- trezorlib/messages_pb2.py | 2 +- trezorlib/tools.py | 4 ++-- trezorlib/transport.py | 6 +++--- trezorlib/transport_pipe.py | 4 ++-- trezorlib/tx_api.py | 8 +++++++- 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/tests/config.py b/tests/config.py index 0bac30f0b..547555214 100644 --- a/tests/config.py +++ b/tests/config.py @@ -4,7 +4,7 @@ sys.path = ['../',] + sys.path from trezorlib.transport_pipe import PipeTransport from trezorlib.transport_hid import HidTransport from trezorlib.transport_socket import SocketTransportClient -from trezorlib.transport_bridge import BridgeTransport +#from trezorlib.transport_bridge import BridgeTransport devices = HidTransport.enumerate() diff --git a/trezorlib/client.py b/trezorlib/client.py index 1ac90cd82..7d16542c5 100644 --- a/trezorlib/client.py +++ b/trezorlib/client.py @@ -7,14 +7,17 @@ import unicodedata import json import getpass -import tools -import mapping -import messages_pb2 as proto -import types_pb2 as types -import protobuf_json -from debuglink import DebugLink from mnemonic import Mnemonic +from . import tools +from . import mapping +from . import messages_pb2 as proto +from . import types_pb2 as types +from .debuglink import DebugLink + +if sys.version_info[0] == 3: + from io import BytesIO + unicode = lambda s, enc: BytesIO(bytes(s, enc)) # try: # from PIL import Image @@ -44,7 +47,7 @@ def pprint(msg): if isinstance(msg, proto.FirmwareUpload): return "<%s> (%d bytes):\n" % (msg_class, msg_size) else: - return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, msg) + return "<%s> (%d bytes):\n%s" % (msg_class, msg_size, '#FIXME') # msg) def log(msg): sys.stderr.write("%s\n" % msg) diff --git a/trezorlib/debuglink.py b/trezorlib/debuglink.py index a9cbff340..1f208459e 100644 --- a/trezorlib/debuglink.py +++ b/trezorlib/debuglink.py @@ -8,7 +8,7 @@ def button_press(yes_no): print("User pressed", '"y"' if yes_no else '"n"') def pprint(msg): - return "<%s> (%d bytes):\n%s" % (msg.__class__.__name__, msg.ByteSize(), msg) + return "<%s> (%d bytes):\n%s" % (msg.__class__.__name__, msg.ByteSize(), '#FIXME') # msg)) class DebugLink(object): def __init__(self, transport, pin_func=pin_info, button_func=button_press): diff --git a/trezorlib/messages_pb2.py b/trezorlib/messages_pb2.py index 30d24b23f..b7427b6d3 100644 --- a/trezorlib/messages_pb2.py +++ b/trezorlib/messages_pb2.py @@ -14,7 +14,7 @@ from google.protobuf import descriptor_pb2 _sym_db = _symbol_database.Default() -import types_pb2 +from . import types_pb2 DESCRIPTOR = _descriptor.FileDescriptor( diff --git a/trezorlib/tools.py b/trezorlib/tools.py index e01a88771..7b7805b34 100644 --- a/trezorlib/tools.py +++ b/trezorlib/tools.py @@ -33,7 +33,7 @@ __b58base = len(__b58chars) def b58encode(v): """ encode v, which is a string of bytes, to base58.""" - long_value = 0L + long_value = 0 for (i, c) in enumerate(v[::-1]): long_value += (256 ** i) * ord(c) @@ -57,7 +57,7 @@ def b58encode(v): def b58decode(v, length): """ decode v into a string of len bytes.""" - long_value = 0L + long_value = 0 for (i, c) in enumerate(v[::-1]): long_value += __b58chars.find(c) * (__b58base ** i) diff --git a/trezorlib/transport.py b/trezorlib/transport.py index 07db89350..4cdbf24b0 100644 --- a/trezorlib/transport.py +++ b/trezorlib/transport.py @@ -66,7 +66,7 @@ class Transport(object): """ ser = msg.SerializeToString() header = struct.pack(">HL", mapping.get_type(msg), len(ser)) - self._write("##%s%s" % (header, ser), msg) + self._write(b"##" + header + ser, msg) def read(self): """ @@ -110,14 +110,14 @@ class Transport(object): # Align cursor to the beginning of the header ("##") c = read_f.read(1) i = 0 - while c != '#': + while c != b"#": i += 1 if i >= 64: # timeout raise Exception("Timed out while waiting for the magic character") c = read_f.read(1) - if read_f.read(1) != "#": + if read_f.read(1) != b"#": # Second character must be # to be valid header raise Exception("Second magic character is broken") diff --git a/trezorlib/transport_pipe.py b/trezorlib/transport_pipe.py index 2f6789832..9fd47b278 100644 --- a/trezorlib/transport_pipe.py +++ b/trezorlib/transport_pipe.py @@ -26,10 +26,10 @@ class PipeTransport(Transport): raise Exception("Not connected") self.write_fd = os.open(self.filename_write, os.O_RDWR)#|os.O_NONBLOCK) - self.write_f = os.fdopen(self.write_fd, 'w+') + self.write_f = os.fdopen(self.write_fd, 'w+b', 0) self.read_fd = os.open(self.filename_read, os.O_RDWR)#|os.O_NONBLOCK) - self.read_f = os.fdopen(self.read_fd, 'rb') + self.read_f = os.fdopen(self.read_fd, 'rb', 0) def _close(self): self.read_f.close() diff --git a/trezorlib/tx_api.py b/trezorlib/tx_api.py index 2da2f4ad7..187a93a3c 100644 --- a/trezorlib/tx_api.py +++ b/trezorlib/tx_api.py @@ -1,10 +1,16 @@ import binascii -import urllib2 import json from decimal import Decimal # from filecache import filecache, DAY from . import types_pb2 as proto_types +try: + # For Python 3.0 and later + from urllib.request import urlopen +except ImportError: + # Fall back to Python 2's urllib2 + from urllib2 import urlopen + def insight_tx(url, rawdata=False): if not rawdata: try: