diff options
author | Florian Dold <florian@dold.me> | 2024-04-08 22:29:51 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2024-04-08 22:29:51 +0200 |
commit | 859d5c56c7a5b26e741254d6d1e9c5731a787ae1 (patch) | |
tree | 55a45b686974318b0a8bb63f307ade0830e7c0e3 /packages/taler-wallet-core/src/withdraw.ts | |
parent | 30dea85f1e2410f974f8c16e4e53d4ba1290442d (diff) | |
download | wallet-core-859d5c56c7a5b26e741254d6d1e9c5731a787ae1.tar.xz |
wallet-core: support cancellation for getWithdrawalDetailsForAmount
Diffstat (limited to 'packages/taler-wallet-core/src/withdraw.ts')
-rw-r--r-- | packages/taler-wallet-core/src/withdraw.ts | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index 960ffa525..68ff9d494 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -49,6 +49,7 @@ import { ExchangeWithdrawResponse, ExchangeWithdrawalDetails, ForcedDenomSel, + GetWithdrawalDetailsForAmountRequest, HttpStatusCode, LibtoolVersion, Logger, @@ -69,6 +70,7 @@ import { UnblindedSignature, WalletNotification, WithdrawUriInfoResponse, + WithdrawalDetailsForAmount, WithdrawalExchangeAccountDetails, WithdrawalType, addPaytoQueryParams, @@ -1273,7 +1275,6 @@ export async function updateWithdrawalDenoms( wex: WalletExecutionContext, exchangeBaseUrl: string, ): Promise<void> { - logger.trace( `updating denominations used for withdrawal for ${exchangeBaseUrl}`, ); @@ -1931,7 +1932,9 @@ export async function getExchangeWithdrawalInfo( ageRestricted: number | undefined, ): Promise<ExchangeWithdrawalDetails> { logger.trace("updating exchange"); - const exchange = await fetchFreshExchange(wex, exchangeBaseUrl); + const exchange = await fetchFreshExchange(wex, exchangeBaseUrl, { + cancellationToken: wex.cancellationToken, + }); if (exchange.currency != instructedAmount.currency) { // Specifying the amount in the conversion input currency is not yet supported. @@ -1947,7 +1950,7 @@ export async function getExchangeWithdrawalInfo( exchange, instructedAmount, }, - CancellationToken.CONTINUE, + wex.cancellationToken, ); logger.trace("updating withdrawal denoms"); @@ -3152,3 +3155,54 @@ async function internalWaitWithdrawalFinal( flag.reset(); } } + +export async function getWithdrawalDetailsForAmount( + wex: WalletExecutionContext, + cts: CancellationToken.Source, + req: GetWithdrawalDetailsForAmountRequest, +): Promise<WithdrawalDetailsForAmount> { + const clientCancelKey = req.clientCancellationId + ? `ccid:getWithdrawalDetailsForAmount:${req.clientCancellationId}` + : undefined; + if (clientCancelKey) { + const prevCts = wex.ws.clientCancellationMap.get(clientCancelKey); + if (prevCts) { + prevCts.cancel(); + } + wex.ws.clientCancellationMap.set(clientCancelKey, cts); + } + try { + return internalGetWithdrawalDetailsForAmount(wex, req); + } finally { + if (clientCancelKey && !cts.token.isCancelled) { + wex.ws.clientCancellationMap.delete(clientCancelKey); + } + } +} + +async function internalGetWithdrawalDetailsForAmount( + wex: WalletExecutionContext, + req: GetWithdrawalDetailsForAmountRequest, +): Promise<WithdrawalDetailsForAmount> { + const wi = await getExchangeWithdrawalInfo( + wex, + req.exchangeBaseUrl, + Amounts.parseOrThrow(req.amount), + req.restrictAge, + ); + let numCoins = 0; + for (const x of wi.selectedDenoms.selectedDenoms) { + numCoins += x.count; + } + const resp: WithdrawalDetailsForAmount = { + amountRaw: req.amount, + amountEffective: Amounts.stringify(wi.selectedDenoms.totalCoinValue), + paytoUris: wi.exchangePaytoUris, + tosAccepted: wi.termsOfServiceAccepted, + ageRestrictionOptions: wi.ageRestrictionOptions, + withdrawalAccountsList: wi.exchangeCreditAccountDetails, + numCoins, + scopeInfo: wi.scopeInfo, + }; + return resp; +} |