From 13f397521c0bb2b1587fa287de5be49c74f69d12 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 7 Dec 2023 20:08:51 +0100 Subject: wallet-core: implement prepareWithdrawExchange request --- packages/taler-wallet-core/src/wallet.ts | 35 +++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'packages/taler-wallet-core/src/wallet.ts') diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 6092830f6..25cfd7f6f 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -51,12 +51,15 @@ import { ManualWithdrawalDetails, MerchantUsingTemplateDetails, NotificationType, + PrepareWithdrawExchangeRequest, + PrepareWithdrawExchangeResponse, RecoverStoredBackupRequest, RefreshReason, ScopeType, StoredBackupList, TalerError, TalerErrorCode, + TalerUriAction, TaskThrottler, TestingWaitTransactionRequest, TransactionState, @@ -109,6 +112,7 @@ import { codecForPreparePeerPushCreditRequest, codecForPrepareRefundRequest, codecForPrepareRewardRequest, + codecForPrepareWithdrawExchangeRequest, codecForRecoverStoredBackupRequest, codecForResumeTransaction, codecForRetryTransactionRequest, @@ -133,6 +137,7 @@ import { j2s, parsePayTemplateUri, parsePaytoUri, + parseTalerUri, sampleWalletCoreTransactions, setDangerousTimetravel, validateIban, @@ -1045,6 +1050,31 @@ async function recoverStoredBackup( logger.info(`import done`); } +async function handlePrepareWithdrawExchange( + ws: InternalWalletState, + req: PrepareWithdrawExchangeRequest, +): Promise { + const parsedUri = parseTalerUri(req.talerUri); + if (parsedUri?.type !== TalerUriAction.WithdrawExchange) { + throw Error("expected a taler://withdraw-exchange URI"); + } + const exchangeBaseUrl = parsedUri.exchangeBaseUrl; + const exchange = await updateExchangeFromUrl(ws, exchangeBaseUrl); + if (exchange.exchangeDetails.masterPublicKey != parsedUri.exchangePub) { + throw Error("mismatch of exchange master public key (URI vs actual)"); + } + if (parsedUri.amount) { + const amt = Amounts.parseOrThrow(parsedUri.amount); + if (amt.currency !== exchange.exchangeDetails.currency) { + throw Error("mismatch of currency (URI vs exchange)"); + } + } + return { + exchangeBaseUrl, + amount: parsedUri.amount, + }; +} + /** * Implementation of the "wallet-core" API. */ @@ -1313,7 +1343,10 @@ async function dispatchRequestInternal( const req = codecForSharePaymentRequest().decode(payload); return await sharePayment(ws, req.merchantBaseUrl, req.orderId); } - + case WalletApiOperation.PrepareWithdrawExchange: { + const req = codecForPrepareWithdrawExchangeRequest().decode(payload); + return handlePrepareWithdrawExchange(ws, req); + } case WalletApiOperation.PreparePayForUri: { const req = codecForPreparePayRequest().decode(payload); return await preparePayForUri(ws, req.talerPayUri); -- cgit v1.2.3