From 098b384348a9740f02f4190047ca0b77d7464fd1 Mon Sep 17 00:00:00 2001 From: Sander van Grieken Date: Tue, 14 Jun 2022 16:29:07 +0200 Subject: [PATCH] enable generating lightning request. currently very simple heuristics: if requested amount < lightningCanReceive then create a lightning request, else onchain --- electrum/gui/qml/components/Receive.qml | 11 +++++++++-- electrum/gui/qml/components/RequestDialog.qml | 2 ++ electrum/gui/qml/qeinvoicelistmodel.py | 3 ++- electrum/gui/qml/qewallet.py | 11 +++++++---- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/electrum/gui/qml/components/Receive.qml b/electrum/gui/qml/components/Receive.qml index 44cbf0f1c..4a8868dd1 100644 --- a/electrum/gui/qml/components/Receive.qml +++ b/electrum/gui/qml/components/Receive.qml @@ -199,8 +199,15 @@ Pane { } function createRequest(ignoreGaplimit = false) { - var a = Config.unitsToSats(amount.text) - Daemon.currentWallet.create_request(a, message.text, expires.currentValue, false, ignoreGaplimit) + var qamt = Config.unitsToSats(amount.text) + console.log('about to create req for ' + qamt.satsInt + ' sats') + if (qamt.satsInt > Daemon.currentWallet.lightningCanReceive) { + console.log('Creating OnChain request') + Daemon.currentWallet.create_request(qamt, message.text, expires.currentValue, false, ignoreGaplimit) + } else { + console.log('Creating Lightning request') + Daemon.currentWallet.create_request(qamt, message.text, expires.currentValue, true) + } } Connections { diff --git a/electrum/gui/qml/components/RequestDialog.qml b/electrum/gui/qml/components/RequestDialog.qml index 437602077..caebe04a9 100644 --- a/electrum/gui/qml/components/RequestDialog.qml +++ b/electrum/gui/qml/components/RequestDialog.qml @@ -209,6 +209,8 @@ Dialog { if (!modelItem.is_lightning) { _bip21uri = bitcoin.create_bip21_uri(modelItem.address, modelItem.amount, modelItem.message, modelItem.timestamp, modelItem.expiration - modelItem.timestamp) qr.source = 'image://qrgen/' + _bip21uri + } else { + qr.source = 'image://qrgen/' + modelItem.lightning_invoice } } diff --git a/electrum/gui/qml/qeinvoicelistmodel.py b/electrum/gui/qml/qeinvoicelistmodel.py index c9815c775..0951c1291 100644 --- a/electrum/gui/qml/qeinvoicelistmodel.py +++ b/electrum/gui/qml/qeinvoicelistmodel.py @@ -19,7 +19,8 @@ class QEAbstractInvoiceListModel(QAbstractListModel): # define listmodel rolemap _ROLE_NAMES=('key', 'is_lightning', 'timestamp', 'date', 'message', 'amount', - 'status', 'status_str', 'address', 'expiration', 'type', 'onchain_fallback') + 'status', 'status_str', 'address', 'expiration', 'type', 'onchain_fallback', + 'lightning_invoice') _ROLE_KEYS = range(Qt.UserRole, Qt.UserRole + len(_ROLE_NAMES)) _ROLE_MAP = dict(zip(_ROLE_KEYS, [bytearray(x.encode()) for x in _ROLE_NAMES])) _ROLE_RMAP = dict(zip(_ROLE_NAMES, _ROLE_KEYS)) diff --git a/electrum/gui/qml/qewallet.py b/electrum/gui/qml/qewallet.py index 9b070bf7e..62c185312 100644 --- a/electrum/gui/qml/qewallet.py +++ b/electrum/gui/qml/qewallet.py @@ -415,8 +415,8 @@ class QEWallet(QObject): ## TODO: check this flow. Only if alias is defined in config. OpenAlias? #pass ##self.sign_payment_request(addr) - self._requestModel.add_invoice(self.wallet.get_request(req_key)) - return addr + + return req_key, addr @pyqtSlot(QEAmount, 'QString', int) @pyqtSlot(QEAmount, 'QString', int, bool) @@ -428,9 +428,11 @@ class QEWallet(QObject): self.requestCreateError.emit('fatal',_("You need to open a Lightning channel first.")) return # TODO maybe show a warning if amount exceeds lnworker.num_sats_can_receive (as in kivy) - key = self.wallet.lnworker.add_request(amount.satsInt, message, expiration) + # TODO fallback address robustness + addr = self.wallet.get_unused_address() + key = self.wallet.create_request(amount.satsInt, message, expiration, addr, True) else: - key = self.create_bitcoin_request(amount.satsInt, message, expiration, ignore_gap) + key, addr = self.create_bitcoin_request(amount.satsInt, message, expiration, ignore_gap) if not key: return self.addressModel.init_model() @@ -439,6 +441,7 @@ class QEWallet(QObject): return assert key is not None + self._requestModel.add_invoice(self.wallet.get_request(key)) self.requestCreateSuccess.emit() @pyqtSlot('QString')