From 238619f1ed7cb8e7bc60f9de0bfddc0116914b79 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Thu, 28 Apr 2022 21:08:56 +0200 Subject: [PATCH] hardware wallets: smarter pairing during sign_transaction Scenario: - 2of2 multisig wallet with device1 and device2 - disconnect all devices - open wallet file - fail all pairings at wallet-open - connect device2 - try to sign a tx At this point Electrum will try to find the device for keystore1 first, and there is only a single unpaired device: device2. Automatic pairing of keystore1 and device2 will fail, due to device id mismatching compared to what is persisted on disk for keystore1, so the user is prompted for manual selection. The selection dialog is somewhat confusing as it is not clear that the app is asking to select a device for keystore1. Pairing would fail, so the user is expected to cancel the dialog. If they cancel, keystore1 is skipped, and we try to pair for keystore2 now, and device2 will pair with it automatically. fixes https://github.com/spesmilo/electrum/issues/4199#issuecomment-1112552416 --- electrum/keystore.py | 8 +++++++- electrum/wallet.py | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/electrum/keystore.py b/electrum/keystore.py index 985654aa6..c1c1bff65 100644 --- a/electrum/keystore.py +++ b/electrum/keystore.py @@ -847,7 +847,13 @@ class Hardware_KeyStore(Xpub, KeyStore): def has_usable_connection_with_device(self) -> bool: if not hasattr(self, 'plugin'): return False - client = self.plugin.get_client(self, force_pair=False) + # we try to create a client even if there isn't one already, + # but do not prompt the user if auto-select fails: + client = self.plugin.get_client( + self, + force_pair=True, + allow_user_interaction=False, + ) if client is None: return False return client.has_usable_connection_with_device() diff --git a/electrum/wallet.py b/electrum/wallet.py index f6867a324..e09bf8dc5 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -2100,6 +2100,9 @@ class Abstract_Wallet(AddressSynchronizer, ABC): tmp_tx = copy.deepcopy(tx) tmp_tx.add_info_from_wallet(self, include_xpubs=True) # sign. start with ready keystores. + # note: ks.ready_to_sign() side-effect: we trigger pairings with potential hw devices. + # We only do this once, before the loop, however we could rescan after each iteration, + # to see if the user connected/disconnected devices in the meantime. for k in sorted(self.get_keystores(), key=lambda ks: ks.ready_to_sign(), reverse=True): try: if k.can_sign(tmp_tx):