taler-typescript-core

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

commit 63671c3d90fb234d98ff0830319a2a907f6671ff
parent 381bd4eaf165dddf8ff2c388f1b6e849de0f0cdb
Author: Florian Dold <dold@taler.net>
Date:   Thu, 26 Nov 2026 04:22:52 +0100

wallet-webui: use quoted amounts for demo withdrawals and deposits

Demo wire instructions used a fixed amount, and deposit history ignored
the quoted fees. Use the withdrawal quote for the bank transfer and the
deposit quote for the wallet debit and bank credit. Check that the
balance covers the fees.

Diffstat:
Mpackages/taler-harness/src/integrationtests/test-wallet-web-ui-demo.ts | 11+++++++++++
Mpackages/wallet-webui/src/testing/demo-wallet.ts | 129+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------
Mpackages/wallet-webui/test/demo-wallet.test.ts | 86+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 203 insertions(+), 23 deletions(-)

diff --git a/packages/taler-harness/src/integrationtests/test-wallet-web-ui-demo.ts b/packages/taler-harness/src/integrationtests/test-wallet-web-ui-demo.ts @@ -223,13 +223,24 @@ export async function runWalletWebUiDemoTest(t: GlobalTestState) { await page .getByLabel("Bank account") .selectOption({ label: "Everyday account" }); + await page.getByLabel("Amount").fill("100"); + await page.getByRole("button", { name: "Review deposit" }).click(); + await page.getByText(/not enough for CHF:100/).waitFor(); + t.assertDeepEqual( + await page.getByRole("button", { name: "Confirm deposit" }).count(), + 0, + ); await page.getByLabel("Amount").fill("2"); await page.getByRole("button", { name: "Review deposit" }).click(); + await page.getByText("CHF:2.20", { exact: true }).waitFor(); + await page.getByText("CHF:1.90", { exact: true }).waitFor(); await page.getByRole("button", { name: "Confirm deposit" }).click(); await page .getByRole("heading", { name: "Transaction details" }) .waitFor(); await page.getByText("Completed", { exact: true }).waitFor(); + await page.getByText("CHF:2.20", { exact: true }).first().waitFor(); + await page.getByText("CHF:1.90", { exact: true }).first().waitFor(); }); await stage.step("simulated integrated withdrawal", async (page) => { diff --git a/packages/wallet-webui/src/testing/demo-wallet.ts b/packages/wallet-webui/src/testing/demo-wallet.ts @@ -1,5 +1,8 @@ import { AbsoluteTime, + depositQuotesMatch, + type CheckDepositResponse, + Amounts, type TalerPreciseTimestamp, ConfirmPayResultType, getQrCodesForPayto, @@ -9,6 +12,7 @@ import { ExchangeRecommendationReason, ExchangeTosStatus, ExchangeUpdateStatus, + InsufficientBalanceHint, NotificationType, observationLeaseDurationMs, timer, @@ -19,6 +23,8 @@ import { PaytoType, Result, ScopeType, + TalerError, + TalerErrorCode, TransactionAction, TransactionMajorState, TransactionMinorState, @@ -121,6 +127,54 @@ function timestamp(seconds: number) { return { t_s: seconds } as const; } +function demoDepositQuote( + requested: AmountString, + balances: Record<string, number>, +) { + const currency = requested.split(":", 1)[0]; + const requestedCents = amountCents(requested); + const totalCostCents = requestedCents + 20; + const availableCents = balances[currency] ?? 0; + if (totalCostCents > availableCents) { + const available = amount(currency, availableCents); + throw TalerError.fromDetail( + TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE, + { + insufficientBalanceDetails: { + amountRequested: requested, + causeHint: + requestedCents <= availableCents + ? InsufficientBalanceHint.FeesNotCovered + : InsufficientBalanceHint.WalletBalanceAvailableInsufficient, + balanceAvailable: available, + balanceMaterial: available, + balanceAgeAcceptable: available, + balanceReceiverAcceptable: available, + balanceReceiverExchangeUrlAcceptable: available, + balanceReceiverExchangePubAcceptable: available, + balanceReceiverAuditorUrlAcceptable: available, + balanceReceiverDepositable: available, + balanceExchangeDepositable: available, + maxEffectiveSpendAmount: amount( + currency, + Math.max(0, availableCents - 20), + ), + perExchange: {}, + }, + }, + ); + } + return { + totalDepositCost: amount(currency, totalCostCents), + effectiveDepositAmount: amount(currency, Math.max(0, requestedCents - 10)), + fees: { + coin: amount(currency, 10), + wire: amount(currency, Math.min(10, requestedCents)), + refresh: amount(currency, 10), + }, + }; +} + function withdrawalAccount(transferAmount: AmountString = "CHF:10.50") { const paytoUri = `${exchangePayto}&amount=${encodeURIComponent(transferAmount)}&message=DEMO-RESERVE`; return { @@ -257,14 +311,16 @@ function manualWithdrawal( id: string, at: number, state: TransactionMajorState, + transferAmount: AmountString = "CHF:10.50", + walletCredit: AmountString = "CHF:10.00", ): Transaction { return { ...commonTransaction( id, TransactionType.Withdrawal, at, - "CHF:10.50", - "CHF:10.00", + transferAmount, + walletCredit, state, state === TransactionMajorState.Pending ? TransactionMinorState.ExchangeWaitReserve @@ -274,9 +330,9 @@ function manualWithdrawal( withdrawalDetails: { type: WithdrawalType.ManualTransfer, exchangePaytoUris: [ - `${exchangePayto}&amount=CHF%3A10.50&message=DEMO-RESERVE`, + `${exchangePayto}&amount=${encodeURIComponent(transferAmount)}&message=DEMO-RESERVE`, ], - exchangeCreditAccountDetails: [withdrawalAccount()], + exchangeCreditAccountDetails: [withdrawalAccount(transferAmount)], reservePub: "DEMORESERVEPUB", reserveIsReady: state === TransactionMajorState.Done, reserveClosingDelay: { d_us: 86_400_000_000 }, @@ -497,8 +553,18 @@ export class DemoWalletConnection implements WalletConnection { this.client = { call: async (operation, request) => this.dispatch(operation, request) as never, - callForResult: async (operation, request) => - Result.of(this.dispatch(operation, request)) as never, + callForResult: async (operation, request) => { + try { + return Result.of(this.dispatch(operation, request)) as never; + } catch (error) { + if (error instanceof TalerError) + return Result.errorWithDetail( + error.errorDetail.code, + error.errorDetail, + ) as never; + throw error; + } + }, }; } @@ -987,7 +1053,7 @@ export class DemoWalletConnection implements WalletConnection { case WalletApiOperation.ListExchanges: return { exchanges: structuredClone(this.state.exchanges) } as never; case WalletApiOperation.ListWithdrawalExchangeCandidates: - return ({ + return { candidates: [ { exchangeBaseUrl, @@ -1006,7 +1072,7 @@ export class DemoWalletConnection implements WalletConnection { }, }, ], - } satisfies ListWithdrawalExchangeCandidatesResponse) as never; + } satisfies ListWithdrawalExchangeCandidatesResponse as never; case WalletApiOperation.GetExchangeEntryByUrl: return structuredClone( this.state.exchanges.find( @@ -1092,26 +1158,39 @@ export class DemoWalletConnection implements WalletConnection { return {} as never; } case WalletApiOperation.CheckDeposit: { - const requested = String(request.amount); - return { - totalDepositCost: amount("CHF", amountCents(requested) + 20), - effectiveDepositAmount: amount( - "CHF", - Math.max(0, amountCents(requested) - 10), - ), - fees: { coin: "CHF:0.10", wire: "CHF:0.10", refresh: "CHF:0" }, - } as never; + return demoDepositQuote( + request.amount as AmountString, + this.state.balances, + ) as never; } case WalletApiOperation.CreateDepositGroup: { - const id = this.nextId("deposit"); const raw = String(request.amount) as AmountString; + const quote = demoDepositQuote(raw, this.state.balances); + const expected = request.expectedQuote as + | CheckDepositResponse + | undefined; + if (expected && !depositQuotesMatch(expected, quote)) { + throw TalerError.fromDetail( + TalerErrorCode.WALLET_CORE_API_BAD_REQUEST, + { parameter: "expectedQuote" }, + ); + } + const id = + (request.transactionId as TransactionIdStr | undefined) ?? + this.nextId("deposit"); + if ( + this.state.transactions.some( + (transaction) => transaction.transactionId === id, + ) + ) + throw Error("deposit group already exists"); const transaction = { ...commonTransaction( id, TransactionType.Deposit, Math.floor(this.now() / 1000), - raw, - raw, + quote.effectiveDepositAmount, + quote.totalDepositCost, TransactionMajorState.Done, ), depositGroupId: id, @@ -1125,7 +1204,11 @@ export class DemoWalletConnection implements WalletConnection { trackingState: [], } as unknown as Transaction; this.state.transactions.unshift(transaction); - this.adjust(raw.split(":", 1)[0], -amountCents(raw), id); + this.adjust( + raw.split(":", 1)[0], + -amountCents(quote.totalDepositCost), + id, + ); this.emitTransaction(transaction); return { transactionId: id, @@ -1154,9 +1237,9 @@ export class DemoWalletConnection implements WalletConnection { id, Math.floor(this.now() / 1000), TransactionMajorState.Pending, + amount("CHF", amountCents(raw) + 50), + raw, ); - transaction.amountRaw = amount("CHF", amountCents(raw) + 50); - transaction.amountEffective = raw; this.state.transactions.unshift(transaction); this.emitTransaction(transaction); return { diff --git a/packages/wallet-webui/test/demo-wallet.test.ts b/packages/wallet-webui/test/demo-wallet.test.ts @@ -3,6 +3,7 @@ import test from "node:test"; import { NotificationType, Result, + TalerErrorCode, TransactionMajorState, } from "@gnu-taler/taler-util"; import { WalletApiOperation } from "@gnu-taler/taler-wallet-core"; @@ -95,6 +96,91 @@ test("demo platform accepts only its built-in actions and never opens a URL", as await services.platform.openExternal("https://merchant.example/"); }); +test("demo deposit debits the quoted total and manual withdrawal keeps its quoted transfer amount", async () => { + const client = new DemoWalletConnection(fixedNow).client; + const deposit = { + amount: "CHF:3.25", + depositPaytoUri: "payto://iban/CH9300762011623852957", + } as const; + const quote = await client.call(WalletApiOperation.CheckDeposit, deposit); + const created = await client.call( + WalletApiOperation.CreateDepositGroup, + deposit, + ); + const receipt = await client.call(WalletApiOperation.GetTransactionById, { + transactionId: created.transactionId, + }); + assert.equal(receipt.amountRaw, quote.effectiveDepositAmount); + assert.equal(receipt.amountEffective, quote.totalDepositCost); + const balances = await client.call(WalletApiOperation.GetBalances, {}); + assert.equal(balances.balances[0].available, "CHF:39.05"); + const withdrawal = { + exchangeBaseUrl: "https://exchange.demo.invalid/", + amount: "CHF:1.25", + } as const; + const preview = await client.call( + WalletApiOperation.GetWithdrawalDetailsForAmount, + withdrawal, + ); + const accepted = await client.call( + WalletApiOperation.AcceptManualWithdrawal, + withdrawal, + ); + const transaction = await client.call(WalletApiOperation.GetTransactionById, { + transactionId: accepted.transactionId, + }); + assert.equal(transaction.amountRaw, preview.amountRaw); + assert.equal(transaction.amountEffective, preview.amountEffective); + assert.equal(transaction.type, "withdrawal"); + if ( + transaction.type !== "withdrawal" || + transaction.withdrawalDetails.type !== "manual-transfer" + ) + throw Error("expected manual instructions"); + assert.deepEqual( + transaction.withdrawalDetails.exchangeCreditAccountDetails, + preview.withdrawalAccountsList, + ); + assert.ok( + transaction.withdrawalDetails.exchangePaytoUris[0].includes( + "amount=CHF%3A1.75", + ), + ); +}); + +test("demo deposits require the full quoted debit to remain available", async () => { + const connection = new DemoWalletConnection(fixedNow); + const { client } = connection; + const deposit = { + amount: "CHF:42.31", + depositPaytoUri: "payto://iban/CH9300762011623852957", + } as const; + const before = connection.snapshot(); + for (const operation of [ + WalletApiOperation.CheckDeposit, + WalletApiOperation.CreateDepositGroup, + ] as const) { + const result = await client.callForResult(operation, deposit); + assert(Result.isError(result)); + assert.equal( + result.error, + TalerErrorCode.WALLET_DEPOSIT_GROUP_INSUFFICIENT_BALANCE, + ); + } + assert.deepEqual(connection.snapshot(), before); + const exact = { ...deposit, amount: "CHF:42.30" } as const; + const quote = await client.call(WalletApiOperation.CheckDeposit, exact); + assert.equal(quote.totalDepositCost, "CHF:42.50"); + await client.call(WalletApiOperation.CreateDepositGroup, exact); + const balances = await client.call(WalletApiOperation.GetBalances, {}); + assert.equal(balances.balances[0].available, "CHF:0.00"); + const settled = connection.snapshot(); + await assert.rejects(() => + client.call(WalletApiOperation.CreateDepositGroup, exact), + ); + assert.deepEqual(connection.snapshot(), settled); +}); + test("unsupported demo operations fail visibly", async () => { const connection = new DemoWalletConnection(fixedNow); await assert.rejects(