diff --git a/src/libs/util.ts b/src/libs/util.ts index 03ee7637..030ba416 100644 --- a/src/libs/util.ts +++ b/src/libs/util.ts @@ -45,6 +45,7 @@ import { PlatformServiceFactory } from "@/services/PlatformServiceFactory"; import { sha256 } from "ethereum-cryptography/sha256"; import { IIdentifier } from "@veramo/core"; import { insertDidSpecificSettings, parseJsonField } from "../db/databaseUtil"; +import { DEFAULT_ROOT_DERIVATION_PATH } from "./crypto"; export interface GiverReceiverInputInfo { did?: string; @@ -998,3 +999,38 @@ export const contactsToExportJson = (contacts: Contact[]): DatabaseExport => { }, }; }; + +/** + * Imports an account from a mnemonic phrase + * @param mnemonic - The seed phrase to import from + * @param derivationPath - The derivation path to use (defaults to DEFAULT_ROOT_DERIVATION_PATH) + * @param shouldErase - Whether to erase existing accounts before importing + * @returns Promise that resolves when import is complete + * @throws Error if mnemonic is invalid or import fails + */ +export async function importFromMnemonic( + mnemonic: string, + derivationPath: string = DEFAULT_ROOT_DERIVATION_PATH, + shouldErase: boolean = false, +): Promise { + const mne: string = mnemonic.trim().toLowerCase(); + + // Derive address and keys from mnemonic + const [address, privateHex, publicHex] = deriveAddress(mne, derivationPath); + + // Create new identifier + const newId = newIdentifier(address, publicHex, privateHex, derivationPath); + + // Handle database operations + const accountsDB = await accountsDBPromise; + if (shouldErase) { + const platformService = PlatformServiceFactory.getInstance(); + await platformService.dbExec("DELETE FROM accounts"); + if (USE_DEXIE_DB) { + await accountsDB.accounts.clear(); + } + } + + // Save the new identity + await saveNewIdentity(newId, mne, derivationPath); +} diff --git a/src/views/AccountViewView.vue b/src/views/AccountViewView.vue index 896b22fa..c3fee747 100644 --- a/src/views/AccountViewView.vue +++ b/src/views/AccountViewView.vue @@ -953,12 +953,6 @@ > Logs - - Database Migration - + + +
+

Database Tools

+
+ + Database Migration + +
+