From 175bdf12329b40202424439201ae488021bd186d Mon Sep 17 00:00:00 2001 From: blink-claw-bot Date: Sun, 22 Mar 2026 15:43:01 +0100 Subject: [PATCH] fix: populate settlementVia.preImage in lnInvoicePaymentSend response - 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 #506 --- core/api/src/app/payments/index.types.d.ts | 1 + core/api/src/app/payments/ln-send-result.ts | 3 ++- core/api/src/app/payments/send-lightning.ts | 13 ++++++++++++- core/api/src/services/ledger/helpers.ts | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/api/src/app/payments/index.types.d.ts b/core/api/src/app/payments/index.types.d.ts index e6ad22b814..b0605b4bb4 100644 --- a/core/api/src/app/payments/index.types.d.ts +++ b/core/api/src/app/payments/index.types.d.ts @@ -25,6 +25,7 @@ type LnSendAttemptResult = | { type: PaymentSendAttemptResultTypeObj["Ok"] journalId: LedgerJournalId + revealedPreImage?: RevealedPreImage } | { type: PaymentSendAttemptResultTypeObj["Pending"] diff --git a/core/api/src/app/payments/ln-send-result.ts b/core/api/src/app/payments/ln-send-result.ts index c8e7bdf8b4..a9a27a46d4 100644 --- a/core/api/src/app/payments/ln-send-result.ts +++ b/core/api/src/app/payments/ln-send-result.ts @@ -21,9 +21,10 @@ export const IntraLedgerSendAttemptResult = { } export const LnSendAttemptResult = { - ok: (journalId: LedgerJournalId): LnSendAttemptResult => ({ + ok: (journalId: LedgerJournalId, revealedPreImage?: RevealedPreImage): LnSendAttemptResult => ({ type: PaymentSendAttemptResultType.Ok, journalId, + revealedPreImage, }), pending: (journalId: LedgerJournalId): LnSendAttemptResult => ({ type: PaymentSendAttemptResultType.Pending, diff --git a/core/api/src/app/payments/send-lightning.ts b/core/api/src/app/payments/send-lightning.ts index 57dea4ad31..8045f27d1f 100644 --- a/core/api/src/app/payments/send-lightning.ts +++ b/core/api/src/app/payments/send-lightning.ts @@ -790,6 +790,17 @@ const executePaymentViaLn = async ({ journalId: paymentSendAttemptResult.journalId, }) if (walletTransaction instanceof Error) return walletTransaction + + // Inject revealedPreImage into settlementVia if available and this was a successful Lightning payment + if ( + paymentSendAttemptResult.type === PaymentSendAttemptResultType.Ok && + paymentSendAttemptResult.revealedPreImage && + walletTransaction.settlementVia.type === "lightning" + ) { + // Type assertion is safe here because we've checked the transaction is Lightning + (walletTransaction.settlementVia as any).revealedPreImage = paymentSendAttemptResult.revealedPreImage + } + NotificationsService().sendTransaction({ recipient: notificationRecipient, transaction: walletTransaction, @@ -1050,7 +1061,7 @@ const lockedPaymentViaLnSteps = async ({ if (updateJournalTxnsState instanceof Error) { return LnSendAttemptResult.err(updateJournalTxnsState) } - return LnSendAttemptResult.ok(journalId) + return LnSendAttemptResult.ok(journalId, payResult.revealedPreImage) } const getAlreadyPaidResponse = async (args: { diff --git a/core/api/src/services/ledger/helpers.ts b/core/api/src/services/ledger/helpers.ts index 011b8fa287..a01ab7acbc 100644 --- a/core/api/src/services/ledger/helpers.ts +++ b/core/api/src/services/ledger/helpers.ts @@ -24,7 +24,7 @@ export const persistAndReturnEntry = async ({ hash, revealedPreImage, })) - txMetadataRepo.persistAll(txsMetadataToPersist) + await txMetadataRepo.persistAll(txsMetadataToPersist) return journalEntry } catch (err) {