taler-typescript-core

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

commit d05be94361082e44fe09c385e182d93dd1dddeff
parent a2e28c585d64de213c11e0627c7125326181bf2a
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 19:00:09 +0100

wallet peer payments: link recovery withdrawals to peer-pull credits

Create the withdrawal and record its ID on the peer-pull credit in one
database transaction so a retry can resume the same withdrawal.

Diffstat:
Mpackages/taler-wallet-core/src/db/testing/conformance-cases.ts | 80+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpackages/taler-wallet-core/src/pay-peer-pull-credit.ts | 65+++++++++++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 125 insertions(+), 20 deletions(-)

diff --git a/packages/taler-wallet-core/src/db/testing/conformance-cases.ts b/packages/taler-wallet-core/src/db/testing/conformance-cases.ts @@ -1044,6 +1044,86 @@ export const conformanceCases: ConformanceCase[] = [ }, }, { + name: "peer-pull recovery: withdrawal and parent transition commit together", + async run(t, runner) { + const { completePeerPullCreditRecovery } = + await import("../../pay-peer-pull-credit.js"); + const started: string[] = []; + const wex = makeRecoveryTestContext(started); + const withdrawal = makeWithdrawalGroup("atomic-peer-pull-withdrawal"); + withdrawal.status = WithdrawalGroupStatus.PendingQueryingStatus; + withdrawal.exchangeBaseUrl = "https://peer-pull.exchange.test/"; + withdrawal.effectiveWithdrawalAmount = amt("TESTKUDOS:1"); + withdrawal.wgInfo = { + withdrawalType: WithdrawalRecordType.PeerPullCredit, + contractPriv: ck("contract-priv"), + }; + const prep = { + withdrawalGroup: withdrawal, + transactionId: txnId(`txn:withdrawal:${withdrawal.withdrawalGroupId}`), + }; + const parent = makePeerPullCredit("atomic-parent-pull"); + parent.exchangeBaseUrl = withdrawal.exchangeBaseUrl; + parent.status = PeerPullPaymentCreditStatus.PendingReady; + // Older records need the newly allocated identifier attached atomically. + parent.withdrawalGroupId = undefined; + await runner.runReadWriteTx(async (tx) => { + await seedRecoveryExchange(tx, withdrawal.exchangeBaseUrl!); + await tx.upsertPeerPullCredit(parent); + }); + const finish = (tx: WalletDbTransaction) => + completePeerPullCreditRecovery( + wex, + tx, + parent.pursePub, + prep, + "confirmed-merge", + ); + try { + await runner.runReadWriteTx((tx) => + finish(interruptBefore(tx, "upsertPeerPullCredit")), + ); + t.fail("expected interruption"); + } catch (e) { + t.equal((e as Error).message, "interrupted between writes"); + } + t.deepEqual(started, []); + await runner.runReadWriteTx(async (tx) => { + t.equal( + await tx.getWithdrawalGroup(withdrawal.withdrawalGroupId), + undefined, + ); + t.equal( + (await tx.getPeerPullCredit(parent.pursePub))!.status, + PeerPullPaymentCreditStatus.PendingReady, + ); + await finish(tx); + t.deepEqual(started, [], "task must wait for commit"); + }); + t.equal(started.length, 1); + await runner.runReadWriteTx(async (tx) => { + t.ok(await tx.getWithdrawalGroup(withdrawal.withdrawalGroupId)); + t.equal( + (await tx.getPeerPullCredit(parent.pursePub))!.status, + PeerPullPaymentCreditStatus.PendingWithdrawing, + ); + t.equal( + (await tx.getPeerPullCredit(parent.pursePub))!.withdrawalGroupId, + withdrawal.withdrawalGroupId, + ); + await finish(tx); + t.equal( + (await tx.listAllWithdrawalGroups()).filter( + (g) => g.withdrawalGroupId === withdrawal.withdrawalGroupId, + ).length, + 1, + ); + }); + t.equal(started.length, 1, "replay must not start another withdrawal"); + }, + }, + + { name: "permanent recoup and refund reasons survive storage and replacement", async run(t, runner) { const reason = { diff --git a/packages/taler-wallet-core/src/pay-peer-pull-credit.ts b/packages/taler-wallet-core/src/pay-peer-pull-credit.ts @@ -127,7 +127,9 @@ import { import { WithdrawTransactionContext, getExchangeWithdrawalInfo, - internalCreateWithdrawalGroup, + internalPrepareCreateWithdrawalGroup, + PrepareCreateWithdrawalGroupResult, + internalPerformCreateWithdrawalGroup, waitWithdrawalFinal, } from "./withdraw.js"; @@ -786,7 +788,7 @@ async function recoverPeerPullCreditFromReserve( ); } - await internalCreateWithdrawalGroup(wex, { + const withdrawal = await internalPrepareCreateWithdrawalGroup(wex, { amount: Amounts.parseOrThrow(recoveredAmount), wgInfo: { withdrawalType: WithdrawalRecordType.PeerPullCredit, @@ -801,27 +803,50 @@ async function recoverPeerPullCreditFromReserve( }, }); - const ctx = new PeerPullCreditTransactionContext(wex, pullIni.pursePub); - await wex.runWalletDbTx(async (tx) => { - const [rec, h] = await ctx.getRecordHandle(tx); - if (!rec) { - return; - } - switch (rec.status) { - case PeerPullPaymentCreditStatus.PendingReady: - case PeerPullPaymentCreditStatus.AbortingDeletePurse: - case PeerPullPaymentCreditStatus.ExpiredDeletePurse: - case PeerPullPaymentCreditStatus.FinalizingKycHardLimit: - rec.status = PeerPullPaymentCreditStatus.PendingWithdrawing; - break; - default: - return; - } - await h.update(rec, reason); - }); + await wex.runWalletDbTx((tx) => + completePeerPullCreditRecovery( + wex, + tx, + pullIni.pursePub, + withdrawal, + reason, + ), + ); return TaskRunResult.progress(); } +/** Attach the withdrawal and advance its parent in one transaction. */ +export async function completePeerPullCreditRecovery( + wex: WalletExecutionContext, + tx: WalletDbTransaction, + pursePub: string, + withdrawal: PrepareCreateWithdrawalGroupResult, + reason: string, +): Promise<void> { + const ctx = new PeerPullCreditTransactionContext(wex, pursePub); + const [rec, h] = await ctx.getRecordHandle(tx); + if (!rec) { + return; + } + switch (rec.status) { + case PeerPullPaymentCreditStatus.PendingReady: + case PeerPullPaymentCreditStatus.AbortingDeletePurse: + case PeerPullPaymentCreditStatus.ExpiredDeletePurse: + case PeerPullPaymentCreditStatus.FinalizingKycHardLimit: + const result = await internalPerformCreateWithdrawalGroup( + wex, + tx, + withdrawal, + ); + rec.withdrawalGroupId = result.withdrawalGroup.withdrawalGroupId; + rec.status = PeerPullPaymentCreditStatus.PendingWithdrawing; + break; + default: + return; + } + await h.update(rec, reason); +} + async function processPendingReady( wex: WalletExecutionContext, pullIni: WalletPeerPullCredit,