diff options
author | Florian Dold <florian@dold.me> | 2023-12-07 20:08:51 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-12-07 20:08:51 +0100 |
commit | 13f397521c0bb2b1587fa287de5be49c74f69d12 (patch) | |
tree | e279b8fca4003fe7c401f432ddcc37adbd4d7577 /packages/taler-wallet-core/src/wallet.ts | |
parent | 1a7cd398fb752d059e05ce928236727d0754dad9 (diff) | |
download | wallet-core-13f397521c0bb2b1587fa287de5be49c74f69d12.tar.xz |
wallet-core: implement prepareWithdrawExchange request
Diffstat (limited to 'packages/taler-wallet-core/src/wallet.ts')
-rw-r--r-- | packages/taler-wallet-core/src/wallet.ts | 35 |
1 files changed, 34 insertions, 1 deletions
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<PrepareWithdrawExchangeResponse> { + 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<Op extends WalletApiOperation>( 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); |