taler-typescript-core

Wallet core logic and WebUIs for various components
Log | Files | Refs | Submodules | README | LICENSE

commit c47cdeea31176532484c754a0897f02ffb64f28f
parent d740c391ab796e9d393e321f39ac91ae40e62f2f
Author: Florian Dold <florian@dold.me>
Date:   Tue, 14 Jan 2025 23:26:56 +0100

wallet-core: implement checkPeerPushDebitV2

Diffstat:
Mpackages/taler-util/src/types-taler-wallet.ts | 14+++++++++++++-
Mpackages/taler-wallet-core/src/pay-peer-push-debit.ts | 36++++++++++++++++++++++++++++++------
Mpackages/taler-wallet-core/src/wallet-api-types.ts | 13+++++++++++++
Mpackages/taler-wallet-core/src/wallet.ts | 8++++++--
4 files changed, 62 insertions(+), 9 deletions(-)

diff --git a/packages/taler-util/src/types-taler-wallet.ts b/packages/taler-util/src/types-taler-wallet.ts @@ -2938,7 +2938,19 @@ export const codecForCheckPeerPushDebitRequest = .property("clientCancellationId", codecOptional(codecForString())) .build("CheckPeerPushDebitRequest"); -export interface CheckPeerPushDebitResponse { +export type CheckPeerPushDebitResponse = + | CheckPeerPushDebitOkResponse + | CheckPeerPushDebitInsufficientBalanceResponse; + +export interface CheckPeerPushDebitInsufficientBalanceResponse { + type: "insufficient-balance"; + + insufficientBalanceDetails: PaymentInsufficientBalanceDetails; +} + +export interface CheckPeerPushDebitOkResponse { + type: "ok"; + amountRaw: AmountString; amountEffective: AmountString; diff --git a/packages/taler-wallet-core/src/pay-peer-push-debit.ts b/packages/taler-wallet-core/src/pay-peer-push-debit.ts @@ -16,6 +16,7 @@ import { Amounts, + CheckPeerPushDebitOkResponse, CheckPeerPushDebitRequest, CheckPeerPushDebitResponse, CoinRefreshRequest, @@ -416,9 +417,32 @@ export class PeerPushDebitTransactionContext implements TransactionContext { } } +/** + * @deprecated use V2 instead + */ export async function checkPeerPushDebit( wex: WalletExecutionContext, req: CheckPeerPushDebitRequest, +): Promise<CheckPeerPushDebitOkResponse> { + const res = await checkPeerPushDebitV2(wex, req); + switch (res.type) { + case "ok": + return res; + case "insufficient-balance": + throw TalerError.fromDetail( + TalerErrorCode.WALLET_PEER_PUSH_PAYMENT_INSUFFICIENT_BALANCE, + { + insufficientBalanceDetails: res.insufficientBalanceDetails, + }, + ); + default: + assertUnreachable(res); + } +} + +export async function checkPeerPushDebitV2( + wex: WalletExecutionContext, + req: CheckPeerPushDebitRequest, ): Promise<CheckPeerPushDebitResponse> { return runWithClientCancellation( wex, @@ -457,6 +481,7 @@ async function internalCheckPeerPushDebit( throw Error("no exchange found for payment"); } return { + type: "ok", amountEffective: req.amount, amountRaw: req.amount, exchangeBaseUrl, @@ -471,12 +496,10 @@ async function internalCheckPeerPushDebit( let coins: SelectedProspectiveCoin[] | undefined = undefined; switch (coinSelRes.type) { case "failure": - throw TalerError.fromDetail( - TalerErrorCode.WALLET_PEER_PUSH_PAYMENT_INSUFFICIENT_BALANCE, - { - insufficientBalanceDetails: coinSelRes.insufficientBalanceDetails, - }, - ); + return { + type: "insufficient-balance", + insufficientBalanceDetails: coinSelRes.insufficientBalanceDetails, + }; case "prospective": coins = coinSelRes.result.prospectiveCoins; break; @@ -490,6 +513,7 @@ async function internalCheckPeerPushDebit( const totalAmount = await getTotalPeerPaymentCost(wex, coins); logger.trace("computed total peer payment cost"); return { + type: "ok", exchangeBaseUrl: coinSelRes.result.exchangeBaseUrl, amountEffective: Amounts.stringify(totalAmount), amountRaw: req.amount, diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts b/packages/taler-wallet-core/src/wallet-api-types.ts @@ -49,6 +49,7 @@ import { CheckPayTemplateRequest, CheckPeerPullCreditRequest, CheckPeerPullCreditResponse, + CheckPeerPushDebitOkResponse, CheckPeerPushDebitRequest, CheckPeerPushDebitResponse, CoinDumpJson, @@ -251,6 +252,7 @@ export enum WalletApiOperation { ExportDbToFile = "exportDbToFile", PreparePeerPushCredit = "preparePeerPushCredit", CheckPeerPushDebit = "checkPeerPushDebit", + CheckPeerPushDebitV2 = "checkPeerPushDebitV2", InitiatePeerPushDebit = "initiatePeerPushDebit", ConfirmPeerPushCredit = "confirmPeerPushCredit", CheckPeerPullCredit = "checkPeerPullCredit", @@ -981,6 +983,16 @@ export type DeleteStoredBackupOp = { export type CheckPeerPushDebitOp = { op: WalletApiOperation.CheckPeerPushDebit; request: CheckPeerPushDebitRequest; + response: CheckPeerPushDebitOkResponse; +}; + +/** + * Check if initiating a peer push payment is possible + * based on the funds in the wallet. + */ +export type CheckPeerPushDebitV2Op = { + op: WalletApiOperation.CheckPeerPushDebitV2; + request: CheckPeerPushDebitRequest; response: CheckPeerPushDebitResponse; }; @@ -1408,6 +1420,7 @@ export type WalletOperations = { [WalletApiOperation.ExportDb]: ExportDbOp; [WalletApiOperation.ImportDb]: ImportDbOp; [WalletApiOperation.CheckPeerPushDebit]: CheckPeerPushDebitOp; + [WalletApiOperation.CheckPeerPushDebitV2]: CheckPeerPushDebitV2Op; [WalletApiOperation.InitiatePeerPushDebit]: InitiatePeerPushDebitOp; [WalletApiOperation.PreparePeerPushCredit]: PreparePeerPushCreditOp; [WalletApiOperation.ConfirmPeerPushCredit]: ConfirmPeerPushCreditOp; diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts @@ -327,6 +327,7 @@ import { } from "./pay-peer-push-credit.js"; import { checkPeerPushDebit, + checkPeerPushDebitV2, initiatePeerPushDebit, } from "./pay-peer-push-debit.js"; import { @@ -1278,8 +1279,7 @@ async function handleGetDepositWireTypes( if (!det) { continue; } - if (req.currency !== undefined - && det.currency !== req.currency) { + if (req.currency !== undefined && det.currency !== req.currency) { continue; } for (const acc of det.wireInfo.accounts) { @@ -2151,6 +2151,10 @@ const handlers: { [T in WalletApiOperation]: HandlerWithValidator<T> } = { codec: codecForCheckPeerPushDebitRequest(), handler: checkPeerPushDebit, }, + [WalletApiOperation.CheckPeerPushDebitV2]: { + codec: codecForCheckPeerPushDebitRequest(), + handler: checkPeerPushDebitV2, + }, [WalletApiOperation.InitiatePeerPushDebit]: { codec: codecForInitiatePeerPushDebitRequest(), handler: initiatePeerPushDebit,