aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-wallet-core/src/wallet.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-wallet-core/src/wallet.ts')
-rw-r--r--packages/taler-wallet-core/src/wallet.ts50
1 files changed, 37 insertions, 13 deletions
diff --git a/packages/taler-wallet-core/src/wallet.ts b/packages/taler-wallet-core/src/wallet.ts
index c9ccda20d..9f754ed69 100644
--- a/packages/taler-wallet-core/src/wallet.ts
+++ b/packages/taler-wallet-core/src/wallet.ts
@@ -139,6 +139,7 @@ import {
clearDatabase,
exportDb,
importDb,
+ openTalerDatabase,
} from "./db.js";
import { DevExperimentHttpLib, applyDevExperiment } from "./dev-experiments.js";
import {
@@ -315,6 +316,7 @@ import {
getMaxPeerPushAmount,
convertWithdrawalAmount,
} from "./util/instructedAmountConversion.js";
+import { IDBFactory } from "@gnu-taler/idb-bridge";
const logger = new Logger("wallet.ts");
@@ -1539,7 +1541,7 @@ async function dispatchRequestInternal<Op extends WalletApiOperation>(
return {};
}
case WalletApiOperation.ExportDb: {
- const dbDump = await exportDb(ws.db.idbHandle());
+ const dbDump = await exportDb(ws.idb);
return dbDump;
}
case WalletApiOperation.ImportDb: {
@@ -1654,14 +1656,14 @@ export class Wallet {
private _client: WalletCoreApiClient | undefined;
private constructor(
- db: DbAccess<typeof WalletStoresV1>,
+ idb: IDBFactory,
http: HttpRequestLibrary,
timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
config?: WalletConfigParameter,
) {
this.ws = new InternalWalletStateImpl(
- db,
+ idb,
http,
timer,
cryptoWorkerFactory,
@@ -1677,13 +1679,13 @@ export class Wallet {
}
static async create(
- db: DbAccess<typeof WalletStoresV1>,
+ idb: IDBFactory,
http: HttpRequestLibrary,
timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
config?: WalletConfigParameter,
): Promise<Wallet> {
- const w = new Wallet(db, http, timer, cryptoWorkerFactory, config);
+ const w = new Wallet(idb, http, timer, cryptoWorkerFactory, config);
w._client = await getClientFromWalletState(w.ws);
return w;
}
@@ -1725,19 +1727,22 @@ export class Wallet {
this.ws.stop();
}
- runPending(): Promise<void> {
+ async runPending(): Promise<void> {
+ await this.ws.ensureWalletDbOpen();
return runPending(this.ws);
}
- runTaskLoop(opts?: RetryLoopOpts): Promise<TaskLoopResult> {
+ async runTaskLoop(opts?: RetryLoopOpts): Promise<TaskLoopResult> {
+ await this.ws.ensureWalletDbOpen();
return runTaskLoop(this.ws, opts);
}
- handleCoreApiRequest(
+ async handleCoreApiRequest(
operation: string,
id: string,
payload: unknown,
): Promise<CoreApiResponse> {
+ await this.ws.ensureWalletDbOpen();
return handleCoreApiRequest(this.ws, operation, id, payload);
}
}
@@ -1801,12 +1806,17 @@ class InternalWalletStateImpl implements InternalWalletState {
config: Readonly<WalletConfig>;
+ private _db: DbAccess<typeof WalletStoresV1> | undefined = undefined;
+
+ get db(): DbAccess<typeof WalletStoresV1> {
+ if (!this._db) {
+ throw Error("db not initialized");
+ }
+ return this._db;
+ }
+
constructor(
- // FIXME: Make this a getter and make
- // the actual value nullable.
- // Check if we are in a DB migration / garbage collection
- // and throw an error in that case.
- public db: DbAccess<typeof WalletStoresV1>,
+ public idb: IDBFactory,
public http: HttpRequestLibrary,
public timer: TimerAPI,
cryptoWorkerFactory: CryptoWorkerFactory,
@@ -1821,6 +1831,20 @@ class InternalWalletStateImpl implements InternalWalletState {
}
}
+ async ensureWalletDbOpen(): Promise<void> {
+ if (this._db) {
+ return;
+ }
+ const myVersionChange = (): Promise<void> => {
+ logger.error("version change requested, should not happen");
+ throw Error(
+ "BUG: wallet DB version change event can't happen with memory IDB",
+ );
+ };
+ const myDb = await openTalerDatabase(this.idb, myVersionChange);
+ this._db = myDb;
+ }
+
async getTransactionState(
ws: InternalWalletState,
tx: GetReadOnlyAccess<typeof WalletStoresV1>,