1
0

hww: rm some code duplication: add "scan_and_create_client_for_device"

This commit is contained in:
SomberNight
2020-04-01 20:22:39 +02:00
parent 18c98483ac
commit 81fc3fcce2
7 changed files with 21 additions and 54 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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