|
22 | 22 | # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
23 | 23 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
24 | 24 | # SOFTWARE.
|
25 |
| -from typing import TYPE_CHECKING |
| 25 | +import asyncio |
| 26 | +import concurrent |
| 27 | +from typing import TYPE_CHECKING, List, Tuple, Optional |
26 | 28 |
|
| 29 | +from PyQt6.QtCore import QObject, pyqtSignal, pyqtProperty, pyqtSlot |
| 30 | + |
| 31 | +from electrum import util |
27 | 32 | from electrum.plugin import hook
|
| 33 | +from electrum.transaction import PartialTransaction, tx_from_any |
28 | 34 | from electrum.wallet import Multisig_Wallet
|
| 35 | +from electrum.util import EventListener, event_listener |
| 36 | + |
| 37 | +from electrum.gui.qml.qewallet import QEWallet |
29 | 38 |
|
30 | 39 | from .psbt_nostr import PsbtNostrPlugin, CosignerWallet
|
31 | 40 |
|
|
34 | 43 | from electrum.gui.qml import ElectrumQmlApplication
|
35 | 44 |
|
36 | 45 |
|
| 46 | +class QReceiveSignalObject(QObject): |
| 47 | + def __init__(self, plugin: 'Plugin'): |
| 48 | + QObject.__init__(self) |
| 49 | + self._plugin = plugin |
| 50 | + |
| 51 | + cosignerReceivedPsbt = pyqtSignal(str, str, str) |
| 52 | + sendPsbtFailed = pyqtSignal(str, arguments=['reason']) |
| 53 | + sendPsbtSuccess = pyqtSignal() |
| 54 | + |
| 55 | + @pyqtProperty(str) |
| 56 | + def loader(self): |
| 57 | + return 'main.qml' |
| 58 | + |
| 59 | + @pyqtSlot(QEWallet, str) |
| 60 | + def sendPsbt(self, wallet: 'QEWallet', tx: str): |
| 61 | + cosigner_wallet = self._plugin.cosigner_wallets[wallet.wallet] |
| 62 | + if not cosigner_wallet: |
| 63 | + return |
| 64 | + cosigner_wallet.send_psbt(tx_from_any(tx, deserialize=True)) |
| 65 | + |
| 66 | + @pyqtSlot(QEWallet, str) |
| 67 | + def acceptPsbt(self, wallet: 'QEWallet', event_id: str): |
| 68 | + cosigner_wallet = self._plugin.cosigner_wallets[wallet.wallet] |
| 69 | + if not cosigner_wallet: |
| 70 | + return |
| 71 | + cosigner_wallet.accept_psbt(event_id) |
| 72 | + |
| 73 | + |
37 | 74 | class Plugin(PsbtNostrPlugin):
|
38 | 75 | def __init__(self, parent, config, name):
|
39 | 76 | super().__init__(parent, config, name)
|
| 77 | + self.so = QReceiveSignalObject(self) |
40 | 78 | self._app = None
|
41 | 79 |
|
42 | 80 | @hook
|
43 | 81 | def init_qml(self, app: 'ElectrumQmlApplication'):
|
44 |
| - # if self._init_qt_received: # only need/want the first signal |
45 |
| - # return |
46 |
| - # self._init_qt_received = True |
47 | 82 | self._app = app
|
48 |
| - # plugin enable for already open wallets |
49 |
| - for wallet in app.daemon.get_wallets(): |
| 83 | + self.so.setParent(app) # parent in QObject tree |
| 84 | + # plugin enable for already open wallet |
| 85 | + wallet = app.daemon.currentWallet.wallet if app.daemon.currentWallet else None |
| 86 | + if wallet: |
50 | 87 | self.load_wallet(wallet)
|
51 | 88 |
|
52 | 89 | @hook
|
53 | 90 | def load_wallet(self, wallet: 'Abstract_Wallet'):
|
| 91 | + # remove existing, only foreground wallet active |
| 92 | + if len(self.cosigner_wallets): |
| 93 | + self.remove_cosigner_wallet(self.cosigner_wallets[0]) |
54 | 94 | if not isinstance(wallet, Multisig_Wallet):
|
55 | 95 | return
|
56 |
| - self.add_cosigner_wallet(wallet, CosignerWallet(wallet)) |
57 |
| - |
58 |
| - # @hook |
59 |
| - # def on_close_window(self, window): |
60 |
| - # wallet = window.wallet |
61 |
| - # self.remove_cosigner_wallet(wallet) |
62 |
| - # |
63 |
| - # @hook |
64 |
| - # def transaction_dialog(self, d: 'TxDialog'): |
65 |
| - # if cw := self.cosigner_wallets.get(d.wallet): |
66 |
| - # assert isinstance(cw, QtCosignerWallet) |
67 |
| - # cw.hook_transaction_dialog(d) |
68 |
| - # |
69 |
| - # @hook |
70 |
| - # def transaction_dialog_update(self, d: 'TxDialog'): |
71 |
| - # if cw := self.cosigner_wallets.get(d.wallet): |
72 |
| - # assert isinstance(cw, QtCosignerWallet) |
73 |
| - # cw.hook_transaction_dialog_update(d) |
| 96 | + self.add_cosigner_wallet(wallet, QmlCosignerWallet(wallet, self)) |
| 97 | + |
| 98 | + |
| 99 | +class QmlCosignerWallet(EventListener, CosignerWallet): |
| 100 | + |
| 101 | + def __init__(self, wallet: 'Multisig_Wallet', plugin: 'Plugin'): |
| 102 | + CosignerWallet.__init__(self, wallet) |
| 103 | + self.plugin = plugin |
| 104 | + self.register_callbacks() |
| 105 | + |
| 106 | + self.pending = None |
| 107 | + |
| 108 | + @event_listener |
| 109 | + def on_event_psbt_nostr_received(self, wallet, pubkey, event, tx: 'PartialTransaction'): |
| 110 | + if self.wallet == wallet: |
| 111 | + self.plugin.so.cosignerReceivedPsbt.emit(pubkey, event, tx.serialize()) |
| 112 | + self.on_receive(pubkey, event, tx) |
| 113 | + |
| 114 | + def close(self): |
| 115 | + super().close() |
| 116 | + self.unregister_callbacks() |
| 117 | + |
| 118 | + def do_send(self, messages: List[Tuple[str, str]], txid: Optional[str] = None): |
| 119 | + if not messages: |
| 120 | + return |
| 121 | + coro = self.send_direct_messages(messages) |
| 122 | + |
| 123 | + loop = util.get_asyncio_loop() |
| 124 | + assert util.get_running_loop() != loop, 'must not be called from asyncio thread' |
| 125 | + self._result = None |
| 126 | + self._future = asyncio.run_coroutine_threadsafe(coro, loop) |
| 127 | + |
| 128 | + try: |
| 129 | + self._result = self._future.result() |
| 130 | + self.plugin.so.sendPsbtSuccess.emit() |
| 131 | + except concurrent.futures.CancelledError: |
| 132 | + pass |
| 133 | + except Exception as e: |
| 134 | + self.plugin.so.sendPsbtFailed.emit(str(e)) |
| 135 | + |
| 136 | + def on_receive(self, pubkey, event_id, tx): |
| 137 | + self.pending = (pubkey, event_id, tx) |
| 138 | + |
| 139 | + def accept_psbt(self, my_event_id): |
| 140 | + pubkey, event_id, tx = self.pending |
| 141 | + if event_id == my_event_id: |
| 142 | + self.mark_event_rcvd(event_id) |
| 143 | + self.pending = None |
0 commit comments