fix a bug with hw devices.

if a device is unplugged and then replugged before we notice (via scan_devices) then it will get into an unusable state, throwing all kinds of low level exceptions when we don't expect it. affects ledger, keepkey, dbb, but for some reason not trezor.
This commit is contained in:
SomberNight 2018-03-18 03:54:28 +01:00
parent 2ce2b36846
commit 85b36e027f
5 changed files with 31 additions and 8 deletions

View file

@ -552,7 +552,7 @@ class DeviceMgr(ThreadJob, PrintError):
with self.lock: with self.lock:
connected = {} connected = {}
for client, pair in self.clients.items(): for client, pair in self.clients.items():
if pair in pairs: if pair in pairs and client.has_usable_connection_with_device():
connected[client] = pair connected[client] = pair
else: else:
disconnected_ids.append(pair[1]) disconnected_ids.append(pair[1])

View file

@ -82,6 +82,13 @@ class DigitalBitbox_Client():
def is_paired(self): def is_paired(self):
return self.password is not None return self.password is not None
def has_usable_connection_with_device(self):
try:
self.dbb_has_password()
except BaseException:
return False
return True
def _get_xpub(self, bip32_path): def _get_xpub(self, bip32_path):
if self.check_device_dialog(): if self.check_device_dialog():
return self.hid_send_encrypt(b'{"xpub": "%s"}' % bip32_path.encode('utf8')) return self.hid_send_encrypt(b'{"xpub": "%s"}' % bip32_path.encode('utf8'))

View file

@ -113,6 +113,14 @@ class KeepKeyClientBase(GuiMixin, PrintError):
def is_pairable(self): def is_pairable(self):
return not self.features.bootloader_mode return not self.features.bootloader_mode
def has_usable_connection_with_device(self):
try:
res = self.ping("electrum pinging device")
assert res == "electrum pinging device"
except BaseException:
return False
return True
def used(self): def used(self):
self.last_operation = time.time() self.last_operation = time.time()

View file

@ -57,6 +57,13 @@ class Ledger_Client():
def i4b(self, x): def i4b(self, x):
return pack('>I', x) return pack('>I', x)
def has_usable_connection_with_device(self):
try:
self.dongleObject.getFirmwareVersion()
except BaseException:
return False
return True
def test_pin_unlocked(func): def test_pin_unlocked(func):
"""Function decorator to test the Ledger for being unlocked, and if not, """Function decorator to test the Ledger for being unlocked, and if not,
raise a human-readable exception. raise a human-readable exception.
@ -513,13 +520,6 @@ class LedgerPlugin(HW_PluginBase):
if self.libraries_available: if self.libraries_available:
self.device_manager().register_devices(self.DEVICE_IDS) self.device_manager().register_devices(self.DEVICE_IDS)
def btchip_is_connected(self, keystore):
try:
self.get_client(keystore).getFirmwareVersion()
except Exception as e:
return False
return True
def get_btchip_device(self, device): def get_btchip_device(self, device):
ledger = False ledger = False
if (device.product_key[0] == 0x2581 and device.product_key[1] == 0x3b7c) or (device.product_key[0] == 0x2581 and device.product_key[1] == 0x4b7c) or (device.product_key[0] == 0x2c97): if (device.product_key[0] == 0x2581 and device.product_key[1] == 0x3b7c) or (device.product_key[0] == 0x2581 and device.product_key[1] == 0x4b7c) or (device.product_key[0] == 0x2c97):

View file

@ -119,6 +119,14 @@ class TrezorClientBase(GuiMixin, PrintError):
def is_pairable(self): def is_pairable(self):
return not self.features.bootloader_mode return not self.features.bootloader_mode
def has_usable_connection_with_device(self):
try:
res = self.ping("electrum pinging device")
assert res == "electrum pinging device"
except BaseException:
return False
return True
def used(self): def used(self):
self.last_operation = time.time() self.last_operation = time.time()