taler-typescript-core

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

commit afa3675ff648d982645878e969ff36f26aff8823
parent 8a8df4e7fa17ca2e746d62b0cec90f2a4f8346f5
Author: Florian Dold <dold@taler.net>
Date:   Tue, 17 Nov 2026 17:22:46 +0100

wallet protocol: support signed batch recoup confirmations

Add the v42 batch recoup endpoints and verify their signed destination,
commitment, coin and credited amount. Normalize the new coin history
blinding-secret field while retaining compatibility with legacy records.

Diffstat:
Mpackages/taler-util/src/http-client/exchange-client.ts | 31++++++++++++++++++++++++++++---
Mpackages/taler-util/src/taler_signatures.ts | 6++++++
Mpackages/taler-util/src/types-taler-common.test.ts | 33+++++++++++++++++++++++++++++++++
Mpackages/taler-util/src/types-taler-common.ts | 28+++++++++++++++++++++++++++-
Mpackages/taler-util/src/types-taler-exchange.ts | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpackages/taler-wallet-core/src/exchange-signatures.ts | 128++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
6 files changed, 256 insertions(+), 25 deletions(-)

diff --git a/packages/taler-util/src/http-client/exchange-client.ts b/packages/taler-util/src/http-client/exchange-client.ts @@ -91,6 +91,10 @@ import { PurseConflictPartial, PurseCreateSuccessResponse, PurseDepositSuccessResponse, + RecoupBatchResponse, + RecoupWithdrawBatchRequest, + RecoupRefreshBatchRequest, + codecForRecoupBatchResponse, RecoupConfirmation, RecoupRefreshRequest, RecoupRequest, @@ -1938,9 +1942,30 @@ export class TalerExchangeHttpClient { } } - /** - * https://docs.taler.net/core/api-exchange.html#post--coins-$COIN_PUB-recoup - */ + /** Exchange protocol v42 batch recoup, retaining hidden sibling envelopes. */ + async recoupWithdrawBatch( + body: RecoupWithdrawBatchRequest, + ): Promise<OperationOk<RecoupBatchResponse> | OperationFail<HttpStatusCode>> { + return this.recoupBatch("recoup-withdraw", body); + } + + async recoupRefreshBatch( + body: RecoupRefreshBatchRequest, + ): Promise<OperationOk<RecoupBatchResponse> | OperationFail<HttpStatusCode>> { + return this.recoupBatch("recoup-refresh", body); + } + + private async recoupBatch( + path: string, + body: RecoupWithdrawBatchRequest | RecoupRefreshBatchRequest, + ): Promise<OperationOk<RecoupBatchResponse> | OperationFail<HttpStatusCode>> { + const resp = await this.fetch(path, { method: "POST", body }); + if (resp.status === HttpStatusCode.Ok) + return opSuccessFromHttp(resp, codecForRecoupBatchResponse()); + return opKnownHttpFailure(resp.status, resp); + } + + /** Legacy per-coin recoup endpoint, used before exchange protocol v42. */ async recoupCoin( coinPub: string, body: RecoupRequest, diff --git a/packages/taler-util/src/taler_signatures.ts b/packages/taler-util/src/taler_signatures.ts @@ -187,6 +187,12 @@ export enum TalerSignaturePurpose { */ EXCHANGE_CONFIRM_WITHDRAW = 1050, + /** Confirm a batch recoup to a reserve (exchange protocol v42). */ + EXCHANGE_CONFIRM_RECOUP_WITHDRAW_BATCH = 1051, + + /** Confirm a batch recoup to a refresh input (exchange protocol v42). */ + EXCHANGE_CONFIRM_RECOUP_REFRESH_BATCH = 1052, + /** * Signature where the auditor confirms that he is aware of certain denomination keys from the exchange. */ diff --git a/packages/taler-util/src/types-taler-common.test.ts b/packages/taler-util/src/types-taler-common.test.ts @@ -236,3 +236,36 @@ test("pre-v32 melt history does not require a refresh seed", () => { assert.strictEqual(decoded.history[0].type, "MELT"); assert.strictEqual(decoded.history[0].refresh_seed, undefined); }); + +for (const type of ["RECOUP-WITHDRAW", "RECOUP-REFRESH"] as const) { + test(`coin history accepts legacy and v42 recoup secrets (${type})`, () => { + const record = { + type, + history_offset: 1, + amount: "TESTKUDOS:1", + exchange_sig: "sig", + exchange_pub: "pub", + coin_sig: "coin-sig", + h_denom_pub: "denom", + reserve_pub: "reserve", + old_coin_pub: "old-coin", + timestamp: { t_s: 1 }, + }; + const codec = common.codecForCoinSpendHistoryItem(); + for (const field of ["coin_blind", "coin_blinding_secret"]) { + const decoded = codec.decode({ ...record, [field]: "secret" }); + assert.ok( + decoded.type === "RECOUP-WITHDRAW" || decoded.type === "RECOUP-REFRESH", + ); + assert.equal(decoded.coin_blind, "secret"); + } + assert.throws(() => codec.decode(record)); + assert.throws(() => + codec.decode({ + ...record, + coin_blind: "one", + coin_blinding_secret: "two", + }), + ); + }); +} diff --git a/packages/taler-util/src/types-taler-common.ts b/packages/taler-util/src/types-taler-common.ts @@ -31,6 +31,7 @@ import { codecForAmountString } from "./amounts.js"; import { CancellationToken } from "./CancellationToken.js"; import { Codec, + DecodingError, buildCodecForObject, buildCodecForUnion, codecForBoolean, @@ -389,7 +390,7 @@ export type CoinSpendHistoryItem = | CoinPurseRefundTransaction | CoinReserveOpenDepositTransaction; -export const codecForCoinSpendHistoryItem = (): Codec<CoinSpendHistoryItem> => +const rawCoinSpendHistoryItemCodec = (): Codec<CoinSpendHistoryItem> => buildCodecForUnion<CoinSpendHistoryItem>() .discriminateOn("type") .alternative( @@ -530,6 +531,31 @@ export const codecForCoinSpendHistoryItem = (): Codec<CoinSpendHistoryItem> => ) .build("CoinSpendHistoryItem"); +/** Normalize the v42 spelling while retaining the legacy in-memory field. */ +export const codecForCoinSpendHistoryItem = (): Codec<CoinSpendHistoryItem> => { + const raw = rawCoinSpendHistoryItemCodec(); + return { + decode(value, context) { + if ( + value && + (value.type === "RECOUP-WITHDRAW" || value.type === "RECOUP-REFRESH") && + value.coin_blinding_secret !== undefined + ) { + if ( + value.coin_blind !== undefined && + value.coin_blind !== value.coin_blinding_secret + ) { + throw new DecodingError( + "conflicting recoup blinding secrets in coin history", + ); + } + value = { ...value, coin_blind: value.coin_blinding_secret }; + } + return raw.decode(value, context); + }, + }; +}; + export const codecForCoinHistoryResponse = (): Codec<CoinHistoryResponse> => buildCodecForObject<CoinHistoryResponse>() .property("balance", codecForAmountString()) diff --git a/packages/taler-util/src/types-taler-exchange.ts b/packages/taler-util/src/types-taler-exchange.ts @@ -3262,3 +3262,58 @@ export const codecForPurseContractConflict = (): Codec<PurseContractConflict> => .property("econtract_sig", codecForString()) .property("contract_pub", codecForString()) .build("PurseContractConflict"); + +/** Protocol-v42 recoup proofs retain the ordering of the original operation. */ +export type RecoupBatchCoinData = + | { type: "hash"; h_coin_ev: string } + | { + type: "recoup"; + coin_pub: string; + denom_pub_h: string; + denom_sig: RecoupRequest["denom_sig"]; + coin_blinding_secret: string; + h_age_commitment?: string; + coin_sig: string; + }; + +export interface RecoupWithdrawBatchRequest { + reserve_pub: string; + planchets_h: string; + coin_data: RecoupBatchCoinData[]; +} +export interface RecoupRefreshBatchRequest { + old_coin_pub: string; + rc: string; + coin_data: RecoupBatchCoinData[]; +} +export interface RecoupBatchResponse { + reserve_pub?: string; + planchets_h?: string; + old_coin_pub?: string; + rc?: string; + timestamp: TalerProtocolTimestamp; + total_amount: AmountString; + recoups: { coin_pub: string; amount: AmountString }[]; + exchange_pub: string; + exchange_sig: string; +} +export const codecForRecoupBatchResponse = (): Codec<RecoupBatchResponse> => + buildCodecForObject<RecoupBatchResponse>() + .property("reserve_pub", codecOptional(codecForString())) + .property("planchets_h", codecOptional(codecForString())) + .property("old_coin_pub", codecOptional(codecForString())) + .property("rc", codecOptional(codecForString())) + .property("timestamp", codecForTimestamp) + .property("total_amount", codecForAmountString()) + .property( + "recoups", + codecForList( + buildCodecForObject<{ coin_pub: string; amount: AmountString }>() + .property("coin_pub", codecForString()) + .property("amount", codecForAmountString()) + .build("RecoupedCoinResult"), + ), + ) + .property("exchange_pub", codecForString()) + .property("exchange_sig", codecForString()) + .build("RecoupBatchResponse"); diff --git a/packages/taler-wallet-core/src/exchange-signatures.ts b/packages/taler-wallet-core/src/exchange-signatures.ts @@ -15,6 +15,14 @@ */ import { + RecoupBatchResponse, + TalerSignaturePurpose, + buildSigPS, + bufferFromAmount, + createHashContext, + decodeCrock, + eddsaVerify, + timestampRoundedToBuffer, AbsoluteTime, AmountString, Amounts, @@ -75,6 +83,7 @@ async function isKnownExchangeSigningKey( exchangeBaseUrl: string, exchangePub: EddsaPublicKeyString, signedAt: AbsoluteTime, + allowReplay = false, ): Promise<boolean> { return await wex.runWalletDbTx(async (tx) => { const details = await tx.getExchangeDetails(exchangeBaseUrl); @@ -82,7 +91,11 @@ async function isKnownExchangeSigningKey( return false; } const signKeys = await tx.getExchangeSignKeysByDetailsRowId(details.rowId); - return exchangeSigningKeyIsUsable(signKeys, exchangePub, signedAt); + return ( + exchangeSigningKeyIsUsable(signKeys, exchangePub, signedAt) || + (allowReplay && + exchangeSigningKeyIsUsable(signKeys, exchangePub, AbsoluteTime.now())) + ); }); } @@ -183,27 +196,34 @@ export async function requireValidExchangeCoinHistory( ); } - const exchangeSignatures = args.response.history.flatMap((item) => { - switch (item.type) { - case "RECOUP-WITHDRAW": - case "RECOUP-REFRESH": - case "RECOUP-REFRESH-RECEIVER": - return [ - { - pub: item.exchange_pub, - at: AbsoluteTime.fromProtocolTimestamp(item.timestamp), - }, - ]; - case "PURSE-REFUND": - return [{ pub: item.exchange_pub, at: AbsoluteTime.now() }]; - default: - return []; - } - }); const known = await Promise.all( - exchangeSignatures.map((sig) => - isKnownExchangeSigningKey(wex, args.exchangeBaseUrl, sig.pub, sig.at), - ), + args.response.history.flatMap((item) => { + switch (item.type) { + case "RECOUP-WITHDRAW": + case "RECOUP-REFRESH": + case "RECOUP-REFRESH-RECEIVER": + return [ + isKnownExchangeSigningKey( + wex, + args.exchangeBaseUrl, + item.exchange_pub, + AbsoluteTime.fromProtocolTimestamp(item.timestamp), + true, + ), + ]; + case "PURSE-REFUND": + return [ + isKnownExchangeSigningKey( + wex, + args.exchangeBaseUrl, + item.exchange_pub, + AbsoluteTime.now(), + ), + ]; + default: + return []; + } + }), ); if (known.some((x) => !x)) { throw invalidExchangeSignature( @@ -439,3 +459,69 @@ export async function requireValidExchangeWireConfirmation( ); } } + +/** Bind a single disclosed coin to the signed v42 batch confirmation. */ +export async function requireValidRecoupBatchConfirmation( + wex: WalletExecutionContext, + exchangeBaseUrl: string, + kind: "withdraw" | "refresh", + destination: string, + commitment: string, + coinPub: string, + maximum: AmountString, + response: RecoupBatchResponse, +): Promise<AmountString> { + const target = + kind === "withdraw" ? response.reserve_pub : response.old_coin_pub; + const actualCommitment = + kind === "withdraw" ? response.planchets_h : response.rc; + if ( + target !== destination || + actualCommitment !== commitment || + response.recoups.length !== 1 || + response.recoups[0].coin_pub !== coinPub || + Amounts.cmp(response.total_amount, response.recoups[0].amount) !== 0 || + Amounts.cmp(response.total_amount, maximum) > 0 + ) { + throw invalidExchangeSignature( + "recoup confirmation does not match the request", + ); + } + const hc = createHashContext(); + for (const item of response.recoups) { + hc.update(decodeCrock(item.coin_pub)); + hc.update(bufferFromAmount(Amounts.parseOrThrow(item.amount))); + } + // Protocol signature purposes defined in taler_signatures.h (v42). + const payload = buildSigPS( + kind === "withdraw" + ? TalerSignaturePurpose.EXCHANGE_CONFIRM_RECOUP_WITHDRAW_BATCH + : TalerSignaturePurpose.EXCHANGE_CONFIRM_RECOUP_REFRESH_BATCH, + ) + .put(timestampRoundedToBuffer(response.timestamp)) + .put(decodeCrock(destination)) + .put(decodeCrock(commitment)) + .put(bufferFromAmount(Amounts.parseOrThrow(response.total_amount))) + .put(hc.finish()) + .build(); + if ( + !(await isKnownExchangeSigningKey( + wex, + exchangeBaseUrl, + response.exchange_pub, + AbsoluteTime.fromProtocolTimestamp(response.timestamp), + // A replay may attest the original credit with the current signing key. + true, + )) || + !eddsaVerify( + payload, + decodeCrock(response.exchange_sig), + decodeCrock(response.exchange_pub), + ) + ) { + throw invalidExchangeSignature( + "invalid recoup batch confirmation signature", + ); + } + return response.total_amount; +}