Browse Source

hww: rm some code duplication: add "scan_and_create_client_for_device"

hard-fail-on-bad-server-string
SomberNight 5 years ago
parent
commit
81fc3fcce2
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 11
      electrum/plugins/coldcard/coldcard.py
  2. 11
      electrum/plugins/digitalbitbox/digitalbitbox.py
  3. 9
      electrum/plugins/hw_wallet/plugin.py
  4. 11
      electrum/plugins/keepkey/keepkey.py
  5. 11
      electrum/plugins/ledger/ledger.py
  6. 11
      electrum/plugins/safe_t/safe_t.py
  7. 11
      electrum/plugins/trezor/trezor.py

11
electrum/plugins/coldcard/coldcard.py

@ -522,22 +522,15 @@ class ColdcardPlugin(HW_PluginBase):
return None return None
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
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)
def get_xpub(self, device_id, derivation, xtype, wizard): def get_xpub(self, device_id, derivation, xtype, wizard):
# this seems to be part of the pairing process only, not during normal ops? # this seems to be part of the pairing process only, not during normal ops?
# base_wizard:on_hw_derivation # base_wizard:on_hw_derivation
if xtype not in self.SUPPORTED_XTYPES: if xtype not in self.SUPPORTED_XTYPES:
raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
client.ping_check() client.ping_check()
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)

11
electrum/plugins/digitalbitbox/digitalbitbox.py

@ -703,13 +703,8 @@ class DigitalBitboxPlugin(HW_PluginBase):
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
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)
if purpose == HWD_SETUP_NEW_WALLET: if purpose == HWD_SETUP_NEW_WALLET:
client.setupRunning = True client.setupRunning = True
client.get_xpub("m/44'/0'", 'standard') 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)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
if is_all_public_derivation(derivation): if is_all_public_derivation(derivation):
raise Exception(f"The {self.device} does not reveal xpubs corresponding to non-hardened paths. (path: {derivation})") raise Exception(f"The {self.device} does not reveal xpubs corresponding to non-hardened paths. (path: {derivation})")
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
client.check_device_dialog() client.check_device_dialog()
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)
return xpub return xpub

9
electrum/plugins/hw_wallet/plugin.py

@ -65,6 +65,15 @@ class HW_PluginBase(BasePlugin):
if isinstance(keystore, self.keystore_class): if isinstance(keystore, self.keystore_class):
self.device_manager().unpair_xpub(keystore.xpub) 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): def setup_device(self, device_info: DeviceInfo, wizard: 'BaseWizard', purpose):
"""Called when creating a new wallet or when using the device to decrypt """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 an existing wallet. Select the device to use. If the device is

11
electrum/plugins/keepkey/keepkey.py

@ -275,13 +275,8 @@ class KeepKeyPlugin(HW_PluginBase):
return self.types.HDNodePathType(node=node, address_n=address_n) return self.types.HDNodePathType(node=node, address_n=address_n)
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
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)
if not device_info.initialized: if not device_info.initialized:
self.initialize_device(device_id, wizard, client.handler) self.initialize_device(device_id, wizard, client.handler)
client.get_xpub('m', 'standard') client.get_xpub('m', 'standard')
@ -290,9 +285,7 @@ class KeepKeyPlugin(HW_PluginBase):
def get_xpub(self, device_id, derivation, xtype, wizard): def get_xpub(self, device_id, derivation, xtype, wizard):
if xtype not in self.SUPPORTED_XTYPES: if xtype not in self.SUPPORTED_XTYPES:
raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)
client.used() client.used()
return xpub return xpub

11
electrum/plugins/ledger/ledger.py

@ -589,21 +589,14 @@ class LedgerPlugin(HW_PluginBase):
return client return client
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
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.get_xpub("m/44'/0'", 'standard') # TODO replace by direct derivation once Nano S > 1.1 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): def get_xpub(self, device_id, derivation, xtype, wizard):
if xtype not in self.SUPPORTED_XTYPES: if xtype not in self.SUPPORTED_XTYPES:
raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
client.checkDevice() client.checkDevice()
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)
return xpub return xpub

11
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) return self.types.HDNodePathType(node=node, address_n=address_n)
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
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)
if not device_info.initialized: if not device_info.initialized:
self.initialize_device(device_id, wizard, client.handler) self.initialize_device(device_id, wizard, client.handler)
client.get_xpub('m', 'standard') client.get_xpub('m', 'standard')
@ -264,9 +259,7 @@ class SafeTPlugin(HW_PluginBase):
def get_xpub(self, device_id, derivation, xtype, wizard): def get_xpub(self, device_id, derivation, xtype, wizard):
if xtype not in self.SUPPORTED_XTYPES: if xtype not in self.SUPPORTED_XTYPES:
raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)
client.used() client.used()
return xpub return xpub

11
electrum/plugins/trezor/trezor.py

@ -268,12 +268,8 @@ class TrezorPlugin(HW_PluginBase):
return HDNodePathType(node=node, address_n=address_n) return HDNodePathType(node=node, address_n=address_n)
def setup_device(self, device_info, wizard, purpose): def setup_device(self, device_info, wizard, purpose):
devmgr = self.device_manager()
device_id = device_info.device.id_ device_id = device_info.device.id_
client = devmgr.client_by_id(device_id) client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
if client is None:
raise UserFacingException(_('Failed to create a client for this device.') + '\n' +
_('Make sure it is in the correct state.'))
if not client.is_uptodate(): if not client.is_uptodate():
msg = (_('Outdated {} firmware for device labelled {}. Please ' msg = (_('Outdated {} firmware for device labelled {}. Please '
@ -281,7 +277,6 @@ class TrezorPlugin(HW_PluginBase):
.format(self.device, client.label(), self.firmware_URL)) .format(self.device, client.label(), self.firmware_URL))
raise OutdatedHwFirmwareException(msg) raise OutdatedHwFirmwareException(msg)
client.handler = self.create_handler(wizard)
if not device_info.initialized: if not device_info.initialized:
self.initialize_device(device_id, wizard, client.handler) self.initialize_device(device_id, wizard, client.handler)
is_creating_wallet = purpose == HWD_SETUP_NEW_WALLET 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): def get_xpub(self, device_id, derivation, xtype, wizard):
if xtype not in self.SUPPORTED_XTYPES: if xtype not in self.SUPPORTED_XTYPES:
raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device)) raise ScriptTypeNotSupported(_('This type of script is not supported with {}.').format(self.device))
devmgr = self.device_manager() client = self.scan_and_create_client_for_device(device_id=device_id, wizard=wizard)
client = devmgr.client_by_id(device_id)
client.handler = self.create_handler(wizard)
xpub = client.get_xpub(derivation, xtype) xpub = client.get_xpub(derivation, xtype)
client.used() client.used()
return xpub return xpub

Loading…
Cancel
Save