1
0

lnworker: make "preimages" dict private

I want to hook into lnworker.save_preimage (not done yet).
Other modules should not put preimages into the dict directly.
This commit is contained in:
SomberNight
2025-05-14 13:23:02 +00:00
parent 37352ea5a9
commit 44f3444795
4 changed files with 13 additions and 9 deletions

View File

@@ -845,7 +845,7 @@ class LNWallet(LNWorker):
self.lnwatcher = LNWatcher(self)
self.lnrater: LNRater = None
self.payment_info = self.db.get_dict('lightning_payments') # RHASH -> amount, direction, is_paid
self.preimages = self.db.get_dict('lightning_preimages') # RHASH -> preimage
self._preimages = self.db.get_dict('lightning_preimages') # RHASH -> preimage
self._bolt11_cache = {}
# note: this sweep_address is only used as fallback; as it might result in address-reuse
self.logs = defaultdict(list) # type: Dict[str, List[HtlcLog]] # key is RHASH # (not persisted)
@@ -2286,13 +2286,13 @@ class LNWallet(LNWorker):
def save_preimage(self, payment_hash: bytes, preimage: bytes, *, write_to_disk: bool = True):
if sha256(preimage) != payment_hash:
raise Exception("tried to save incorrect preimage for payment_hash")
self.preimages[payment_hash.hex()] = preimage.hex()
self._preimages[payment_hash.hex()] = preimage.hex()
if write_to_disk:
self.wallet.save_db()
def get_preimage(self, payment_hash: bytes) -> Optional[bytes]:
assert isinstance(payment_hash, bytes), f"expected bytes, but got {type(payment_hash)}"
preimage_hex = self.preimages.get(payment_hash.hex())
preimage_hex = self._preimages.get(payment_hash.hex())
if preimage_hex is None:
return None
preimage_bytes = bytes.fromhex(preimage_hex)
@@ -2300,6 +2300,10 @@ class LNWallet(LNWorker):
raise Exception("found incorrect preimage for payment_hash")
return preimage_bytes
def get_preimage_hex(self, payment_hash: str) -> Optional[str]:
preimage_bytes = self.get_preimage(bytes.fromhex(payment_hash)) or b""
return preimage_bytes.hex() or None
def get_payment_info(self, payment_hash: bytes) -> Optional[PaymentInfo]:
"""returns None if payment_hash is a payment we are forwarding"""
key = payment_hash.hex()

View File

@@ -534,7 +534,7 @@ class NWCServer(Logger, EventListener):
if direction:
response['result']['type'] = direction
if status == PR_PAID:
response['result']['preimage'] = self.wallet.lnworker.preimages.get(invoice.rhash, "not found")
response['result']['preimage'] = self.wallet.lnworker.get_preimage_hex(invoice.rhash) or "not found"
self.logger.debug(f"lookup_invoice response: {response}")
await self.send_encrypted_response(request_event.pubkey, json.dumps(response), request_event.id)
@@ -731,7 +731,7 @@ class NWCServer(Logger, EventListener):
"amount": request.get_amount_msat(),
"created_at": request.time,
"expires_at": request.get_expiration_date(),
"preimage": self.wallet.lnworker.preimages.get(request.rhash, "not found"),
"preimage": self.wallet.lnworker.get_preimage_hex(invoice.rhash) or "not found",
"metadata": {},
"fees_paid": 0
}
@@ -760,7 +760,7 @@ class NWCServer(Logger, EventListener):
"type": "outgoing",
"invoice": invoice.lightning_invoice or "",
"description": invoice.message,
"preimage": self.wallet.lnworker.preimages.get(key, "not found"),
"preimage": self.wallet.lnworker.get_preimage_hex(invoice.rhash) or "not found",
"payment_hash": invoice.rhash,
"amount": invoice.get_amount_msat(),
"created_at": invoice.time,

View File

@@ -392,7 +392,7 @@ class SwapManager(Logger):
if preimage:
swap.preimage = preimage
self.logger.info(f'found preimage: {preimage.hex()}')
self.lnworker.preimages[swap.payment_hash.hex()] = preimage.hex()
self.lnworker.save_preimage(swap.payment_hash, preimage)
else:
# this is our refund tx
if spent_height > 0:

View File

@@ -206,7 +206,7 @@ class MockLNWallet(Logger, EventListener, NetworkRetryManager[LNPeerAddr]):
self.active_forwardings = {}
self.forwarding_failures = {}
self.inflight_payments = set()
self.preimages = {}
self._preimages = {}
self.stopping_soon = False
self.downstream_to_upstream_htlc = {}
self.dont_settle_htlcs = {}
@@ -593,7 +593,7 @@ class TestPeer(ElectrumTestCase):
def prepare_recipient(self, w2, payment_hash, test_hold_invoice, test_failure):
if not test_hold_invoice and not test_failure:
return
preimage = bytes.fromhex(w2.preimages.pop(payment_hash.hex()))
preimage = bytes.fromhex(w2._preimages.pop(payment_hash.hex()))
if test_hold_invoice:
async def cb(payment_hash):
if not test_failure: