From abafae8a1bf5b8b22b09438eac1d2292b6f836f2 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 20 May 2024 12:48:44 -0300 Subject: fix #8856 #8840 --- packages/taler-wallet-core/src/withdraw.ts | 123 +++++++++++++---------------- 1 file changed, 56 insertions(+), 67 deletions(-) (limited to 'packages/taler-wallet-core/src/withdraw.ts') diff --git a/packages/taler-wallet-core/src/withdraw.ts b/packages/taler-wallet-core/src/withdraw.ts index d14689b12..b95ab8548 100644 --- a/packages/taler-wallet-core/src/withdraw.ts +++ b/packages/taler-wallet-core/src/withdraw.ts @@ -234,10 +234,6 @@ async function updateWithdrawalTransaction( } else if ( wgRecord.wgInfo.withdrawalType === WithdrawalRecordType.BankManual ) { - checkDbInvariant( - wgRecord.exchangeBaseUrl !== undefined, - "manual withdrawal without exchange can't be created", - ); checkDbInvariant( wgRecord.instructedAmount !== undefined, "manual withdrawal without amount can't be created", @@ -917,10 +913,6 @@ async function processPlanchetGenerate( withdrawalGroup: WithdrawalGroupRecord, coinIdx: number, ): Promise { - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); checkDbInvariant( withdrawalGroup.denomsSel !== undefined, "can't process unitialized exchange", @@ -1129,10 +1121,6 @@ async function processPlanchetExchangeBatchRequest( logger.info( `processing planchet exchange batch request ${withdrawalGroup.withdrawalGroupId}, start=${args.coinStartIndex}, len=${args.batchSize}`, ); - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); const exchangeBaseUrl = withdrawalGroup.exchangeBaseUrl; const batchReq: ExchangeBatchWithdrawRequest = { planchets: [] }; @@ -1268,10 +1256,6 @@ async function processPlanchetVerifyAndStoreCoin( resp: ExchangeWithdrawResponse, ): Promise { const withdrawalGroup = wgContext.wgRecord; - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); const exchangeBaseUrl = withdrawalGroup.exchangeBaseUrl; logger.trace(`checking and storing planchet idx=${coinIdx}`); @@ -1454,7 +1438,8 @@ export async function updateWithdrawalDenoms( denom.verificationStatus === DenominationVerificationStatus.Unverified ) { logger.trace( - `Validating denomination (${current + 1}/${denominations.length + `Validating denomination (${current + 1}/${ + denominations.length }) signature of ${denom.denomPubHash}`, ); let valid = false; @@ -1519,10 +1504,6 @@ async function processQueryReserve( if (withdrawalGroup.status !== WithdrawalGroupStatus.PendingQueryingStatus) { return TaskRunResult.backoff(); } - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); checkDbInvariant( withdrawalGroup.denomsSel !== undefined, "can't process unitialized exchange", @@ -1576,8 +1557,10 @@ async function processQueryReserve( ) { amountChanged = true; } - console.log(`amount change ${j2s(result.response)}`) - console.log(`amount change ${j2s(withdrawalGroup.denomsSel.totalWithdrawCost)}`) + console.log(`amount change ${j2s(result.response)}`); + console.log( + `amount change ${j2s(withdrawalGroup.denomsSel.totalWithdrawCost)}`, + ); const exchangeBaseUrl = withdrawalGroup.exchangeBaseUrl; const currency = Amounts.currencyOf(withdrawalGroup.instructedAmount); @@ -1756,10 +1739,6 @@ async function redenominateWithdrawal( if (!wg) { return; } - checkDbInvariant( - wg.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); checkDbInvariant( wg.denomsSel !== undefined, "can't process unitialized exchange", @@ -1899,10 +1878,6 @@ async function processWithdrawalGroupPendingReady( const { withdrawalGroupId } = withdrawalGroup; const ctx = new WithdrawTransactionContext(wex, withdrawalGroupId); - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't process unitialized exchange", - ); checkDbInvariant( withdrawalGroup.denomsSel !== undefined, "can't process unitialized exchange", @@ -2215,7 +2190,7 @@ export async function getExchangeWithdrawalInfo( ) { logger.warn( `wallet's support for exchange protocol version ${WALLET_EXCHANGE_PROTOCOL_VERSION} might be outdated ` + - `(exchange has ${exchange.protocolVersionRange}), checking for updates`, + `(exchange has ${exchange.protocolVersionRange}), checking for updates`, ); } } @@ -2332,10 +2307,6 @@ export async function getFundingPaytoUris( ): Promise { const withdrawalGroup = await tx.withdrawalGroups.get(withdrawalGroupId); checkDbInvariant(!!withdrawalGroup); - checkDbInvariant( - withdrawalGroup.exchangeBaseUrl !== undefined, - "can't get funding uri from uninitialized wg", - ); checkDbInvariant( withdrawalGroup.instructedAmount !== undefined, "can't get funding uri from uninitialized wg", @@ -2684,7 +2655,7 @@ export async function internalPrepareCreateWithdrawalGroup( args: { reserveStatus: WithdrawalGroupStatus; amount?: AmountJson; - exchangeBaseUrl?: string; + exchangeBaseUrl: string; forcedWithdrawalGroupId?: string; forcedDenomSel?: ForcedDenomSel; reserveKeyPair?: EddsaKeypair; @@ -2725,19 +2696,11 @@ export async function internalPrepareCreateWithdrawalGroup( let initialDenomSel: DenomSelectionState | undefined; const denomSelUid = encodeCrock(getRandomBytes(16)); - const creationInfo = - exchangeBaseUrl !== undefined && amount !== undefined - ? { - canonExchange: exchangeBaseUrl, - amount, - } - : undefined; - - if (creationInfo) { + if (amount !== undefined) { initialDenomSel = await getInitialDenomsSelection( wex, - creationInfo.canonExchange, - creationInfo.amount, + exchangeBaseUrl, + amount, args.forcedDenomSel, ); } @@ -2764,9 +2727,7 @@ export async function internalPrepareCreateWithdrawalGroup( wgInfo: args.wgInfo, }; - if (creationInfo) { - await fetchFreshExchange(wex, creationInfo.canonExchange); - } + await fetchFreshExchange(wex, exchangeBaseUrl); const transactionId = constructTransactionIdentifier({ tag: TransactionType.Withdrawal, @@ -2776,7 +2737,12 @@ export async function internalPrepareCreateWithdrawalGroup( return { withdrawalGroup, transactionId, - creationInfo, + creationInfo: !amount + ? undefined + : { + amount, + canonExchange: exchangeBaseUrl, + }, }; } @@ -2871,8 +2837,8 @@ export async function internalCreateWithdrawalGroup( wex: WalletExecutionContext, args: { reserveStatus: WithdrawalGroupStatus; + exchangeBaseUrl: string; amount?: AmountJson; - exchangeBaseUrl?: string; forcedWithdrawalGroupId?: string; forcedDenomSel?: ForcedDenomSel; reserveKeyPair?: EddsaKeypair; @@ -2917,6 +2883,7 @@ export async function prepareBankIntegratedWithdrawal( wex: WalletExecutionContext, req: { talerWithdrawUri: string; + selectedExchange?: string; }, ): Promise { const existingWithdrawalGroup = await wex.db.runReadOnlyTx( @@ -2929,13 +2896,6 @@ export async function prepareBankIntegratedWithdrawal( ); if (existingWithdrawalGroup) { - let url: string | undefined; - if ( - existingWithdrawalGroup.wgInfo.withdrawalType === - WithdrawalRecordType.BankIntegrated - ) { - url = existingWithdrawalGroup.wgInfo.bankInfo.confirmUrl; - } const info = await getWithdrawalDetailsForUri(wex, req.talerWithdrawUri); return { transactionId: constructTransactionIdentifier({ @@ -2945,6 +2905,18 @@ export async function prepareBankIntegratedWithdrawal( info, }; } + const withdrawInfo = await getBankWithdrawalInfo( + wex.http, + req.talerWithdrawUri, + ); + + const info = await getWithdrawalDetailsForUri(wex, req.talerWithdrawUri); + + const exchangeBaseUrl = + req.selectedExchange ?? withdrawInfo.suggestedExchange; + if (!exchangeBaseUrl) { + return { info }; + } /** * Withdrawal group without exchange and amount @@ -2954,20 +2926,20 @@ export async function prepareBankIntegratedWithdrawal( * same URI */ const withdrawalGroup = await internalCreateWithdrawalGroup(wex, { + exchangeBaseUrl, wgInfo: { withdrawalType: WithdrawalRecordType.BankIntegrated, bankInfo: { talerWithdrawUri: req.talerWithdrawUri, - confirmUrl: undefined, + confirmUrl: withdrawInfo.confirmTransferUrl, timestampBankConfirmed: undefined, timestampReserveInfoPosted: undefined, + wireTypes: withdrawInfo.wireTypes, }, }, reserveStatus: WithdrawalGroupStatus.DialogProposed, }); - const info = await getWithdrawalDetailsForUri(wex, req.talerWithdrawUri); - const withdrawalGroupId = withdrawalGroup.withdrawalGroupId; const ctx = new WithdrawTransactionContext(wex, withdrawalGroupId); @@ -3010,19 +2982,34 @@ export async function confirmWithdrawal( const exchange = await fetchFreshExchange(wex, selectedExchange); const talerWithdrawUri = withdrawalGroup.wgInfo.bankInfo.talerWithdrawUri; + const confirmUrl = withdrawalGroup.wgInfo.bankInfo.confirmUrl; + + /** + * The only reasong this to be undefined is because it is an old wallet + * database before adding the wireType field was added + */ + let wtypes: string[]; + if (withdrawalGroup.wgInfo.bankInfo.wireTypes === undefined) { + const withdrawInfo = await getBankWithdrawalInfo( + wex.http, + talerWithdrawUri, + ); + wtypes = withdrawInfo.wireTypes; + } else { + wtypes = withdrawalGroup.wgInfo.bankInfo.wireTypes; + } - const withdrawInfo = await getBankWithdrawalInfo(wex.http, talerWithdrawUri); const exchangePaytoUri = await getExchangePaytoUri( wex, selectedExchange, - withdrawInfo.wireTypes, + wtypes, ); const withdrawalAccountList = await fetchWithdrawalAccountInfo( wex, { exchange, - instructedAmount: withdrawInfo.amount, + instructedAmount: Amounts.parseOrThrow(req.amount), }, wex.cancellationToken, ); @@ -3057,9 +3044,10 @@ export async function confirmWithdrawal( bankInfo: { exchangePaytoUri, talerWithdrawUri, - confirmUrl: withdrawInfo.confirmTransferUrl, + confirmUrl: confirmUrl, timestampBankConfirmed: undefined, timestampReserveInfoPosted: undefined, + wireTypes: wtypes, }, }; @@ -3157,6 +3145,7 @@ export async function acceptWithdrawalFromUri( confirmUrl: withdrawInfo.confirmTransferUrl, timestampBankConfirmed: undefined, timestampReserveInfoPosted: undefined, + wireTypes: withdrawInfo.wireTypes, }, }, restrictAge: req.restrictAge, -- cgit v1.2.3