From f78d7dc613946b0316a559aa0161dd02b056b05d Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Tue, 27 Feb 2024 15:31:01 +0100 Subject: -move function --- packages/taler-wallet-core/src/recoup.ts | 85 +++----------------------------- 1 file changed, 7 insertions(+), 78 deletions(-) (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 0ec71f4e7..3105b3709 100644 --- a/packages/taler-wallet-core/src/recoup.ts +++ b/packages/taler-wallet-core/src/recoup.ts @@ -39,7 +39,6 @@ import { codecForReserveStatus, encodeCrock, getRandomBytes, - j2s, } from "@gnu-taler/taler-util"; import { readSuccessResponseJsonOrThrow } from "@gnu-taler/taler-util/http"; import { @@ -56,23 +55,23 @@ 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 type { InternalWalletState } from "./wallet.js"; +import { getDenomInfo, type InternalWalletState } from "./wallet.js"; import { internalCreateWithdrawalGroup } from "./withdraw.js"; -const logger = new Logger("operations/recoup.ts"); +export const logger = new Logger("operations/recoup.ts"); /** * Store a recoup group record in the database after marking * a coin in the group as finished. */ -async function putGroupAsFinished( +export async function putGroupAsFinished( ws: InternalWalletState, tx: WalletDbReadWriteTransaction< ["recoupGroups", "denominations", "refreshGroups", "coins"] @@ -114,76 +113,6 @@ async function recoupRewardCoin( ); } -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 ws.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); - }, - ); -} - async function recoupRefreshCoin( ws: InternalWalletState, recoupGroupId: string, @@ -194,7 +123,7 @@ async function recoupRefreshCoin( const d = await ws.db.runReadOnlyTx( ["coins", "denominations"], async (tx) => { - const denomInfo = await ws.getDenomInfo( + const denomInfo = await getDenomInfo( ws, tx, coin.exchangeBaseUrl, @@ -258,13 +187,13 @@ async function recoupRefreshCoin( logger.warn("refresh old coin for recoup not found"); return; } - const oldCoinDenom = await ws.getDenomInfo( + const oldCoinDenom = await getDenomInfo( ws, tx, oldCoin.exchangeBaseUrl, oldCoin.denomPubHash, ); - const revokedCoinDenom = await ws.getDenomInfo( + const revokedCoinDenom = await getDenomInfo( ws, tx, revokedCoin.exchangeBaseUrl, -- cgit v1.2.3