electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 81fc3fcce2413bae55fc3e80a43744f7743745aa
parent 18c98483acc6adb13b22dfd6d356dac5128cd8e5
Author: SomberNight <somber.night@protonmail.com>
Date:   Wed,  1 Apr 2020 20:22:39 +0200

hww: rm some code duplication: add "scan_and_create_client_for_device"

Diffstat:
Melectrum/plugins/coldcard/coldcard.py | 11++---------
Melectrum/plugins/digitalbitbox/digitalbitbox.py | 11++---------
Melectrum/plugins/hw_wallet/plugin.py | 9+++++++++
Melectrum/plugins/keepkey/keepkey.py | 11++---------
Melectrum/plugins/ledger/ledger.py | 11++---------
Melectrum/plugins/safe_t/safe_t.py | 11++---------
Melectrum/plugins/trezor/trezor.py | 11++---------
7 files changed, 21 insertions(+), 54 deletions(-)

diff --git a/electrum/plugins/coldcard/coldcard.py b/electrum/plugins/coldcard/coldcard.py @@ -522,22 +522,15 @@ class ColdcardPlugin(HW_PluginBase): return None def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise UserFacingException(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) def get_xpub(self, device_id, derivation, xtype, wizard): # this seems to be part of the pairing process only, not during normal ops? # base_wizard:on_hw_derivation if xtype not in self.SUPPORTED_XTYPES: raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) client.ping_check() xpub = client.get_xpub(derivation, xtype) diff --git a/electrum/plugins/digitalbitbox/digitalbitbox.py b/electrum/plugins/digitalbitbox/digitalbitbox.py @@ -703,13 +703,8 @@ class DigitalBitboxPlugin(HW_PluginBase): def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise Exception(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) if purpose == HWD_SETUP_NEW_WALLET: client.setupRunning = True client.get_xpub("m/44'/0'", 'standard') @@ -739,9 +734,7 @@ class DigitalBitboxPlugin(HW_PluginBase): raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) if is_all_public_derivation(derivation): raise Exception(f"The {self.device} does not reveal xpubs corresponding to non-hardened paths. (path: {derivation})") - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) client.check_device_dialog() xpub = client.get_xpub(derivation, xtype) return xpub diff --git a/electrum/plugins/hw_wallet/plugin.py b/electrum/plugins/hw_wallet/plugin.py @@ -65,6 +65,15 @@ class HW_PluginBase(BasePlugin): if isinstance(keystore, self.keystore_class): self.device_manager().unpair_xpub(keystore.xpub) + def scan_and_create_client_for_device(self, *, device_id: str, wizard: 'BaseWizard') -> 'HardwareClientBase': + devmgr = self.device_manager() + client = devmgr.client_by_id(device_id) + if client is None: + raise UserFacingException(_('Failed to create a client for this device.') + '\n' + + _('Make sure it is in the correct state.')) + client.handler = self.create_handler(wizard) + return client + def setup_device(self, device_info: DeviceInfo, wizard: 'BaseWizard', purpose): """Called when creating a new wallet or when using the device to decrypt an existing wallet. Select the device to use. If the device is diff --git a/electrum/plugins/keepkey/keepkey.py b/electrum/plugins/keepkey/keepkey.py @@ -275,13 +275,8 @@ class KeepKeyPlugin(HW_PluginBase): return self.types.HDNodePathType(node=node, address_n=address_n) def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise UserFacingException(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) if not device_info.initialized: self.initialize_device(device_id, wizard, client.handler) client.get_xpub('m', 'standard') @@ -290,9 +285,7 @@ class KeepKeyPlugin(HW_PluginBase): def get_xpub(self, device_id, derivation, xtype, wizard): if xtype not in self.SUPPORTED_XTYPES: raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) xpub = client.get_xpub(derivation, xtype) client.used() return xpub diff --git a/electrum/plugins/ledger/ledger.py b/electrum/plugins/ledger/ledger.py @@ -589,21 +589,14 @@ class LedgerPlugin(HW_PluginBase): return client def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise UserFacingException(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) client.get_xpub("m/44'/0'", 'standard') # TODO replace by direct derivation once Nano S > 1.1 def get_xpub(self, device_id, derivation, xtype, wizard): if xtype not in self.SUPPORTED_XTYPES: raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) client.checkDevice() xpub = client.get_xpub(derivation, xtype) return xpub diff --git a/electrum/plugins/safe_t/safe_t.py b/electrum/plugins/safe_t/safe_t.py @@ -249,13 +249,8 @@ class SafeTPlugin(HW_PluginBase): return self.types.HDNodePathType(node=node, address_n=address_n) def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise UserFacingException(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) if not device_info.initialized: self.initialize_device(device_id, wizard, client.handler) client.get_xpub('m', 'standard') @@ -264,9 +259,7 @@ class SafeTPlugin(HW_PluginBase): def get_xpub(self, device_id, derivation, xtype, wizard): if xtype not in self.SUPPORTED_XTYPES: raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) xpub = client.get_xpub(derivation, xtype) client.used() return xpub diff --git a/electrum/plugins/trezor/trezor.py b/electrum/plugins/trezor/trezor.py @@ -268,12 +268,8 @@ class TrezorPlugin(HW_PluginBase): return HDNodePathType(node=node, address_n=address_n) def setup_device(self, device_info, wizard, purpose): - devmgr = self.device_manager() device_id = device_info.device.id_ - client = devmgr.client_by_id(device_id) - if client is None: - raise UserFacingException(_('Failed to create a client for this device.') + '\n' + - _('Make sure it is in the correct state.')) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) if not client.is_uptodate(): msg = (_('Outdated {} firmware for device labelled {}. Please ' @@ -281,7 +277,6 @@ class TrezorPlugin(HW_PluginBase): .format(self.device, client.label(), self.firmware_URL)) raise OutdatedHwFirmwareException(msg) - client.handler = self.create_handler(wizard) if not device_info.initialized: self.initialize_device(device_id, wizard, client.handler) is_creating_wallet = purpose == HWD_SETUP_NEW_WALLET @@ -291,9 +286,7 @@ class TrezorPlugin(HW_PluginBase): def get_xpub(self, device_id, derivation, xtype, wizard): if xtype not in self.SUPPORTED_XTYPES: raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) - devmgr = self.device_manager() - client = devmgr.client_by_id(device_id) - client.handler = self.create_handler(wizard) + client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard) xpub = client.get_xpub(derivation, xtype) client.used() return xpub