From 9bd082cd825e3bbffe146cbf6534e6e7c1e7c1f0 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 29 May 2018 13:37:30 +0200 Subject: [PATCH] trezor/keepkey: better handling of exceptions during device initialization notably Trezor T is returning a different msg type when trying to get an xpub from an uninitialized device, which we are not handling. instead we should just realise ourselves that we did not initialize the device --- plugins/keepkey/keepkey.py | 13 ++++++++++--- plugins/trezor/trezor.py | 13 ++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/plugins/keepkey/keepkey.py b/plugins/keepkey/keepkey.py index 5b1515947..bf52a8680 100644 --- a/plugins/keepkey/keepkey.py +++ b/plugins/keepkey/keepkey.py @@ -181,19 +181,26 @@ class KeepKeyPlugin(HW_PluginBase): t = threading.Thread(target=self._initialize_device_safe, args=(settings, method, device_id, wizard, handler)) t.setDaemon(True) t.start() - wizard.loop.exec_() + exit_code = wizard.loop.exec_() + if exit_code != 0: + # this method (initialize_device) was called with the expectation + # of leaving the device in an initialized state when finishing. + # signal that this is not the case: + raise UserCancelled() wizard.choice_dialog(title=_('Initialize Device'), message=msg, choices=choices, run_next=f) def _initialize_device_safe(self, settings, method, device_id, wizard, handler): + exit_code = 0 try: self._initialize_device(settings, method, device_id, wizard, handler) except UserCancelled: - pass + exit_code = 1 except BaseException as e: traceback.print_exc(file=sys.stderr) handler.show_error(str(e)) + exit_code = 1 finally: - wizard.loop.exit(0) + wizard.loop.exit(exit_code) def _initialize_device(self, settings, method, device_id, wizard, handler): item, label, pin_protection, passphrase_protection = settings diff --git a/plugins/trezor/trezor.py b/plugins/trezor/trezor.py index d5cd33d2b..575aba5c9 100644 --- a/plugins/trezor/trezor.py +++ b/plugins/trezor/trezor.py @@ -202,19 +202,26 @@ class TrezorPlugin(HW_PluginBase): t = threading.Thread(target=self._initialize_device_safe, args=(settings, method, device_id, wizard, handler)) t.setDaemon(True) t.start() - wizard.loop.exec_() + exit_code = wizard.loop.exec_() + if exit_code != 0: + # this method (initialize_device) was called with the expectation + # of leaving the device in an initialized state when finishing. + # signal that this is not the case: + raise UserCancelled() wizard.choice_dialog(title=_('Initialize Device'), message=msg, choices=choices, run_next=f) def _initialize_device_safe(self, settings, method, device_id, wizard, handler): + exit_code = 0 try: self._initialize_device(settings, method, device_id, wizard, handler) except UserCancelled: - pass + exit_code = 1 except BaseException as e: traceback.print_exc(file=sys.stderr) handler.show_error(str(e)) + exit_code = 1 finally: - wizard.loop.exit(0) + wizard.loop.exit(exit_code) def _initialize_device(self, settings, method, device_id, wizard, handler): item, label, pin_protection, passphrase_protection, recovery_type = settings