URI: 
       tqt history: fixes for tx context-menu "View invoice" if more than one - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 6b4edc650abb66fcd1190452a0e2ad7e89d1e8e4
   DIR parent 72950bf3794b2a2dce67c152ebc8fadb8d31b8a1
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Mon, 31 Aug 2020 20:55:14 +0200
       
       qt history: fixes for tx context-menu "View invoice" if more than one
       
       fixes #6516
       
       coalesce "View invoice" options into submenu if there are multiple;
       also make sure lambda uses bound argument
       
       Diffstat:
         M electrum/gui/qt/history_list.py     |      12 +++++++-----
         M electrum/wallet.py                  |      15 ++++++++++-----
       
       2 files changed, 17 insertions(+), 10 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/history_list.py b/electrum/gui/qt/history_list.py
       t@@ -675,7 +675,6 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
                tx_URL = block_explorer_URL(self.config, 'tx', tx_hash)
                tx_details = self.wallet.get_tx_info(tx)
                is_unconfirmed = tx_details.tx_mined_status.height <= 0
       -        invoice_keys = self.wallet.get_relevant_invoice_keys_for_tx(tx)
                menu = QMenu()
                if tx_details.can_remove:
                    menu.addAction(_("Remove"), lambda: self.remove_local_tx(tx_hash))
       t@@ -699,10 +698,13 @@ class HistoryList(MyTreeView, AcceptFileDragDrop):
                        child_tx = self.wallet.cpfp(tx, 0)
                        if child_tx:
                            menu.addAction(_("Child pays for parent"), lambda: self.parent.cpfp(tx, child_tx))
       -        for key in invoice_keys:
       -            invoice = self.parent.wallet.get_invoice(key)
       -            if invoice:
       -                menu.addAction(_("View invoice"), lambda: self.parent.show_onchain_invoice(invoice))
       +        invoices = self.wallet.get_relevant_invoices_for_tx(tx)
       +        if len(invoices) == 1:
       +            menu.addAction(_("View invoice"), lambda inv=invoices[0]: self.parent.show_onchain_invoice(inv))
       +        elif len(invoices) > 1:
       +            menu_invs = menu.addMenu(_("Related invoices"))
       +            for inv in invoices:
       +                menu_invs.addAction(_("View invoice"), lambda inv=inv: self.parent.show_onchain_invoice(inv))
                if tx_URL:
                    menu.addAction(_("View on block explorer"), lambda: webopen(tx_URL))
                menu.exec_(self.viewport().mapToGlobal(position))
   DIR diff --git a/electrum/wallet.py b/electrum/wallet.py
       t@@ -764,7 +764,7 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
            def export_invoices(self, path):
                write_json_file(path, list(self.invoices.values()))
        
       -    def get_relevant_invoice_keys_for_tx(self, tx: Transaction) -> Set[str]:
       +    def _get_relevant_invoice_keys_for_tx(self, tx: Transaction) -> Set[str]:
                relevant_invoice_keys = set()
                for txout in tx.outputs():
                    for invoice_key in self._invoices_from_scriptpubkey_map.get(txout.scriptpubkey, set()):
       t@@ -773,6 +773,14 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                            relevant_invoice_keys.add(invoice_key)
                return relevant_invoice_keys
        
       +    def get_relevant_invoices_for_tx(self, tx: Transaction) -> Sequence[OnchainInvoice]:
       +        invoice_keys = self._get_relevant_invoice_keys_for_tx(tx)
       +        invoices = [self.get_invoice(key) for key in invoice_keys]
       +        invoices = [inv for inv in invoices if inv]  # filter out None
       +        for inv in invoices:
       +            assert isinstance(inv, OnchainInvoice), f"unexpected type {type(inv)}"
       +        return invoices
       +
            def _prepare_onchain_invoice_paid_detection(self):
                # scriptpubkey -> list(invoice_keys)
                self._invoices_from_scriptpubkey_map = defaultdict(set)  # type: Dict[bytes, Set[str]]
       t@@ -811,10 +819,7 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                tx_hash = tx.txid()
                with self.transaction_lock:
                    labels = []
       -            for invoice_key in self.get_relevant_invoice_keys_for_tx(tx):
       -                invoice = self.invoices.get(invoice_key)
       -                if invoice is None: continue
       -                assert isinstance(invoice, OnchainInvoice)
       +            for invoice in self.get_relevant_invoices_for_tx(tx):
                        if invoice.message:
                            labels.append(invoice.message)
                if labels: