1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-22 22:38:08 +00:00

Adding bridge transport to TrezorDevice, using as default transport

This commit is contained in:
slush 2018-02-02 19:17:48 +01:00
parent 562a19c812
commit 03a11450c1
2 changed files with 20 additions and 8 deletions

View File

@ -17,6 +17,7 @@
# along with this library. If not, see <http://www.gnu.org/licenses/>.
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)

View File

@ -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():
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