In transition ... experimenting

This commit is contained in:
Matthew Aaron Raymer
2022-12-15 16:36:23 +08:00
parent 2c57bbf4ee
commit 9232afb5af
8 changed files with 81 additions and 8 deletions

34
src/db/index.ts Normal file
View File

@@ -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<T extends any = DexieTables> = BaseDexie & T;
export type Dexie<T extends unknown = DexieTables> = 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);
}
});

15
src/db/tables/accounts.ts Normal file
View File

@@ -0,0 +1,15 @@
import { Table } from "dexie";
export type Account = {
id?: number;
publicKey: string;
identity: string;
};
export type AccountsTable = {
accounts: Table<Account>;
};
export const accountsSchema = {
accounts: "++id, publicKey, $identity",
};

View File

@@ -25,7 +25,6 @@ if (localStorage.getItem("secret") == null) {
localStorage.setItem("secret", secret);
}
console.log("secret", secret);
/**
* Preconfigured DexieWrapper
*/

View File

@@ -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..."}))