From 8ae2a7868e41f03ca56eeef729375440d91c191c Mon Sep 17 00:00:00 2001 From: Darsey Litzenberger Date: Thu, 18 Jul 2024 23:22:07 -0600 Subject: [PATCH] trezor: Fix for trezor library version 0.13.9 This enables support for the Trezor Safe 5. --- electrum/plugins/trezor/clientbase.py | 12 ++++++++++-- electrum/plugins/trezor/qt.py | 6 +++--- electrum/plugins/trezor/trezor.py | 10 +++++----- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/electrum/plugins/trezor/clientbase.py b/electrum/plugins/trezor/clientbase.py index a7c92f026..cee55945e 100644 --- a/electrum/plugins/trezor/clientbase.py +++ b/electrum/plugins/trezor/clientbase.py @@ -12,10 +12,18 @@ from electrum.plugins.hw_wallet.plugin import OutdatedHwFirmwareException, Hardw from trezorlib.client import TrezorClient, PASSPHRASE_ON_DEVICE from trezorlib.exceptions import TrezorFailure, Cancelled, OutdatedFirmwareError -from trezorlib.messages import WordRequestType, FailureType, RecoveryDeviceType, ButtonRequestType +from trezorlib.messages import WordRequestType, FailureType, ButtonRequestType import trezorlib.btc import trezorlib.device +try: + # trezor >= 0.13.9 + from trezorlib.messages import RecoveryDeviceInputMethod +except ImportError: + # Backward compatibility for trezor < 0.13.9 + from trezorlib.messages import RecoveryDeviceType as RecoveryDeviceInputMethod + + MESSAGES = { ButtonRequestType.ConfirmOutput: _("Confirm the transaction output on your {} device"), @@ -346,7 +354,7 @@ class TrezorClientBase(HardwareClientBase, Logger): if recovery_type is None: return None - if recovery_type == RecoveryDeviceType.Matrix: + if recovery_type == RecoveryDeviceInputMethod.Matrix: return self._matrix_char step = 0 diff --git a/electrum/plugins/trezor/qt.py b/electrum/plugins/trezor/qt.py index 5914ebb79..80adffd0b 100644 --- a/electrum/plugins/trezor/qt.py +++ b/electrum/plugins/trezor/qt.py @@ -21,7 +21,7 @@ from electrum.gui.qt.util import (WindowModalDialog, WWLabel, Buttons, CancelBut from electrum.gui.qt.wizard.wallet import WCScriptAndDerivation, WCHWUnlock, WCHWXPub, WalletWizardComponent from .trezor import (TrezorPlugin, TIM_NEW, TIM_RECOVER, TrezorInitSettings, - PASSPHRASE_ON_DEVICE, Capability, BackupType, RecoveryDeviceType) + PASSPHRASE_ON_DEVICE, Capability, BackupType, RecoveryDeviceInputMethod) if TYPE_CHECKING: from electrum.gui.qt.wizard.wallet import QENewWalletWizard @@ -409,14 +409,14 @@ class InitSettingsLayout(QVBoxLayout): rb1 = QRadioButton(gb_rectype) rb1.setText(_('Scrambled words')) self.bg_rectype.addButton(rb1) - self.bg_rectype.setId(rb1, RecoveryDeviceType.ScrambledWords) + self.bg_rectype.setId(rb1, RecoveryDeviceInputMethod.ScrambledWords) hbox_rectype.addWidget(rb1) rb1.setChecked(True) rb2 = QRadioButton(gb_rectype) rb2.setText(_('Matrix')) self.bg_rectype.addButton(rb2) - self.bg_rectype.setId(rb2, RecoveryDeviceType.Matrix) + self.bg_rectype.setId(rb2, RecoveryDeviceInputMethod.Matrix) hbox_rectype.addWidget(rb2) # no backup diff --git a/electrum/plugins/trezor/trezor.py b/electrum/plugins/trezor/trezor.py index da6a57170..8eb702052 100644 --- a/electrum/plugins/trezor/trezor.py +++ b/electrum/plugins/trezor/trezor.py @@ -26,10 +26,10 @@ try: import trezorlib.transport from trezorlib.transport.bridge import BridgeTransport, call_bridge - from .clientbase import TrezorClientBase + from .clientbase import TrezorClientBase, RecoveryDeviceInputMethod from trezorlib.messages import ( - Capability, BackupType, RecoveryDeviceType, HDNodeType, HDNodePathType, + Capability, BackupType, HDNodeType, HDNodePathType, InputScriptType, OutputScriptType, MultisigRedeemScriptType, TxInputType, TxOutputType, TxOutputBinType, TransactionType, AmountUnit) @@ -56,7 +56,7 @@ except Exception as e: Capability = _EnumMissing() BackupType = _EnumMissing() - RecoveryDeviceType = _EnumMissing() + RecoveryDeviceInputMethod = _EnumMissing() AmountUnit = _EnumMissing() PASSPHRASE_ON_DEVICE = object() @@ -251,7 +251,7 @@ class TrezorPlugin(HW_PluginBase): @runs_in_hwd_thread def _initialize_device(self, settings: TrezorInitSettings, method, device_id, handler): - if method == TIM_RECOVER and settings.recovery_type == RecoveryDeviceType.ScrambledWords: + if method == TIM_RECOVER and settings.recovery_type == RecoveryDeviceInputMethod.ScrambledWords: handler.show_error(_( "You will be asked to enter 24 words regardless of your " "seed's actual length. If you enter a word incorrectly or " @@ -281,7 +281,7 @@ class TrezorPlugin(HW_PluginBase): passphrase_protection=settings.passphrase_enabled, pin_protection=settings.pin_enabled, label=settings.label) - if settings.recovery_type == RecoveryDeviceType.Matrix: + if settings.recovery_type == RecoveryDeviceInputMethod.Matrix: handler.close_matrix_dialog() else: raise RuntimeError("Unsupported recovery method")