Skip to content

fix: populate settlementVia.preImage in lnInvoicePaymentSend response#507

Open
blink-claw-bot wants to merge 1 commit intoblinkbitcoin:mainfrom
blink-claw-bot:fix/506-preimage-in-payment-send
Open

fix: populate settlementVia.preImage in lnInvoicePaymentSend response#507
blink-claw-bot wants to merge 1 commit intoblinkbitcoin:mainfrom
blink-claw-bot:fix/506-preimage-in-payment-send

Conversation

@blink-claw-bot
Copy link
Copy Markdown

Closes #506

Problem

The lnInvoicePaymentSend GraphQL mutation was returning null for settlementVia.preImage even when Lightning payments succeeded. This was due to a race condition where:

  1. After a successful LND payment, lockedPaymentViaLnSteps calls LedgerFacade.updateMetadataByHash() to persist the preimage
  2. Then executePaymentViaLn calls getTransactionForWalletByJournalId to fetch the wallet transaction
  3. However, in helpers.ts, persistAndReturnEntry does NOT await txMetadataRepo.persistAll() (fire-and-forget)
  4. So when updateMetadataByHash runs, the metadata documents may not exist yet, causing modifiedCount: 0NoTransactionToUpdateError
  5. The error is silently ignored, so the preimage is never persisted

Root Cause Analysis

The race condition occurs in core/api/src/services/ledger/helpers.ts line 28 where txMetadataRepo.persistAll() is called without await.

Solution

This PR implements two fixes:

Primary Fix: Direct PreImage Injection

  • Modified LnSendAttemptResult.ok() to accept an optional revealedPreImage parameter
  • Updated the type definitions to include the preimage field
  • In lockedPaymentViaLnSteps, pass the payResult.revealedPreImage to LnSendAttemptResult.ok()
  • In executePaymentViaLn, inject the preImage directly into the wallet transaction's settlementVia before returning

Secondary Fix: Race Condition

  • Added await to txMetadataRepo.persistAll() in helpers.ts to prevent the race condition

Testing

  • TypeScript compilation passes (pnpm tsc --noEmit)
  • All changes are surgical, minimal edits to existing code
  • Preserves existing functionality while fixing the bug

Files Modified

  • core/api/src/app/payments/send-lightning.ts — main fix
  • core/api/src/app/payments/ln-send-result.ts — result type update
  • core/api/src/app/payments/index.types.d.ts — type definitions
  • core/api/src/services/ledger/helpers.ts — race condition fix

🤖 PR by claude-sonnet-4 via Blink-Claw-Bot

- Add optional revealedPreImage field to LnSendAttemptResult type
- Pass revealedPreImage from successful Lightning payments to result
- Inject preImage into wallet transaction settlementVia before returning
- Fix race condition by awaiting persistAll() in helpers.ts

Closes blinkbitcoin#506
@github-actions github-actions Bot added the core label Mar 22, 2026
@openoms openoms requested a review from dolcalmi March 23, 2026 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Return preImage inline from lnInvoicePaymentSend via settlementVia resolver population

1 participant