Skip to content

Commit 110e52e

Browse files
committed
add option to show warnings on wallet close, add warning for ongoing submarine swap
1 parent cca29ef commit 110e52e

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

electrum/gui/messages.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ def to_rtf(msg):
6161
_("Please remain online until the funding transaction is confirmed.") + "\n\n" +
6262
_('The swap will be finalized once your transaction is confirmed.') + " " +
6363
_("After the funding transaction is mined, the server will reveal the preimage needed to "
64-
"fulfill the pending received lightning HTLCs. The HTLCs expire in {} blocks. "
64+
"fulfill the pending received lightning HTLCs.")
65+
)
66+
67+
MSG_FORWARD_SWAP_HTLC_EXPIRY = _(
68+
"The HTLCs expire in {} blocks. "
6569
"You will need to be online after the funding transaction is confirmed but before the HTLCs expire, "
6670
"to claim your money. If you go offline for several days while the swap is pending, "
6771
"you risk losing the swap amount!").format(MIN_FINAL_CLTV_DELTA_FOR_CLIENT)
68-
)
72+
6973

7074
MSG_REVERSE_SWAP_FUNDING_MEMPOOL = (
7175
_('The funding transaction has been detected.') + " " +

electrum/gui/qt/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import signal
2828
import sys
2929
import threading
30-
from typing import Optional, TYPE_CHECKING, List, Sequence
30+
from typing import Optional, TYPE_CHECKING, List, Sequence, Callable
3131

3232
try:
3333
import PyQt6

electrum/gui/qt/main_window.py

+38-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import csv
3232
from decimal import Decimal
3333
import base64
34+
from email.policy import default
3435
from functools import partial
3536
import queue
3637
import asyncio
@@ -2619,8 +2620,35 @@ def settings_dialog(self):
26192620
if d.need_restart:
26202621
self.show_warning(_('Please restart Electrum to activate the new GUI settings'), title=_('Success'))
26212622

2623+
def _show_closing_warnings(self) -> bool:
2624+
"""Show any closing warnings and return True if the user chose to quit anyways."""
2625+
for callback in self.wallet.closing_warning_callbacks:
2626+
try:
2627+
warning: Optional[str] = callback()
2628+
except Exception:
2629+
self.logger.exception("Closing warning callback failed: ")
2630+
continue
2631+
if warning:
2632+
warning = _("An ongoing operation prevents Electrum from closing:") + "\n\n" + warning
2633+
buttons = QMessageBox.StandardButton.Cancel | QMessageBox.StandardButton.Close
2634+
result = self.show_warning(
2635+
msg=warning,
2636+
title=_("Don't close Electrum yet!"),
2637+
buttons=buttons,
2638+
defaultButton=QMessageBox.StandardButton.Cancel,
2639+
)
2640+
if result == QMessageBox.StandardButton.Cancel:
2641+
break
2642+
else:
2643+
# user chose to cancel all warnings or there were no warnings
2644+
return True
2645+
return False
2646+
26222647
def closeEvent(self, event):
26232648
# note that closeEvent is NOT called if the user quits with Ctrl-C
2649+
if not self._show_closing_warnings():
2650+
event.ignore()
2651+
return
26242652
self.clean_up()
26252653
event.accept()
26262654

@@ -2809,7 +2837,16 @@ def on_swap_result(self, txid: Optional[str], *, is_reverse: bool):
28092837
if is_reverse:
28102838
msg += messages.MSG_REVERSE_SWAP_FUNDING_MEMPOOL
28112839
else:
2812-
msg += messages.MSG_FORWARD_SWAP_FUNDING_MEMPOOL
2840+
msg += messages.MSG_FORWARD_SWAP_FUNDING_MEMPOOL + messages.MSG_FORWARD_SWAP_HTLC_EXPIRY
2841+
2842+
def wallet_closing_warning_callback() -> Optional[str]:
2843+
# gets called when the wallet GUI is closed
2844+
warning = messages.MSG_REVERSE_SWAP_FUNDING_MEMPOOL if is_reverse \
2845+
else messages.MSG_FORWARD_SWAP_FUNDING_MEMPOOL
2846+
if self.wallet.adb.get_tx_height(txid).height < 1:
2847+
return _("Ongoing submarine swap") + ": " + warning
2848+
self.wallet.register_closing_warning_callback(wallet_closing_warning_callback)
2849+
28132850
self.show_message_signal.emit(msg)
28142851
else:
28152852
msg += _("Lightning funds were not received.") # FIXME should this not depend on is_reverse?

electrum/wallet.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
from functools import partial
3535
from collections import defaultdict
3636
from decimal import Decimal
37-
from typing import TYPE_CHECKING, List, Optional, Tuple, Union, NamedTuple, Sequence, Dict, Any, Set, Iterable, Mapping
37+
from typing import (TYPE_CHECKING, List, Optional, Tuple, Union, NamedTuple, Sequence,
38+
Dict, Any, Set, Iterable, Mapping, Callable)
3839
from abc import ABC, abstractmethod
3940
import itertools
4041
import threading
@@ -401,6 +402,7 @@ def __init__(self, db: WalletDB, *, config: SimpleConfig):
401402
db.load_addresses(self.wallet_type)
402403
self.keystore = None # type: Optional[KeyStore] # will be set by load_keystore
403404
self._password_in_memory = None # see self.unlock
405+
self.closing_warning_callbacks = [] # type: List[Callable[[], Optional[str]]]
404406
Logger.__init__(self)
405407

406408
self.network = None
@@ -3456,6 +3458,16 @@ def add_future_tx(self, sweep_info: 'SweepInfo', wanted_height: int):
34563458
util.trigger_callback('wallet_updated', self)
34573459
self.adb.set_future_tx(tx.txid(), wanted_height=wanted_height)
34583460

3461+
def register_closing_warning_callback(self, callback: Callable[[], Optional[str]]) -> None:
3462+
"""
3463+
Registers a callback that will be called when the wallet is closed. If the callback
3464+
returns a string it will be shown to the user as a warning to prevent them closing the wallet.
3465+
"""
3466+
if not self.config.get("cmd") == "gui":
3467+
return
3468+
self.logger.debug(f"registering wallet closing warning callback")
3469+
self.closing_warning_callbacks.append(callback)
3470+
34593471

34603472
class Simple_Wallet(Abstract_Wallet):
34613473
# wallet with a single keystore

0 commit comments

Comments
 (0)