commit d110955ec59e39751b2ce567c0c6d81b2c175ab3
parent d05be94361082e44fe09c385e182d93dd1dddeff
Author: Florian Dold <dold@taler.net>
Date: Tue, 17 Nov 2026 19:00:09 +0100
wallet payments: resume token and tax receipt preparation
Prepare missing token slates and donation planchets in the payment task.
Save the wallet data before submitting payment so retries send the same
outputs.
Diffstat:
4 files changed, 141 insertions(+), 33 deletions(-)
diff --git a/packages/taler-wallet-core/src/db/records.ts b/packages/taler-wallet-core/src/db/records.ts
@@ -917,6 +917,8 @@ export interface WalletTokenSelection {
}
export interface WalletPurchasePayInfo {
+ /** Frozen before submitting /pay; replay must keep the same wallet-data hash. */
+ submittedWalletData?: import("@gnu-taler/taler-util").PayWalletData;
/**
* Undefined if payment is blocked by a pending refund.
*/
diff --git a/packages/taler-wallet-core/src/donau.ts b/packages/taler-wallet-core/src/donau.ts
@@ -482,6 +482,9 @@ export async function generateDonauPlanchets(
if (!rec.donauBaseUrl) {
return undefined;
}
+ // A complete set is committed atomically below. Never regenerate it on
+ // retries: donation nonces are random and bind the merchant's payment.
+ if ((await tx.countDonationPlanchetsByProposal(proposalId)) > 0) return;
if (!rec.donauAmount) {
throw Error("db consistency error: donau amount not set");
}
diff --git a/packages/taler-wallet-core/src/pay-merchant.test.ts b/packages/taler-wallet-core/src/pay-merchant.test.ts
@@ -1695,3 +1695,77 @@ for (const cached of [false, true]) {
assert.strictEqual(token.transactionId, "payment:current");
});
}
+
+test("payment preparation resumes after a crash between repeated token outputs", async () => {
+ const { preparePaymentOutputs } = await import("./pay-merchant.js");
+ const slates = new Map<string, WalletSlate>();
+ let attempts = 0;
+ const terms = {
+ version: MerchantContractVersion.V1,
+ merchant_base_url: "https://merchant.example/",
+ choices: [
+ {
+ outputs: [
+ {
+ type: "token",
+ token_family_slug: "reward",
+ key_index: 0,
+ count: 2,
+ },
+ ],
+ },
+ ],
+ token_families: {
+ reward: {
+ name: "Reward",
+ description: "Reward",
+ details: { class: "discount" },
+ keys: [
+ {
+ signature_validity_start: { t_s: 1 },
+ signature_validity_end: { t_s: 2000000000 },
+ },
+ ],
+ },
+ },
+ } as any;
+ const purchase = {
+ proposalId: "prepare",
+ choiceIndex: 0,
+ secretSeed: "seed",
+ } as WalletPurchase;
+ const wex = {
+ runWalletDbTx: async (f: any) =>
+ f({
+ getSlate: async (_p: string, _c: number, _o: number, j: number) =>
+ slates.get(String(j)),
+ upsertSlate: async (s: WalletSlate) => {
+ slates.set(String(s.repeatIndex), structuredClone(s));
+ },
+ }),
+ cryptoApi: {
+ createSlate: async (r: any) => {
+ if (++attempts === 2) throw Error("interrupted");
+ return {
+ tokenIssuePubHash: "issue",
+ tokenPub: `token-${r.repeatIndex}`,
+ tokenPriv: "priv",
+ tokenEv: { cipher: "RSA", rsa_blinded_planchet: "envelope" },
+ tokenEvHash: "hash",
+ blindingKey: "blind",
+ };
+ },
+ },
+ } as unknown as WalletExecutionContext;
+ await assert.rejects(
+ preparePaymentOutputs(wex, purchase, terms, terms),
+ /interrupted/,
+ );
+ assert.equal(slates.size, 1);
+ const first = structuredClone(slates.get("0"));
+ await preparePaymentOutputs(wex, purchase, terms, terms);
+ assert.equal(slates.size, 2);
+ assert.deepStrictEqual(slates.get("0"), first);
+ await preparePaymentOutputs(wex, purchase, terms, terms);
+ assert.equal(attempts, 3);
+});
diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts
@@ -3850,39 +3850,6 @@ export async function confirmPay(
return waitPaymentResult(wex, proposalId, sessionIdOverride);
}
- // TODO: pre-generate slates based on choice priority!
- if (
- choiceIndex != null &&
- contractTerms.version === MerchantContractVersion.V1
- ) {
- const choice = contractTerms.choices[choiceIndex];
- for (let j = 0; j < choice.outputs.length; j++) {
- const tok = choice.outputs[j];
- switch (tok.type) {
- case MerchantContractOutputType.Token: {
- const tokCount = tok.count ?? 1;
- for (let repeatIndex = 0; repeatIndex < tokCount; repeatIndex++) {
- await generateSlate(
- wex,
- proposal,
- contractTerms,
- d.contractTermsRaw,
- choiceIndex,
- j,
- repeatIndex,
- );
- }
-
- break;
- }
- case MerchantContractOutputType.TaxReceipt:
- // FIXME: What if we have multiple?
- await generateDonauPlanchets(wex, proposalId);
- break;
- }
- }
- }
-
// In case we're sharing the payment and we're long-polling
await wex.taskScheduler.resetTask(ctx.taskId);
@@ -3975,6 +3942,39 @@ export async function processPurchase(
}
}
+/** Preparation is durable and repeatable after interrupted confirmation. */
+export async function preparePaymentOutputs(
+ wex: WalletExecutionContext,
+ purchase: WalletPurchase,
+ terms: MerchantContractTerms,
+ raw: any,
+): Promise<void> {
+ if (
+ terms.version !== MerchantContractVersion.V1 ||
+ purchase.choiceIndex == null
+ )
+ return;
+ const choice = terms.choices[purchase.choiceIndex];
+ for (let i = 0; i < choice.outputs.length; i++) {
+ const output = choice.outputs[i];
+ if (output.type === MerchantContractOutputType.Token) {
+ for (let j = 0; j < (output.count ?? 1); j++) {
+ await generateSlate(
+ wex,
+ purchase,
+ terms,
+ raw,
+ purchase.choiceIndex,
+ i,
+ j,
+ );
+ }
+ } else if (output.type === MerchantContractOutputType.TaxReceipt) {
+ await generateDonauPlanchets(wex, purchase.proposalId);
+ }
+ }
+}
+
async function processPurchasePay(
wex: WalletExecutionContext,
proposalId: string,
@@ -4125,6 +4125,13 @@ async function processPurchasePay(
}
if (!purchase.merchantPaySig) {
+ if (!payInfo.submittedWalletData)
+ await preparePaymentOutputs(
+ wex,
+ purchase,
+ download.contractTerms,
+ download.contractTermsRaw,
+ );
let slates: WalletSlate[] | undefined = undefined;
let donauPlanchets: WalletDonationPlanchet[] | undefined = undefined;
let wallet_data: PayWalletData | undefined = undefined;
@@ -4194,6 +4201,28 @@ async function processPurchasePay(
// as there are other output types (e.g. slates).
}
+ if (wallet_data) {
+ const preparedData = wallet_data;
+ wallet_data = await wex.runWalletDbTx(async (tx) => {
+ const current = await tx.getPurchase(proposalId);
+ checkDbInvariant(
+ !!current?.payInfo,
+ "payment disappeared while preparing outputs",
+ );
+ if (current.payInfo.submittedWalletData) {
+ checkDbInvariant(
+ JSON.stringify(current.payInfo.submittedWalletData) ===
+ JSON.stringify(preparedData),
+ "payment output material differs from the submitted wallet data",
+ );
+ return current.payInfo.submittedWalletData;
+ }
+ current.payInfo.submittedWalletData = preparedData;
+ await tx.upsertPurchase(current);
+ return preparedData;
+ });
+ }
+
let depositPermissions: CoinDepositPermission[];
// FIXME: Cache!
depositPermissions = await generateDepositPermissions(