URI: 
       twallet: factor out "what key to use for invoice" - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 897f90d6e88b694c1bde990a5abce85ccb8e8c96
   DIR parent 1a4e55a911f9c6783d5840c781f36e246c186efe
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Fri, 12 Mar 2021 17:29:54 +0100
       
       wallet: factor out "what key to use for invoice"
       
       fix: qt request list was not using the correct key
       
       Diffstat:
         M electrum/gui/kivy/uix/screens.py    |       8 +++-----
         M electrum/gui/qt/invoice_list.py     |       3 +--
         M electrum/gui/qt/request_list.py     |      11 +----------
         M electrum/wallet.py                  |      46 +++++++++++++++++--------------
       
       4 files changed, 31 insertions(+), 37 deletions(-)
       ---
   DIR diff --git a/electrum/gui/kivy/uix/screens.py b/electrum/gui/kivy/uix/screens.py
       t@@ -237,10 +237,10 @@ class SendScreen(CScreen, Logger):
                status = self.app.wallet.get_invoice_status(item)
                status_str = item.get_status_str(status)
                is_lightning = item.type == PR_TYPE_LN
       +        key = self.app.wallet.get_key_for_outgoing_invoice(item)
                if is_lightning:
                    assert isinstance(item, LNInvoice)
       -            key = item.rhash
       -            address = key
       +            address = item.rhash
                    if self.app.wallet.lnworker:
                        log = self.app.wallet.lnworker.logs.get(key)
                        if status == PR_INFLIGHT and log:
       t@@ -248,7 +248,6 @@ class SendScreen(CScreen, Logger):
                    is_bip70 = False
                else:
                    assert isinstance(item, OnchainInvoice)
       -            key = item.id
                    address = item.get_address()
                    is_bip70 = bool(item.bip70)
                return {
       t@@ -467,11 +466,10 @@ class ReceiveScreen(CScreen):
                if not is_lightning:
                    assert isinstance(req, OnchainInvoice)
                    address = req.get_address()
       -            key = address
                else:
                    assert isinstance(req, LNInvoice)
       -            key = req.rhash
                    address = req.invoice
       +        key = self.app.wallet.get_key_for_receive_request(req)
                amount = req.get_amount_sat()
                description = req.message
                status = self.app.wallet.get_request_status(key)
   DIR diff --git a/electrum/gui/qt/invoice_list.py b/electrum/gui/qt/invoice_list.py
       t@@ -99,11 +99,10 @@ class InvoiceList(MyTreeView):
                self.std_model.clear()
                self.update_headers(self.__class__.headers)
                for idx, item in enumerate(self.parent.wallet.get_unpaid_invoices()):
       +            key = self.parent.wallet.get_key_for_outgoing_invoice(item)
                    if item.is_lightning():
       -                key = item.rhash
                        icon_name = 'lightning.png'
                    else:
       -                key = item.id
                        icon_name = 'bitcoin.png'
                        if item.bip70:
                            icon_name = 'seal.png'
   DIR diff --git a/electrum/gui/qt/request_list.py b/electrum/gui/qt/request_list.py
       t@@ -148,12 +148,7 @@ class RequestList(MyTreeView):
                self.std_model.clear()
                self.update_headers(self.__class__.headers)
                for req in self.wallet.get_unpaid_requests():
       -            if req.is_lightning():
       -                assert isinstance(req, LNInvoice)
       -                key = req.rhash
       -            else:
       -                assert isinstance(req, OnchainInvoice)
       -                key = req.id
       +            key = self.wallet.get_key_for_receive_request(req)
                    status = self.parent.wallet.get_request_status(key)
                    status_str = req.get_status_str(status)
                    request_type = req.type
       t@@ -164,13 +159,9 @@ class RequestList(MyTreeView):
                    amount_str = self.parent.format_amount(amount) if amount else ""
                    labels = [date, message, amount_str, status_str]
                    if req.is_lightning():
       -                assert isinstance(req, LNInvoice)
       -                key = req.rhash
                        icon = read_QIcon("lightning.png")
                        tooltip = 'lightning request'
                    else:
       -                assert isinstance(req, OnchainInvoice)
       -                key = req.get_address()
                        icon = read_QIcon("bitcoin.png")
                        tooltip = 'onchain request'
                    items = [QStandardItem(e) for e in labels]
   DIR diff --git a/electrum/wallet.py b/electrum/wallet.py
       t@@ -766,20 +766,14 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                return invoice
        
            def save_invoice(self, invoice: Invoice) -> None:
       -        invoice_type = invoice.type
       -        if invoice_type == PR_TYPE_LN:
       -            assert isinstance(invoice, LNInvoice)
       -            key = invoice.rhash
       -        elif invoice_type == PR_TYPE_ONCHAIN:
       +        key = self.get_key_for_outgoing_invoice(invoice)
       +        if not invoice.is_lightning():
                    assert isinstance(invoice, OnchainInvoice)
       -            key = invoice.id
                    if self.is_onchain_invoice_paid(invoice, 0):
                        self.logger.info("saving invoice... but it is already paid!")
                    with self.transaction_lock:
                        for txout in invoice.outputs:
                            self._invoices_from_scriptpubkey_map[txout.scriptpubkey].add(key)
       -        else:
       -            raise Exception('Unsupported invoice type')
                self.invoices[key] = invoice
                self.save_db()
        
       t@@ -2073,12 +2067,7 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                    return self.export_request(x)
        
            def export_request(self, x: Invoice) -> Dict[str, Any]:
       -        if x.is_lightning():
       -            assert isinstance(x, LNInvoice)
       -            key = x.rhash
       -        else:
       -            assert isinstance(x, OnchainInvoice)
       -            key = x.get_address()
       +        key = self.get_key_for_receive_request(x)
                status = self.get_request_status(key)
                status_str = x.get_status_str(status)
                is_lightning = x.is_lightning()
       t@@ -2188,21 +2177,38 @@ class Abstract_Wallet(AddressSynchronizer, ABC):
                req['sig'] = bh2u(pr.signature)
                self.receive_requests[key] = req
        
       -    def add_payment_request(self, req: Invoice):
       +    @classmethod
       +    def get_key_for_outgoing_invoice(cls, invoice: Invoice) -> str:
       +        """Return the key to use for this invoice in self.invoices."""
       +        if invoice.is_lightning():
       +            assert isinstance(invoice, LNInvoice)
       +            key = invoice.rhash
       +        else:
       +            assert isinstance(invoice, OnchainInvoice)
       +            key = invoice.id
       +        return key
       +
       +    def get_key_for_receive_request(self, req: Invoice, *, sanity_checks: bool = False) -> str:
       +        """Return the key to use for this invoice in self.receive_requests."""
                if not req.is_lightning():
                    assert isinstance(req, OnchainInvoice)
                    addr = req.get_address()
       -            if not bitcoin.is_address(addr):
       -                raise Exception(_('Invalid Bitcoin address.'))
       -            if not self.is_mine(addr):
       -                raise Exception(_('Address not in wallet.'))
       +            if sanity_checks:
       +                if not bitcoin.is_address(addr):
       +                    raise Exception(_('Invalid Bitcoin address.'))
       +                if not self.is_mine(addr):
       +                    raise Exception(_('Address not in wallet.'))
                    key = addr
                else:
                    assert isinstance(req, LNInvoice)
                    key = req.rhash
       +        return key
       +
       +    def add_payment_request(self, req: Invoice):
       +        key = self.get_key_for_receive_request(req, sanity_checks=True)
                message = req.message
                self.receive_requests[key] = req
       -        self.set_label(key, message) # should be a default label
       +        self.set_label(key, message)  # should be a default label
                return req
        
            def delete_request(self, key):