diff --git a/trezorlib/device.py b/trezorlib/device.py index 247974dd0f..f1e9d15286 100644 --- a/trezorlib/device.py +++ b/trezorlib/device.py @@ -17,6 +17,7 @@ # along with this library. If not, see . +from .transport_bridge import BridgeTransport from .transport_hid import HidTransport from .transport_udp import UdpTransport from .transport_webusb import WebUsbTransport @@ -27,6 +28,9 @@ class TrezorDevice(object): def enumerate(cls): devices = [] + for d in BridgeTransport.enumerate(): + devices.append(d) + for d in UdpTransport.enumerate(): devices.append(d) @@ -46,9 +50,11 @@ class TrezorDevice(object): except IndexError: raise Exception("No TREZOR device found") - prefix = path.split(':')[0] + if prefix == BridgeTransport.PATH_PREFIX: + return BridgeTransport.find_by_path(path) + if prefix == UdpTransport.PATH_PREFIX: return UdpTransport.find_by_path(path) diff --git a/trezorlib/transport_bridge.py b/trezorlib/transport_bridge.py index 60a0fff080..cb5ff4121f 100644 --- a/trezorlib/transport_bridge.py +++ b/trezorlib/transport_bridge.py @@ -41,6 +41,8 @@ class BridgeTransport(Transport): BridgeTransport implements transport through TREZOR Bridge (aka trezord). ''' + PATH_PREFIX = 'bridge' + def __init__(self, device): super(BridgeTransport, self).__init__() @@ -50,17 +52,21 @@ class BridgeTransport(Transport): self.response = None def __str__(self): - return self.device['path'] + return '%s:%s' % (self.PATH_PREFIX, self.device['path']) @staticmethod def enumerate(): - r = requests.post(TREZORD_HOST + '/enumerate') - if r.status_code != 200: - raise TransportException('trezord: Could not enumerate devices' + get_error(r)) - return [BridgeTransport(dev) for dev in r.json()] + try: + r = requests.post(TREZORD_HOST + '/enumerate') + if r.status_code != 200: + raise TransportException('trezord: Could not enumerate devices' + get_error(r)) + return [BridgeTransport(dev) for dev in r.json()] + except: + return [] - @staticmethod - def find_by_path(path): + @classmethod + def find_by_path(cls, path): + path = path.replace('%s:' % cls.PATH_PREFIX, '') for transport in BridgeTransport.enumerate(): if path is None or transport.device['path'] == path: return transport