From 9232afb5afa29fc4df5f30ddb5339dffc15f19e7 Mon Sep 17 00:00:00 2001 From: Matthew Aaron Raymer Date: Thu, 15 Dec 2022 16:36:23 +0800 Subject: [PATCH] In transition ... experimenting --- .eslintrc.js | 1 + package-lock.json | 10 ++++++++++ package.json | 1 + src/db/index.ts | 34 ++++++++++++++++++++++++++++++++++ src/db/tables/accounts.ts | 15 +++++++++++++++ src/services/DexieWrapper.ts | 1 - src/views/AccountViewView.vue | 23 ++++++++++++++++++----- vue.config.js | 4 ++-- 8 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/db/index.ts create mode 100644 src/db/tables/accounts.ts diff --git a/.eslintrc.js b/.eslintrc.js index d3404af6..32972303 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -15,5 +15,6 @@ module.exports = { rules: { "no-console": process.env.NODE_ENV === "production" ? "warn" : "off", "no-debugger": process.env.NODE_ENV === "production" ? "warn" : "off", + "@typescript-eslint/no-unnecessary-type-constraint": "off", }, }; diff --git a/package-lock.json b/package-lock.json index 16de7ec8..0fb200fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -41,6 +41,7 @@ "register-service-worker": "^1.7.2", "vue": "^3.2.45", "vue-class-component": "^8.0.0-0", + "vue-property-decorator": "^9.1.2", "vue-router": "^4.1.6", "web-did-resolver": "^2.0.21" }, @@ -25420,6 +25421,15 @@ "node": ">=8" } }, + "node_modules/vue-property-decorator": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/vue-property-decorator/-/vue-property-decorator-9.1.2.tgz", + "integrity": "sha512-xYA8MkZynPBGd/w5QFJ2d/NM0z/YeegMqYTphy7NJQXbZcuU6FC6AOdUAcy4SXP+YnkerC6AfH+ldg7PDk9ESQ==", + "peerDependencies": { + "vue": "*", + "vue-class-component": "*" + } + }, "node_modules/vue-router": { "version": "4.1.6", "resolved": "https://registry.npmmirror.com/vue-router/-/vue-router-4.1.6.tgz", diff --git a/package.json b/package.json index 78293b69..0df90932 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "register-service-worker": "^1.7.2", "vue": "^3.2.45", "vue-class-component": "^8.0.0-0", + "vue-property-decorator": "^9.1.2", "vue-router": "^4.1.6", "web-did-resolver": "^2.0.21" }, diff --git a/src/db/index.ts b/src/db/index.ts new file mode 100644 index 00000000..b740eb92 --- /dev/null +++ b/src/db/index.ts @@ -0,0 +1,34 @@ +import BaseDexie from "dexie"; +import { encrypted, Encryption } from "@pvermeer/dexie-encrypted-addon"; +import { accountsSchema, AccountsTable } from "./tables/accounts"; +type DexieTables = AccountsTable; + +/** + * In order to make the next line be acceptable, the program needs to have its linter suppress a rule: + * https://typescript-eslint.io/rules/no-unnecessary-type-constraint/ + * + * and change *any* to *unknown* + * + * https://9to5answer.com/how-to-bypass-warning-unexpected-any-specify-a-different-type-typescript-eslint-no-explicit-any + */ +//export type Dexie = BaseDexie & T; +export type Dexie = BaseDexie & T; + +export const db = new BaseDexie("kickStarter") as Dexie; +const schema = Object.assign({}, accountsSchema); + +// if db already made, skip creation +BaseDexie.exists("kickStarter").then(function (exists) { + if (exists == false) { + // create password and place password in localStorage + const secret = + localStorage.getItem("secret") || Encryption.createRandomEncryptionKey(); + + if (localStorage.getItem("secret") == null) { + localStorage.setItem("secret", secret); + } + + encrypted(db, { secretKey: secret }); + db.version(1).stores(schema); + } +}); diff --git a/src/db/tables/accounts.ts b/src/db/tables/accounts.ts new file mode 100644 index 00000000..c14ac956 --- /dev/null +++ b/src/db/tables/accounts.ts @@ -0,0 +1,15 @@ +import { Table } from "dexie"; + +export type Account = { + id?: number; + publicKey: string; + identity: string; +}; + +export type AccountsTable = { + accounts: Table; +}; + +export const accountsSchema = { + accounts: "++id, publicKey, $identity", +}; diff --git a/src/services/DexieWrapper.ts b/src/services/DexieWrapper.ts index cf577cc9..fa04a6ae 100644 --- a/src/services/DexieWrapper.ts +++ b/src/services/DexieWrapper.ts @@ -25,7 +25,6 @@ if (localStorage.getItem("secret") == null) { localStorage.setItem("secret", secret); } -console.log("secret", secret); /** * Preconfigured DexieWrapper */ diff --git a/src/views/AccountViewView.vue b/src/views/AccountViewView.vue index bdfa0a94..c99fdfe8 100644 --- a/src/views/AccountViewView.vue +++ b/src/views/AccountViewView.vue @@ -172,9 +172,7 @@ import { useAccountStore } from "../store/account"; import { createIdentifier, deriveAddress, newIdentifier } from "../libs/crypto"; import { IIdentifier } from "@veramo/core"; import * as R from "ramda"; -import useDBAccounts from "@/use/useDBAccounts"; - -const { addAccount } = useDBAccounts(); +import { db } from "../db"; @Options({ components: {}, @@ -222,8 +220,23 @@ export default class AccountViewView extends Vue { privateHex, UPORT_ROOT_DERIVATION_PATH ); - console.log(newId); - addAccount("me", "you", "identity"); + db.open().catch(function (err) { + console.error("Failed to open db: " + (err.stack || err)); + }); + db.accounts + .add({ + publicKey: newId.keys[0].publicKeyHex, + identity: JSON.stringify(newId), + }) + .then(function () { + db.accounts.each(function (account) { + console.log("Found close friend: " + account.publicKey); + }); + }) + .catch(function (e) { + // Something failed. It may be already in the open() call. + console.error(e.stack || e); + }); //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... created new ID..."})) accountStore.account = JSON.stringify(newId); //appStore.dispatch(appSlice.actions.addLog({log: false, msg: "... stored new ID..."})) diff --git a/vue.config.js b/vue.config.js index bebc5287..68babd01 100644 --- a/vue.config.js +++ b/vue.config.js @@ -2,6 +2,6 @@ const { defineConfig } = require("@vue/cli-service"); module.exports = defineConfig({ transpileDependencies: true, configureWebpack: { - devtool: 'source-map' - } + devtool: "source-map", + }, });