From 130a90825609bd2a754d65041aab55d254eea8e9 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 27 Feb 2024 15:39:50 +0100 Subject: -fix botched refactor --- packages/taler-wallet-core/src/recoup.ts | 72 +++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) (limited to 'packages/taler-wallet-core/src/recoup.ts') diff --git a/packages/taler-wallet-core/src/recoup.ts b/packages/taler-wallet-core/src/recoup.ts index 3105b3709..3ef494c3a 100644 --- a/packages/taler-wallet-core/src/recoup.ts +++ b/packages/taler-wallet-core/src/recoup.ts @@ -39,6 +39,7 @@ import { codecForReserveStatus, encodeCrock, getRandomBytes, + j2s, } from "@gnu-taler/taler-util"; import { readSuccessResponseJsonOrThrow } from "@gnu-taler/taler-util/http"; import { @@ -55,11 +56,11 @@ import { RecoupOperationStatus, RefreshCoinSource, WalletDbReadWriteTransaction, + WithdrawCoinSource, WithdrawalGroupStatus, WithdrawalRecordType, timestampPreciseToDb, } from "./db.js"; -import { recoupWithdrawCoin } from "./recoupWithdrawCoin.js"; import { createRefreshGroup } from "./refresh.js"; import { constructTransactionIdentifier } from "./transactions.js"; import { getDenomInfo, type InternalWalletState } from "./wallet.js"; @@ -224,6 +225,75 @@ async function recoupRefreshCoin( ); } +export async function recoupWithdrawCoin( + ws: InternalWalletState, + recoupGroupId: string, + coinIdx: number, + coin: CoinRecord, + cs: WithdrawCoinSource, +): Promise { + const reservePub = cs.reservePub; + const denomInfo = await ws.db.runReadOnlyTx(["denominations"], async (tx) => { + const denomInfo = await getDenomInfo( + ws, + tx, + coin.exchangeBaseUrl, + coin.denomPubHash, + ); + return denomInfo; + }); + if (!denomInfo) { + // FIXME: We should at least emit some pending operation / warning for this? + return; + } + + const recoupRequest = await ws.cryptoApi.createRecoupRequest({ + blindingKey: coin.blindingKey, + coinPriv: coin.coinPriv, + coinPub: coin.coinPub, + denomPub: denomInfo.denomPub, + denomPubHash: coin.denomPubHash, + denomSig: coin.denomSig, + }); + const reqUrl = new URL(`/coins/${coin.coinPub}/recoup`, coin.exchangeBaseUrl); + logger.trace(`requesting recoup via ${reqUrl.href}`); + const resp = await ws.http.fetch(reqUrl.href, { + method: "POST", + body: recoupRequest, + }); + const recoupConfirmation = await readSuccessResponseJsonOrThrow( + resp, + codecForRecoupConfirmation(), + ); + + logger.trace(`got recoup confirmation ${j2s(recoupConfirmation)}`); + + if (recoupConfirmation.reserve_pub !== reservePub) { + throw Error(`Coin's reserve doesn't match reserve on recoup`); + } + + // FIXME: verify that our expectations about the amount match + await ws.db.runReadWriteTx( + ["coins", "denominations", "recoupGroups", "refreshGroups"], + async (tx) => { + const recoupGroup = await tx.recoupGroups.get(recoupGroupId); + if (!recoupGroup) { + return; + } + if (recoupGroup.recoupFinishedPerCoin[coinIdx]) { + return; + } + const updatedCoin = await tx.coins.get(coin.coinPub); + if (!updatedCoin) { + return; + } + updatedCoin.status = CoinStatus.Dormant; + await tx.coins.put(updatedCoin); + await putGroupAsFinished(ws, tx, recoupGroup, coinIdx); + }, + ); +} + export async function processRecoupGroup( ws: InternalWalletState, recoupGroupId: string, -- cgit v1.2.3