1
0

submarine swaps: check preimage before labeling a refund transaction

This commit is contained in:
ThomasV
2025-03-14 09:13:18 +01:00
parent ba2e4ff99a
commit fff90a1426

View File

@@ -316,6 +316,12 @@ class SwapManager(Logger):
if not swap.is_funded(): if not swap.is_funded():
self.swaps.pop(swap.payment_hash.hex()) self.swaps.pop(swap.payment_hash.hex())
def extract_preimage(self, swap, claim_tx):
for txin in claim_tx.inputs():
preimage = txin.witness_elements()[1]
if sha256(preimage) == swap.payment_hash:
return preimage
@log_exceptions @log_exceptions
async def _claim_swap(self, swap: SwapData) -> None: async def _claim_swap(self, swap: SwapData) -> None:
assert self.network assert self.network
@@ -363,13 +369,11 @@ class SwapManager(Logger):
if swap.preimage is None and spent_height is not None: if swap.preimage is None and spent_height is not None:
# extract the preimage, add it to lnwatcher # extract the preimage, add it to lnwatcher
claim_tx = self.lnwatcher.adb.get_transaction(txin.spent_txid) claim_tx = self.lnwatcher.adb.get_transaction(txin.spent_txid)
for txin in claim_tx.inputs(): preimage = self.extract_preimage(swap, claim_tx)
preimage = txin.witness_elements()[1] if preimage:
if sha256(preimage) == swap.payment_hash:
swap.preimage = preimage swap.preimage = preimage
self.logger.info(f'found preimage: {preimage.hex()}') self.logger.info(f'found preimage: {preimage.hex()}')
self.lnworker.preimages[swap.payment_hash.hex()] = preimage.hex() self.lnworker.preimages[swap.payment_hash.hex()] = preimage.hex()
break
else: else:
# this is our refund tx # this is our refund tx
if spent_height > 0: if spent_height > 0:
@@ -1178,6 +1182,8 @@ class SwapManager(Logger):
'group_label': group_label, 'group_label': group_label,
} }
if not swap.is_reverse: if not swap.is_reverse:
claim_tx = self.lnwatcher.adb.get_transaction(swap.spending_txid)
if claim_tx and not self.extract_preimage(swap, claim_tx):
# if the spending_tx is in the wallet, this will add it # if the spending_tx is in the wallet, this will add it
# to the group (see wallet.get_full_history) # to the group (see wallet.get_full_history)
d[swap.spending_txid] = { d[swap.spending_txid] = {