lnworker: store invoices based on payment_hash

This commit is contained in:
SomberNight 2018-10-23 18:54:23 +02:00
parent 0890a2beaa
commit 53027d6b2f
No known key found for this signature in database
GPG key ID: B33B5F232C6271E9
2 changed files with 18 additions and 18 deletions

View file

@ -70,7 +70,7 @@ class RequestList(MyTreeWidget):
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 on_update(self):
@ -111,23 +111,22 @@ class RequestList(MyTreeWidget):
item.setData(1, Qt.UserRole, address)
self.addTopLevelItem(item)
# 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)
item = QTreeWidgetItem([date, r, description, amount_str, ''])
item = QTreeWidgetItem([date, invoice, description, amount_str, ''])
item.setIcon(1, self.icon_cache.get(":icons/lightning.png"))
item.setData(0, Qt.UserRole, REQUEST_TYPE_LN)
item.setData(1, Qt.UserRole, payreq_key)
item.setData(1, Qt.UserRole, payreq_key) # RHASH hex
self.addTopLevelItem(item)
def create_menu(self, position):
@ -163,7 +162,7 @@ class RequestList(MyTreeWidget):
def create_menu_ln_payreq(self, item):
payreq_key = item.data(1, Qt.UserRole)
req = self.wallet.lnworker.invoices.get(payreq_key)
preimage, req = self.wallet.lnworker.invoices.get(payreq_key)
if req is None:
self.update()
return

View file

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