From 8fed5b4b7370431602c0b25f8142009e61f7b906 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Wed, 30 Aug 2023 18:33:56 +0200 Subject: [PATCH] wallet-core: allow version change event --- packages/taler-wallet-cli/src/index.ts | 10 ++++++++ packages/taler-wallet-core/src/db.ts | 14 +++++------ packages/taler-wallet-core/src/util/query.ts | 12 +++++----- packages/taler-wallet-core/src/wallet.ts | 24 ++++++++++--------- .../src/wxBackend.ts | 1 - 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/packages/taler-wallet-cli/src/index.ts b/packages/taler-wallet-cli/src/index.ts index 9652f84f3..36e7f7768 100644 --- a/packages/taler-wallet-cli/src/index.ts +++ b/packages/taler-wallet-cli/src/index.ts @@ -883,6 +883,16 @@ backupCli.subcommand("exportDb", "export-db").action(async (args) => { }); }); +backupCli.subcommand("storeBackup", "store-backup").action(async (args) => { + await withWallet(args, async (wallet) => { + const resp = await wallet.client.call( + WalletApiOperation.CreateStoredBackup, + {}, + ); + console.log(JSON.stringify(resp, undefined, 2)); + }); +}); + backupCli.subcommand("importDb", "import-db").action(async (args) => { await withWallet(args, async (wallet) => { const dumpRaw = await read(process.stdin); diff --git a/packages/taler-wallet-core/src/db.ts b/packages/taler-wallet-core/src/db.ts index 1255e8c71..a642c0203 100644 --- a/packages/taler-wallet-core/src/db.ts +++ b/packages/taler-wallet-core/src/db.ts @@ -114,6 +114,11 @@ export const TALER_WALLET_MAIN_DB_NAME = "taler-wallet-main-v9"; */ export const TALER_WALLET_META_DB_NAME = "taler-wallet-meta"; +/** + * Stored backups, mainly created when manually importing a backup. + */ +export const TALER_WALLET_STORED_BACKUPS_DB_NAME = "taler-wallet-stored-backups"; + export const CURRENT_DB_CONFIG_KEY = "currentMainDbName"; /** @@ -2773,15 +2778,10 @@ export interface StoredBackupMeta { name: string; } -export interface StoredBackupData { - name: string; - data: any; -} - export const StoredBackupStores = { backupMeta: describeStore( "backupMeta", - describeContents({ keyPath: "name" }), + describeContents({ keyPath: "name" }), {}, ), backupData: describeStore("backupData", describeContents({}), {}), @@ -3250,7 +3250,7 @@ export async function openStoredBackupsDatabase( ): Promise> { const backupsDbHandle = await openDatabase( idbFactory, - TALER_WALLET_META_DB_NAME, + TALER_WALLET_STORED_BACKUPS_DB_NAME, 1, () => {}, onStoredBackupsDbUpgradeNeeded, diff --git a/packages/taler-wallet-core/src/util/query.ts b/packages/taler-wallet-core/src/util/query.ts index eb2bddec1..7697ed3db 100644 --- a/packages/taler-wallet-core/src/util/query.ts +++ b/packages/taler-wallet-core/src/util/query.ts @@ -376,8 +376,8 @@ export interface InsertResponse { export interface StoreReadWriteAccessor { get(key: IDBValidKey): Promise; iter(query?: IDBValidKey): ResultStream; - put(r: RecordType): Promise; - add(r: RecordType): Promise; + put(r: RecordType, key?: IDBValidKey): Promise; + add(r: RecordType, key?: IDBValidKey): Promise; delete(key: IDBValidKey): Promise; indexes: GetIndexReadWriteAccess; } @@ -652,15 +652,15 @@ function makeWriteContext( const req = tx.objectStore(storeName).openCursor(query); return new ResultStream(req); }, - async add(r) { - const req = tx.objectStore(storeName).add(r); + async add(r, k) { + const req = tx.objectStore(storeName).add(r, k); const key = await requestToPromise(req); return { key: key, }; }, - async put(r) { - const req = tx.objectStore(storeName).put(r); + async put(r, k) { + const req = tx.objectStore(storeName).put(r, k); const key = await requestToPromise(req); return { key: key, diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts index 283539a08..626409dd6 100644 --- a/packages/taler-wallet-core/src/wallet.ts +++ b/packages/taler-wallet-core/src/wallet.ts @@ -343,9 +343,8 @@ async function callOperationHandler( return await processRecoupGroup(ws, pending.recoupGroupId); case PendingTaskType.ExchangeCheckRefresh: return await autoRefresh(ws, pending.exchangeBaseUrl); - case PendingTaskType.Deposit: { + case PendingTaskType.Deposit: return await processDepositGroup(ws, pending.depositGroupId); - } case PendingTaskType.Backup: return await processBackupForProvider(ws, pending.backupProviderBaseUrl); case PendingTaskType.PeerPushDebit: @@ -1031,9 +1030,15 @@ async function createStoredBackup( const backup = await exportDb(ws.idb); const backupsDb = await openStoredBackupsDatabase(ws.idb); const name = `backup-${new Date().getTime()}`; - backupsDb.mktxAll().runReadWrite(async (tx) => {}); - - throw Error("not implemented"); + await backupsDb.mktxAll().runReadWrite(async (tx) => { + await tx.backupMeta.add({ + name, + }); + await tx.backupData.add(backup, name); + }); + return { + name, + }; } /** @@ -1634,7 +1639,7 @@ export function getVersion(ws: InternalWalletState): WalletCoreVersion { /** * Handle a request to the wallet-core API. */ -export async function handleCoreApiRequest( +async function handleCoreApiRequest( ws: InternalWalletState, operation: string, id: string, @@ -1849,11 +1854,8 @@ class InternalWalletStateImpl implements InternalWalletState { if (this._db) { return; } - const myVersionChange = (): Promise => { - logger.error("version change requested, should not happen"); - throw Error( - "BUG: wallet DB version change event can't happen with memory IDB", - ); + const myVersionChange = async (): Promise => { + logger.info("version change requested for Taler DB"); }; const myDb = await openTalerDatabase(this.idb, myVersionChange); this._db = myDb; diff --git a/packages/taler-wallet-webextension/src/wxBackend.ts b/packages/taler-wallet-webextension/src/wxBackend.ts index b7484164d..e7385abe5 100644 --- a/packages/taler-wallet-webextension/src/wxBackend.ts +++ b/packages/taler-wallet-webextension/src/wxBackend.ts @@ -50,7 +50,6 @@ import { exportDb, importDb, openPromise, - openTalerDatabase, } from "@gnu-taler/taler-wallet-core"; import { MessageFromBackend,