taler-typescript-core

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

commit eaedde9518d0387dd65b6eb0a457b2323a636d02
parent 06674d30269b1a6fe032c668430b2c657dd06c29
Author: Florian Dold <dold@taler.net>
Date:   Mon, 23 Nov 2026 19:50:13 +0100

wallet-core: recognize the wallet's own melts in coin history

Keep the melt signatures with the input coin after the refresh session
is gone. If an older refresh has no saved signatures, skip double-spend
warnings for melts on that coin.

Diffstat:
Mpackages/taler-harness/src/integrationtests/test-peer-spent-coin.ts | 32++++++++++++++++++++++++++++++++
Mpackages/taler-wallet-core/src/coin-history.ts | 60+++++++++++++++++++++++++++++++++++++++++++++---------------
Mpackages/taler-wallet-core/src/db/records.ts | 2++
Mpackages/taler-wallet-core/src/refresh.ts | 12++++++++++++
Mpackages/taler-wallet-core/src/requests.ts | 11++++++++++-
Mpackages/taler-wallet-core/src/wallet-notifications.test.ts | 44+++++++++++++++++++++++++++++++++++++++++++-
6 files changed, 144 insertions(+), 17 deletions(-)

diff --git a/packages/taler-harness/src/integrationtests/test-peer-spent-coin.ts b/packages/taler-harness/src/integrationtests/test-peer-spent-coin.ts @@ -17,6 +17,7 @@ import { AbsoluteTime, Duration, + NotificationType, TransactionIdStr, TransactionType, TransactionMajorState as Major, @@ -270,6 +271,7 @@ export async function runPeerSpentCoinHistoryTest(t: GlobalTestState) { await withdrawal.withdrawalFinishedCond; const [coin] = (await sender.call(Op.DumpCoins, {})).coins; t.assertDeepEqual(coin.denomValue, "TESTKUDOS:2"); + const unspentWallet = await sender.call(Op.ExportDb, {}); const { walletClient: recipient } = await createWalletDaemonWithClient(t, { name: "recipient", }); @@ -389,5 +391,35 @@ export async function runPeerSpentCoinHistoryTest(t: GlobalTestState) { (await sender.call(Op.GetBalances, {})).balances[0].available, "TESTKUDOS:1", ); + // A restored wallet also allocates one unit to change. The original wallet's + // signed change melt and purse deposit both belong to the original wallet. + exchange.faultProxy.clearAllFaults(); + t.allowLog({ + file: "exchange-httpd-testexchange-1-stderr.log", + level: "WARNING", + message: + /^Coin [0-9A-Z]+ has insufficient balance for purse deposit of amount TESTKUDOS:1$/, + maxOccurrences: 1, + justification: + "the restored wallet submits the original wallet's spent input", + }); + const { walletClient: restored } = await createWalletDaemonWithClient(t, { + name: "restored", + }); + await restored.call(Op.ImportDb, { dump: unspentWallet }); + const rejected = restored.waitForNotificationCond( + (n) => + n.type === NotificationType.TransactionStateTransition && + n.errorInfo?.code === + TalerErrorCode.WALLET_PEER_PUSH_PAYMENT_INSUFFICIENT_BALANCE, + ); + const payment = await restored.call(Op.InitiatePeerPushDebit, request); + await rejected; + const conflicts = (await restored.call(Op.GetUserAttention, {})).items.filter( + (item) => item.type === "coin-double-spent", + ); + t.assertDeepEqual(conflicts.length, 1); + t.assertDeepEqual(conflicts[0].transactionId, payment.transactionId); + t.assertAmountEquals(conflicts[0].amountSpentElsewhere, "TESTKUDOS:2"); } runPeerSpentCoinHistoryTest.suites = ["wallet"]; diff --git a/packages/taler-wallet-core/src/coin-history.ts b/packages/taler-wallet-core/src/coin-history.ts @@ -19,6 +19,8 @@ import { Amounts, assertUnreachable, CoinHistoryResponse, + decodeCrock, + encodeCrock, TransactionType, TransactionIdStr, DenominationInfo, @@ -298,13 +300,30 @@ export async function recordConflictingCoinHistory( if (!local?.history.some((h) => h.transactionId === transactionId)) return; const deposits: Array<{ target: string; amount: AmountString }> = []; const purses: Array<{ target: string; amount: AmountString }> = []; + const localMelts = new Set<string>(); + let unknownMelt = false; + let unknownDeposit = false; + let unknownPurse = false; for (const allocation of local.history) { + if (allocation.type === "refresh") { + if (allocation.meltSignatures) { + for (const signature of allocation.meltSignatures) + localMelts.add(signature); + } else if (Amounts.isNonZero(allocation.amount)) { + // Older wallets did not retain authorizations after deleting a refresh session. + unknownMelt = true; + } + continue; + } if (allocation.type !== "spend") continue; const ref = parseTransactionIdentifier(allocation.transactionId); switch (ref?.tag) { case TransactionType.Payment: { const purchase = await tx.getPurchase(ref.proposalId); - if (!purchase?.download) return; + if (!purchase?.download) { + unknownDeposit = true; + break; + } deposits.push({ target: purchase.download.contractTermsHash, amount: allocation.amount, @@ -313,7 +332,10 @@ export async function recordConflictingCoinHistory( } case TransactionType.Deposit: { const deposit = await tx.getDepositGroup(ref.depositGroupId); - if (!deposit) return; + if (!deposit) { + unknownDeposit = true; + break; + } deposits.push({ target: deposit.contractTermsHash, amount: allocation.amount, @@ -325,12 +347,16 @@ export async function recordConflictingCoinHistory( break; case TransactionType.PeerPullDebit: { const debit = await tx.getPeerPullDebit(ref.peerPullDebitId); - if (!debit) return; + if (!debit) { + unknownPurse = true; + break; + } purses.push({ target: debit.pursePub, amount: allocation.amount }); break; } default: - return; + unknownDeposit = true; + unknownPurse = true; } } const conflicts: Array<{ signature: string; amount: AmountString }> = []; @@ -338,6 +364,7 @@ export async function recordConflictingCoinHistory( switch (debit.type) { case "DEPOSIT": if ( + unknownDeposit || deposits.some( (d) => d.target === debit.h_contract_terms && @@ -348,6 +375,7 @@ export async function recordConflictingCoinHistory( break; case "PURSE-DEPOSIT": if ( + unknownPurse || purses.some( (p) => p.target === debit.purse_pub && @@ -356,17 +384,19 @@ export async function recordConflictingCoinHistory( ) continue; break; - case "MELT": - // A consumed refresh session may already have been deleted; its - // absence cannot establish that a different wallet made the melt. - // Zero change allocations never authorize a melt. - if ( - local.history.some( - (h) => h.type === "refresh" && Amounts.isNonZero(h.amount), - ) - ) - continue; - break; + case "MELT": { + const signature = encodeCrock(decodeCrock(debit.coin_sig)); + if (!unknownMelt && !localMelts.has(signature)) + conflicts.push({ signature, amount: debit.amount }); + continue; + } + case "RESERVE-OPEN-DEPOSIT": + // This wallet does not authorize reserve-opening coin contributions. + conflicts.push({ + signature: encodeCrock(decodeCrock(debit.coin_sig)), + amount: debit.coin_contribution, + }); + continue; default: continue; } diff --git a/packages/taler-wallet-core/src/db/records.ts b/packages/taler-wallet-core/src/db/records.ts @@ -1796,6 +1796,8 @@ export type WalletCoinHistoryItem = type: "refresh"; transactionId: TransactionIdStr; amount: AmountString; + /** Authorizations submitted for this allocation. Absent on older wallets. */ + meltSignatures?: string[]; } | { type: "recoup"; diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts @@ -1420,6 +1420,17 @@ async function refreshMelt( !!allocation, ) : "ordinary"; + if (!attempt) return; + // Keep authorizations with the coin even after a session is replaced or deleted. + if ( + history && + allocation?.type === "refresh" && + allocation.meltSignatures && + !allocation.meltSignatures.includes(derived.confirmSig) + ) { + allocation.meltSignatures.push(derived.confirmSig); + await tx.upsertCoinHistory(history); + } return attempt; }); if (!attempt) return; @@ -2873,6 +2884,7 @@ async function applyRefreshToOldCoins( } histEntry.history.push({ type: "refresh", + meltSignatures: [], transactionId: constructTransactionIdentifier({ tag: TransactionType.Refresh, refreshGroupId, diff --git a/packages/taler-wallet-core/src/requests.ts b/packages/taler-wallet-core/src/requests.ts @@ -672,7 +672,16 @@ async function dumpCoins(wex: WalletExecutionContext): Promise<CoinDumpJson> { withdrawalReservePub: withdrawalReservePub, coinStatus: c.status, ageCommitmentProof: c.ageCommitmentProof, - history: historyRec ? historyRec.history : [], + history: + historyRec?.history.map((entry) => + entry.type === "refresh" + ? { + type: entry.type, + transactionId: entry.transactionId, + amount: entry.amount, + } + : entry, + ) ?? [], }); } }); diff --git a/packages/taler-wallet-core/src/wallet-notifications.test.ts b/packages/taler-wallet-core/src/wallet-notifications.test.ts @@ -622,7 +622,7 @@ for (const makeRunner of runnerFactories) { { type: "MELT", amount: "TESTKUDOS:4", - coin_sig: "other", + coin_sig: encodeCrock(new Uint8Array(64).fill(1)), } as CoinHistoryResponse["history"][number], ], }, @@ -634,6 +634,48 @@ for (const makeRunner of runnerFactories) { assert.equal(grouped[0].type, "coin-double-spent"); assert.equal(grouped[0].attentionId, items[0].attentionId); assert.equal(grouped[0].amountSpentElsewhere, "TESTKUDOS:5"); + const localSig = encodeCrock(new Uint8Array(64).fill(42)); + const foreignSig = encodeCrock(new Uint8Array(64).fill(43)); + const confirmed = { + ...history("foreign"), + history: [ + { + type: "MELT", + amount: "TESTKUDOS:1", + coin_sig: localSig.toLowerCase(), + }, + { type: "MELT", amount: "TESTKUDOS:1", coin_sig: foreignSig }, + { + type: "RESERVE-OPEN-DEPOSIT", + coin_contribution: "TESTKUDOS:2", + coin_sig: signature, + reserve_sig: signature, + history_offset: 2, + }, + ], + } as CoinHistoryResponse; + await f.wex.runWalletDbTx(async (tx) => { + const local = (await tx.getCoinHistory(coinPub))!; + const allocation = local.history.find((h) => h.type === "refresh")!; + allocation.amount = "TESTKUDOS:3"; + await tx.upsertCoinHistory(local); + // With no historical melt signatures, only the reserve contribution is certain. + await recordConflictingCoinHistory(tx, coin, id, confirmed); + }); + const legacy = (await getUserAttention(f.wex)).items[0]; + assert.equal(legacy.type, "coin-double-spent"); + assert.equal(legacy.amountSpentElsewhere, "TESTKUDOS:7"); + await f.wex.runWalletDbTx(async (tx) => { + const local = (await tx.getCoinHistory(coinPub))!; + const allocation = local.history.find((h) => h.type === "refresh")!; + allocation.meltSignatures = [localSig]; + await tx.upsertCoinHistory(local); + await recordConflictingCoinHistory(tx, coin, id, confirmed); + await recordConflictingCoinHistory(tx, coin, change, confirmed, id); + }); + const tracked = (await getUserAttention(f.wex)).items[0]; + assert.equal(tracked.type, "coin-double-spent"); + assert.equal(tracked.amountSpentElsewhere, "TESTKUDOS:8"); // A standalone refresh owns its own warning. A later partial refund // changes the exchange balance but keeps the original debit in the inbox. const refundedDeposit: CoinHistoryResponse = {