forked from trent_larson/crowd-funder-for-time-pwa
convert all remaining DB writes & reads to SQL (with successful registration & claim)
This commit is contained in:
@@ -80,8 +80,12 @@ import {
|
||||
} from "../libs/crypto";
|
||||
import { accountsDBPromise, db } from "../db/index";
|
||||
import { MASTER_SETTINGS_KEY } from "../db/tables/settings";
|
||||
import { retrieveAllFullyDecryptedAccounts } from "../libs/util";
|
||||
import * as databaseUtil from "../db/databaseUtil";
|
||||
import { retrieveAllAccountsMetadata } from "../libs/util";
|
||||
import { logger } from "../utils/logger";
|
||||
import { AccountEncrypted } from "@/db/tables/accounts";
|
||||
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory";
|
||||
import { USE_DEXIE_DB } from "@/constants/app";
|
||||
@Component({
|
||||
components: {},
|
||||
})
|
||||
@@ -94,11 +98,15 @@ export default class ImportAccountView extends Vue {
|
||||
selectedArrayFirstDid = "";
|
||||
|
||||
async mounted() {
|
||||
const accounts = await retrieveAllFullyDecryptedAccounts(); // let's match derived accounts differently so we don't need the private info
|
||||
const accounts: AccountEncrypted[] = await retrieveAllAccountsMetadata();
|
||||
const seedDids: Record<string, Array<string>> = {};
|
||||
accounts.forEach((account) => {
|
||||
const prevDids: Array<string> = seedDids[account.mnemonic] || [];
|
||||
seedDids[account.mnemonic] = prevDids.concat([account.did]);
|
||||
const mnemonicMaybeEncrypted =
|
||||
account.mnemonic || account.mnemonicEncrBase64;
|
||||
if (mnemonicMaybeEncrypted) {
|
||||
const prevDids: Array<string> = seedDids[mnemonicMaybeEncrypted] || [];
|
||||
seedDids[mnemonicMaybeEncrypted] = prevDids.concat([account.did]);
|
||||
}
|
||||
});
|
||||
this.didArrays = Object.values(seedDids);
|
||||
this.selectedArrayFirstDid = this.didArrays[0][0];
|
||||
@@ -117,14 +125,29 @@ export default class ImportAccountView extends Vue {
|
||||
const selectedArray: Array<string> =
|
||||
this.didArrays.find((dids) => dids[0] === this.selectedArrayFirstDid) ||
|
||||
[];
|
||||
const accountsDB = await accountsDBPromise; // let's match derived accounts differently so we don't need the private info
|
||||
const allMatchingAccounts = await accountsDB.accounts
|
||||
.where("did")
|
||||
.anyOf(...selectedArray)
|
||||
.toArray();
|
||||
const platformService = PlatformServiceFactory.getInstance();
|
||||
const qmarks = selectedArray.map(() => "?").join(",");
|
||||
const queryResult = await platformService.dbQuery(
|
||||
`SELECT * FROM accounts WHERE did IN (${qmarks})`,
|
||||
selectedArray,
|
||||
);
|
||||
let allMatchingAccounts = databaseUtil.mapQueryResultToValues(
|
||||
queryResult,
|
||||
) as unknown as AccountEncrypted[];
|
||||
if (USE_DEXIE_DB) {
|
||||
const accountsDB = await accountsDBPromise; // let's match derived accounts differently so we don't need the private info
|
||||
allMatchingAccounts = (await accountsDB.accounts
|
||||
.where("did")
|
||||
.anyOf(...selectedArray)
|
||||
.toArray()) as AccountEncrypted[];
|
||||
}
|
||||
const accountWithMaxDeriv = allMatchingAccounts[0];
|
||||
allMatchingAccounts.slice(1).forEach((account) => {
|
||||
if (account.derivationPath > accountWithMaxDeriv.derivationPath) {
|
||||
if (
|
||||
account.derivationPath &&
|
||||
accountWithMaxDeriv.derivationPath &&
|
||||
account.derivationPath > accountWithMaxDeriv.derivationPath
|
||||
) {
|
||||
accountWithMaxDeriv.derivationPath = account.derivationPath;
|
||||
}
|
||||
});
|
||||
@@ -140,20 +163,40 @@ export default class ImportAccountView extends Vue {
|
||||
const newId = newIdentifier(address, publicHex, privateHex, newDerivPath);
|
||||
|
||||
try {
|
||||
await accountsDB.accounts.add({
|
||||
dateCreated: new Date().toISOString(),
|
||||
derivationPath: newDerivPath,
|
||||
did: newId.did,
|
||||
identity: JSON.stringify(newId),
|
||||
mnemonic: mne,
|
||||
publicKeyHex: newId.keys[0].publicKeyHex,
|
||||
});
|
||||
const { sql, params } = databaseUtil.generateInsertStatement(
|
||||
{
|
||||
dateCreated: new Date().toISOString(),
|
||||
derivationPath: newDerivPath,
|
||||
did: newId.did,
|
||||
identity: JSON.stringify(newId),
|
||||
mnemonic: mne,
|
||||
publicKeyHex: newId.keys[0].publicKeyHex,
|
||||
},
|
||||
"accounts",
|
||||
);
|
||||
const platformService = PlatformServiceFactory.getInstance();
|
||||
await platformService.dbExec(sql, params);
|
||||
if (USE_DEXIE_DB) {
|
||||
const accountsDB = await accountsDBPromise;
|
||||
await accountsDB.accounts.add({
|
||||
dateCreated: new Date().toISOString(),
|
||||
derivationPath: newDerivPath,
|
||||
did: newId.did,
|
||||
identity: JSON.stringify(newId),
|
||||
mnemonic: mne,
|
||||
publicKeyHex: newId.keys[0].publicKeyHex,
|
||||
});
|
||||
}
|
||||
|
||||
// record that as the active DID
|
||||
await db.open();
|
||||
await db.settings.update(MASTER_SETTINGS_KEY, {
|
||||
activeDid: newId.did,
|
||||
});
|
||||
await platformService.dbExec("UPDATE settings SET activeDid = ?", [
|
||||
newId.did,
|
||||
]);
|
||||
if (USE_DEXIE_DB) {
|
||||
await db.settings.update(MASTER_SETTINGS_KEY, {
|
||||
activeDid: newId.did,
|
||||
});
|
||||
}
|
||||
this.$router.push({ name: "account" });
|
||||
} catch (err) {
|
||||
logger.error("Error saving mnemonic & updating settings:", err);
|
||||
|
||||
Reference in New Issue
Block a user