ttransport.py - electrum - Electrum Bitcoin wallet
HTML git clone https://git.parazyd.org/electrum
DIR Log
DIR Files
DIR Refs
DIR Submodules
---
ttransport.py (3566B)
---
1 from electrum.logging import get_logger
2
3
4 _logger = get_logger(__name__)
5
6
7 class SafeTTransport:
8
9 @staticmethod
10 def all_transports():
11 """Reimplemented safetlib.transport.all_transports so that we can
12 enable/disable specific transports.
13 """
14 # NOTE: the bridge and UDP transports are disabled as they are using
15 # the same ports as trezor
16 try:
17 # only to detect safetlib version
18 from safetlib.transport import all_transports
19 except ImportError:
20 # old safetlib. compat for safetlib < 0.9.2
21 transports = []
22 #try:
23 # from safetlib.transport_bridge import BridgeTransport
24 # transports.append(BridgeTransport)
25 #except BaseException:
26 # pass
27 try:
28 from safetlib.transport_hid import HidTransport
29 transports.append(HidTransport)
30 except BaseException:
31 pass
32 #try:
33 # from safetlib.transport_udp import UdpTransport
34 # transports.append(UdpTransport)
35 #except BaseException:
36 # pass
37 try:
38 from safetlib.transport_webusb import WebUsbTransport
39 transports.append(WebUsbTransport)
40 except BaseException:
41 pass
42 else:
43 # new safetlib.
44 transports = []
45 #try:
46 # from safetlib.transport.bridge import BridgeTransport
47 # transports.append(BridgeTransport)
48 #except BaseException:
49 # pass
50 try:
51 from safetlib.transport.hid import HidTransport
52 transports.append(HidTransport)
53 except BaseException:
54 pass
55 #try:
56 # from safetlib.transport.udp import UdpTransport
57 # transports.append(UdpTransport)
58 #except BaseException:
59 # pass
60 try:
61 from safetlib.transport.webusb import WebUsbTransport
62 transports.append(WebUsbTransport)
63 except BaseException:
64 pass
65 return transports
66 return transports
67
68 def enumerate_devices(self):
69 """Just like safetlib.transport.enumerate_devices,
70 but with exception catching, so that transports can fail separately.
71 """
72 devices = []
73 for transport in self.all_transports():
74 try:
75 new_devices = transport.enumerate()
76 except BaseException as e:
77 _logger.info(f'enumerate failed for {transport.__name__}. error {e}')
78 else:
79 devices.extend(new_devices)
80 return devices
81
82 def get_transport(self, path=None):
83 """Reimplemented safetlib.transport.get_transport,
84 (1) for old safetlib
85 (2) to be able to disable specific transports
86 (3) to call our own enumerate_devices that catches exceptions
87 """
88 if path is None:
89 try:
90 return self.enumerate_devices()[0]
91 except IndexError:
92 raise Exception("No Safe-T mini found") from None
93
94 def match_prefix(a, b):
95 return a.startswith(b) or b.startswith(a)
96 transports = [t for t in self.all_transports() if match_prefix(path, t.PATH_PREFIX)]
97 if transports:
98 return transports[0].find_by_path(path)
99 raise Exception("Unknown path prefix '%s'" % path)