commit ec618c8c9146c76daa571d6dd42e58b60fa5d11f
parent 92cda5bdcc01dd2259e654d602a800bc7933ecb9
Author: Florian Dold <dold@taler.net>
Date: Mon, 23 Nov 2026 23:17:12 +0100
wallet-core: report changes to exchange terms, KYC and currency scopes
A payment can depend on exchange terms or a KYC retry, and its balance
scope can change when the wallet adds or removes an exchange or auditor.
Wake listeners when those operations commit.
Diffstat:
5 files changed, 91 insertions(+), 41 deletions(-)
diff --git a/packages/taler-wallet-core/src/exchanges.ts b/packages/taler-wallet-core/src/exchanges.ts
@@ -3948,10 +3948,21 @@ export async function getExchangeTos(
await wex.runWalletDbTx(async (tx) => {
const updateExchangeEntry = await tx.getExchange(exchangeBaseUrl);
- if (updateExchangeEntry) {
+ if (
+ updateExchangeEntry &&
+ updateExchangeEntry.tosCurrentEtag !== tosDownload.tosEtag
+ ) {
+ const oldExchangeState = getExchangeState(updateExchangeEntry);
updateExchangeEntry.tosCurrentEtag = tosDownload.tosEtag;
wex.ws.exchangeCache.clear();
await tx.upsertExchange(updateExchangeEntry);
+ tx.notify({
+ type: NotificationType.ExchangeStateTransition,
+ exchangeBaseUrl,
+ causeHint: "tos-downloaded",
+ oldExchangeState,
+ newExchangeState: getExchangeState(updateExchangeEntry),
+ });
}
});
@@ -5525,6 +5536,16 @@ async function handleExchangeKycPendingLegitimization(
current.amlReview = resp.body.aml_review;
current.kycAccessToken = resp.body.access_token;
await tx.upsertReserve(current);
+ const entry = await tx.getExchange(exchange.baseUrl);
+ if (!entry || entry.currentMergeReserveRowId !== current.rowId) return;
+ const state = getExchangeState(entry);
+ tx.notify({
+ type: NotificationType.ExchangeStateTransition,
+ exchangeBaseUrl: exchange.baseUrl,
+ causeHint: "wallet-kyc-review",
+ oldExchangeState: state,
+ newExchangeState: state,
+ });
});
return TaskRunResult.longpollReturnedPending();
case HttpStatusCode.NoContent:
diff --git a/packages/taler-wallet-core/src/observe-transaction.test.ts b/packages/taler-wallet-core/src/observe-transaction.test.ts
@@ -1,4 +1,8 @@
-import { processExchangeKyc } from "./exchanges.js";
+import {
+ acceptExchangeTermsOfService,
+ getExchangeTos,
+ processExchangeKyc,
+} from "./exchanges.js";
/*
* This file is part of GNU Taler
* (C) 2026 Taler Systems S.A.
@@ -30,7 +34,6 @@ import {
} from "@gnu-taler/taler-util";
import { HeadersImpl, HttpRequestLibrary } from "@gnu-taler/taler-util/http";
import {
- DbRetryInfo,
TaskIdentifiers,
constructTaskIdentifier,
PendingTaskType,
@@ -60,7 +63,11 @@ import { dispatchRequestInternal } from "./requests.js";
import { WalletDbTransaction } from "./db/transaction.js";
import { WithdrawTransactionContext } from "./withdraw.js";
import { RefundTransactionContext } from "./pay-merchant.js";
-import { TaskSchedulerImpl } from "./shepherd.js";
+import {
+ clearTaskRetry,
+ storePendingTaskError,
+ TaskSchedulerImpl,
+} from "./shepherd.js";
import {
constructTransactionIdentifier,
parseTransactionIdentifier,
@@ -132,7 +139,7 @@ async function fixture(makeRunner: (typeof runnerFactories)[number]) {
tosAcceptedTimestamp: stamp,
lastKeysEtag: undefined,
lastUpdate: stamp,
- nextUpdateStamp: stamp,
+ nextUpdateStamp: timestampPreciseToDb({ t_s: "never" }),
nextRefreshCheckStamp: stamp,
};
const details: WalletExchangeDetails = {
@@ -359,9 +366,23 @@ for (const makeRunner of runnerFactories) {
try {
const first = await f.observe();
let next = f.nextSnapshot();
- await f.wex.runWalletDbTx(async (tx) => {
- await tx.upsertExchange({ ...f.exchange, tosCurrentEtag: "v2" });
- });
+ const http: HttpRequestLibrary = {
+ async fetch(url) {
+ assert.equal(url, exchangeUrl + "terms");
+ const headers = new HeadersImpl();
+ headers.set("Taler-Terms-Version", "v2");
+ return {
+ requestUrl: url,
+ requestMethod: "GET",
+ status: 200,
+ headers,
+ text: async () => "Updated terms",
+ json: async () => ({}),
+ bytes: async () => new Uint8Array(),
+ };
+ },
+ };
+ await getExchangeTos({ ...f.wex, http }, exchangeUrl);
const tos = await next;
assert.equal(tos.revision, 2);
assert.deepEqual(
@@ -373,17 +394,12 @@ for (const makeRunner of runnerFactories) {
]);
next = f.nextSnapshot();
- await f.ws.runStandaloneWalletDbTx(async (tx) => {
- await tx.upsertOperationRetry({
- id: TaskIdentifiers.forPeerPushCredit(f.credit),
- retryInfo: DbRetryInfo.reset(),
- lastError: {
- code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
- hint: "h".repeat(2000),
- message: "try again",
- details: { body: "large-error-data".repeat(10000) },
- },
- });
+ const retryId = TaskIdentifiers.forPeerPushCredit(f.credit);
+ await storePendingTaskError(f.ws, retryId, {
+ code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
+ hint: "h".repeat(2000),
+ message: "try again",
+ details: { body: "large-error-data".repeat(10000) },
});
const errored = await next;
assert.equal(errored.revision, 3);
@@ -391,27 +407,35 @@ for (const makeRunner of runnerFactories) {
assert.equal(errored.snapshot?.transaction.error?.message, "try again");
assert.ok(!JSON.stringify(errored).includes("large-error-data"));
- const count = f.notifications.length;
+ const snapshots = () =>
+ f.notifications.filter(
+ (n) => n.type === NotificationType.ObservationSnapshot,
+ );
+ const count = snapshots().length;
await assert.rejects(
f.wex.runWalletDbTx(async (tx) => {
- await tx.upsertPeerPushCredit({
+ await f.updateCredit(tx, {
...f.credit,
status: PeerPushCreditStatus.Aborted,
});
throw Error("rollback");
}),
);
- await f.wex.runWalletDbTx((tx) => tx.upsertPeerPushCredit(f.credit));
+ await f.wex.runWalletDbTx((tx) => f.updateCredit(tx, f.credit));
await f.settle();
- assert.equal(f.notifications.length, count);
+ assert.equal(snapshots().length, count);
next = f.nextSnapshot();
- await f.wex.runWalletDbTx((tx) =>
- tx.deleteOperationRetry(TaskIdentifiers.forPeerPushCredit(f.credit)),
- );
+ await clearTaskRetry(f.ws, retryId);
const cleared = await next;
assert.equal(cleared.revision, 4);
assert.equal(cleared.snapshot?.transaction.error, undefined);
+ next = f.nextSnapshot();
+ await acceptExchangeTermsOfService(f.wex, exchangeUrl, "v2");
+ assert.deepEqual(
+ (await next).snapshot?.confirmation.unavailableReasons,
+ [],
+ );
} finally {
await f.ws.shutdown();
}
@@ -831,16 +855,10 @@ for (const makeRunner of runnerFactories) {
);
assert.equal(t.kycUrl, `${exchangeUrl}kyc-spa/balance-token`);
t = await change(() =>
- f.wex.runWalletDbTx((tx) =>
- tx.upsertOperationRetry({
- id: retryId,
- retryInfo: DbRetryInfo.reset(),
- lastError: {
- code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
- hint: "KYC connection failed",
- },
- }),
- ),
+ storePendingTaskError(f.ws, retryId, {
+ code: TalerErrorCode.WALLET_UNEXPECTED_EXCEPTION,
+ hint: "KYC connection failed",
+ }),
);
assert.equal(t.error?.hint, "KYC connection failed");
assert.ok(t.retryInfo);
@@ -878,9 +896,7 @@ for (const makeRunner of runnerFactories) {
processExchangeKyc({ ...f.wex, http }, exchangeUrl),
);
assert.equal(t.kycAmlReview, false);
- t = await change(() =>
- f.wex.runWalletDbTx((tx) => tx.deleteOperationRetry(retryId)),
- );
+ t = await change(() => clearTaskRetry(f.ws, retryId));
assert.equal(t.error, undefined);
assert.equal(t.retryInfo, undefined);
t = await change(() =>
diff --git a/packages/taler-wallet-core/src/requests.test.ts b/packages/taler-wallet-core/src/requests.test.ts
@@ -444,7 +444,7 @@ test("application-resumed hint reports DB failures independently", async () => {
);
});
-test("removing one global exchange retains shared currency metadata", async () => {
+test("removing one global exchange retains shared currency metadata", async (t) => {
const exchanges = [
{
id: 1,
@@ -460,7 +460,9 @@ test("removing one global exchange retains shared currency metadata", async () =
},
];
let currencyInfoDeletes = 0;
+ const notify = t.mock.fn();
const tx = {
+ notify,
async getGlobalCurrencyExchange(
currency: string,
exchangeBaseUrl: string,
@@ -508,6 +510,13 @@ test("removing one global exchange retains shared currency metadata", async () =
exchangeMasterPub: "master-two",
});
assert.strictEqual(currencyInfoDeletes, 1);
+ assert.deepEqual(
+ notify.mock.calls.map((c) => c.arguments[0]),
+ [
+ { type: NotificationType.BalanceChange, isInternal: true },
+ { type: NotificationType.BalanceChange, isInternal: true },
+ ],
+ );
});
function makeCoinSuspensionContext(
diff --git a/packages/taler-wallet-core/src/requests.ts b/packages/taler-wallet-core/src/requests.ts
@@ -1871,6 +1871,7 @@ export async function handleAddGlobalCurrencyExchange(
exchangeBaseUrl: req.exchangeBaseUrl,
exchangeMasterPub: req.exchangeMasterPub,
});
+ tx.notify({ type: NotificationType.BalanceChange, isInternal: true });
});
return {};
}
@@ -1894,6 +1895,7 @@ async function handleRemoveGlobalCurrencyAuditor(
);
await tx.deleteGlobalCurrencyAuditor(existingRec.id);
wex.ws.exchangeCache.clear();
+ tx.notify({ type: NotificationType.BalanceChange, isInternal: true });
});
return {};
}
@@ -1924,6 +1926,7 @@ export async function handleRemoveGlobalCurrencyExchange(
currency: req.currency,
});
}
+ tx.notify({ type: NotificationType.BalanceChange, isInternal: true });
});
return {};
}
@@ -1947,6 +1950,7 @@ async function handleAddGlobalCurrencyAuditor(
auditorPub: req.auditorPub,
});
wex.ws.exchangeCache.clear();
+ tx.notify({ type: NotificationType.BalanceChange, isInternal: true });
});
return {};
}
diff --git a/packages/taler-wallet-core/src/shepherd.ts b/packages/taler-wallet-core/src/shepherd.ts
@@ -933,6 +933,7 @@ async function taskToRetryNotification(
switch (parsedTaskId.tag) {
case PendingTaskType.ExchangeUpdate:
+ case PendingTaskType.ExchangeWalletKyc:
return makeExchangeRetryNotification(ws, tx, pendingTaskId, e);
case PendingTaskType.PeerPullCredit:
case PendingTaskType.PeerPullDebit:
@@ -946,7 +947,6 @@ async function taskToRetryNotification(
case PendingTaskType.RefreshBalance:
case PendingTaskType.ValidateDenoms:
case PendingTaskType.CleanupExpiredTransactions:
- case PendingTaskType.ExchangeWalletKyc:
case PendingTaskType.ExchangeAutoRefresh:
return undefined;
case PendingTaskType.Recoup: