diff --git a/electrum/lnworker.py b/electrum/lnworker.py index 7be4673a1..7e074afbb 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -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() diff --git a/electrum/plugins/nwc/nwcserver.py b/electrum/plugins/nwc/nwcserver.py index be77a661f..9d96f424e 100644 --- a/electrum/plugins/nwc/nwcserver.py +++ b/electrum/plugins/nwc/nwcserver.py @@ -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, diff --git a/electrum/submarine_swaps.py b/electrum/submarine_swaps.py index ece5f20fa..d9785d6e9 100644 --- a/electrum/submarine_swaps.py +++ b/electrum/submarine_swaps.py @@ -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: diff --git a/tests/test_lnpeer.py b/tests/test_lnpeer.py index b5624167f..083ddea01 100644 --- a/tests/test_lnpeer.py +++ b/tests/test_lnpeer.py @@ -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: