timprove Qt Receive tab for LN payment requests - electrum - Electrum Bitcoin wallet (HTM) git clone https://git.parazyd.org/electrum (DIR) Log (DIR) Files (DIR) Refs (DIR) Submodules --- (DIR) commit 0d4593eebf84a13a8c8e351b66e817cc8c53ddbd (DIR) parent c7e47b74a9894f0320d11508e909c58813923b36 (HTM) Author: SomberNight <somber.night@protonmail.com> Date: Wed, 27 Jun 2018 21:54:57 +0200 improve Qt Receive tab for LN payment requests Diffstat: M electrum/gui/qt/main_window.py | 7 ++++++- M electrum/gui/qt/request_list.py | 41 ++++++++++++++++++++++++++++--- M lib/lnworker.py | 11 ++++++++++- 3 files changed, 53 insertions(+), 6 deletions(-) --- (DIR) diff --git a/electrum/gui/qt/main_window.py b/electrum/gui/qt/main_window.py t@@ -1005,6 +1005,11 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.request_list.update() self.clear_receive_tab() + def delete_lightning_payreq(self, payreq_key): + self.wallet.lnworker.delete_invoice(payreq_key) + self.request_list.update() + self.clear_receive_tab() + def get_request_URI(self, addr): req = self.wallet.receive_requests[addr] message = self.wallet.labels.get(addr, '') t@@ -1080,7 +1085,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger): self.show_error(_('Error adding payment request') + ':\n' + repr(e)) else: self.sign_payment_request(addr) - self.save_request_button.setEnabled(False) + #self.save_request_button.setEnabled(False) finally: self.address_list.update() (DIR) diff --git a/electrum/gui/qt/request_list.py b/electrum/gui/qt/request_list.py t@@ -34,9 +34,12 @@ from electrum.util import format_time, age from electrum.plugin import run_hook from electrum.paymentrequest import PR_UNKNOWN from electrum.wallet import InternalAddressCorruption +from electrum.bitcoin import COIN from .util import MyTreeView, pr_tooltips, pr_icons, read_QIcon +REQUEST_TYPE_BITCOIN = 0 +REQUEST_TYPE_LN = 1 class RequestList(MyTreeView): t@@ -97,7 +100,7 @@ class RequestList(MyTreeView): self.wallet = self.parent.wallet # hide receive tab if no receive requests available if self.parent.isVisible(): - b = len(self.wallet.receive_requests) > 0 + b = len(self.wallet.receive_requests) > 0 or len(self.wallet.lnworker.invoices) > 0 self.setVisible(b) self.parent.receive_requests_label.setVisible(b) if not b: t@@ -144,13 +147,16 @@ class RequestList(MyTreeView): items[self.Columns.STATUS].setIcon(read_QIcon(pr_icons.get(status))) items[self.Columns.DESCRIPTION].setData(address, Qt.UserRole) self.model().insertRow(self.model().rowCount(), items) + items[0].setData(REQUEST_TYPE_BITCOIN, Qt.UserRole) + items[0].setData(address, Qt.UserRole+1) self.filter() # lightning - for k, r in self.wallet.lnworker.invoices.items(): + for payreq_key, r in self.wallet.lnworker.invoices.items(): from electrum.lightning_payencode.lnaddr import lndecode import electrum.constants as constants lnaddr = lndecode(r, expected_hrp=constants.net.SEGWIT_HRP) - amount_str = self.parent.format_amount(lnaddr.amount*100000000) + amount_sat = lnaddr.amount*COIN if lnaddr.amount else None + amount_str = self.parent.format_amount(amount_sat) if amount_sat else '' for k,v in lnaddr.tags: if k == 'd': description = v t@@ -161,6 +167,8 @@ class RequestList(MyTreeView): labels = [date, r, '', description, amount_str, ''] items = [QStandardItem(e) for e in labels] items.setIcon(2, QIcon(":icons/lightning.png")) + items[0].setData(REQUEST_TYPE_LN, Qt.UserRole) + items[0].setData(payreq_key, Qt.UserRole+1) self.model().insertRow(self.model().rowCount(), items) def create_menu(self, position): t@@ -171,6 +179,16 @@ class RequestList(MyTreeView): if not item_addr: return addr = item_addr.text() + request_type = self.model().itemFromIndex(idx.sibling(idx.row(), 0)).data(Qt.UserRole) + menu = None + if request_type == REQUEST_TYPE_BITCOIN: + menu = self.create_menu_bitcoin_payreq(idx, addr) + elif request_type == REQUEST_TYPE_LN: + menu = self.create_menu_ln_payreq(idx, addr) + if menu: + menu.exec_(self.viewport().mapToGlobal(position)) + + def create_menu_bitcoin_payreq(self, idx, addr): req = self.wallet.receive_requests.get(addr) if req is None: self.update() t@@ -187,4 +205,19 @@ class RequestList(MyTreeView): menu.addAction(_("Save as BIP70 file"), lambda: self.parent.export_payment_request(addr)) menu.addAction(_("Delete"), lambda: self.parent.delete_payment_request(addr)) run_hook('receive_list_menu', menu, addr) - menu.exec_(self.viewport().mapToGlobal(position)) + return menu + + def create_menu_ln_payreq(self, idx, payreq_key): + req = self.wallet.lnworker.invoices.get(payreq_key) + if req is None: + self.update() + return + column = idx.column() + column_title = self.model().horizontalHeaderItem(column).text() + column_data = self.model().itemFromIndex(idx).text() + menu = QMenu(self) + if column != 2: + menu.addAction(_("Copy {}").format(column_title), lambda: self.parent.app.clipboard().setText(column_data)) + menu.addAction(_("Copy URI"), lambda: self.parent.view_and_paste('URI', '', req)) + menu.addAction(_("Delete"), lambda: self.parent.delete_lightning_payreq(payreq_key)) + return menu (DIR) diff --git a/lib/lnworker.py b/lib/lnworker.py t@@ -209,7 +209,8 @@ class LNWorker(PrintError): is_open = lambda chan: self.channel_state[chan.channel_id] == "OPEN" payment_preimage = os.urandom(32) RHASH = sha256(payment_preimage) - pay_req = lnencode(LnAddr(RHASH, amount_sat/Decimal(COIN), tags=[('d', message)]), self.privkey) + amount_btc = amount_sat/Decimal(COIN) if amount_sat else None + pay_req = lnencode(LnAddr(RHASH, amount_btc, tags=[('d', message)]), self.privkey) decoded = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP) assert decoded.pubkey.serialize() == privkey_to_pubkey(self.privkey) self.invoices[bh2u(payment_preimage)] = pay_req t@@ -217,6 +218,14 @@ class LNWorker(PrintError): self.wallet.storage.write() return pay_req + def delete_invoice(self, payreq_key): + try: + del self.invoices[payreq_key] + except KeyError: + return + self.wallet.storage.put('lightning_invoices', self.invoices) + self.wallet.storage.write() + def list_channels(self): return serialize_channels(self.channels)