lnworker: store invoices based on payment_hash
This commit is contained in:
@@ -84,7 +84,7 @@ class RequestList(MyTreeView):
|
|||||||
if request_type == REQUEST_TYPE_BITCOIN:
|
if request_type == REQUEST_TYPE_BITCOIN:
|
||||||
req = self.parent.get_request_URI(key)
|
req = self.parent.get_request_URI(key)
|
||||||
elif request_type == REQUEST_TYPE_LN:
|
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)
|
self.parent.receive_address_e.setText(req)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
@@ -140,18 +140,17 @@ class RequestList(MyTreeView):
|
|||||||
items[0].setData(address, Qt.UserRole+1)
|
items[0].setData(address, Qt.UserRole+1)
|
||||||
self.filter()
|
self.filter()
|
||||||
# lightning
|
# 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
|
from electrum.lnaddr import lndecode
|
||||||
import electrum.constants as constants
|
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_sat = lnaddr.amount*COIN if lnaddr.amount else None
|
||||||
amount_str = self.parent.format_amount(amount_sat) if amount_sat else ''
|
amount_str = self.parent.format_amount(amount_sat) if amount_sat else ''
|
||||||
|
description = ''
|
||||||
for k,v in lnaddr.tags:
|
for k,v in lnaddr.tags:
|
||||||
if k == 'd':
|
if k == 'd':
|
||||||
description = v
|
description = v
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
description = ''
|
|
||||||
date = format_time(lnaddr.date)
|
date = format_time(lnaddr.date)
|
||||||
labels = [date, r, '', description, amount_str, '']
|
labels = [date, r, '', description, amount_str, '']
|
||||||
items = [QStandardItem(e) for e in labels]
|
items = [QStandardItem(e) for e in labels]
|
||||||
@@ -197,7 +196,7 @@ class RequestList(MyTreeView):
|
|||||||
return menu
|
return menu
|
||||||
|
|
||||||
def create_menu_ln_payreq(self, idx, payreq_key):
|
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:
|
if req is None:
|
||||||
self.update()
|
self.update()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ class LNWorker(PrintError):
|
|||||||
for c in self.channels.values():
|
for c in self.channels.values():
|
||||||
c.lnwatcher = network.lnwatcher
|
c.lnwatcher = network.lnwatcher
|
||||||
c.sweep_address = self.sweep_address
|
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():
|
for chan_id, chan in self.channels.items():
|
||||||
self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str())
|
self.network.lnwatcher.watch_channel(chan.get_funding_address(), chan.funding_outpoint.to_str())
|
||||||
self._last_tried_peer = {} # LNPeerAddr -> unix timestamp
|
self._last_tried_peer = {} # LNPeerAddr -> unix timestamp
|
||||||
@@ -342,18 +342,19 @@ class LNWorker(PrintError):
|
|||||||
('c', MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE)]
|
('c', MIN_FINAL_CLTV_EXPIRY_FOR_INVOICE)]
|
||||||
+ routing_hints),
|
+ routing_hints),
|
||||||
self.node_keypair.privkey)
|
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.put('lightning_invoices', self.invoices)
|
||||||
self.wallet.storage.write()
|
self.wallet.storage.write()
|
||||||
return pay_req
|
return pay_req
|
||||||
|
|
||||||
def get_invoice(self, payment_hash: bytes) -> Tuple[bytes, LnAddr]:
|
def get_invoice(self, payment_hash: bytes) -> Tuple[bytes, LnAddr]:
|
||||||
for k in self.invoices.keys():
|
try:
|
||||||
preimage = bfh(k)
|
preimage_hex, pay_req = self.invoices[bh2u(payment_hash)]
|
||||||
if sha256(preimage) == payment_hash:
|
preimage = bfh(preimage_hex)
|
||||||
return preimage, lndecode(self.invoices[k], expected_hrp=constants.net.SEGWIT_HRP)
|
assert sha256(preimage) == payment_hash
|
||||||
else:
|
return preimage, lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
|
||||||
raise UnknownPaymentHash()
|
except KeyError as e:
|
||||||
|
raise UnknownPaymentHash(payment_hash) from e
|
||||||
|
|
||||||
def _calc_routing_hints_for_invoice(self, amount_sat):
|
def _calc_routing_hints_for_invoice(self, amount_sat):
|
||||||
"""calculate routing hints (BOLT-11 'r' field)"""
|
"""calculate routing hints (BOLT-11 'r' field)"""
|
||||||
@@ -395,9 +396,9 @@ class LNWorker(PrintError):
|
|||||||
cltv_expiry_delta)]))
|
cltv_expiry_delta)]))
|
||||||
return routing_hints
|
return routing_hints
|
||||||
|
|
||||||
def delete_invoice(self, payreq_key):
|
def delete_invoice(self, payment_hash_hex: str):
|
||||||
try:
|
try:
|
||||||
del self.invoices[payreq_key]
|
del self.invoices[payment_hash_hex]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return
|
return
|
||||||
self.wallet.storage.put('lightning_invoices', self.invoices)
|
self.wallet.storage.put('lightning_invoices', self.invoices)
|
||||||
|
|||||||
Reference in New Issue
Block a user