diff options
Diffstat (limited to 'packages/taler-wallet-core/src/pay-peer-pull-debit.ts')
-rw-r--r-- | packages/taler-wallet-core/src/pay-peer-pull-debit.ts | 93 |
1 files changed, 44 insertions, 49 deletions
diff --git a/packages/taler-wallet-core/src/pay-peer-pull-debit.ts b/packages/taler-wallet-core/src/pay-peer-pull-debit.ts index 30bd1a2c8..828f68113 100644 --- a/packages/taler-wallet-core/src/pay-peer-pull-debit.ts +++ b/packages/taler-wallet-core/src/pay-peer-pull-debit.ts @@ -95,7 +95,7 @@ import { notifyTransition, parseTransactionIdentifier, } from "./transactions.js"; -import { InternalWalletState } from "./wallet.js"; +import { InternalWalletState, WalletExecutionContext } from "./wallet.js"; const logger = new Logger("pay-peer-pull-debit.ts"); @@ -103,13 +103,13 @@ const logger = new Logger("pay-peer-pull-debit.ts"); * Common context for a peer-pull-debit transaction. */ export class PeerPullDebitTransactionContext implements TransactionContext { - ws: InternalWalletState; + wex: WalletExecutionContext; readonly transactionId: TransactionIdStr; readonly taskId: TaskIdStr; peerPullDebitId: string; - constructor(ws: InternalWalletState, peerPullDebitId: string) { - this.ws = ws; + constructor(wex: WalletExecutionContext, peerPullDebitId: string) { + this.wex = wex; this.transactionId = constructTransactionIdentifier({ tag: TransactionType.PeerPullDebit, peerPullDebitId, @@ -123,7 +123,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { async deleteTransaction(): Promise<void> { const transactionId = this.transactionId; - const ws = this.ws; + const ws = this.wex; const peerPullDebitId = this.peerPullDebitId; await ws.db.runReadWriteTx(["peerPullDebit", "tombstones"], async (tx) => { const debit = await tx.peerPullDebit.get(peerPullDebitId); @@ -137,9 +137,9 @@ export class PeerPullDebitTransactionContext implements TransactionContext { async suspendTransaction(): Promise<void> { const taskId = this.taskId; const transactionId = this.transactionId; - const ws = this.ws; + const wex = this.wex; const peerPullDebitId = this.peerPullDebitId; - const transitionInfo = await ws.db.runReadWriteTx( + const transitionInfo = await wex.db.runReadWriteTx( ["peerPullDebit"], async (tx) => { const pullDebitRec = await tx.peerPullDebit.get(peerPullDebitId); @@ -183,8 +183,8 @@ export class PeerPullDebitTransactionContext implements TransactionContext { return undefined; }, ); - notifyTransition(ws, transactionId, transitionInfo); - ws.taskScheduler.stopShepherdTask(taskId); + notifyTransition(wex, transactionId, transitionInfo); + wex.taskScheduler.stopShepherdTask(taskId); } async resumeTransaction(): Promise<void> { @@ -206,7 +206,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { return TransitionResult.Stay; } }); - this.ws.taskScheduler.startShepherdTask(this.taskId); + this.wex.taskScheduler.startShepherdTask(this.taskId); } async failTransaction(): Promise<void> { @@ -224,7 +224,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { return TransitionResult.Stay; } }); - this.ws.taskScheduler.stopShepherdTask(this.taskId); + this.wex.taskScheduler.stopShepherdTask(this.taskId); } async abortTransaction(): Promise<void> { @@ -262,7 +262,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { } const refresh = await createRefreshGroup( - ctx.ws, + ctx.wex, tx, currency, coinPubs, @@ -300,7 +300,7 @@ export class PeerPullDebitTransactionContext implements TransactionContext { >, ) => Promise<TransitionResult>, ): Promise<void> { - const ws = this.ws; + const ws = this.wex; const extraStores = opts.extraStores ?? []; const transitionInfo = await ws.db.runReadWriteTx( ["peerPullDebit", ...extraStores], @@ -336,7 +336,7 @@ async function handlePurseCreationConflict( peerPullInc: PeerPullPaymentIncomingRecord, resp: HttpResponse, ): Promise<TaskRunResult> { - const ws = ctx.ws; + const ws = ctx.wex; const errResp = await readTalerErrorResponse(resp); if (errResp.code !== TalerErrorCode.EXCHANGE_GENERIC_INSUFFICIENT_FUNDS) { await ctx.failTransaction(); @@ -411,9 +411,8 @@ async function handlePurseCreationConflict( } async function processPeerPullDebitPendingDeposit( - ws: InternalWalletState, + wex: WalletExecutionContext, peerPullInc: PeerPullPaymentIncomingRecord, - cancellationToken: CancellationToken, ): Promise<TaskRunResult> { const pursePub = peerPullInc.pursePub; @@ -422,9 +421,9 @@ async function processPeerPullDebitPendingDeposit( throw Error("invalid state, no coins selected"); } - const coins = await queryCoinInfosForSelection(ws, coinSel); + const coins = await queryCoinInfosForSelection(wex, coinSel); - const depositSigsResp = await ws.cryptoApi.signPurseDeposits({ + const depositSigsResp = await wex.cryptoApi.signPurseDeposits({ exchangeBaseUrl: peerPullInc.exchangeBaseUrl, pursePub: peerPullInc.pursePub, coins, @@ -443,14 +442,14 @@ async function processPeerPullDebitPendingDeposit( logger.trace(`purse deposit payload: ${j2s(depositPayload)}`); } - const httpResp = await ws.http.fetch(purseDepositUrl.href, { + const httpResp = await wex.http.fetch(purseDepositUrl.href, { method: "POST", body: depositPayload, - cancellationToken, + cancellationToken: wex.cancellationToken, }); const ctx = new PeerPullDebitTransactionContext( - ws, + wex, peerPullInc.peerPullDebitId, ); @@ -489,9 +488,8 @@ async function processPeerPullDebitPendingDeposit( } async function processPeerPullDebitAbortingRefresh( - ws: InternalWalletState, + wex: WalletExecutionContext, peerPullInc: PeerPullPaymentIncomingRecord, - _cancellationToken: CancellationToken, ): Promise<TaskRunResult> { const peerPullDebitId = peerPullInc.peerPullDebitId; const abortRefreshGroupId = peerPullInc.abortRefreshGroupId; @@ -500,7 +498,7 @@ async function processPeerPullDebitAbortingRefresh( tag: TransactionType.PeerPullDebit, peerPullDebitId, }); - const transitionInfo = await ws.db.runReadWriteTx( + const transitionInfo = await wex.db.runReadWriteTx( ["peerPullDebit", "refreshGroups"], async (tx) => { const refreshGroup = await tx.refreshGroups.get(abortRefreshGroupId); @@ -533,17 +531,16 @@ async function processPeerPullDebitAbortingRefresh( return undefined; }, ); - notifyTransition(ws, transactionId, transitionInfo); + notifyTransition(wex, transactionId, transitionInfo); // FIXME: Shouldn't this be finished in some cases?! return TaskRunResult.backoff(); } export async function processPeerPullDebit( - ws: InternalWalletState, + wex: WalletExecutionContext, peerPullDebitId: string, - cancellationToken: CancellationToken, ): Promise<TaskRunResult> { - const peerPullInc = await ws.db.runReadOnlyTx( + const peerPullInc = await wex.db.runReadOnlyTx( ["peerPullDebit"], async (tx) => { return tx.peerPullDebit.get(peerPullDebitId); @@ -556,22 +553,20 @@ export async function processPeerPullDebit( switch (peerPullInc.status) { case PeerPullDebitRecordStatus.PendingDeposit: return await processPeerPullDebitPendingDeposit( - ws, + wex, peerPullInc, - cancellationToken, ); case PeerPullDebitRecordStatus.AbortingRefresh: return await processPeerPullDebitAbortingRefresh( - ws, + wex, peerPullInc, - cancellationToken, ); } return TaskRunResult.finished(); } export async function confirmPeerPullDebit( - ws: InternalWalletState, + wex: WalletExecutionContext, req: ConfirmPeerPullDebitRequest, ): Promise<AcceptPeerPullPaymentResponse> { let peerPullDebitId: string; @@ -588,7 +583,7 @@ export async function confirmPeerPullDebit( throw Error("invalid request, transactionId or peerPullDebitId required"); } - const peerPullInc = await ws.db.runReadOnlyTx( + const peerPullInc = await wex.db.runReadOnlyTx( ["peerPullDebit"], async (tx) => { return tx.peerPullDebit.get(peerPullDebitId); @@ -603,7 +598,7 @@ export async function confirmPeerPullDebit( const instructedAmount = Amounts.parseOrThrow(peerPullInc.amount); - const coinSelRes = await selectPeerCoins(ws, { instructedAmount }); + const coinSelRes = await selectPeerCoins(wex, { instructedAmount }); if (logger.shouldLogTrace()) { logger.trace(`selected p2p coins (pull): ${j2s(coinSelRes)}`); } @@ -620,11 +615,11 @@ export async function confirmPeerPullDebit( const sel = coinSelRes.result; const totalAmount = await getTotalPeerPaymentCost( - ws, + wex, coinSelRes.result.coins, ); - await ws.db.runReadWriteTx( + await wex.db.runReadWriteTx( [ "exchanges", "coins", @@ -634,7 +629,7 @@ export async function confirmPeerPullDebit( "coinAvailability", ], async (tx) => { - await spendCoins(ws, tx, { + await spendCoins(wex, tx, { // allocationId: `txn:peer-pull-debit:${req.peerPullDebitId}`, allocationId: constructTransactionIdentifier({ tag: TransactionType.PeerPullDebit, @@ -663,16 +658,16 @@ export async function confirmPeerPullDebit( }, ); - const ctx = new PeerPullDebitTransactionContext(ws, peerPullDebitId); + const ctx = new PeerPullDebitTransactionContext(wex, peerPullDebitId); const transactionId = ctx.transactionId; - ws.notify({ + wex.ws.notify({ type: NotificationType.BalanceChange, hintTransactionId: transactionId, }); - ws.taskScheduler.startShepherdTask(ctx.taskId); + wex.taskScheduler.startShepherdTask(ctx.taskId); return { transactionId, @@ -684,7 +679,7 @@ export async function confirmPeerPullDebit( * Store the results in the wallet DB. */ export async function preparePeerPullDebit( - ws: InternalWalletState, + wex: WalletExecutionContext, req: PreparePeerPullDebitRequest, ): Promise<PreparePeerPullDebitResponse> { const uri = parsePayPullUri(req.talerUri); @@ -693,7 +688,7 @@ export async function preparePeerPullDebit( throw Error("got invalid taler://pay-pull URI"); } - const existing = await ws.db.runReadOnlyTx( + const existing = await wex.db.runReadOnlyTx( ["peerPullDebit", "contractTerms"], async (tx) => { const peerPullDebitRecord = @@ -734,7 +729,7 @@ export async function preparePeerPullDebit( const getContractUrl = new URL(`contracts/${contractPub}`, exchangeBaseUrl); - const contractHttpResp = await ws.http.fetch(getContractUrl.href); + const contractHttpResp = await wex.http.fetch(getContractUrl.href); const contractResp = await readSuccessResponseJsonOrThrow( contractHttpResp, @@ -743,7 +738,7 @@ export async function preparePeerPullDebit( const pursePub = contractResp.purse_pub; - const dec = await ws.cryptoApi.decryptContractForDeposit({ + const dec = await wex.cryptoApi.decryptContractForDeposit({ ciphertext: contractResp.econtract, contractPriv: contractPriv, pursePub: pursePub, @@ -751,7 +746,7 @@ export async function preparePeerPullDebit( const getPurseUrl = new URL(`purses/${pursePub}/merge`, exchangeBaseUrl); - const purseHttpResp = await ws.http.fetch(getPurseUrl.href); + const purseHttpResp = await wex.http.fetch(getPurseUrl.href); const purseStatus = await readSuccessResponseJsonOrThrow( purseHttpResp, @@ -777,7 +772,7 @@ export async function preparePeerPullDebit( const instructedAmount = Amounts.parseOrThrow(contractTerms.amount); - const coinSelRes = await selectPeerCoins(ws, { instructedAmount }); + const coinSelRes = await selectPeerCoins(wex, { instructedAmount }); if (logger.shouldLogTrace()) { logger.trace(`selected p2p coins (pull): ${j2s(coinSelRes)}`); } @@ -792,11 +787,11 @@ export async function preparePeerPullDebit( } const totalAmount = await getTotalPeerPaymentCost( - ws, + wex, coinSelRes.result.coins, ); - await ws.db.runReadWriteTx(["peerPullDebit", "contractTerms"], async (tx) => { + await wex.db.runReadWriteTx(["peerPullDebit", "contractTerms"], async (tx) => { await tx.contractTerms.put({ h: contractTermsHash, contractTermsRaw: contractTerms, |