From 70dd320c1fe58371e7cb603331d3f068e494b2c3 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Sat, 26 Jul 2014 17:15:12 +0200 Subject: [PATCH] fix protobuf_json while handling enums use error field from trezord --- trezorlib/protobuf_json.py | 8 +++++--- trezorlib/transport_bridge.py | 13 ++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/trezorlib/protobuf_json.py b/trezorlib/protobuf_json.py index 412a885..908b906 100644 --- a/trezorlib/protobuf_json.py +++ b/trezorlib/protobuf_json.py @@ -85,6 +85,8 @@ def pb2json(pb): for field,value in fields: if field.type == FD.TYPE_MESSAGE: ftype = pb2json + elif field.type == FD.TYPE_ENUM: + ftype = lambda x: field.enum_type.values[x].name elif field.type in _ftype2js: ftype = _ftype2js[field.type] else: @@ -109,10 +111,10 @@ _ftype2js = { FD.TYPE_FIXED32: float, FD.TYPE_BOOL: bool, FD.TYPE_STRING: unicode, - #FD.TYPE_MESSAGE: pb2json, #handled specially + #FD.TYPE_MESSAGE handled specially FD.TYPE_BYTES: lambda x: binascii.hexlify(x), FD.TYPE_UINT32: int, - FD.TYPE_ENUM: str, + # FD.TYPE_ENUM: handled specially FD.TYPE_SFIXED32: float, FD.TYPE_SFIXED64: float, FD.TYPE_SINT32: int, @@ -129,7 +131,7 @@ _js2ftype = { FD.TYPE_FIXED32: float, FD.TYPE_BOOL: bool, FD.TYPE_STRING: unicode, - # FD.TYPE_MESSAGE: json2pb, #handled specially + # FD.TYPE_MESSAGE handled specially FD.TYPE_BYTES: lambda x: binascii.unhexlify(x), FD.TYPE_UINT32: int, FD.TYPE_ENUM: lambda x: getattr(types, x), diff --git a/trezorlib/transport_bridge.py b/trezorlib/transport_bridge.py index 77e864e..a0be923 100644 --- a/trezorlib/transport_bridge.py +++ b/trezorlib/transport_bridge.py @@ -11,6 +11,9 @@ import messages_pb2 as proto TREZORD_HOST = 'http://localhost:21324' CONFIG_URL = 'https://mytrezor.com/data/plugin/config_signed.bin' +def get_error(resp): + return ' (error=%d str=%s)' % (resp.status_code, resp.json()['error']) + class BridgeTransport(Transport): def __init__(self, device, *args, **kwargs): @@ -22,11 +25,11 @@ class BridgeTransport(Transport): r = requests.post(TREZORD_HOST + '/configure', data=config) if r.status_code != 200: - raise Exception('trezord: Could not configure') + raise Exception('trezord: Could not configure' + get_error(r)) r = requests.get(TREZORD_HOST + '/enumerate') if r.status_code != 200: - raise Exception('trezord: Could not enumerate devices') + raise Exception('trezord: Could not enumerate devices' + get_error(r)) enum = r.json() if len(enum) < 1: @@ -41,14 +44,14 @@ class BridgeTransport(Transport): def _open(self): r = requests.post(TREZORD_HOST + '/acquire/%s' % self.path) if r.status_code != 200: - raise Exception('trezord: Could not acquire session') + raise Exception('trezord: Could not acquire session' + get_error(r)) resp = r.json() self.session = resp['session'] def _close(self): r = requests.post(TREZORD_HOST + '/release/%s' % self.session) if r.status_code != 200: - raise Exception('trezord: Could not release session') + raise Exception('trezord: Could not release session' + get_error(r)) else: self.session = None @@ -61,7 +64,7 @@ class BridgeTransport(Transport): payload = '{"type": "%s","message": %s}' % (cls, json.dumps(msg)) r = requests.post(TREZORD_HOST + '/call/%s' % self.session, data=payload) if r.status_code != 200: - raise Exception('trezord: Could not write message') + raise Exception('trezord: Could not write message' + get_error(r)) else: self.response = r.json()