diff options
author | Florian Dold <florian@dold.me> | 2024-02-27 15:39:50 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-02-27 15:39:50 +0100 |
commit | 130a90825609bd2a754d65041aab55d254eea8e9 (patch) | |
tree | a11758de65c2986cadfb719a546fea9f40bee34c /packages/taler-wallet-core/src | |
parent | 7e5827eb4fbde75e109d68393145151901c7a8ea (diff) | |
download | wallet-core-130a90825609bd2a754d65041aab55d254eea8e9.tar.xz |
-fix botched refactor
Diffstat (limited to 'packages/taler-wallet-core/src')
-rw-r--r-- | packages/taler-wallet-core/src/recoup.ts | 72 |
1 files changed, 71 insertions, 1 deletions
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<void> { + 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, |