diff options
Diffstat (limited to 'packages/taler-wallet-core/src/withdraw.ts')
-rw-r--r-- | packages/taler-wallet-core/src/withdraw.ts | 77 |
1 files changed, 26 insertions, 51 deletions
diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index 8767c1eca..960ffa525 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -58,7 +58,6 @@ import { TalerErrorCode, TalerErrorDetail, TalerPreciseTimestamp, - TalerProtocolTimestamp, Transaction, TransactionAction, TransactionIdStr, @@ -76,7 +75,6 @@ import { assertUnreachable, canonicalizeBaseUrl, checkDbInvariant, - checkLogicInvariant, codeForBankWithdrawalOperationPostResponse, codecForCashinConversionResponse, codecForConversionBankConfig, @@ -129,6 +127,7 @@ import { WithdrawalGroupRecord, WithdrawalGroupStatus, WithdrawalRecordType, + timestampAbsoluteFromDb, timestampPreciseFromDb, timestampPreciseToDb, } from "./db.js"; @@ -1274,6 +1273,7 @@ export async function updateWithdrawalDenoms( wex: WalletExecutionContext, exchangeBaseUrl: string, ): Promise<void> { + logger.trace( `updating denominations used for withdrawal for ${exchangeBaseUrl}`, ); @@ -1347,6 +1347,7 @@ export async function updateWithdrawalDenoms( await tx.denominations.put(denom); } }); + wex.ws.denomInfoCache.clear(); logger.trace("done with DB write"); } } @@ -1583,6 +1584,8 @@ async function redenominateWithdrawal( let amountRemaining = zero; let prevTotalCoinValue = zero; let prevTotalWithdrawalCost = zero; + let prevHasDenomWithAgeRestriction = false; + let prevEarliestDepositExpiration = AbsoluteTime.never(); let prevDenoms: DenomSelItem[] = []; let coinIndex = 0; for (let i = 0; i < oldSel.selectedDenoms.length; i++) { @@ -1615,6 +1618,12 @@ async function redenominateWithdrawal( denomPubHash: sel.denomPubHash, skip: sel.skip, }); + prevHasDenomWithAgeRestriction = + prevHasDenomWithAgeRestriction || denom.denomPub.age_mask > 0; + prevEarliestDepositExpiration = AbsoluteTime.min( + prevEarliestDepositExpiration, + timestampAbsoluteFromDb(denom.stampExpireDeposit), + ); } else { amountRemaining = amountRemaining.add(denomValue, denomFeeWithdraw); prevDenoms.push({ @@ -1663,6 +1672,16 @@ async function redenominateWithdrawal( totalWithdrawCost: zero .add(prevTotalWithdrawalCost, newSel.totalWithdrawCost) .toString(), + hasDenomWithAgeRestriction: + prevHasDenomWithAgeRestriction || newSel.hasDenomWithAgeRestriction, + earliestDepositExpiration: AbsoluteTime.toProtocolTimestamp( + AbsoluteTime.min( + prevEarliestDepositExpiration, + AbsoluteTime.fromProtocolTimestamp( + newSel.earliestDepositExpiration, + ), + ), + ), }; wg.denomsSel = mergedSel; if (logger.shouldLogTrace()) { @@ -1935,15 +1954,16 @@ export async function getExchangeWithdrawalInfo( await updateWithdrawalDenoms(wex, exchangeBaseUrl); logger.trace("getting candidate denoms"); - const denoms = await getCandidateWithdrawalDenoms( + const candidateDenoms = await getCandidateWithdrawalDenoms( wex, exchangeBaseUrl, instructedAmount.currency, ); logger.trace("selecting withdrawal denoms"); + // FIXME: Why not in a transaction? const selectedDenoms = selectWithdrawalDenominations( instructedAmount, - denoms, + candidateDenoms, wex.ws.config.testing.denomselAllowLate, ); @@ -1963,48 +1983,6 @@ export async function getExchangeWithdrawalInfo( exchangeWireAccounts.push(account.payto_uri); } - let hasDenomWithAgeRestriction = false; - - logger.trace("computing earliest deposit expiration"); - - let earliestDepositExpiration: TalerProtocolTimestamp | undefined; - - await wex.db.runReadOnlyTx(["denominations"], async (tx) => { - for (let i = 0; i < selectedDenoms.selectedDenoms.length; i++) { - const ds = selectedDenoms.selectedDenoms[i]; - const denom = await getDenomInfo( - wex, - tx, - exchangeBaseUrl, - ds.denomPubHash, - ); - checkDbInvariant(!!denom); - hasDenomWithAgeRestriction = - hasDenomWithAgeRestriction || denom.denomPub.age_mask > 0; - const expireDeposit = denom.stampExpireDeposit; - if (!earliestDepositExpiration) { - earliestDepositExpiration = expireDeposit; - continue; - } - if ( - AbsoluteTime.cmp( - AbsoluteTime.fromProtocolTimestamp(expireDeposit), - AbsoluteTime.fromProtocolTimestamp(earliestDepositExpiration), - ) < 0 - ) { - earliestDepositExpiration = expireDeposit; - } - } - }); - - checkLogicInvariant(!!earliestDepositExpiration); - - const possibleDenoms = await getCandidateWithdrawalDenoms( - wex, - exchangeBaseUrl, - instructedAmount.currency, - ); - let versionMatch; if (exchange.protocolVersionRange) { versionMatch = LibtoolVersion.compare( @@ -2037,15 +2015,12 @@ export async function getExchangeWithdrawalInfo( } const ret: ExchangeWithdrawalDetails = { - earliestDepositExpiration, + earliestDepositExpiration: selectedDenoms.earliestDepositExpiration, exchangePaytoUris: paytoUris, exchangeWireAccounts, exchangeCreditAccountDetails: withdrawalAccountsList, exchangeVersion: exchange.protocolVersionRange || "unknown", - numOfferedDenoms: possibleDenoms.length, selectedDenoms, - // FIXME: delete this field / replace by something we can display to the user - trustedAuditorPubs: [], versionMatch, walletVersion: WALLET_EXCHANGE_PROTOCOL_VERSION, termsOfServiceAccepted: tosAccepted, @@ -2053,7 +2028,7 @@ export async function getExchangeWithdrawalInfo( withdrawalAmountRaw: Amounts.stringify(instructedAmount), // TODO: remove hardcoding, this should be calculated from the denominations info // force enabled for testing - ageRestrictionOptions: hasDenomWithAgeRestriction + ageRestrictionOptions: selectedDenoms.hasDenomWithAgeRestriction ? AGE_MASK_GROUPS : undefined, scopeInfo: exchange.scopeInfo, |