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 | |
parent | 1a7cd398fb752d059e05ce928236727d0754dad9 (diff) | |
download | wallet-core-13f397521c0bb2b1587fa287de5be49c74f69d12.tar.gz wallet-core-13f397521c0bb2b1587fa287de5be49c74f69d12.tar.bz2 wallet-core-13f397521c0bb2b1587fa287de5be49c74f69d12.zip |
wallet-core: implement prepareWithdrawExchange request
Diffstat (limited to 'packages/taler-wallet-core')
-rw-r--r-- | packages/taler-wallet-core/src/wallet-api-types.ts | 13 | ||||
-rw-r--r-- | packages/taler-wallet-core/src/wallet.ts | 35 |
2 files changed, 47 insertions, 1 deletions
diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts b/packages/taler-wallet-core/src/wallet-api-types.ts index f1a4d5acf..b83813874 100644 --- a/packages/taler-wallet-core/src/wallet-api-types.ts +++ b/packages/taler-wallet-core/src/wallet-api-types.ts @@ -95,6 +95,8 @@ import { PrepareRefundRequest, PrepareRewardRequest, PrepareTipResult as PrepareRewardResult, + PrepareWithdrawExchangeRequest, + PrepareWithdrawExchangeResponse, RecoverStoredBackupRequest, RecoveryLoadRequest, RetryTransactionRequest, @@ -231,6 +233,7 @@ export enum WalletApiOperation { UpdateExchangeEntry = "updateExchangeEntry", TestingWaitTasksProcessed = "testingWaitTasksProcessed", ListExchangesForScopedCurrency = "listExchangesForScopedCurrency", + PrepareWithdrawExchange = "prepareWithdrawExchange", } // group: Initialization @@ -574,6 +577,15 @@ export type ListExchangesForScopedCurrencyOp = { }; /** + * Prepare for withdrawing via a taler://withdraw-exchange URI. + */ +export type PrepareWithdrawExchangeOp = { + op: WalletApiOperation.PrepareWithdrawExchange; + request: PrepareWithdrawExchangeRequest; + response: PrepareWithdrawExchangeResponse; +}; + +/** * Add / force-update an exchange. */ export type AddExchangeOp = { @@ -1181,6 +1193,7 @@ export type WalletOperations = { [WalletApiOperation.DeleteStoredBackup]: DeleteStoredBackupOp; [WalletApiOperation.RecoverStoredBackup]: RecoverStoredBackupsOp; [WalletApiOperation.UpdateExchangeEntry]: UpdateExchangeEntryOp; + [WalletApiOperation.PrepareWithdrawExchange]: PrepareWithdrawExchangeOp; }; export type WalletCoreRequestType< 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); |