taler-typescript-core

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

commit 0c5e5193ed91e915ccf551ba21340503ebc53fd5
parent 843aefc42b0b02c6e8858adc794447956f45875a
Author: Florian Dold <florian@dold.me>
Date:   Fri,  5 Jul 2024 15:04:32 +0200

wallet-core: cleanup

Diffstat:
Mpackages/taler-wallet-core/src/wallet.ts | 198+++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 97 insertions(+), 101 deletions(-)

diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts @@ -76,6 +76,7 @@ import { ListExchangesForScopedCurrencyRequest, ListGlobalCurrencyAuditorsResponse, ListGlobalCurrencyExchangesResponse, + ListKnownBankAccountsRequest, Logger, NotificationType, ObservabilityContext, @@ -214,7 +215,6 @@ import { markAttentionRequestAsRead, } from "./attention.js"; import { - RunBackupCycleRequest, addBackupProvider, codecForAddBackupProviderRequest, codecForRemoveBackupProvider, @@ -452,11 +452,12 @@ export async function getDenomInfo( * List bank accounts known to the wallet from * previous withdrawals. */ -async function listKnownBankAccounts( +async function handleListKnownBankAccounts( wex: WalletExecutionContext, - currency?: string, + req: ListKnownBankAccountsRequest, ): Promise<KnownBankAccounts> { const accounts: KnownBankAccountsInfo[] = []; + const currency = req.currency; await wex.db.runReadOnlyTx({ storeNames: ["bankAccounts"] }, async (tx) => { const knownAccounts = await tx.bankAccounts.iter().toArray(); for (const r of knownAccounts) { @@ -625,7 +626,12 @@ async function getClientFromWalletState( const client: WalletCoreApiClient = { async call(op, payload): Promise<any> { id = (id + 1) % (Number.MAX_SAFE_INTEGER - 100); - const res = await handleCoreApiRequest(ws, op, String(id), payload); + const res = await dispatchWalletCoreApiRequest( + ws, + op, + String(id), + payload, + ); switch (res.type) { case "error": throw TalerError.fromUncheckedDetail(res.error); @@ -1119,14 +1125,6 @@ async function handleStartRefundQuery( return {}; } -async function handleAddBackupProvider( - wex: WalletExecutionContext, - req: RunBackupCycleRequest, -): Promise<EmptyObject> { - await runBackupCycle(wex, req); - return {}; -} - async function handleHintNetworkAvailability( wex: WalletExecutionContext, req: HintNetworkAvailabilityRequest, @@ -1184,7 +1182,7 @@ async function handleGetDepositWireTypesForCurrency( async function handleListGlobalCurrencyExchanges( wex: WalletExecutionContext, - req: EmptyObject, + _req: EmptyObject, ): Promise<ListGlobalCurrencyExchangesResponse> { const resp: ListGlobalCurrencyExchangesResponse = { exchanges: [], @@ -1207,7 +1205,7 @@ async function handleListGlobalCurrencyExchanges( async function handleListGlobalCurrencyAuditors( wex: WalletExecutionContext, - req: EmptyObject, + _req: EmptyObject, ): Promise<ListGlobalCurrencyAuditorsResponse> { const resp: ListGlobalCurrencyAuditorsResponse = { auditors: [], @@ -1324,7 +1322,7 @@ async function handleAddGlobalCurrencyAuditor( async function handleShutdown( wex: WalletExecutionContext, - req: EmptyObject, + _req: EmptyObject, ): Promise<EmptyObject> { wex.ws.stop(); return {}; @@ -1340,7 +1338,7 @@ async function handleTestingSetTimetravel( } async function handleCanonicalizeBaseUrl( - wex: WalletExecutionContext, + _wex: WalletExecutionContext, req: CanonicalizeBaseUrlRequest, ): Promise<CanonicalizeBaseUrlResponse> { return { @@ -1358,7 +1356,7 @@ async function handleDeleteExchange( async function handleCreateStoredBackup( wex: WalletExecutionContext, - req: EmptyObject, + _req: EmptyObject, ): Promise<CreateStoredBackupResponse> { return await createStoredBackup(wex); } @@ -1376,6 +1374,84 @@ async function handleAcceptBankIntegratedWithdrawal( }); } +async function handleGetCurrencySpecification( + wex: WalletExecutionContext, + req: GetCurrencySpecificationRequest, +): Promise<GetCurrencySpecificationResponse> { + const spec = await wex.db.runReadOnlyTx( + { + storeNames: ["currencyInfo"], + }, + async (tx) => { + return WalletDbHelpers.getCurrencyInfo(tx, req.scope); + }, + ); + if (spec) { + return { + currencySpecification: spec.currencySpec, + }; + } + // Hard-coded mock for KUDOS and TESTKUDOS + if (req.scope.currency === "KUDOS") { + const kudosResp: GetCurrencySpecificationResponse = { + currencySpecification: { + name: "Kudos (Taler Demonstrator)", + num_fractional_input_digits: 2, + num_fractional_normal_digits: 2, + num_fractional_trailing_zero_digits: 2, + alt_unit_names: { + "0": "ク", + }, + }, + }; + return kudosResp; + } else if (req.scope.currency === "TESTKUDOS") { + const testkudosResp: GetCurrencySpecificationResponse = { + currencySpecification: { + name: "Test (Taler Unstable Demonstrator)", + num_fractional_input_digits: 0, + num_fractional_normal_digits: 0, + num_fractional_trailing_zero_digits: 0, + alt_unit_names: { + "0": "テ", + }, + }, + }; + return testkudosResp; + } + const defaultResp: GetCurrencySpecificationResponse = { + currencySpecification: { + name: req.scope.currency, + num_fractional_input_digits: 2, + num_fractional_normal_digits: 2, + num_fractional_trailing_zero_digits: 2, + alt_unit_names: { + "0": req.scope.currency, + }, + }, + }; + return defaultResp; +} + +async function handleGetVersion( + wex: WalletExecutionContext, +): Promise<WalletCoreVersion> { + const result: WalletCoreVersion = { + implementationSemver: walletCoreBuildInfo.implementationSemver, + implementationGitHash: walletCoreBuildInfo.implementationGitHash, + hash: undefined, + version: WALLET_CORE_API_PROTOCOL_VERSION, + exchange: WALLET_EXCHANGE_PROTOCOL_VERSION, + merchant: WALLET_MERCHANT_PROTOCOL_VERSION, + bankConversionApiRange: WALLET_BANK_CONVERSION_API_PROTOCOL_VERSION, + bankIntegrationApiRange: TalerBankIntegrationHttpClient.PROTOCOL_VERSION, + corebankApiRange: WALLET_COREBANK_API_PROTOCOL_VERSION, + bank: TalerBankIntegrationHttpClient.PROTOCOL_VERSION, + devMode: wex.ws.config.testing.devModeActive, + }; + return result; +} + interface HandlerWithValidator<Tag extends WalletApiOperation> { codec: Codec<WalletCoreRequestType<Tag>>; handler: ( @@ -1484,7 +1560,7 @@ const handlers: { [T in WalletApiOperation]: HandlerWithValidator<T> } = { }, [WalletApiOperation.ListKnownBankAccounts]: { codec: codecForListKnownBankAccounts(), - handler: (wex, req) => listKnownBankAccounts(wex, req.currency), + handler: handleListKnownBankAccounts, }, [WalletApiOperation.AddKnownBankAccounts]: { codec: codecForAddKnownBankAccounts(), @@ -1965,84 +2041,6 @@ async function dispatchRequestInternal( return await h.handler(wex, req); } -export async function handleGetCurrencySpecification( - wex: WalletExecutionContext, - req: GetCurrencySpecificationRequest, -): Promise<GetCurrencySpecificationResponse> { - const spec = await wex.db.runReadOnlyTx( - { - storeNames: ["currencyInfo"], - }, - async (tx) => { - return WalletDbHelpers.getCurrencyInfo(tx, req.scope); - }, - ); - if (spec) { - return { - currencySpecification: spec.currencySpec, - }; - } - // Hard-coded mock for KUDOS and TESTKUDOS - if (req.scope.currency === "KUDOS") { - const kudosResp: GetCurrencySpecificationResponse = { - currencySpecification: { - name: "Kudos (Taler Demonstrator)", - num_fractional_input_digits: 2, - num_fractional_normal_digits: 2, - num_fractional_trailing_zero_digits: 2, - alt_unit_names: { - "0": "ク", - }, - }, - }; - return kudosResp; - } else if (req.scope.currency === "TESTKUDOS") { - const testkudosResp: GetCurrencySpecificationResponse = { - currencySpecification: { - name: "Test (Taler Unstable Demonstrator)", - num_fractional_input_digits: 0, - num_fractional_normal_digits: 0, - num_fractional_trailing_zero_digits: 0, - alt_unit_names: { - "0": "テ", - }, - }, - }; - return testkudosResp; - } - const defaultResp: GetCurrencySpecificationResponse = { - currencySpecification: { - name: req.scope.currency, - num_fractional_input_digits: 2, - num_fractional_normal_digits: 2, - num_fractional_trailing_zero_digits: 2, - alt_unit_names: { - "0": req.scope.currency, - }, - }, - }; - return defaultResp; -} - -async function handleGetVersion( - wex: WalletExecutionContext, -): Promise<WalletCoreVersion> { - const result: WalletCoreVersion = { - implementationSemver: walletCoreBuildInfo.implementationSemver, - implementationGitHash: walletCoreBuildInfo.implementationGitHash, - hash: undefined, - version: WALLET_CORE_API_PROTOCOL_VERSION, - exchange: WALLET_EXCHANGE_PROTOCOL_VERSION, - merchant: WALLET_MERCHANT_PROTOCOL_VERSION, - bankConversionApiRange: WALLET_BANK_CONVERSION_API_PROTOCOL_VERSION, - bankIntegrationApiRange: TalerBankIntegrationHttpClient.PROTOCOL_VERSION, - corebankApiRange: WALLET_COREBANK_API_PROTOCOL_VERSION, - bank: TalerBankIntegrationHttpClient.PROTOCOL_VERSION, - devMode: wex.ws.config.testing.devModeActive, - }; - return result; -} - export function getObservedWalletExecutionContext( ws: InternalWalletState, cancellationToken: CancellationToken, @@ -2091,7 +2089,7 @@ export function getNormalWalletExecutionContext( /** * Handle a request to the wallet-core API. */ -async function handleCoreApiRequest( +async function dispatchWalletCoreApiRequest( ws: InternalWalletState, operation: string, id: string, @@ -2169,9 +2167,7 @@ async function handleCoreApiRequest( } } -export function applyRunConfigDefaults( - wcp?: PartialWalletRunConfig, -): WalletRunConfig { +function applyRunConfigDefaults(wcp?: PartialWalletRunConfig): WalletRunConfig { return { builtin: { exchanges: wcp?.builtin?.exchanges ?? [ @@ -2247,7 +2243,7 @@ export class Wallet { payload: unknown, ): Promise<CoreApiResponse> { await this.ws.ensureWalletDbOpen(); - return handleCoreApiRequest(this.ws, operation, id, payload); + return dispatchWalletCoreApiRequest(this.ws, operation, id, payload); } }