From 43c6f450408b2c34e2cf0efd91c4b6f78a855d04 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Tue, 12 Aug 2025 11:10:36 +0200 Subject: [PATCH] qml: handle invoice validation errors on save --- electrum/gui/qml/components/InvoiceDialog.qml | 7 +++--- .../gui/qml/components/WalletMainView.qml | 9 +++++-- electrum/gui/qml/qeinvoice.py | 25 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/electrum/gui/qml/components/InvoiceDialog.qml b/electrum/gui/qml/components/InvoiceDialog.qml index 80bb7a535..59590c622 100644 --- a/electrum/gui/qml/components/InvoiceDialog.qml +++ b/electrum/gui/qml/components/InvoiceDialog.qml @@ -470,9 +470,10 @@ ElDialog { if (amountMax.checked) invoice.amountOverride.isMax = true } - invoice.saveInvoice() - app.stack.push(Qt.resolvedUrl('Invoices.qml')) - dialog.close() + if (invoice.saveInvoice()) { + app.stack.push(Qt.resolvedUrl('Invoices.qml')) + dialog.close() + } } } FlatButton { diff --git a/electrum/gui/qml/components/WalletMainView.qml b/electrum/gui/qml/components/WalletMainView.qml index b044d5189..d87bbd4bf 100644 --- a/electrum/gui/qml/components/WalletMainView.qml +++ b/electrum/gui/qml/components/WalletMainView.qml @@ -114,7 +114,8 @@ Item { var canComplete = !Daemon.currentWallet.isWatchOnly && Daemon.currentWallet.canSignWithoutCosigner dialog.accepted.connect(function() { if (invoice.canSave) - invoice.saveInvoice() + if (!invoice.saveInvoice()) + return if (!canComplete) { if (Daemon.currentWallet.isWatchOnly) { dialog.finalizer.saveOrShow() @@ -413,7 +414,11 @@ Item { dialog.open() } onInvoiceCreateError: (code, message) => { - console.log(code + ' ' + message) + var msg = qsTr('Cannot save invoice') + ': ' + message + var dialog = app.messageDialog.createObject(app, { + text: msg + }) + dialog.open() } onLnurlRetrieved: { diff --git a/electrum/gui/qml/qeinvoice.py b/electrum/gui/qml/qeinvoice.py index 12b6d2251..9e8d14aaf 100644 --- a/electrum/gui/qml/qeinvoice.py +++ b/electrum/gui/qml/qeinvoice.py @@ -23,6 +23,7 @@ from .qetypes import QEAmount from .qewallet import QEWallet from .util import status_update_timer_interval, QtEventListener, event_listener from ...fee_policy import FeePolicy +from ...util import InvoiceError class QEInvoice(QObject, QtEventListener): @@ -690,18 +691,22 @@ class QEInvoiceParser(QEInvoice): self.recipient = invoice.lightning_invoice - @pyqtSlot() - def saveInvoice(self): + @pyqtSlot(result=bool) + def saveInvoice(self) -> bool: if not self._effectiveInvoice: - return + return False if self.isSaved: - return + return False - if not self._effectiveInvoice.amount_msat and not self.amountOverride.isEmpty: - if self.invoiceType == QEInvoice.Type.OnchainInvoice and self.amountOverride.isMax: - self._effectiveInvoice.set_amount_msat('!') - else: - self._effectiveInvoice.set_amount_msat(self.amountOverride.satsInt * 1000) + try: + if not self._effectiveInvoice.amount_msat and not self.amountOverride.isEmpty: + if self.invoiceType == QEInvoice.Type.OnchainInvoice and self.amountOverride.isMax: + self._effectiveInvoice.set_amount_msat('!') + else: + self._effectiveInvoice.set_amount_msat(self.amountOverride.satsInt * 1000) + except InvoiceError as e: + self.invoiceCreateError.emit('validation', str(e)) + return False self.canSave = False @@ -709,3 +714,5 @@ class QEInvoiceParser(QEInvoice): self._key = self._effectiveInvoice.get_id() self._wallet.invoiceModel.addInvoice(self._key) self.invoiceSaved.emit(self._key) + + return True