trezor: Use composite transport for device detection.

This commit is contained in:
slush 2018-03-03 01:25:19 +01:00
parent 3f1604d609
commit 66e3e60370
2 changed files with 20 additions and 27 deletions

View File

@ -41,12 +41,6 @@ class Trezor(interface.Device):
@property
def _defs(self):
from . import trezor_defs
# Allow using TREZOR bridge transport (instead of the HID default)
trezor_defs.Transport = {
'bridge': trezor_defs.BridgeTransport,
'udp': trezor_defs.UdpTransport,
'hid': trezor_defs.HidTransport,
}[os.environ.get('TREZOR_TRANSPORT', 'hid')]
return trezor_defs
required_version = '>=1.4.0'
@ -122,26 +116,27 @@ class Trezor(interface.Device):
def connect(self):
"""Enumerate and connect to the first USB HID interface."""
for transport in self._defs.Transport.enumerate():
log.debug('transport: %s', transport)
for _ in range(5):
connection = self._defs.Client(transport)
self._override_pin_handler(connection)
self._override_passphrase_handler(connection)
self._verify_version(connection)
transport = self._defs.TrezorDevice.enumerate()
if not transport:
raise interface.NotFoundError('{} not connected'.format(self))
try:
connection.ping(msg='', pin_protection=True) # unlock PIN
return connection
except (self._defs.PinException, ValueError) as e:
log.error('Invalid PIN: %s, retrying...', e)
continue
except Exception as e:
log.exception('ping failed: %s', e)
connection.close() # so the next HID open() will succeed
raise
log.debug('transports: %s', transport)
for _ in range(5):
connection = self._defs.Client(transport[0])
self._override_pin_handler(connection)
self._override_passphrase_handler(connection)
self._verify_version(connection)
raise interface.NotFoundError('{} not connected'.format(self))
try:
connection.ping(msg='', pin_protection=True) # unlock PIN
return connection
except (self._defs.PinException, ValueError) as e:
log.error('Invalid PIN: %s, retrying...', e)
continue
except Exception as e:
log.exception('ping failed: %s', e)
connection.close() # so the next HID open() will succeed
raise
def close(self):
"""Close connection."""

View File

@ -5,6 +5,4 @@
from trezorlib.client import CallException, PinException
from trezorlib.client import TrezorClient as Client
from trezorlib.messages import IdentityType, PassphraseAck, PinMatrixAck
from trezorlib.transport_bridge import BridgeTransport
from trezorlib.transport_hid import HidTransport
from trezorlib.transport_udp import UdpTransport
from trezorlib.device import TrezorDevice