wallet-core: fix/deduplicate funding payto URI generation for withdrawals

This commit is contained in:
Florian Dold 2022-10-07 14:45:25 +02:00
parent a93a0cae13
commit f479ca0139
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 42 additions and 19 deletions

View File

@ -58,7 +58,10 @@ import { processPurchasePay } from "./pay.js";
import { processRefreshGroup } from "./refresh.js"; import { processRefreshGroup } from "./refresh.js";
import { applyRefundFromPurchaseId } from "./refund.js"; import { applyRefundFromPurchaseId } from "./refund.js";
import { processTip } from "./tip.js"; import { processTip } from "./tip.js";
import { processWithdrawalGroup } from "./withdraw.js"; import {
augmentPaytoUrisForWithdrawal,
processWithdrawalGroup,
} from "./withdraw.js";
const logger = new Logger("taler-wallet-core:transactions.ts"); const logger = new Logger("taler-wallet-core:transactions.ts");
@ -473,31 +476,39 @@ function buildTransactionForBankIntegratedWithdraw(
} }
function buildTransactionForManualWithdraw( function buildTransactionForManualWithdraw(
wsr: WithdrawalGroupRecord, withdrawalGroup: WithdrawalGroupRecord,
exchangeDetails: ExchangeDetailsRecord, exchangeDetails: ExchangeDetailsRecord,
ort?: OperationRetryRecord, ort?: OperationRetryRecord,
): Transaction { ): Transaction {
if (wsr.wgInfo.withdrawalType !== WithdrawalRecordType.BankManual) if (withdrawalGroup.wgInfo.withdrawalType !== WithdrawalRecordType.BankManual)
throw Error(""); throw Error("");
const plainPaytoUris =
exchangeDetails.wireInfo?.accounts.map((x) => x.payto_uri) ?? [];
const exchangePaytoUris = augmentPaytoUrisForWithdrawal(
plainPaytoUris,
withdrawalGroup.reservePub,
withdrawalGroup.instructedAmount,
);
return { return {
type: TransactionType.Withdrawal, type: TransactionType.Withdrawal,
amountEffective: Amounts.stringify(wsr.denomsSel.totalCoinValue), amountEffective: Amounts.stringify(
amountRaw: Amounts.stringify(wsr.rawWithdrawalAmount), withdrawalGroup.denomsSel.totalCoinValue,
),
amountRaw: Amounts.stringify(withdrawalGroup.rawWithdrawalAmount),
withdrawalDetails: { withdrawalDetails: {
type: WithdrawalType.ManualTransfer, type: WithdrawalType.ManualTransfer,
reservePub: wsr.reservePub, reservePub: withdrawalGroup.reservePub,
exchangePaytoUris: exchangePaytoUris,
exchangeDetails.wireInfo?.accounts.map((x) =>
addPaytoQueryParams(x.payto_uri, { subject: wsr.reservePub }),
) ?? [],
}, },
exchangeBaseUrl: wsr.exchangeBaseUrl, exchangeBaseUrl: withdrawalGroup.exchangeBaseUrl,
pending: !wsr.timestampFinish, pending: !withdrawalGroup.timestampFinish,
timestamp: wsr.timestampStart, timestamp: withdrawalGroup.timestampStart,
transactionId: makeEventId( transactionId: makeEventId(
TransactionType.Withdrawal, TransactionType.Withdrawal,
wsr.withdrawalGroupId, withdrawalGroup.withdrawalGroupId,
), ),
frozen: false, frozen: false,
...(ort?.lastError ? { error: ort.lastError } : {}), ...(ort?.lastError ? { error: ort.lastError } : {}),

View File

@ -1483,6 +1483,19 @@ export async function getFundingPaytoUrisTx(
.runReadWrite((tx) => getFundingPaytoUris(tx, withdrawalGroupId)); .runReadWrite((tx) => getFundingPaytoUris(tx, withdrawalGroupId));
} }
export function augmentPaytoUrisForWithdrawal(
plainPaytoUris: string[],
reservePub: string,
instructedAmount: AmountJson,
): string[] {
return plainPaytoUris.map((x) =>
addPaytoQueryParams(x, {
amount: Amounts.stringify(instructedAmount),
message: `Taler Withdrawal ${reservePub}`,
}),
);
}
/** /**
* Get payto URIs that can be used to fund a withdrawal operation. * Get payto URIs that can be used to fund a withdrawal operation.
*/ */
@ -1512,11 +1525,10 @@ export async function getFundingPaytoUris(
); );
return []; return [];
} }
return plainPaytoUris.map((x) => return augmentPaytoUrisForWithdrawal(
addPaytoQueryParams(x, { plainPaytoUris,
amount: Amounts.stringify(withdrawalGroup.instructedAmount), withdrawalGroup.reservePub,
message: `Taler Withdrawal ${withdrawalGroup.reservePub}`, withdrawalGroup.instructedAmount,
}),
); );
} }