taler-typescript-core

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

commit 4d542c5204e5f48508f3c9b855ee09eadb3d3797
parent 57aad93720a39cc56d45217c98c5c293310f62ae
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 23:19:27 +0100

wallet-core: add renewal reports to the observation registry

Diffstat:
Mpackages/taler-util/src/notifications.ts | 12+++++++++++-
Mpackages/taler-util/src/types-taler-wallet-observations.ts | 22+++++++++++++++++++++-
Mpackages/taler-wallet-core/src/observations.ts | 110++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Mpackages/taler-wallet-core/src/observe-transaction.test.ts | 24++++++++++++++++++++----
Mpackages/wallet-webui/test/observations-hook.test.tsx | 3++-
Mpackages/wallet-webui/test/peer-credit-observation.test.tsx | 2++
6 files changed, 137 insertions(+), 36 deletions(-)

diff --git a/packages/taler-util/src/notifications.ts b/packages/taler-util/src/notifications.ts @@ -78,7 +78,17 @@ export interface TransactionSnapshotNotification { } /** Extend this union with a discriminated variant for each observed domain. */ -export type ObservationSnapshotNotification = TransactionSnapshotNotification; +export interface BalanceRefreshSnapshotNotification { + type: NotificationType.ObservationSnapshot; + observationType: ObservationType.BalanceRefreshInfo; + observationId: string; + revision: number; + scopeInfo: import("./types-taler-wallet.js").ScopeInfo; + snapshot: import("./types-taler-wallet.js").WalletRefreshInfo | null; +} +export type ObservationSnapshotNotification = + | TransactionSnapshotNotification + | BalanceRefreshSnapshotNotification; /** Ends the observation; does not describe the outcome of the transaction. */ export interface ObservationErrorNotification { diff --git a/packages/taler-util/src/types-taler-wallet-observations.ts b/packages/taler-util/src/types-taler-wallet-observations.ts @@ -15,7 +15,10 @@ import type { AmountString, CurrencySpecification, } from "./types-taler-common.js"; -import { codecForTransactionIdStr } from "./types-taler-wallet.js"; +import { + codecForScopeInfo, + codecForTransactionIdStr, +} from "./types-taler-wallet.js"; import type { ChoiceSelectionDetail, ExchangeKeyChangeInfo, @@ -44,6 +47,7 @@ import type { export enum ObservationType { Transaction = "transaction", + BalanceRefreshInfo = "balance-refresh-info", } export interface ObserveTransactionRequest { @@ -52,6 +56,22 @@ export interface ObserveTransactionRequest { observationId: string; } +export interface BalanceRefreshInfoRequest { + scopeInfo: ScopeInfo; +} +export interface ObserveBalanceRefreshInfoRequest extends BalanceRefreshInfoRequest { + observationId: string; +} +export const codecForBalanceRefreshInfoRequest = () => + buildCodecForObject<BalanceRefreshInfoRequest>() + .property("scopeInfo", codecForScopeInfo()) + .build("BalanceRefreshInfoRequest"); +export const codecForObserveBalanceRefreshInfoRequest = () => + buildCodecForObject<ObserveBalanceRefreshInfoRequest>() + .property("scopeInfo", codecForScopeInfo()) + .property("observationId", codecForString()) + .build("ObserveBalanceRefreshInfoRequest"); + export interface StopObservationRequest { observationId: string; } diff --git a/packages/taler-wallet-core/src/observations.ts b/packages/taler-wallet-core/src/observations.ts @@ -6,11 +6,15 @@ import { AbsoluteTime, + CancellationToken, canonicalJson, ErrorInfoSummary, getErrorDetailFromException, NotificationType, ObservationType, + ObservationSnapshotNotification, + ScopeInfo, + WalletRefreshInfo, TransactionSnapshot, TalerError, TalerErrorCode, @@ -48,10 +52,11 @@ export function validateObservationId(observationId: string): void { } } -interface TransactionObservation { +interface Observation { observationId: string; - transactionId: TransactionIdStr; - read: () => Promise<TransactionSnapshot | null>; + observationType: ObservationType; + cancelRead?: () => void; + read: () => Promise<ObservationSnapshotNotification>; refreshAt?: () => number | undefined; revision: number; lastValue?: string; @@ -62,7 +67,7 @@ interface TransactionObservation { /** In-memory subscriptions, independent of the registering request's lifetime. */ export class WalletObservations { - private entries = new Map<string, TransactionObservation>(); + private entries = new Map<string, Observation>(); private stopped = false; constructor( @@ -76,6 +81,51 @@ export class WalletObservations { read: () => Promise<TransactionSnapshot | null>, refreshAt?: () => number | undefined, ): Promise<void> { + await this.register( + observationId, + ObservationType.Transaction, + async () => ({ + type: NotificationType.ObservationSnapshot, + observationType: ObservationType.Transaction, + observationId, + revision: 0, + transactionId, + snapshot: await read(), + }), + refreshAt, + ); + } + + async registerRefreshInfo( + observationId: string, + scopeInfo: ScopeInfo, + read: (token: CancellationToken) => Promise<WalletRefreshInfo | null>, + refreshAt: () => number | undefined, + ): Promise<void> { + const source = CancellationToken.create(); + await this.register( + observationId, + ObservationType.BalanceRefreshInfo, + async () => ({ + type: NotificationType.ObservationSnapshot, + observationType: ObservationType.BalanceRefreshInfo, + observationId, + revision: 0, + scopeInfo, + snapshot: await read(source.token), + }), + refreshAt, + () => source.cancel(), + ); + } + + private async register( + observationId: string, + observationType: ObservationType, + read: Observation["read"], + refreshAt?: Observation["refreshAt"], + cancelRead?: () => void, + ): Promise<void> { validateObservationId(observationId); if (this.stopped || this.entries.has(observationId)) { throw TalerError.fromDetail( @@ -84,9 +134,10 @@ export class WalletObservations { this.stopped ? "wallet has stopped" : "observationId is already active", ); } - const entry: TransactionObservation = { + const entry: Observation = { observationId, - transactionId, + observationType, + cancelRead, read, refreshAt, revision: 0, @@ -98,17 +149,22 @@ export class WalletObservations { try { const snapshot = await read(); if (!this.isActive(entry)) return; - if (!snapshot) { + if ( + snapshot.observationType === ObservationType.Transaction && + !snapshot.snapshot + ) { throw TalerError.fromDetail( TalerErrorCode.WALLET_TRANSACTION_NOT_FOUND, - { transactionId }, + { transactionId: snapshot.transactionId }, "observed transaction does not exist", ); } this.publish(entry, snapshot); } catch (e) { - if (this.isActive(entry)) this.stop(observationId); - throw e; + if (this.isActive(entry)) { + this.stop(observationId); + throw e; + } } finally { entry.running = false; if (entry.dirty) this.schedule(entry); @@ -127,6 +183,7 @@ export class WalletObservations { if (!entry) return; entry.timer?.clear(); this.entries.delete(observationId); + entry.cancelRead?.(); } shutdown(): void { @@ -134,11 +191,11 @@ export class WalletObservations { for (const id of this.entries.keys()) this.stop(id); } - private isActive(entry: TransactionObservation): boolean { + private isActive(entry: Observation): boolean { return this.entries.get(entry.observationId) === entry; } - private schedule(entry: TransactionObservation): void { + private schedule(entry: Observation): void { if (!this.isActive(entry) || entry.running) return; entry.timer?.clear(); entry.timer = this.timers.after(0, () => { @@ -147,7 +204,7 @@ export class WalletObservations { }); } - private async refresh(entry: TransactionObservation): Promise<void> { + private async refresh(entry: Observation): Promise<void> { if (!this.isActive(entry)) return; entry.running = true; entry.dirty = false; @@ -158,7 +215,7 @@ export class WalletObservations { if (this.isActive(entry)) { this.notify({ type: NotificationType.ObservationError, - observationType: ObservationType.Transaction, + observationType: entry.observationType, observationId: entry.observationId, error: summarizeObservationError(getErrorDetailFromException(e))!, }); @@ -171,29 +228,24 @@ export class WalletObservations { } private publish( - entry: TransactionObservation, - snapshot: TransactionSnapshot | null, + entry: Observation, + event: ObservationSnapshotNotification, ): void { - // Compare exactly what can appear on the wire (undefined fields omitted). - const value = canonicalJson(JSON.parse(JSON.stringify(snapshot))); + const value = canonicalJson(JSON.parse(JSON.stringify(event.snapshot))); if (value !== entry.lastValue) { entry.lastValue = value; - this.notify({ - type: NotificationType.ObservationSnapshot, - observationType: ObservationType.Transaction, - observationId: entry.observationId, - transactionId: entry.transactionId, - revision: ++entry.revision, - snapshot, - }); + event.revision = ++entry.revision; + this.notify(event); } - if (!snapshot) { + if (!event.snapshot) { this.stop(entry.observationId); return; } - // Confirmation can become unavailable without a database write. const info = - "info" in snapshot.transaction ? snapshot.transaction.info : undefined; + event.observationType === ObservationType.Transaction && + "info" in event.snapshot.transaction + ? event.snapshot.transaction.info + : undefined; const expiration = info && "expiration" in info ? info.expiration?.t_s : undefined; const now = AbsoluteTime.now().t_ms; diff --git a/packages/taler-wallet-core/src/observe-transaction.test.ts b/packages/taler-wallet-core/src/observe-transaction.test.ts @@ -179,7 +179,12 @@ async function fixture(makeRunner: (typeof runnerFactories)[number]) { }); const notifications: WalletNotification[] = []; ws.addPublicNotificationListener((n) => notifications.push(n)); - function nextSnapshot(id = "view", revision?: number, deleted = false) { + function nextSnapshot( + id = "view", + revision?: number, + deleted = false, + matches?: (n: GenericTransactionSnapshotNotification) => boolean, + ) { const result = openPromise<TransactionSnapshotNotification>(); const cancel = ws.addPublicNotificationListener((n) => { if ( @@ -193,7 +198,9 @@ async function fixture(makeRunner: (typeof runnerFactories)[number]) { n.type === NotificationType.ObservationSnapshot && n.observationId === id && (revision === undefined || n.revision === revision) && - (!deleted || n.snapshot === null) + n.observationType === "transaction" && + (!deleted || n.snapshot === null) && + (!matches || matches(n)) ) { cancel(); result.resolve(n as TransactionSnapshotNotification); @@ -1042,6 +1049,7 @@ for (const makeRunner of runnerFactories) { ); assert.ok( notification?.type === U.NotificationType.ObservationSnapshot && + notification.observationType === U.ObservationType.Transaction && notification.snapshot, id, ); @@ -1347,6 +1355,15 @@ for (const makeRunner of runnerFactories) { // A future wire-fee period can make the same coins usable again. const wireStart = Math.floor(Date.now() / 1000) + 2; + nextPayment = f.nextSnapshot( + "txn:payment:payment", + undefined, + false, + (event) => + event.snapshot?.type === U.TransactionType.Payment && + event.snapshot.payment?.choices[0].status === + U.ChoiceSelectionDetailType.PaymentPossible, + ); await f.wex.runWalletDbTx(async (tx) => { const denomination = await tx.getDenomination({ exchangeMasterPub: master, @@ -1366,8 +1383,6 @@ for (const makeRunner of runnerFactories) { details.wireInfo.feesForType.iban[0].startStamp = { t_s: wireStart }; await tx.upsertExchangeDetails(details); }); - await f.settle(); - nextPayment = f.nextSnapshot("txn:payment:payment"); await new Promise((resolve) => setTimeout(resolve, Math.max(0, wireStart * 1000 - Date.now()) + 50), ); @@ -1452,6 +1467,7 @@ for (const makeRunner of runnerFactories) { ); assert.ok( bank?.type === U.NotificationType.ObservationSnapshot && + bank.observationType === U.ObservationType.Transaction && bank.snapshot?.type === U.TransactionType.Withdrawal, ); assert.equal(bank.snapshot.bank?.status, "pending"); diff --git a/packages/wallet-webui/test/observations-hook.test.tsx b/packages/wallet-webui/test/observations-hook.test.tsx @@ -59,6 +59,7 @@ async function fixture(summary: string) { subscribe((n) => { if ( n.type === NotificationType.ObservationSnapshot && + n.observationType === "transaction" && n.snapshot?.type === TransactionType.PeerPushCredit ) { const snapshot = n.snapshot; @@ -92,7 +93,7 @@ async function fixture(summary: string) { }; } -test("the hook resets on connection and target changes and rejects late notifications", async () => { +function installDom() { const dom = new Window({ url: "https://wallet.example/" }); for (const [key, value] of Object.entries({ window: dom, diff --git a/packages/wallet-webui/test/peer-credit-observation.test.tsx b/packages/wallet-webui/test/peer-credit-observation.test.tsx @@ -111,6 +111,7 @@ async function harness( const unsubscribe = subscribe((n) => { if ( n.type === NotificationType.ObservationSnapshot && + n.observationType === "transaction" && n.snapshot?.type === TransactionType.PeerPushCredit && n.snapshot.exchanges[0] ) { @@ -145,6 +146,7 @@ async function harness( } else if ( mode === "bank" && n.type === NotificationType.ObservationSnapshot && + n.observationType === "transaction" && n.snapshot?.type === TransactionType.Withdrawal ) { // The exchange can complete a funded reserve while cached bank status is stale.