From e81ae0f3e5a510424076b611ac32385057cbdaed Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Fri, 21 Apr 2023 22:02:34 +0200 Subject: wallet-harness: make sure events are not lost in deposit test --- .../taler-wallet-core/src/operations/deposits.ts | 46 +++++++++++++++++----- 1 file changed, 36 insertions(+), 10 deletions(-) (limited to 'packages/taler-wallet-core/src/operations/deposits.ts') diff --git a/packages/taler-wallet-core/src/operations/deposits.ts b/packages/taler-wallet-core/src/operations/deposits.ts index 700b875d3..f5ea41e01 100644 --- a/packages/taler-wallet-core/src/operations/deposits.ts +++ b/packages/taler-wallet-core/src/operations/deposits.ts @@ -67,7 +67,7 @@ import { getTotalRefreshCost, KycPendingInfo, KycUserType } from "../index.js"; import { InternalWalletState } from "../internal-wallet-state.js"; import { readSuccessResponseJsonOrThrow } from "@gnu-taler/taler-util/http"; import { OperationAttemptResult } from "../util/retries.js"; -import { makeTransactionId, spendCoins } from "./common.js"; +import { spendCoins } from "./common.js"; import { getExchangeDetails } from "./exchanges.js"; import { extractContractData, @@ -75,13 +75,20 @@ import { getTotalPaymentCost, } from "./pay-merchant.js"; import { selectPayCoinsNew } from "../util/coinSelection.js"; -import { constructTransactionIdentifier } from "./transactions.js"; +import { + constructTransactionIdentifier, + parseTransactionIdentifier, +} from "./transactions.js"; /** * Logger. */ const logger = new Logger("deposits.ts"); +/** + * Get the (DD37-style) transaction status based on the + * database record of a deposit group. + */ export async function computeDepositTransactionStatus( ws: InternalWalletState, dg: DepositGroupRecord, @@ -151,7 +158,8 @@ export async function abortDepositGroup( } /** - * Check KYC status with the exchange, throw an appropriate exception when KYC is required. + * Check KYC status with the exchange, throw an appropriate exception when KYC + * is required. * * FIXME: Why does this throw an exception when KYC is required? * Should we not return some proper result record here? @@ -221,6 +229,7 @@ export async function processDepositGroup( // Check for cancellation before expensive operations. options.cancellationToken?.throwIfCancelled(); + // FIXME: Cache these! const depositPermissions = await generateDepositPermissions( ws, depositGroup.payCoinSelection, @@ -438,7 +447,7 @@ async function trackDepositPermission( wireHash, }); url.searchParams.set("merchant_sig", sigResp.sig); - const httpResp = await ws.http.get(url.href); + const httpResp = await ws.http.fetch(url.href, { method: "GET" }); switch (httpResp.status) { case HttpStatusCode.Accepted: { const accepted = await readSuccessResponseJsonOrThrow( @@ -463,6 +472,9 @@ async function trackDepositPermission( } /** + * Check if creating a deposit group is possible and calculate + * the associated fees. + * * FIXME: This should be renamed to checkDepositGroup, * as it doesn't prepare anything */ @@ -671,9 +683,18 @@ export async function createDepositGroup( const totalDepositCost = await getTotalPaymentCost(ws, payCoinSel.coinSel); - const depositGroupId = encodeCrock(getRandomBytes(32)); + let depositGroupId: string; + if (req.transactionId) { + const txId = parseTransactionIdentifier(req.transactionId); + if (!txId || txId.tag !== TransactionType.Deposit) { + throw Error("invalid transaction ID"); + } + depositGroupId = txId.depositGroupId; + } else { + depositGroupId = encodeCrock(getRandomBytes(32)); + } - const countarpartyEffectiveDepositAmount = + const counterpartyEffectiveDepositAmount = await getCounterpartyEffectiveDepositAmount( ws, p.targetType, @@ -698,7 +719,7 @@ export async function createDepositGroup( merchantPub: merchantPair.pub, totalPayCost: Amounts.stringify(totalDepositCost), effectiveDepositAmount: Amounts.stringify( - countarpartyEffectiveDepositAmount, + counterpartyEffectiveDepositAmount, ), wire: { payto_uri: req.depositPaytoUri, @@ -707,6 +728,11 @@ export async function createDepositGroup( operationStatus: OperationStatus.Pending, }; + const transactionId = constructTransactionIdentifier({ + tag: TransactionType.Deposit, + depositGroupId, + }); + await ws.db .mktx((x) => [ x.depositGroups, @@ -718,7 +744,7 @@ export async function createDepositGroup( ]) .runReadWrite(async (tx) => { await spendCoins(ws, tx, { - allocationId: `txn:deposit:${depositGroup.depositGroupId}`, + allocationId: transactionId, coinPubs: payCoinSel.coinSel.coinPubs, contributions: payCoinSel.coinSel.coinContributions.map((x) => Amounts.parseOrThrow(x), @@ -729,8 +755,8 @@ export async function createDepositGroup( }); return { - depositGroupId: depositGroupId, - transactionId: makeTransactionId(TransactionType.Deposit, depositGroupId), + depositGroupId, + transactionId, }; } -- cgit v1.2.3