URI: 
       tlnworker: store invoices based on payment_hash - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 54edc9488a5b774d672a150b470dc4f2fc7a9ce8
   DIR parent d9facabc8ca1ea9491da62da860118b7959b06e2
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Tue, 23 Oct 2018 18:54:23 +0200
       
       lnworker: store invoices based on payment_hash
       
       Diffstat:
         M electrum/gui/qt/request_list.py     |      11 +++++------
         M electrum/lnworker.py                |      21 +++++++++++----------
       
       2 files changed, 16 insertions(+), 16 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/request_list.py b/electrum/gui/qt/request_list.py
       t@@ -84,7 +84,7 @@ class RequestList(MyTreeView):
                if request_type == REQUEST_TYPE_BITCOIN:
                    req = self.parent.get_request_URI(key)
                elif request_type == REQUEST_TYPE_LN:
       -            req = self.wallet.lnworker.invoices.get(key)
       +            preimage, req = self.wallet.lnworker.invoices.get(key)
                self.parent.receive_address_e.setText(req)
        
            def update(self):
       t@@ -140,18 +140,17 @@ class RequestList(MyTreeView):
                    items[0].setData(address, Qt.UserRole+1)
                self.filter()
                # lightning
       -        for payreq_key, r in self.wallet.lnworker.invoices.items():
       +        for payreq_key, (preimage_hex, invoice) in self.wallet.lnworker.invoices.items():
                    from electrum.lnaddr import lndecode
                    import electrum.constants as constants
       -            lnaddr = lndecode(r, expected_hrp=constants.net.SEGWIT_HRP)
       +            lnaddr = lndecode(invoice, expected_hrp=constants.net.SEGWIT_HRP)
                    amount_sat = lnaddr.amount*COIN if lnaddr.amount else None
                    amount_str = self.parent.format_amount(amount_sat) if amount_sat else ''
       +            description = ''
                    for k,v in lnaddr.tags:
                        if k == 'd':
                            description = v
                            break
       -                else:
       -                    description = ''
                    date = format_time(lnaddr.date)
                    labels = [date, r, '', description, amount_str, '']
                    items = [QStandardItem(e) for e in labels]
       t@@ -197,7 +196,7 @@ class RequestList(MyTreeView):
                return menu
        
            def create_menu_ln_payreq(self, idx, payreq_key):
       -        req = self.wallet.lnworker.invoices.get(payreq_key)
       +        preimage, req = self.wallet.lnworker.invoices.get(payreq_key)
                if req is None:
                    self.update()
                    return
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -64,7 +64,7 @@ class LNWorker(PrintError):
                for c in self.channels.values():
                    c.lnwatcher = network.lnwatcher
                    c.sweep_address = self.sweep_address
       -        self.invoices = wallet.storage.get('lightning_invoices', {})
       +        self.invoices = wallet.storage.get('lightning_invoices', {})  # type: Dict[str, Tuple[str,str]]  # RHASH -> (preimage, invoice)
                for chan_id, chan in self.channels.items():
                    self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str())
                self._last_tried_peer = {}  # LNPeerAddr -> unix timestamp
       t@@ -342,18 +342,19 @@ class LNWorker(PrintError):
                                                ('c', MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE)]
                                               + routing_hints),
                                   self.node_keypair.privkey)
       -        self.invoices[bh2u(payment_preimage)] = pay_req
       +        self.invoices[bh2u(RHASH)] = (bh2u(payment_preimage), pay_req)
                self.wallet.storage.put('lightning_invoices', self.invoices)
                self.wallet.storage.write()
                return pay_req
        
            def get_invoice(self, payment_hash: bytes) -> Tuple[bytes, LnAddr]:
       -        for k in self.invoices.keys():
       -            preimage = bfh(k)
       -            if sha256(preimage) == payment_hash:
       -                return preimage, lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP)
       -        else:
       -            raise UnknownPaymentHash()
       +        try:
       +            preimage_hex, pay_req = self.invoices[bh2u(payment_hash)]
       +            preimage = bfh(preimage_hex)
       +            assert sha256(preimage) == payment_hash
       +            return preimage, lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
       +        except KeyError as e:
       +            raise UnknownPaymentHash(payment_hash) from e
        
            def _calc_routing_hints_for_invoice(self, amount_sat):
                """calculate routing hints (BOLT-11 'r' field)"""
       t@@ -395,9 +396,9 @@ class LNWorker(PrintError):
                                                 cltv_expiry_delta)]))
                return routing_hints
        
       -    def delete_invoice(self, payreq_key):
       +    def delete_invoice(self, payment_hash_hex: str):
                try:
       -            del self.invoices[payreq_key]
       +            del self.invoices[payment_hash_hex]
                except KeyError:
                    return
                self.wallet.storage.put('lightning_invoices', self.invoices)