From ed9d4e4216be4fd69d7c0613cb6ee4605a6de3ce Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Thu, 13 Jun 2024 14:32:27 +0200 Subject: wallet-core: implement getDepositWireTypesForCurrency --- packages/taler-util/src/wallet-types.ts | 27 ++++++++++++++ packages/taler-wallet-core/src/wallet-api-types.ts | 15 +++++++- packages/taler-wallet-core/src/wallet.ts | 42 ++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) (limited to 'packages') diff --git a/packages/taler-util/src/wallet-types.ts b/packages/taler-util/src/wallet-types.ts index d23780145..8fa5b1e69 100644 --- a/packages/taler-util/src/wallet-types.ts +++ b/packages/taler-util/src/wallet-types.ts @@ -3372,3 +3372,30 @@ export const codecForHintNetworkAvailabilityRequest = buildCodecForObject() .property("isNetworkAvailable", codecForBoolean()) .build("HintNetworkAvailabilityRequest"); + +export interface GetDepositWireTypesForCurrencyRequest { + currency: string; + /** + * Optional scope info to further restrict the result. + * Currency must match the currency field. + */ + scopeInfo?: ScopeInfo; +} + +export const codecForGetDepositWireTypesForCurrencyRequest = + (): Codec => + buildCodecForObject() + .property("currency", codecForString()) + .property("scopeInfo", codecOptional(codecForScopeInfo())) + .build("GetDepositWireTypesForCurrencyRequest"); + +/** + * Response with wire types that are supported for a deposit. + * + * In the future, we might surface more information here, such as debit restrictions + * by the exchange, which then can be shown by UIs to the user before they + * enter their payment information. + */ +export interface GetDepositWireTypesForCurrencyResponse { + wireTypes: string[]; +} diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts b/packages/taler-wallet-core/src/wallet-api-types.ts index 398feddac..c54ec1360 100644 --- a/packages/taler-wallet-core/src/wallet-api-types.ts +++ b/packages/taler-wallet-core/src/wallet-api-types.ts @@ -71,6 +71,8 @@ import { GetContractTermsDetailsRequest, GetCurrencySpecificationRequest, GetCurrencySpecificationResponse, + GetDepositWireTypesForCurrencyRequest, + GetDepositWireTypesForCurrencyResponse, GetExchangeEntryByUrlRequest, GetExchangeEntryByUrlResponse, GetExchangeResourcesRequest, @@ -266,7 +268,7 @@ export enum WalletApiOperation { Shutdown = "shutdown", HintNetworkAvailability = "hintNetworkAvailability", CanonicalizeBaseUrl = "canonicalizeBaseUrl", - GetDepositWireTypesForCurency = "getDepositWireTypesForCurrency", + GetDepositWireTypesForCurrency = "getDepositWireTypesForCurrency", TestingWaitTransactionsFinal = "testingWaitTransactionsFinal", TestingWaitRefreshesFinal = "testingWaitRefreshesFinal", TestingWaitTransactionState = "testingWaitTransactionState", @@ -729,6 +731,16 @@ export type GetExchangeTosOp = { response: GetExchangeTosResult; }; +/** + * Get wire types that can be used for a deposit operation + * with the provided currency. + */ +export type GetDepositWireTypesForCurrencyOp = { + op: WalletApiOperation.GetDepositWireTypesForCurrency; + request: GetDepositWireTypesForCurrencyRequest; + response: GetDepositWireTypesForCurrencyResponse; +}; + /** * Get the current terms of a service of an exchange. */ @@ -1369,6 +1381,7 @@ export type WalletOperations = { [WalletApiOperation.TestingGetReserveHistory]: TestingGetReserveHistoryOp; [WalletApiOperation.TestingResetAllRetries]: TestingResetAllRetriesOp; [WalletApiOperation.HintNetworkAvailability]: HintNetworkAvailabilityOp; + [WalletApiOperation.GetDepositWireTypesForCurrency]: GetDepositWireTypesForCurrencyOp; }; export type WalletCoreRequestType< diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 7a69fcb21..2a84d912d 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -103,6 +103,7 @@ import { codecForGetBalanceDetailRequest, codecForGetContractTermsDetails, codecForGetCurrencyInfoRequest, + codecForGetDepositWireTypesForCurrencyRequest, codecForGetExchangeEntryByUrlRequest, codecForGetExchangeResourcesRequest, codecForGetExchangeTosRequest, @@ -215,6 +216,7 @@ import { getExchangeDetailedInfo, getExchangeResources, getExchangeTos, + getExchangeWireDetailsInTx, listExchanges, lookupExchangeByUri, } from "./exchanges.js"; @@ -1316,6 +1318,46 @@ async function dispatchRequestInternal( const dbDump = await exportDb(wex.ws.idb); return dbDump; } + case WalletApiOperation.GetDepositWireTypesForCurrency: { + const req = + codecForGetDepositWireTypesForCurrencyRequest().decode(payload); + const wtSet: Set = new Set(); + await wex.db.runReadOnlyTx( + { storeNames: ["exchanges", "exchangeDetails"] }, + async (tx) => { + const exchanges = await tx.exchanges.getAll(); + for (const exchange of exchanges) { + const det = await getExchangeWireDetailsInTx(tx, exchange.baseUrl); + if (!det) { + continue; + } + if (det.currency !== req.currency) { + continue; + } + for (const acc of det.wireInfo.accounts) { + let usable = true; + for (const dr of acc.debit_restrictions) { + if (dr.type === "deny") { + usable = false; + break; + } + } + if (!usable) { + break; + } + const parsedPayto = parsePaytoUri(acc.payto_uri); + if (!parsedPayto) { + continue; + } + wtSet.add(parsedPayto.targetType); + } + } + }, + ); + return { + wireTypes: [...wtSet], + }; + } case WalletApiOperation.ListGlobalCurrencyExchanges: { const resp: ListGlobalCurrencyExchangesResponse = { exchanges: [], -- cgit v1.2.3