diff options
author | Florian Dold <florian@dold.me> | 2021-06-09 15:14:17 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-06-09 15:24:19 +0200 |
commit | 5c26461247040c07c86291babf0c87631df638b5 (patch) | |
tree | 8ff93454d0c21d2675d6734f210d4e8ff91d2bfb /packages/taler-wallet-core/src/operations/backup/state.ts | |
parent | 68dddc848f2f650d74697bb3a5c05d649e5db3c7 (diff) | |
download | wallet-core-5c26461247040c07c86291babf0c87631df638b5.tar.xz |
database access refactor
Diffstat (limited to 'packages/taler-wallet-core/src/operations/backup/state.ts')
-rw-r--r-- | packages/taler-wallet-core/src/operations/backup/state.ts | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/packages/taler-wallet-core/src/operations/backup/state.ts b/packages/taler-wallet-core/src/operations/backup/state.ts index e2a0f4cf3..226880439 100644 --- a/packages/taler-wallet-core/src/operations/backup/state.ts +++ b/packages/taler-wallet-core/src/operations/backup/state.ts @@ -15,9 +15,11 @@ */ import { Timestamp } from "@gnu-taler/taler-util"; -import { ConfigRecord, Stores } from "../../db.js"; -import { getRandomBytes, encodeCrock, TransactionHandle } from "../../index.js"; +import { ConfigRecord, WalletStoresV1 } from "../../db.js"; +import { getRandomBytes, encodeCrock } from "../../index.js"; import { checkDbInvariant } from "../../util/invariants"; +import { GetReadOnlyAccess } from "../../util/query.js"; +import { Wallet } from "../../wallet.js"; import { InternalWalletState } from "../state"; export interface WalletBackupConfState { @@ -48,10 +50,13 @@ export const WALLET_BACKUP_STATE_KEY = "walletBackupState"; export async function provideBackupState( ws: InternalWalletState, ): Promise<WalletBackupConfState> { - const bs: ConfigRecord<WalletBackupConfState> | undefined = await ws.db.get( - Stores.config, - WALLET_BACKUP_STATE_KEY, - ); + const bs: ConfigRecord<WalletBackupConfState> | undefined = await ws.db + .mktx((x) => ({ + config: x.config, + })) + .runReadOnly(async (tx) => { + return tx.config.get(WALLET_BACKUP_STATE_KEY); + }); if (bs) { return bs.value; } @@ -62,32 +67,36 @@ export async function provideBackupState( // FIXME: device ID should be configured when wallet is initialized // and be based on hostname const deviceId = `wallet-core-${encodeCrock(d)}`; - return await ws.db.runWithWriteTransaction([Stores.config], async (tx) => { - let backupStateEntry: - | ConfigRecord<WalletBackupConfState> - | undefined = await tx.get(Stores.config, WALLET_BACKUP_STATE_KEY); - if (!backupStateEntry) { - backupStateEntry = { - key: WALLET_BACKUP_STATE_KEY, - value: { - deviceId, - clocks: { [deviceId]: 1 }, - walletRootPub: k.pub, - walletRootPriv: k.priv, - lastBackupPlainHash: undefined, - }, - }; - await tx.put(Stores.config, backupStateEntry); - } - return backupStateEntry.value; - }); + return await ws.db + .mktx((x) => ({ + config: x.config, + })) + .runReadWrite(async (tx) => { + let backupStateEntry: + | ConfigRecord<WalletBackupConfState> + | undefined = await tx.config.get(WALLET_BACKUP_STATE_KEY); + if (!backupStateEntry) { + backupStateEntry = { + key: WALLET_BACKUP_STATE_KEY, + value: { + deviceId, + clocks: { [deviceId]: 1 }, + walletRootPub: k.pub, + walletRootPriv: k.priv, + lastBackupPlainHash: undefined, + }, + }; + await tx.config.put(backupStateEntry); + } + return backupStateEntry.value; + }); } export async function getWalletBackupState( ws: InternalWalletState, - tx: TransactionHandle<typeof Stores.config>, + tx: GetReadOnlyAccess<{ config: typeof WalletStoresV1.config }>, ): Promise<WalletBackupConfState> { - let bs = await tx.get(Stores.config, WALLET_BACKUP_STATE_KEY); + const bs = await tx.config.get(WALLET_BACKUP_STATE_KEY); checkDbInvariant(!!bs, "wallet backup state should be in DB"); return bs.value; } |