taler-typescript-core

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

commit cb305704b09979b23b0054d7bf5436d58000048c
parent 453906ee609e92cacffe81fbb65f462b4ca6610a
Author: Florian Dold <dold@taler.net>
Date:   Thu, 26 Nov 2026 00:18:10 +0100

wallet-core: recover age proofs for previously refreshed coins

Older wallets could store a private key that no longer matches the age
commitment. Rebuild the proof from refresh history when an
age-restricted payment needs it, and verify it against the original
commitment before saving it.

Diffstat:
Mpackages/taler-wallet-core/src/pay-merchant.ts | 45+++++++++++++++++++++++++--------------------
Mpackages/taler-wallet-core/src/refresh.ts | 94++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 114 insertions(+), 25 deletions(-)

diff --git a/packages/taler-wallet-core/src/pay-merchant.ts b/packages/taler-wallet-core/src/pay-merchant.ts @@ -192,6 +192,7 @@ import { classifyRecoveryRefresh, createRefreshGroup, getTotalRefreshCosts, + refreshCoinAgeProof, RefreshTransactionContext, } from "./refresh.js"; import { @@ -3027,26 +3028,30 @@ export async function generateDepositPermissions( const walletDataHash = walletData ? encodeCrock(hashPayWalletData(walletData)) : undefined; - const signRequests = coinWithDenom.map(({ coin, denom }, i) => { - return { - coinPriv: coin.coinPriv, - coinPub: coin.coinPub, - contractTermsHash, - denomPubHash: coin.denomPubHash, - denomKeyType: denom.denomPub.cipher, - denomSig: coin.denomSig, - exchangeBaseUrl: coin.exchangeBaseUrl, - feeDeposit: Amounts.parseOrThrow(denom.fees.feeDeposit), - merchantPub: contractData.merchant_pub, - refundDeadline: contractData.refund_deadline, - spendAmount: Amounts.parseOrThrow(payCoinSel.coinContributions[i]), - timestamp: contractData.timestamp, - wireInfoHash: contractData.h_wire, - ageCommitmentProof: coin.ageCommitmentProof, - requiredMinimumAge: contractData.minimum_age, - walletDataHash, - }; - }); + const signRequests = await Promise.all( + coinWithDenom.map(async ({ coin, denom }, i) => { + return { + coinPriv: coin.coinPriv, + coinPub: coin.coinPub, + contractTermsHash, + denomPubHash: coin.denomPubHash, + denomKeyType: denom.denomPub.cipher, + denomSig: coin.denomSig, + exchangeBaseUrl: coin.exchangeBaseUrl, + feeDeposit: Amounts.parseOrThrow(denom.fees.feeDeposit), + merchantPub: contractData.merchant_pub, + refundDeadline: contractData.refund_deadline, + spendAmount: Amounts.parseOrThrow(payCoinSel.coinContributions[i]), + timestamp: contractData.timestamp, + wireInfoHash: contractData.h_wire, + ageCommitmentProof: contractData.minimum_age + ? await refreshCoinAgeProof(wex, coin, contractData.minimum_age) + : coin.ageCommitmentProof, + requiredMinimumAge: contractData.minimum_age, + walletDataHash, + }; + }), + ); if (!cacheScope) { return await Promise.all( diff --git a/packages/taler-wallet-core/src/refresh.ts b/packages/taler-wallet-core/src/refresh.ts @@ -41,6 +41,7 @@ import { AutoRefreshOutputPlan, evaluateAutoRefresh } from "./autoRefresh.js"; import { AbsoluteTime, AgeRestriction, + AgeCommitmentProof, AmountJson, AmountLike, AmountString, @@ -60,6 +61,7 @@ import { deriveCsRefreshBlindingSeed, Duration, EddsaPublicKeyString, + Edx25519, EddsaSignatureString, encodeCrock, ExchangeMeltRequestV2, @@ -1097,7 +1099,19 @@ export async function recoverRefreshCoinNonce( if (coin.exchangeWithdrawValues.cipher !== DenomKeyType.ClauseSchnorr) { return undefined; } + const planchet = await recoverRefreshPlanchet(wex, coin); + return planchet?.coinEv.cipher === DenomKeyType.ClauseSchnorr + ? planchet.coinEv.cs_nonce + : undefined; +} + +async function recoverRefreshPlanchet( + wex: WalletExecutionContext, + coin: WalletCoin, + repairAncestors?: Set<string>, +): Promise<RefreshPlanchetInfo | undefined> { const src = coin.coinSource; + if (src.type !== CoinSourceType.Refresh) return undefined; const inputs = await wex.runWalletDbTx(async (tx) => { const group = await tx.getRefreshGroup(src.refreshGroupId); if (!group) { @@ -1109,7 +1123,11 @@ export async function recoverRefreshCoinNonce( } const session = await tx.getRefreshSession(src.refreshGroupId, coinIndex); const oldCoin = await tx.getCoin(src.oldCoinPub); - if (!session || !oldCoin || session.norevealIndex == null) { + if ( + !session?.sessionPublicSeed || + !oldCoin || + session.norevealIndex == null + ) { return undefined; } const oldDenom = await getDenomInfo(wex, tx, oldCoin); @@ -1138,6 +1156,14 @@ export async function recoverRefreshCoinNonce( if (!inputs) { return undefined; } + if (repairAncestors) { + inputs.oldCoin.ageCommitmentProof = await refreshCoinAgeProof( + wex, + inputs.oldCoin, + undefined, + repairAncestors, + ); + } const derived = await deriveRefreshSession( wex, inputs.session, @@ -1145,12 +1171,69 @@ export async function recoverRefreshCoinNonce( inputs.oldDenom, inputs.newCoinDenoms, ); - const planchet = derived.planchets[inputs.session.norevealIndex!]?.find( + return derived.planchets[inputs.session.norevealIndex!]?.find( (x) => x.coinPub === coin.coinPub, ); - return planchet?.coinEv.cipher === DenomKeyType.ClauseSchnorr - ? planchet.coinEv.cs_nonce - : undefined; +} + +async function ageProofKeysMatch( + proof: AgeCommitmentProof, + minimumAge?: number, +): Promise<boolean> { + const group = + minimumAge === undefined + ? undefined + : AgeRestriction.getAgeGroupIndex(proof.commitment.mask, minimumAge); + if (group !== undefined && group > proof.proof.privateKeys.length) + return false; + for (const [index, key] of proof.proof.privateKeys.entries()) { + if (group !== undefined && index !== group - 1) continue; + const pub = await Edx25519.getPublic(decodeCrock(key)); + if (encodeCrock(pub) !== proof.commitment.publicKeys[index]) return false; + } + return true; +} + +/** Older wallets could derive mismatched proof keys after a second refresh. */ +export async function refreshCoinAgeProof( + wex: WalletExecutionContext, + coin: WalletCoin, + minimumAge?: number, + ancestors = new Set<string>(), +): Promise<WalletCoin["ageCommitmentProof"]> { + const proof = coin.ageCommitmentProof; + if ( + !proof || + coin.coinSource.type !== CoinSourceType.Refresh || + (await ageProofKeysMatch(proof, minimumAge)) + ) + return proof; + checkDbInvariant(!ancestors.has(coin.coinPub), "cyclic refresh ancestry"); + ancestors.add(coin.coinPub); + const planchet = await recoverRefreshPlanchet(wex, coin, ancestors); + const restored = planchet?.ageCommitmentProof; + if ( + !restored || + restored.commitment.mask !== proof.commitment.mask || + AgeRestriction.hashCommitment(restored.commitment) !== + AgeRestriction.hashCommitment(proof.commitment) || + restored.proof.privateKeys.length !== proof.proof.privateKeys.length || + !(await ageProofKeysMatch(restored)) + ) { + // Deleted refresh history can prevent repair. The coin can still be + // refreshed or spent where no age attestation is required. + if (minimumAge === undefined) return proof; + throw Error( + "Cannot restore the age attestation for this coin from its refresh history.", + ); + } + // A payment or refresh can allocate the coin while its proof is rebuilt. + await wex.runWalletDbTx(async (tx) => { + const current = await tx.getCoin(coin.coinPub); + if (current) + await tx.upsertCoin({ ...current, ageCommitmentProof: restored }); + }); + return restored; } /** Revalidate an automatic session before preparing a new melt commitment. */ @@ -2207,6 +2290,7 @@ async function refreshReveal( checkLogicInvariant(refreshSession.sessionPublicSeed != null); + oldCoin.ageCommitmentProof = await refreshCoinAgeProof(wex, oldCoin); const derived = await deriveRefreshSession( wex, refreshSession,