commit 77cda0dc78ef8d0dd714cf48753452d38f84ff36
parent aeb30cfed748086a77be678d0ffd9a5ccabcd433
Author: Florian Dold <dold@taler.net>
Date: Thu, 26 Nov 2026 04:11:41 +0100
wallet-core: leave the currency choice to the payer
The wallet tried to pick the cheapest offer even when it had no exchange
rate for comparing the prices. Only choose a default when all payable
offers use the same currency.
Diffstat:
2 files changed, 52 insertions(+), 5 deletions(-)
diff --git a/packages/taler-wallet-core/src/pay-merchant.test.ts b/packages/taler-wallet-core/src/pay-merchant.test.ts
@@ -23,6 +23,8 @@ import {
Duration,
MerchantCoinRefundStatus,
CoinStatus,
+ ChoiceSelectionDetailType,
+ type MerchantContractTerms,
DenomKeyType,
LongpollQueue,
ContractTermsUtil,
@@ -31,6 +33,8 @@ import {
eddsaSign,
encodeCrock,
HttpStatusCode,
+ MerchantContractInputType,
+ MerchantContractOutputType,
MerchantContractVersion,
NotificationType,
RefreshReason,
@@ -88,6 +92,7 @@ import {
applyFirstPaySuccessState,
applyPaySessionRequest,
classifyMerchantAbortRecoveryEvidence,
+ calculateDefaultChoice,
cleanupFailedClaimsOnStartup,
computePayMerchantTransactionActions,
computePayMerchantTransactionState,
@@ -131,6 +136,39 @@ import { deleteTransaction, getTransactionById } from "./transactions.js";
import { WalletApiOperation } from "./wallet-api-types.js";
import { TaskIdentifiers, TaskRunResultType } from "./common.js";
+test("payable currencies are never compared or automatically selected across currencies", () => {
+ const options = (amounts: AmountString[]) => {
+ const terms = {
+ version: MerchantContractVersion.V1,
+ choices: amounts.map((amount) => ({
+ amount,
+ inputs: [
+ { type: MerchantContractInputType.Token, token_family_slug: "pass" },
+ ],
+ outputs: [
+ { type: MerchantContractOutputType.Token, token_family_slug: "pass" },
+ ],
+ })),
+ } as unknown as MerchantContractTerms;
+ return calculateDefaultChoice(
+ amounts.map((amountRaw) => ({
+ status: ChoiceSelectionDetailType.PaymentPossible,
+ amountRaw,
+ amountEffective: amountRaw,
+ scopeInfo: undefined,
+ })),
+ terms,
+ );
+ };
+ assert.equal(options(["CHF:3", "CHF:2"]).defaultChoiceIndex, 1);
+ assert.deepEqual(options(["CHF:0", "CHF:2"]), {
+ defaultChoiceIndex: 0,
+ automaticExecution: true,
+ automaticExecutableIndex: 0,
+ });
+ assert.deepEqual(options(["CHF:0", "EUR:2"]), { automaticExecution: false });
+});
+
interface ScheduledTimer {
due: number;
callback: () => void;
diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts
@@ -3420,19 +3420,18 @@ export async function getChoicesForPaymentInTx(
return {
choices,
contractTerms: d.contractTerms,
- ...(await calculateDefaultChoice(wex, choices, d.contractTerms)),
+ ...calculateDefaultChoice(choices, d.contractTerms),
};
}
-async function calculateDefaultChoice(
- wex: WalletExecutionContext,
+export function calculateDefaultChoice(
choiceDetails: ChoiceSelectionDetail[],
contractTerms: MerchantContractTerms,
-): Promise<{
+): {
defaultChoiceIndex?: number;
automaticExecution?: boolean;
automaticExecutableIndex?: number;
-}> {
+} {
let defaultChoiceIndex: number | undefined;
let automaticExecution: boolean | undefined;
let automaticExecutableIndex: number | undefined;
@@ -3471,6 +3470,16 @@ async function calculateDefaultChoice(
if (details.status !== ChoiceSelectionDetailType.PaymentPossible) {
continue;
}
+ // Prices in different currencies cannot be ranked without an exchange rate.
+ if (
+ cheapestPayableChoice &&
+ !Amounts.isSameCurrency(
+ Amounts.currencyOf(choice.amount),
+ Amounts.currencyOf(cheapestPayableChoice.amount),
+ )
+ ) {
+ return { automaticExecution: false };
+ }
if (
cheapestPayableChoice == null ||
Amounts.cmp(choice.amount, cheapestPayableChoice.amount) < 0