Browse Source

lnworker: store invoices based on payment_hash

dependabot/pip/contrib/deterministic-build/ecdsa-0.13.3
SomberNight 6 years ago
committed by ThomasV
parent
commit
54edc9488a
  1. 11
      electrum/gui/qt/request_list.py
  2. 21
      electrum/lnworker.py

11
electrum/gui/qt/request_list.py

@ -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):
@ -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]
@ -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

21
electrum/lnworker.py

@ -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
@ -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)"""
@ -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)

Loading…
Cancel
Save