Browse Source

fix(types): improve account type safety and metadata handling

- Change retrieveAllAccountsMetadata to return Account[] instead of AccountEncrypted[]
  to better reflect its purpose of returning non-sensitive metadata
- Update ImportDerivedAccountView to use Account type and group by derivation path
- Update retrieveAllFullyDecryptedAccounts to use AccountEncrypted type for encrypted data
- Fix import path for Account type in ImportDerivedAccountView

This change improves type safety by making it explicit which functions handle
encrypted data vs metadata, and ensures consistent handling of account data
across the application. The metadata functions now correctly strip sensitive
fields while functions that need encrypted data maintain access to those fields.
pull/137/head
Matthew Raymer 2 weeks ago
parent
commit
bc274bdf7f
  1. 14
      src/libs/util.ts
  2. 20
      src/views/ImportDerivedAccountView.vue

14
src/libs/util.ts

@ -543,16 +543,14 @@ export const retrieveAccountMetadata = async (
return result; return result;
}; };
export const retrieveAllAccountsMetadata = async (): Promise< export const retrieveAllAccountsMetadata = async (): Promise<Account[]> => {
AccountEncrypted[]
> => {
const platformService = PlatformServiceFactory.getInstance(); const platformService = PlatformServiceFactory.getInstance();
const dbAccounts = await platformService.dbQuery(`SELECT * FROM accounts`); const dbAccounts = await platformService.dbQuery(`SELECT * FROM accounts`);
const accounts = databaseUtil.mapQueryResultToValues(dbAccounts) as Account[]; const accounts = databaseUtil.mapQueryResultToValues(dbAccounts) as Account[];
let result = accounts.map((account) => { let result = accounts.map((account) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { identity, mnemonic, ...metadata } = account; const { identity, mnemonic, ...metadata } = account;
return metadata; return metadata as Account;
}); });
if (USE_DEXIE_DB) { if (USE_DEXIE_DB) {
// one of the few times we use accountsDBPromise directly; try to avoid more usage // one of the few times we use accountsDBPromise directly; try to avoid more usage
@ -561,7 +559,7 @@ export const retrieveAllAccountsMetadata = async (): Promise<
result = array.map((account) => { result = array.map((account) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { identity, mnemonic, ...metadata } = account; const { identity, mnemonic, ...metadata } = account;
return metadata; return metadata as Account;
}); });
} }
return result; return result;
@ -620,16 +618,16 @@ export const retrieveFullyDecryptedAccount = async (
// let's try and eliminate this // let's try and eliminate this
export const retrieveAllFullyDecryptedAccounts = async (): Promise< export const retrieveAllFullyDecryptedAccounts = async (): Promise<
Array<AccountKeyInfo> Array<AccountEncrypted>
> => { > => {
const platformService = PlatformServiceFactory.getInstance(); const platformService = PlatformServiceFactory.getInstance();
const queryResult = await platformService.dbQuery("SELECT * FROM accounts"); const queryResult = await platformService.dbQuery("SELECT * FROM accounts");
let allAccounts = databaseUtil.mapQueryResultToValues( let allAccounts = databaseUtil.mapQueryResultToValues(
queryResult, queryResult,
) as unknown as Account[]; ) as unknown as AccountEncrypted[];
if (USE_DEXIE_DB) { if (USE_DEXIE_DB) {
const accountsDB = await accountsDBPromise; const accountsDB = await accountsDBPromise;
allAccounts = await accountsDB.accounts.toArray(); allAccounts = await accountsDB.accounts.toArray() as AccountEncrypted[];
} }
return allAccounts; return allAccounts;
}; };

20
src/views/ImportDerivedAccountView.vue

@ -83,7 +83,7 @@ import { MASTER_SETTINGS_KEY } from "../db/tables/settings";
import * as databaseUtil from "../db/databaseUtil"; import * as databaseUtil from "../db/databaseUtil";
import { retrieveAllAccountsMetadata } from "../libs/util"; import { retrieveAllAccountsMetadata } from "../libs/util";
import { logger } from "../utils/logger"; import { logger } from "../utils/logger";
import { AccountEncrypted } from "@/db/tables/accounts"; import { Account } from "../db/tables/accounts";
import { PlatformServiceFactory } from "@/services/PlatformServiceFactory"; import { PlatformServiceFactory } from "@/services/PlatformServiceFactory";
import { USE_DEXIE_DB } from "@/constants/app"; import { USE_DEXIE_DB } from "@/constants/app";
@Component({ @Component({
@ -98,19 +98,21 @@ export default class ImportAccountView extends Vue {
selectedArrayFirstDid = ""; selectedArrayFirstDid = "";
async mounted() { async mounted() {
const accounts: AccountEncrypted[] = await retrieveAllAccountsMetadata(); const accounts: Account[] = await retrieveAllAccountsMetadata();
const seedDids: Record<string, Array<string>> = {}; const seedDids: Record<string, Array<string>> = {};
accounts.forEach((account) => { accounts.forEach((account) => {
const mnemonicMaybeEncrypted = // Since we're only getting metadata, we can't check mnemonic
account.mnemonic || account.mnemonicEncrBase64; // Instead, we'll group by derivation path
if (mnemonicMaybeEncrypted) { if (account.derivationPath) {
const prevDids: Array<string> = seedDids[mnemonicMaybeEncrypted] || []; const prevDids: Array<string> = seedDids[account.derivationPath] || [];
seedDids[mnemonicMaybeEncrypted] = prevDids.concat([account.did]); seedDids[account.derivationPath] = prevDids.concat([account.did]);
} }
}); });
this.didArrays = Object.values(seedDids); this.didArrays = Object.values(seedDids);
if (this.didArrays.length > 0) {
this.selectedArrayFirstDid = this.didArrays[0][0]; this.selectedArrayFirstDid = this.didArrays[0][0];
} }
}
public onCancelClick() { public onCancelClick() {
this.$router.back(); this.$router.back();
@ -133,13 +135,13 @@ export default class ImportAccountView extends Vue {
); );
let allMatchingAccounts = databaseUtil.mapQueryResultToValues( let allMatchingAccounts = databaseUtil.mapQueryResultToValues(
queryResult, queryResult,
) as unknown as AccountEncrypted[]; ) as unknown as Account[];
if (USE_DEXIE_DB) { if (USE_DEXIE_DB) {
const accountsDB = await accountsDBPromise; // let's match derived accounts differently so we don't need the private info const accountsDB = await accountsDBPromise; // let's match derived accounts differently so we don't need the private info
allMatchingAccounts = (await accountsDB.accounts allMatchingAccounts = (await accountsDB.accounts
.where("did") .where("did")
.anyOf(...selectedArray) .anyOf(...selectedArray)
.toArray()) as AccountEncrypted[]; .toArray()) as Account[];
} }
const accountWithMaxDeriv = allMatchingAccounts[0]; const accountWithMaxDeriv = allMatchingAccounts[0];
allMatchingAccounts.slice(1).forEach((account) => { allMatchingAccounts.slice(1).forEach((account) => {

Loading…
Cancel
Save