From 88851f45403c1995c973bcae7ad2976db3c430c7 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Jan 2024 13:49:11 +0100 Subject: wallet-core: implement DD45 global currency management requests --- packages/taler-wallet-core/src/wallet.ts | 118 ++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 9 deletions(-) (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 3294e2a09..87c5aa995 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -39,6 +39,8 @@ import { InitResponse, KnownBankAccounts, KnownBankAccountsInfo, + ListGlobalCurrencyAuditorsResponse, + ListGlobalCurrencyExchangesResponse, Logger, NotificationType, PrepareWithdrawExchangeRequest, @@ -64,6 +66,8 @@ import { codecForAcceptPeerPullPaymentRequest, codecForAcceptTipRequest, codecForAddExchangeRequest, + codecForAddGlobalCurrencyAuditorRequest, + codecForAddGlobalCurrencyExchangeRequest, codecForAddKnownBankAccounts, codecForAny, codecForApplyDevExperiment, @@ -104,6 +108,8 @@ import { codecForPrepareRewardRequest, codecForPrepareWithdrawExchangeRequest, codecForRecoverStoredBackupRequest, + codecForRemoveGlobalCurrencyAuditorRequest, + codecForRemoveGlobalCurrencyExchangeRequest, codecForResumeTransaction, codecForRetryTransactionRequest, codecForSetCoinSuspendedRequest, @@ -200,7 +206,6 @@ import { lookupExchangeByUri, updateExchangeFromUrlHandler, } from "./operations/exchanges.js"; -import { getMerchantInfo } from "./operations/merchants.js"; import { computePayMerchantTransactionState, computeRefundTransactionState, @@ -302,7 +307,7 @@ import { GetReadOnlyAccess, GetReadWriteAccess, } from "./util/query.js"; -import { TimerAPI, TimerGroup, timer } from "./util/timer.js"; +import { TimerAPI, TimerGroup } from "./util/timer.js"; import { WALLET_BANK_CONVERSION_API_PROTOCOL_VERSION, WALLET_BANK_INTEGRATION_PROTOCOL_VERSION, @@ -1328,13 +1333,6 @@ async function dispatchRequestInternal( await setWalletDeviceId(ws, req.walletDeviceId); return {}; } - case WalletApiOperation.ListCurrencies: { - // FIXME: Remove / change to scoped currency approach. - return { - trustedAuditors: [], - trustedExchanges: [], - }; - } case WalletApiOperation.TestCrypto: { return await ws.cryptoApi.hashString({ str: "hello world" }); } @@ -1349,6 +1347,108 @@ async function dispatchRequestInternal( const dbDump = await exportDb(ws.idb); return dbDump; } + case WalletApiOperation.ListGlobalCurrencyExchanges: { + const resp: ListGlobalCurrencyExchangesResponse = { + exchanges: [], + }; + await ws.db.runReadOnlyTx(["globalCurrencyExchanges"], async (tx) => { + const gceList = await tx.globalCurrencyExchanges.iter().toArray(); + for (const gce of gceList) { + resp.exchanges.push({ + currency: gce.currency, + exchangeBaseUrl: gce.exchangeBaseUrl, + exchangeMasterPub: gce.exchangeMasterPub, + }); + } + }); + return resp; + } + case WalletApiOperation.ListGlobalCurrencyAuditors: { + const resp: ListGlobalCurrencyAuditorsResponse = { + auditors: [], + }; + await ws.db.runReadOnlyTx(["globalCurrencyAuditors"], async (tx) => { + const gcaList = await tx.globalCurrencyAuditors.iter().toArray(); + for (const gca of gcaList) { + resp.auditors.push({ + currency: gca.currency, + auditorBaseUrl: gca.auditorBaseUrl, + auditorPub: gca.auditorPub, + }); + } + }); + return resp; + } + case WalletApiOperation.AddGlobalCurrencyExchange: { + const req = codecForAddGlobalCurrencyExchangeRequest().decode(payload); + await ws.db.runReadWriteTx(["globalCurrencyExchanges"], async (tx) => { + const key = [req.currency, req.exchangeBaseUrl, req.exchangeMasterPub]; + const existingRec = + await tx.globalCurrencyExchanges.indexes.byCurrencyAndUrlAndPub.get( + key, + ); + if (existingRec) { + return; + } + await tx.globalCurrencyExchanges.add({ + currency: req.currency, + exchangeBaseUrl: req.exchangeBaseUrl, + exchangeMasterPub: req.exchangeMasterPub, + }); + }); + return {}; + } + case WalletApiOperation.RemoveGlobalCurrencyExchange: { + const req = codecForRemoveGlobalCurrencyExchangeRequest().decode(payload); + await ws.db.runReadWriteTx(["globalCurrencyExchanges"], async (tx) => { + const key = [req.currency, req.exchangeBaseUrl, req.exchangeMasterPub]; + const existingRec = + await tx.globalCurrencyExchanges.indexes.byCurrencyAndUrlAndPub.get( + key, + ); + if (!existingRec) { + return; + } + checkDbInvariant(!!existingRec.id); + await tx.globalCurrencyExchanges.delete(existingRec.id); + }); + return {}; + } + case WalletApiOperation.AddGlobalCurrencyAuditor: { + const req = codecForAddGlobalCurrencyAuditorRequest().decode(payload); + await ws.db.runReadWriteTx(["globalCurrencyAuditors"], async (tx) => { + const key = [req.currency, req.auditorBaseUrl, req.auditorPub]; + const existingRec = + await tx.globalCurrencyAuditors.indexes.byCurrencyAndUrlAndPub.get( + key, + ); + if (existingRec) { + return; + } + await tx.globalCurrencyAuditors.add({ + currency: req.currency, + auditorBaseUrl: req.auditorBaseUrl, + auditorPub: req.auditorPub, + }); + }); + return {}; + } + case WalletApiOperation.RemoveGlobalCurrencyAuditor: { + const req = codecForRemoveGlobalCurrencyAuditorRequest().decode(payload); + await ws.db.runReadWriteTx(["globalCurrencyAuditors"], async (tx) => { + const key = [req.currency, req.auditorBaseUrl, req.auditorPub]; + const existingRec = + await tx.globalCurrencyAuditors.indexes.byCurrencyAndUrlAndPub.get( + key, + ); + if (!existingRec) { + return; + } + checkDbInvariant(!!existingRec.id); + await tx.globalCurrencyAuditors.delete(existingRec.id); + }); + return {}; + } case WalletApiOperation.ImportDb: { const req = codecForImportDbRequest().decode(payload); await importDb(ws.db.idbHandle(), req.dump); -- cgit v1.2.3