taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit 92cda5bdcc01dd2259e654d602a800bc7933ecb9
parent 86efee05f4f96995f7aad16e6b31c57525deef8a
Author: Florian Dold <dold@taler.net>
Date:   Mon, 23 Nov 2026 23:16:46 +0100

wallet-core: notify clients when transaction details change

A new payment choice, KYC review, or bank confirmation URL can change
what the user sees while the transaction stays in the same state.
Let those writers publish a transaction update.

Diffstat:
Mpackages/taler-wallet-core/src/common.ts | 6+++---
Mpackages/taler-wallet-core/src/observe-transaction.test.ts | 53+++++++++++++++++++++++++++++++++++++++++++----------
Mpackages/taler-wallet-core/src/pay-merchant.ts | 19++++++++++---------
Mpackages/taler-wallet-core/src/pay-peer-pull-credit.ts | 5++++-
Mpackages/taler-wallet-core/src/pay-peer-push-credit.ts | 5++++-
Mpackages/taler-wallet-core/src/transactions.ts | 22+++++++++++-----------
Mpackages/taler-wallet-core/src/withdraw.ts | 42++++++++++++++++++++++++++++++++++++++----
7 files changed, 113 insertions(+), 39 deletions(-)

diff --git a/packages/taler-wallet-core/src/common.ts b/packages/taler-wallet-core/src/common.ts @@ -1096,7 +1096,7 @@ export interface RecordHandle<T> { newRec: T | undefined, causeHint: string, eff?: BalanceEffect, - errorInfo?: ErrorInfoSummary, + notification?: { errorInfo?: ErrorInfoSummary; detailsChanged?: boolean }, ): Promise<void>; } @@ -1127,7 +1127,7 @@ export async function getGenericRecordHandle<T>( newRec: T | undefined, causeHint: string, eff?: BalanceEffect, - errorInfo?: ErrorInfoSummary, + notification?: { errorInfo?: ErrorInfoSummary; detailsChanged?: boolean }, ) => { let newTxState: TransactionState; let newStId: number; @@ -1159,7 +1159,7 @@ export async function getGenericRecordHandle<T>( newTxState, balanceEffect: eff ?? BalanceEffect.Any, causeHint, - errorInfo, + ...notification, oldStId, newStId, }); diff --git a/packages/taler-wallet-core/src/observe-transaction.test.ts b/packages/taler-wallet-core/src/observe-transaction.test.ts @@ -57,6 +57,8 @@ import { } from "./observe-transaction.js"; import { PeerPushCreditTransactionContext } from "./pay-peer-push-credit.js"; import { dispatchRequestInternal } from "./requests.js"; +import { WalletDbTransaction } from "./db/transaction.js"; +import { WithdrawTransactionContext } from "./withdraw.js"; import { RefundTransactionContext } from "./pay-merchant.js"; import { TaskSchedulerImpl } from "./shepherd.js"; import { @@ -228,6 +230,29 @@ async function fixture(makeRunner: (typeof runnerFactories)[number]) { await ws.db.runReadWriteTx(async () => {}); } } + async function updateCredit( + tx: WalletDbTransaction, + updated: WalletPeerPushCredit, + ) { + const ctx = new PeerPushCreditTransactionContext( + wex, + updated.peerPushCreditId, + ); + const [, handle] = await ctx.getRecordHandle(tx); + await handle.update(updated, "credit-update", undefined, { + detailsChanged: true, + }); + } + async function updateWithdrawal( + tx: WalletDbTransaction, + updated: WalletWithdrawalGroup, + ) { + const ctx = new WithdrawTransactionContext(wex, updated.withdrawalGroupId); + const [, handle] = await ctx.getRecordHandle(tx); + await handle.update(updated, "withdrawal-update", undefined, { + detailsChanged: true, + }); + } return { db, ws, @@ -238,6 +263,8 @@ async function fixture(makeRunner: (typeof runnerFactories)[number]) { credit, notifications, nextSnapshot, + updateCredit, + updateWithdrawal, observe, settle, }; @@ -479,7 +506,7 @@ for (const makeRunner of runnerFactories) { const first = await f.observe(); const next = f.nextSnapshot(); await f.wex.runWalletDbTx((tx) => - tx.upsertWithdrawalGroup({ + f.updateWithdrawal(tx, { ...withdrawal, status: WithdrawalGroupStatus.PendingKyc, kycAccessToken: "withdraw-kyc", @@ -613,7 +640,7 @@ for (const makeRunner of runnerFactories) { }); await readDone.promise; await f.wex.runWalletDbTx((tx) => - tx.upsertPeerPushCredit({ + f.updateCredit(tx, { ...f.credit, status: PeerPushCreditStatus.Aborted, }), @@ -690,7 +717,8 @@ for (const makeRunner of runnerFactories) { } let t = await change(() => f.wex.runWalletDbTx((tx) => - tx.upsertPeerPushCredit( + f.updateCredit( + tx, (credit = { ...credit, kycLastAmlReview: true }), ), ), @@ -715,20 +743,21 @@ for (const makeRunner of runnerFactories) { }; t = await change(() => f.wex.runWalletDbTx(async (tx) => { - await tx.upsertWithdrawalGroup(withdrawal); + await f.updateWithdrawal(tx, withdrawal); credit = { ...credit, status: PeerPushCreditStatus.PendingWithdrawing, withdrawalGroupId: "withdrawal", }; - await tx.upsertPeerPushCredit(credit); + await f.updateCredit(tx, credit); }), ); assert.equal(t.kycUrl, undefined); assert.equal(t.kycAmlReview, undefined); t = await change(() => f.wex.runWalletDbTx((tx) => - tx.upsertWithdrawalGroup( + f.updateWithdrawal( + tx, (withdrawal = { ...withdrawal, status: WithdrawalGroupStatus.PendingKyc, @@ -739,7 +768,8 @@ for (const makeRunner of runnerFactories) { assert.equal(t.txState.minor, TransactionMinorState.KycInit); t = await change(() => f.wex.runWalletDbTx((tx) => - tx.upsertWithdrawalGroup( + f.updateWithdrawal( + tx, (withdrawal = { ...withdrawal, kycAccessToken: "withdraw-token", @@ -753,7 +783,8 @@ for (const makeRunner of runnerFactories) { assert.equal(t.kycUrl, `${exchangeUrl}kyc-spa/withdraw-token`); t = await change(() => f.wex.runWalletDbTx((tx) => - tx.upsertPeerPushCredit( + f.updateCredit( + tx, (credit = { ...credit, status: PeerPushCreditStatus.SuspendedWithdrawing, @@ -770,13 +801,15 @@ for (const makeRunner of runnerFactories) { let reserveId = 0; t = await change(() => f.wex.runWalletDbTx(async (tx) => { - await tx.upsertPeerPushCredit( + await f.updateCredit( + tx, (credit = { ...credit, status: PeerPushCreditStatus.PendingWithdrawing, }), ); - await tx.upsertWithdrawalGroup( + await f.updateWithdrawal( + tx, (withdrawal = { ...withdrawal, status: WithdrawalGroupStatus.PendingBalanceKyc, diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts @@ -574,12 +574,9 @@ export class PayMerchantTransactionContext implements TransactionContext { this.transactionId, rec.payInfo?.payTokenSelection?.tokenPubs ?? [], ); - await h.update( - undefined, - opts.causeHint ?? "delete", - undefined, - opts.errorInfo, - ); + await h.update(undefined, opts.causeHint ?? "delete", undefined, { + errorInfo: opts.errorInfo, + }); } async userSuspendTransaction(): Promise<void> { @@ -1256,8 +1253,7 @@ export async function failProposalClaimPermanently( purchase.purchaseStatus = PurchaseStatus.FailedClaim; purchase.failReason = storedError; await h.update(purchase, "claim-failed", BalanceEffect.None, { - code: storedError.code as number, - hint: storedError.hint, + errorInfo: { code: storedError.code as number, hint: storedError.hint }, }); return true; }); @@ -3649,11 +3645,16 @@ export async function confirmPay( "this payment cannot be confirmed in its current state", ); } + const detailsChanged = + p.choiceIndex !== choiceIndex || + (p.download !== undefined && p.download.currency !== currency); p.choiceIndex = choiceIndex; if (p.download) { p.download.currency = currency; } - await h.update(p, "select-payment-choice", BalanceEffect.None); + await h.update(p, "select-payment-choice", BalanceEffect.None, { + detailsChanged, + }); return "selected" as const; }); diff --git a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts @@ -974,6 +974,9 @@ async function processPendingMergeKycRequired( if (!rec) { return; } + const detailsChanged = + rec.kycLastAmlReview !== updatedStatus.lastAmlReview || + rec.kycAccessToken !== updatedStatus.accessToken; rec.kycLastAmlReview = updatedStatus.lastAmlReview; rec.kycLastCheckStatus = updatedStatus.lastCheckStatus; rec.kycLastCheckCode = updatedStatus.lastCheckCode; @@ -987,7 +990,7 @@ async function processPendingMergeKycRequired( rec.status = PeerPullPaymentCreditStatus.FinalizingKycHardLimit; rec.failReason = hardLimitReason; } - await h.update(rec, "merge-kyc-status"); + await h.update(rec, "merge-kyc-status", undefined, { detailsChanged }); }); return hardLimitReason ? TaskRunResult.progress() : algoRes.taskResult; diff --git a/packages/taler-wallet-core/src/pay-peer-push-credit.ts b/packages/taler-wallet-core/src/pay-peer-push-credit.ts @@ -984,6 +984,9 @@ async function processPeerPushDebitMergeKyc( if (!rec) { return; } + const detailsChanged = + rec.kycLastAmlReview !== updatedStatus.lastAmlReview || + rec.kycAccessToken !== updatedStatus.accessToken; rec.kycLastAmlReview = updatedStatus.lastAmlReview; rec.kycLastCheckStatus = updatedStatus.lastCheckStatus; rec.kycLastCheckCode = updatedStatus.lastCheckCode; @@ -997,7 +1000,7 @@ async function processPeerPushDebitMergeKyc( rec.status = PeerPushCreditStatus.FinalizingKycHardLimit; rec.failReason = hardLimitReason; } - await h.update(rec, "merge-kyc-status"); + await h.update(rec, "merge-kyc-status", undefined, { detailsChanged }); }); return hardLimitReason ? TaskRunResult.progress() : algoRes.taskResult; diff --git a/packages/taler-wallet-core/src/transactions.ts b/packages/taler-wallet-core/src/transactions.ts @@ -1194,6 +1194,7 @@ export interface TransitionInfo { newStId: number; oldStId: number; + detailsChanged?: boolean; } export enum BalanceEffect { @@ -1244,16 +1245,14 @@ export function applyNotifyTransition( transactionId: string, transitionInfo: TransitionInfo | undefined, ): void { - if ( - transitionInfo && - !( - transitionInfo.oldStId === transitionInfo.newStId && - transitionInfo.oldTxState.major === transitionInfo.newTxState.major && - transitionInfo.oldTxState.minor === transitionInfo.newTxState.minor && - !!transitionInfo.oldTxState.working === - !!transitionInfo.newTxState.working - ) - ) { + if (!transitionInfo) return; + const stateChanged = !( + transitionInfo.oldStId === transitionInfo.newStId && + transitionInfo.oldTxState.major === transitionInfo.newTxState.major && + transitionInfo.oldTxState.minor === transitionInfo.newTxState.minor && + !!transitionInfo.oldTxState.working === !!transitionInfo.newTxState.working + ); + if (stateChanged || transitionInfo.detailsChanged) { notify({ type: NotificationType.TransactionStateTransition, oldTxState: transitionInfo.oldTxState, @@ -1265,7 +1264,8 @@ export function applyNotifyTransition( ? { errorInfo: transitionInfo.errorInfo } : {}), }); - + } + if (stateChanged) { applyNotifyBalanceEffect( notify, transactionId, diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts @@ -96,6 +96,7 @@ import { WithdrawalType, addPaytoQueryParams, assertUnreachable, + canonicalJson, checkAccountRestriction, checkDbInvariant, checkLogicInvariant, @@ -1356,6 +1357,25 @@ async function transitionSimple( }); } +async function storeBankPresentation( + tx: WalletDbTransaction, + group: WalletWithdrawalGroup, +): Promise<void> { + await tx.upsertWithdrawalGroup(group); + const state = computeWithdrawalTransactionStatus(group); + tx.notify({ + type: NotificationType.TransactionStateTransition, + transactionId: constructTransactionIdentifier({ + tag: TransactionType.Withdrawal, + withdrawalGroupId: group.withdrawalGroupId, + }), + causeHint: "bank-presentation", + oldTxState: state, + newTxState: state, + newStId: group.status, + }); +} + /** Retain bank presentation independently of the wallet transaction state. */ async function storeObservedBankStatus( ctx: WithdrawTransactionContext, @@ -1367,7 +1387,7 @@ async function storeObservedBankStatus( return; const bank = r.wgInfo.bankInfo; if (!bank.presentation) return; // Older records are initialized on preparation. - bank.presentation = { + const presentation = { ...bank.presentation, status: status.status, confirmTransferUrl: status.confirm_transfer_url, @@ -1382,8 +1402,14 @@ async function storeObservedBankStatus( maxAmount: status.max_amount, wireFee: status.card_fees, }; + if ( + bank.confirmUrl === status.confirm_transfer_url && + canonicalJson(bank.presentation) === canonicalJson(presentation) + ) + return; + bank.presentation = presentation; bank.confirmUrl = status.confirm_transfer_url; - await tx.upsertWithdrawalGroup(r); + await storeBankPresentation(tx, r); }); } @@ -3019,6 +3045,9 @@ async function processWithdrawalGroupPendingKyc( if (!rec) { return; } + const detailsChanged = + rec.kycLastAmlReview !== updatedStatus.lastAmlReview || + rec.kycAccessToken !== updatedStatus.accessToken; rec.kycLastAmlReview = updatedStatus.lastAmlReview; rec.kycLastCheckStatus = updatedStatus.lastCheckStatus; rec.kycLastCheckCode = updatedStatus.lastCheckCode; @@ -3029,7 +3058,7 @@ async function processWithdrawalGroupPendingKyc( rec.status = WithdrawalGroupStatus.FinalizingKycHardLimit; rec.failReason = hardLimitReason; } - await h.update(rec, "kyc-status"); + await h.update(rec, "kyc-status", undefined, { detailsChanged }); }); return hardLimitReason ? TaskRunResult.progress() : algoRes.taskResult; @@ -5143,8 +5172,13 @@ export async function prepareBankIntegratedWithdrawal( if ( current?.wgInfo.withdrawalType === WithdrawalRecordType.BankIntegrated ) { + if ( + canonicalJson(current.wgInfo.bankInfo.presentation ?? null) === + canonicalJson(presentation) + ) + return; current.wgInfo.bankInfo.presentation = presentation; - await tx.upsertWithdrawalGroup(current); + await storeBankPresentation(tx, current); } }); return {