From 1d86bb8e9c74f09fd7dbeb2f806d857c8b8b5ea8 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 13 Feb 2024 12:09:28 -0300 Subject: fixes #8228 --- .../src/operations/transactions.ts | 86 +++++++++++++++++----- 1 file changed, 69 insertions(+), 17 deletions(-) (limited to 'packages/taler-wallet-core/src/operations') diff --git a/packages/taler-wallet-core/src/operations/transactions.ts b/packages/taler-wallet-core/src/operations/transactions.ts index 8fd7afae6..13eda7a92 100644 --- a/packages/taler-wallet-core/src/operations/transactions.ts +++ b/packages/taler-wallet-core/src/operations/transactions.ts @@ -28,6 +28,7 @@ import { PeerContractTerms, RefundInfoShort, RefundPaymentInfo, + ScopeType, stringifyPayPullUri, stringifyPayPushUri, TalerErrorCode, @@ -155,11 +156,30 @@ const logger = new Logger("taler-wallet-core:transactions.ts"); function shouldSkipCurrency( transactionsRequest: TransactionsRequest | undefined, currency: string, + exchangesInTransaction: string[], ): boolean { - if (!transactionsRequest?.currency) { - return false; + if (transactionsRequest?.scopeInfo) { + const sameCurrency = transactionsRequest.scopeInfo.currency.toLowerCase() === currency.toLowerCase() + switch (transactionsRequest.scopeInfo.type) { + case ScopeType.Global: { + return !sameCurrency + } + case ScopeType.Exchange: { + const exchangeInvolveInTransaction = exchangesInTransaction.indexOf(transactionsRequest.scopeInfo.url) !== -1 + return !sameCurrency || !exchangeInvolveInTransaction + } + case ScopeType.Auditor: { + // same currency and same auditor + throw Error("filering balance in auditor scope is not implemented") + } + default: assertUnreachable(transactionsRequest.scopeInfo) + } } - return transactionsRequest.currency.toLowerCase() !== currency.toLowerCase(); + // FIXME: remove next release + if (transactionsRequest?.currency) { + return transactionsRequest.currency.toLowerCase() !== currency.toLowerCase(); + } + return false; } function shouldSkipSearch( @@ -539,7 +559,7 @@ function buildTransactionForPeerPullCredit( const silentWithdrawalErrorForInvoice = wsrOrt?.lastError && wsrOrt.lastError.code === - TalerErrorCode.WALLET_WITHDRAWAL_GROUP_INCOMPLETE && + TalerErrorCode.WALLET_WITHDRAWAL_GROUP_INCOMPLETE && Object.values(wsrOrt.lastError.errorsPerCoin ?? {}).every((e) => { return ( e.code === TalerErrorCode.WALLET_UNEXPECTED_REQUEST_ERROR && @@ -569,10 +589,10 @@ function buildTransactionForPeerPullCredit( kycUrl: pullCredit.kycUrl, ...(wsrOrt?.lastError ? { - error: silentWithdrawalErrorForInvoice - ? undefined - : wsrOrt.lastError, - } + error: silentWithdrawalErrorForInvoice + ? undefined + : wsrOrt.lastError, + } : {}), }; } @@ -1052,8 +1072,8 @@ export async function getTransactions( .runReadOnly(async (tx) => { await iterRecordsForPeerPushDebit(tx, filter, async (pi) => { const amount = Amounts.parseOrThrow(pi.amount); - - if (shouldSkipCurrency(transactionsRequest, amount.currency)) { + const exchangesInTx = [pi.exchangeBaseUrl] + if (shouldSkipCurrency(transactionsRequest, amount.currency, exchangesInTx)) { return; } if (shouldSkipSearch(transactionsRequest, [])) { @@ -1068,7 +1088,8 @@ export async function getTransactions( await iterRecordsForPeerPullDebit(tx, filter, async (pi) => { const amount = Amounts.parseOrThrow(pi.amount); - if (shouldSkipCurrency(transactionsRequest, amount.currency)) { + const exchangesInTx = [pi.exchangeBaseUrl] + if (shouldSkipCurrency(transactionsRequest, amount.currency, exchangesInTx)) { return; } if (shouldSkipSearch(transactionsRequest, [])) { @@ -1102,7 +1123,8 @@ export async function getTransactions( // Legacy transaction return; } - if (shouldSkipCurrency(transactionsRequest, pi.currency)) { + const exchangesInTx = [pi.exchangeBaseUrl] + if (shouldSkipCurrency(transactionsRequest, pi.currency, exchangesInTx)) { return; } if (shouldSkipSearch(transactionsRequest, [])) { @@ -1140,7 +1162,8 @@ export async function getTransactions( await iterRecordsForPeerPullCredit(tx, filter, async (pi) => { const currency = Amounts.currencyOf(pi.amount); - if (shouldSkipCurrency(transactionsRequest, currency)) { + const exchangesInTx = [pi.exchangeBaseUrl] + if (shouldSkipCurrency(transactionsRequest, currency, exchangesInTx)) { return; } if (shouldSkipSearch(transactionsRequest, [])) { @@ -1173,7 +1196,19 @@ export async function getTransactions( await iterRecordsForRefund(tx, filter, async (refundGroup) => { const currency = Amounts.currencyOf(refundGroup.amountRaw); - if (shouldSkipCurrency(transactionsRequest, currency)) { + + const exchangesInTx: string[] = [] + const p = await tx.purchases.get(refundGroup.proposalId) + if (!p || !p.payInfo) return; //refund with no payment + + p.payInfo.payCoinSelection.coinPubs.forEach(async (cp) => { + const c = await tx.coins.get(cp) + if (c?.exchangeBaseUrl) { + exchangesInTx.push(c.exchangeBaseUrl) + } + }) + + if (shouldSkipCurrency(transactionsRequest, currency, exchangesInTx)) { return; } const contractData = await lookupMaybeContractData( @@ -1184,7 +1219,8 @@ export async function getTransactions( }); await iterRecordsForRefresh(tx, filter, async (rg) => { - if (shouldSkipCurrency(transactionsRequest, rg.currency)) { + const exchangesInTx = rg.infoPerExchange ? Object.keys(rg.infoPerExchange) : [] + if (shouldSkipCurrency(transactionsRequest, rg.currency, exchangesInTx)) { return; } let required = false; @@ -1204,10 +1240,12 @@ export async function getTransactions( }); await iterRecordsForWithdrawal(tx, filter, async (wsr) => { + const exchangesInTx = [wsr.exchangeBaseUrl] if ( shouldSkipCurrency( transactionsRequest, Amounts.currencyOf(wsr.rawWithdrawalAmount), + exchangesInTx, ) ) { return; @@ -1259,7 +1297,8 @@ export async function getTransactions( await iterRecordsForDeposit(tx, filter, async (dg) => { const amount = Amounts.parseOrThrow(dg.amount); - if (shouldSkipCurrency(transactionsRequest, amount.currency)) { + const exchangesInTx = dg.infoPerExchange ? Object.keys(dg.infoPerExchange) : [] + if (shouldSkipCurrency(transactionsRequest, amount.currency, exchangesInTx)) { return; } const opId = TaskIdentifiers.forDeposit(dg); @@ -1276,7 +1315,16 @@ export async function getTransactions( if (!purchase.payInfo) { return; } - if (shouldSkipCurrency(transactionsRequest, download.currency)) { + + const exchangesInTx: string[] = [] + purchase.payInfo.payCoinSelection.coinPubs.forEach(async (cp) => { + const c = await tx.coins.get(cp) + if (c?.exchangeBaseUrl) { + exchangesInTx.push(c.exchangeBaseUrl) + } + }) + + if (shouldSkipCurrency(transactionsRequest, download.currency, exchangesInTx)) { return; } const contractTermsRecord = await tx.contractTerms.get( @@ -1316,11 +1364,13 @@ export async function getTransactions( ); }); + //FIXME: remove rewards await iterRecordsForReward(tx, filter, async (tipRecord) => { if ( shouldSkipCurrency( transactionsRequest, Amounts.parseOrThrow(tipRecord.rewardAmountRaw).currency, + [tipRecord.exchangeBaseUrl], ) ) { return; @@ -1332,6 +1382,8 @@ export async function getTransactions( const retryRecord = await tx.operationRetries.get(opId); transactions.push(buildTransactionForTip(tipRecord, retryRecord)); }); + //ends REMOVE REWARDS + }); // One-off checks, because of a bug where the wallet previously -- cgit v1.2.3