diff --git a/trezorlib/transport/__init__.py b/trezorlib/transport/__init__.py index 9f60c68d6c..c586e3fcee 100644 --- a/trezorlib/transport/__init__.py +++ b/trezorlib/transport/__init__.py @@ -53,7 +53,7 @@ class Transport(object): raise NotImplementedError @classmethod - def find_by_path(cls, path, prefix_search = True): + def find_by_path(cls, path, prefix_search=False): for device in cls.enumerate(): if path is None or device.get_path() == path \ or (prefix_search and device.get_path().startswith(path)): diff --git a/trezorlib/transport/udp.py b/trezorlib/transport/udp.py index dc688c9ec8..059222377c 100644 --- a/trezorlib/transport/udp.py +++ b/trezorlib/transport/udp.py @@ -53,15 +53,34 @@ class UdpTransport(Transport): host, port = self.device return UdpTransport('{}:{}'.format(host, port+1), self.protocol) - @staticmethod - def enumerate(): + @classmethod + def _try_path(cls, path): + d = cls(path) + try: + d.open() + if d._ping(): + return d + else: + raise TransportException('No TREZOR device found at address {}'.format(path)) + finally: + d.close() + + @classmethod + def enumerate(cls): devices = [] - d = UdpTransport("%s:%d" % (UdpTransport.DEFAULT_HOST, UdpTransport.DEFAULT_PORT)) - d.open() - if d._ping(): - devices.append(d) - d.close() - return devices + default_path = '{}:{}'.format(cls.DEFAULT_HOST, cls.DEFAULT_PORT) + try: + return [cls._try_path(default_path)] + except TransportException: + return [] + + @classmethod + def find_by_path(cls, path, prefix_search=False): + if prefix_search: + return super().find_by_path(path, prefix_search) + else: + path = path.replace('{}:'.format(cls.PATH_PREFIX), '') + return cls._try_path(path) def open(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)