taler-typescript-core

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

commit 353f3ae67b3ec257f8a25f5c247508cfabc4a285
parent 9c70bb8c746c773830c5f98841f5e6b37e324354
Author: Florian Dold <dold@taler.net>
Date:   Thu, 26 Nov 2026 04:12:56 +0100

wallet-webui: use the wallet language to parse transfer amounts

An English wallet read 1,000 as one. Use the selected language's decimal
and grouping separators, and reject misplaced separators before
submitting the amount.

Diffstat:
Mpackages/wallet-webui/src/routes/money-flow-model.ts | 58++++++++++++++++++++++++++++++++++++++++++++++++++--------
Mpackages/wallet-webui/src/screens/PeerCreateScreen.tsx | 15++++++++++++++-
Mpackages/wallet-webui/src/ui/MoneyAmountInput.tsx | 10++++++++++
Mpackages/wallet-webui/test/money-flow-model.test.ts | 44+++++++++++++++++++++++++++++++++++++++++---
4 files changed, 115 insertions(+), 12 deletions(-)

diff --git a/packages/wallet-webui/src/routes/money-flow-model.ts b/packages/wallet-webui/src/routes/money-flow-model.ts @@ -12,31 +12,73 @@ export interface MoneyFormValues { amount: string; } +/** Wallet form input follows the selected wallet language. Protocol amounts always use a dot. */ +export function localizedAmountInput(value: string, locale: string): string { + const decimal = new Intl.NumberFormat(locale) + .formatToParts(1.5) + .find((part) => part.type === "decimal")!.value; + return value.replace(".", decimal); +} + +export function amountInputHint(locale: string): string { + const example = new Intl.NumberFormat(locale).format(1234.56); + return i18n.str`Enter a positive amount using this format: ${example}.`; +} + export function positiveAmountValue( currency: string, input: string, maximumValue?: string, + locale = "en", ): string | undefined { + const formatter = new Intl.NumberFormat(locale, { useGrouping: true }); + const parts = formatter.formatToParts(12345.6); + const group = parts.find((part) => part.type === "group")?.value; + const decimal = parts.find((part) => part.type === "decimal")!.value; + let text = input.trim(); + // Keyboards and ICU releases differ in their space and apostrophe glyphs. + if (group && /^[ '’\u00a0\u202f]$/.test(group)) { + text = /^['’]$/.test(group) + ? text.replace(/['’]/g, group) + : text.replace(/[ \u00a0\u202f]/g, group); + } + const [integer, fraction, excess] = text.split(decimal); + if ( + excess !== undefined || + (fraction !== undefined && !/^\d{1,8}$/.test(fraction)) + ) + return undefined; + let digits = integer; + if (group && integer.includes(group)) { + digits = integer.split(group).join(""); + if ( + !/^\d{1,16}$/.test(digits) || + formatter.format(BigInt(digits)) !== integer + ) + return undefined; + } + if (!/^\d+$/.test(digits)) return undefined; const normalizedCurrency = Amounts.normalizeCurrency(currency.trim()); if (!normalizedCurrency) return undefined; - const normalizedInput = input.trim().replace(",", "."); - const amount = Amounts.parse(`${normalizedCurrency}:${normalizedInput}`); + const amount = Amounts.parse( + `${normalizedCurrency}:${digits}${fraction === undefined ? "" : "." + fraction}`, + ); if (!amount || Amounts.isZero(amount)) return undefined; if (maximumValue !== undefined) { - const maximum = Amounts.parse( - `${normalizedCurrency}:${maximumValue.trim().replace(",", ".")}`, - ); + const maximum = Amounts.parse(`${normalizedCurrency}:${maximumValue}`); if (!maximum || Amounts.cmp(amount, maximum) > 0) return undefined; } return Amounts.stringifyValue(amount); } +/** Form submission callbacks carry a value normalized by positiveAmountValue. */ export function amountFromForm(values: MoneyFormValues): AmountString { const currency = Amounts.normalizeCurrency(values.currency.trim()); if (!currency) throw Error(i18n.str`Enter a valid currency.`); - const amount = positiveAmountValue(currency, values.amount); - if (!amount) throw Error(i18n.str`Enter an amount greater than zero.`); - return `${currency}:${amount}` as AmountString; + const amount = Amounts.parse(`${currency}:${values.amount}`); + if (!amount || Amounts.isZero(amount)) + throw Error(i18n.str`Enter an amount greater than zero.`); + return Amounts.stringify(amount); } export function validatedPaytoUri(value: string): string { diff --git a/packages/wallet-webui/src/screens/PeerCreateScreen.tsx b/packages/wallet-webui/src/screens/PeerCreateScreen.tsx @@ -1,3 +1,4 @@ +import { useLanguage } from "../i18n/LanguageProvider.js"; import { useFocusScope } from "@gnu-taler/web-util/browser"; import { useEffect, useMemo, useRef, useState } from "preact/hooks"; import type { ScopeInfo, TalerProtocolTimestamp } from "@gnu-taler/taler-util"; @@ -11,6 +12,7 @@ import { MoneyAmountInput } from "../ui/MoneyAmountInput.js"; import { expirationAfterDays, positiveAmountValue, + amountInputHint, relativeExpirationLabel, } from "../routes/money-flow-model.js"; @@ -139,6 +141,7 @@ export function PeerCreateScreen(props: { onCancel: () => void; onWithdraw: () => void; }) { + const { language } = useLanguage(); const fallbackScope = useMemo<PeerScopeView>( () => ({ id: `currency:${props.currency}`, @@ -181,7 +184,12 @@ export function PeerCreateScreen(props: { const selectedExchange = exchanges.some((item) => item.url === exchange) ? exchange : exchanges[0]?.url; - const normalizedAmount = positiveAmountValue(currency, amount); + const normalizedAmount = positiveAmountValue( + currency, + amount, + undefined, + language, + ); const expiration: PeerExpirationChoice = expirationMode === "default" ? { kind: "default" } @@ -345,6 +353,11 @@ export function PeerCreateScreen(props: { </select> </label> <MoneyAmountInput + hint={ + amount && !normalizedAmount + ? amountInputHint(language) + : undefined + } inputRef={amountInput} // Translators: %1$s is an ISO-style currency code. label={i18n.str`Amount (${currency})`} diff --git a/packages/wallet-webui/src/ui/MoneyAmountInput.tsx b/packages/wallet-webui/src/ui/MoneyAmountInput.tsx @@ -1,3 +1,4 @@ +import { useId } from "preact/hooks"; import type { Ref } from "preact"; export function MoneyAmountInput(props: { @@ -5,14 +6,18 @@ export function MoneyAmountInput(props: { value: string; currency: string; onInput: (value: string) => void; + hint?: string; inputRef?: Ref<HTMLInputElement>; readOnly?: boolean; }) { + const hintId = useId(); return ( <label class="block py-2 text-center"> <span class="sr-only">{props.label}</span> <span class="flex items-baseline justify-center gap-3 text-5xl"> <input + aria-invalid={Boolean(props.hint)} + aria-describedby={props.hint ? hintId : undefined} ref={props.inputRef} inputMode="decimal" value={props.value} @@ -24,6 +29,11 @@ export function MoneyAmountInput(props: { /> <span aria-hidden="true">{props.currency}</span> </span> + {props.hint && ( + <span id={hintId} role="status" class="block text-sm text-error"> + {props.hint} + </span> + )} </label> ); } diff --git a/packages/wallet-webui/test/money-flow-model.test.ts b/packages/wallet-webui/test/money-flow-model.test.ts @@ -3,6 +3,7 @@ import test from "node:test"; import { amountFromForm, defaultExpirationDate, + localizedAmountInput, expirationAfterDays, expirationFromDate, expirationFromDateTime, @@ -13,11 +14,12 @@ import { validatedPaytoUri, } from "../src/routes/money-flow-model.js"; -test("money form canonicalizes currency and decimal separator", () => { +test("money form receives canonical amounts independently of the UI locale", () => { assert.equal( - amountFromForm({ currency: "chf", amount: "10,25" }), + amountFromForm({ currency: "chf", amount: "10.25" }), "CHF:10.25", ); + assert.throws(() => amountFromForm({ currency: "EUR", amount: "10,25" })); assert.throws( () => amountFromForm({ currency: "CHF", amount: "0" }), /greater than zero/, @@ -32,13 +34,49 @@ test("money form canonicalizes currency and decimal separator", () => { }); test("screen amount validation uses canonical Taler amount limits", () => { - assert.equal(positiveAmountValue("chf", "01,25000000"), "1.25"); + assert.equal( + positiveAmountValue("chf", "01,25000000", undefined, "de"), + "1.25", + ); assert.equal(positiveAmountValue("CHF", "10", "9.99"), undefined); assert.equal(positiveAmountValue("CHF", "9.99", "9.99"), "9.99"); assert.equal(positiveAmountValue("CHF", "1.000000000"), undefined); assert.equal(positiveAmountValue("CHF", "Infinity"), undefined); }); +test("editable amounts accept correctly grouped locale input without changing protocol amounts", () => { + for (const [locale, grouped, ungrouped] of [ + ["en", "1,234.56", "1234.56"], + ["de", "1.234,56", "1234,56"], + ["fr", "1 234,56", "1234,56"], + ["de-CH", "1’234.56", "1234.56"], + ]) { + assert.equal( + positiveAmountValue("CHF", grouped, "1234.56", locale), + "1234.56", + ); + assert.equal( + positiveAmountValue("CHF", ungrouped, "1234.55", locale), + undefined, + ); + assert.equal(localizedAmountInput("1234.56", locale), ungrouped); + } + for (const malformed of [ + "1,23", + "1,234,56", + "12,34.56", + "1.234,56", + "1,234.567890123", + "1,,234", + ]) { + assert.equal( + positiveAmountValue("CHF", malformed, undefined, "en"), + undefined, + malformed, + ); + } +}); + test("deposit destination must be a supported payto URI", () => { assert.equal( validatedPaytoUri(" payto://iban/CH9300762011623852957 "),