From 182accb9fbbdfa818a932ef278b61cf9feb9a445 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Wed, 16 Apr 2025 09:47:41 +0200 Subject: [PATCH] plugins: psbt_nostr: move can_send_psbt logic from GUI to backend, fix qml wallet switch bug --- electrum/plugins/psbt_nostr/psbt_nostr.py | 8 ++++++++ electrum/plugins/psbt_nostr/qml.py | 17 ++++++++++++----- electrum/plugins/psbt_nostr/qml/main.qml | 2 +- electrum/plugins/psbt_nostr/qt.py | 10 +--------- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/electrum/plugins/psbt_nostr/psbt_nostr.py b/electrum/plugins/psbt_nostr/psbt_nostr.py index 6f71ce85a..9c86d8618 100644 --- a/electrum/plugins/psbt_nostr/psbt_nostr.py +++ b/electrum/plugins/psbt_nostr/psbt_nostr.py @@ -218,6 +218,14 @@ class CosignerWallet(Logger): # note that tx could also be unrelated from wallet?... (not ismine inputs) return True + def can_send_psbt(self, tx: Union[Transaction, PartialTransaction]) -> bool: + if tx.is_complete() or self.wallet.can_sign(tx): + return False + for xpub, pubkey in self.cosigner_list: + if self.cosigner_can_sign(tx, xpub): + return True + return False + def mark_pending_event_rcvd(self, event_id): self.logger.debug('marking event rcvd') self.known_events[event_id] = now() diff --git a/electrum/plugins/psbt_nostr/qml.py b/electrum/plugins/psbt_nostr/qml.py index 1818eb59c..6ff9af905 100644 --- a/electrum/plugins/psbt_nostr/qml.py +++ b/electrum/plugins/psbt_nostr/qml.py @@ -58,23 +58,30 @@ class QReceiveSignalObject(QObject): def loader(self): return 'main.qml' + @pyqtSlot(QEWallet, str, result=bool) + def canSendPsbt(self, wallet: 'QEWallet', tx: str) -> bool: + cosigner_wallet = self._plugin.cosigner_wallets.get(wallet.wallet) + if not cosigner_wallet: + return False + return cosigner_wallet.can_send_psbt(tx_from_any(tx, deserialize=True)) + @pyqtSlot(QEWallet, str) def sendPsbt(self, wallet: 'QEWallet', tx: str): - cosigner_wallet = self._plugin.cosigner_wallets[wallet.wallet] + cosigner_wallet = self._plugin.cosigner_wallets.get(wallet.wallet) if not cosigner_wallet: return cosigner_wallet.send_psbt(tx_from_any(tx, deserialize=True)) @pyqtSlot(QEWallet, str) def acceptPsbt(self, wallet: 'QEWallet', event_id: str): - cosigner_wallet = self._plugin.cosigner_wallets[wallet.wallet] + cosigner_wallet = self._plugin.cosigner_wallets.get(wallet.wallet) if not cosigner_wallet: return cosigner_wallet.accept_psbt(event_id) @pyqtSlot(QEWallet, str) def rejectPsbt(self, wallet: 'QEWallet', event_id: str): - cosigner_wallet = self._plugin.cosigner_wallets[wallet.wallet] + cosigner_wallet = self._plugin.cosigner_wallets.get(wallet.wallet) if not cosigner_wallet: return cosigner_wallet.reject_psbt(event_id) @@ -98,8 +105,8 @@ class Plugin(PsbtNostrPlugin): @hook def load_wallet(self, wallet: 'Abstract_Wallet'): # remove existing, only foreground wallet active - if len(self.cosigner_wallets): - self.remove_cosigner_wallet(self.cosigner_wallets[0]) + for wallet in self.cosigner_wallets.copy().keys(): + self.remove_cosigner_wallet(wallet) if not isinstance(wallet, Multisig_Wallet): return self.add_cosigner_wallet(wallet, QmlCosignerWallet(wallet, self)) diff --git a/electrum/plugins/psbt_nostr/qml/main.qml b/electrum/plugins/psbt_nostr/qml/main.qml index b54ceee83..7df6d6aed 100644 --- a/electrum/plugins/psbt_nostr/qml/main.qml +++ b/electrum/plugins/psbt_nostr/qml/main.qml @@ -36,7 +36,7 @@ Item { property variant dialog text: qsTr('Nostr') icon.source: Qt.resolvedUrl('../../../gui/icons/network.png') - visible: Daemon.currentWallet.isMultisig && Daemon.currentWallet.walletType != '2fa' + visible: AppController.plugin('psbt_nostr').canSendPsbt(Daemon.currentWallet, dialog.text) onClicked: { console.log('about to psbt nostr send') psbt_nostr_send_button.enabled = false diff --git a/electrum/plugins/psbt_nostr/qt.py b/electrum/plugins/psbt_nostr/qt.py index 73818e600..49eeef81d 100644 --- a/electrum/plugins/psbt_nostr/qt.py +++ b/electrum/plugins/psbt_nostr/qt.py @@ -77,15 +77,7 @@ class Plugin(PsbtNostrPlugin): def transaction_dialog_update(self, d: 'TxDialog'): if cw := self.cosigner_wallets.get(d.wallet): assert isinstance(cw, QtCosignerWallet) - if d.tx.is_complete() or d.wallet.can_sign(d.tx): - d.cosigner_send_button.setVisible(False) - return - for xpub, pubkey in cw.cosigner_list: - if cw.cosigner_can_sign(d.tx, xpub): - d.cosigner_send_button.setVisible(True) - break - else: - d.cosigner_send_button.setVisible(False) + d.cosigner_send_button.setVisible(cw.can_send_psbt(d.tx)) class QtCosignerWallet(EventListener, CosignerWallet):