From 45b54db01e18b550f98e0e373533744e3037f40d Mon Sep 17 00:00:00 2001 From: Trent Larson Date: Sun, 19 Mar 2023 16:25:19 -0600 Subject: [PATCH 1/5] separate account from other data for backup/restore --- README.md | 4 +-- src/db/index.ts | 41 +++++++++++++------------ src/db/tables/{index.ts => accounts.ts} | 26 ---------------- src/db/tables/contacts.ts | 11 +++++++ src/db/tables/settings.ts | 13 ++++++++ src/router/index.ts | 6 ++-- src/test/index.ts | 6 ++-- src/views/AccountViewView.vue | 33 ++++++++++---------- src/views/ContactsView.vue | 10 +++--- src/views/ImportAccountView.vue | 8 ++--- src/views/NewEditAccountView.vue | 6 ++-- src/views/NewEditProjectView.vue | 14 ++++----- src/views/ProjectViewView.vue | 8 ++--- src/views/ProjectsView.vue | 8 ++--- 14 files changed, 99 insertions(+), 95 deletions(-) rename src/db/tables/{index.ts => accounts.ts} (50%) create mode 100644 src/db/tables/contacts.ts create mode 100644 src/db/tables/settings.ts diff --git a/README.md b/README.md index 9b43e43f..79bbd90b 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,9 @@ On the test server, User #0 has rights to register others, so you can start play - Register someone else under User #0 on the `/account` page: - * Edit the `src/views/AccountViewView.vue` file and uncomment the lines referring to "test". + * Edit the `src/views/AccountViewView.vue` file and uncomment the lines referring to "testServerRegisterUser". - * Use the [Vue Devtools browser extension](https://devtools.vuejs.org/) and type this into the console: `$vm.ctx.testRegisterUser()` + * Visit the `/account` page, open the [Vue Devtools browser extension](https://devtools.vuejs.org/), select a component, then type this into the console: `$vm.ctx.testRegisterUser()` diff --git a/src/db/index.ts b/src/db/index.ts index dc7e911b..c8c785cc 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -1,17 +1,19 @@ import BaseDexie, { Table } from "dexie"; import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon"; +import { Account, AccountsSchema } from "./tables/accounts"; +import { Contact, ContactsSchema } from "./tables/contacts"; import { - Account, - AccountsSchema, - Contact, - ContactsSchema, - MASTER_SETTINGS, + MASTER_SETTINGS_KEY, Settings, SettingsSchema, -} from "./tables"; +} from "./tables/settings"; -type AllTables = { +// a separate DB because the seed is super-sensitive data +type SensitiveTables = { accounts: Table; +}; + +type NonsensitiveTables = { contacts: Table; settings: Table; }; @@ -24,15 +26,14 @@ type AllTables = { * * https://9to5answer.com/how-to-bypass-warning-unexpected-any-specify-a-different-type-typescript-eslint-no-explicit-any */ -type DexieTables = AllTables; -export type Dexie = BaseDexie & T; -export const db = new BaseDexie("KickStart") as Dexie; -const AllSchemas = Object.assign( - {}, - AccountsSchema, - ContactsSchema, - SettingsSchema -); +export type SensitiveDexie = BaseDexie & T; +export const accountsDB = new BaseDexie("KickStartSensitive") as SensitiveDexie; +const SensitiveSchemas = Object.assign({}, AccountsSchema); + +export type NonsensitiveDexie = + BaseDexie & T; +export const db = new BaseDexie("KickStart") as NonsensitiveDexie; +const NonsensitiveSchemas = Object.assign({}, ContactsSchema, SettingsSchema); /** * Needed to enable a special webpack setting to allow *await* below: @@ -48,11 +49,13 @@ if (localStorage.getItem("secret") == null) { } //console.log("IndexedDB Encryption Secret:", secret); -encrypted(db, { secretKey: secret }); -db.version(1).stores(AllSchemas); +encrypted(accountsDB, { secretKey: secret }); +accountsDB.version(1).stores(SensitiveSchemas); + +db.version(1).stores(NonsensitiveSchemas); // initialize, a la https://dexie.org/docs/Tutorial/Design#the-populate-event db.on("populate", function () { // ensure there's an initial entry for settings - db.settings.add({ id: MASTER_SETTINGS }); + db.settings.add({ id: MASTER_SETTINGS_KEY }); }); diff --git a/src/db/tables/index.ts b/src/db/tables/accounts.ts similarity index 50% rename from src/db/tables/index.ts rename to src/db/tables/accounts.ts index 6b7b62fe..96024396 100644 --- a/src/db/tables/index.ts +++ b/src/db/tables/accounts.ts @@ -13,29 +13,3 @@ export const AccountsSchema = { accounts: "++id, dateCreated, derivationPath, $identity, $mnemonic, publicKeyHex", }; - -export interface Contact { - did: string; - name?: string; - publicKeyBase64?: string; - seesMe?: boolean; - registered?: boolean; -} - -export const ContactsSchema = { - contacts: "++did, name, publicKeyBase64, registered, seesMe", -}; - -// a singleton -export type Settings = { - id: number; - firstName?: string; - lastName?: string; - showContactGivesInline?: boolean; -}; - -export const SettingsSchema = { - settings: "id", -}; - -export const MASTER_SETTINGS = 1; diff --git a/src/db/tables/contacts.ts b/src/db/tables/contacts.ts new file mode 100644 index 00000000..00923f5e --- /dev/null +++ b/src/db/tables/contacts.ts @@ -0,0 +1,11 @@ +export interface Contact { + did: string; + name?: string; + publicKeyBase64?: string; + seesMe?: boolean; + registered?: boolean; +} + +export const ContactsSchema = { + contacts: "++did, name, publicKeyBase64, registered, seesMe", +}; diff --git a/src/db/tables/settings.ts b/src/db/tables/settings.ts new file mode 100644 index 00000000..1da602aa --- /dev/null +++ b/src/db/tables/settings.ts @@ -0,0 +1,13 @@ +// a singleton +export type Settings = { + id: number; + firstName?: string; + lastName?: string; + showContactGivesInline?: boolean; +}; + +export const SettingsSchema = { + settings: "id", +}; + +export const MASTER_SETTINGS_KEY = 1; diff --git a/src/router/index.ts b/src/router/index.ts index 376cfd7d..83305a3e 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -1,5 +1,5 @@ import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"; -import { db } from "@/db"; +import { accountsDB } from "@/db"; const routes: Array = [ { @@ -8,8 +8,8 @@ const routes: Array = [ component: () => import(/* webpackChunkName: "start" */ "../views/DiscoverView.vue"), beforeEnter: async (to, from, next) => { - await db.open(); - const num_accounts = await db.accounts.count(); + await accountsDB.open(); + const num_accounts = await accountsDB.accounts.count(); if (num_accounts > 0) { next(); } else { diff --git a/src/test/index.ts b/src/test/index.ts index 4f10fb7a..fe593da5 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -1,7 +1,7 @@ import axios from "axios"; import * as didJwt from "did-jwt"; import { AppString } from "@/constants/app"; -import { db } from "../db"; +import { accountsDB } from "../db"; import { SERVICE_ID } from "../libs/veramo/setup"; import { deriveAddress, newIdentifier } from "../libs/crypto"; @@ -13,8 +13,8 @@ export async function testServerRegisterUser() { const identity0 = newIdentifier(addr, publicHex, privateHex, deriPath); - await db.open(); - const accounts = await db.accounts.toArray(); + await accountsDB.open(); + const accounts = await accountsDB.accounts.toArray(); const thisIdentity = JSON.parse(accounts[0].identity); // Make a claim diff --git a/src/views/AccountViewView.vue b/src/views/AccountViewView.vue index 01bdf490..3a3a514d 100644 --- a/src/views/AccountViewView.vue +++ b/src/views/AccountViewView.vue @@ -200,17 +200,17 @@