forked from trent_larson/crowd-funder-for-time-pwa
feat: add duplicate account import prevention
- Add duplicate check in ImportAccountView before account import - Add duplicate check in ImportDerivedAccountView for derived accounts - Add safety check in saveNewIdentity function to prevent duplicate saves - Implement user-friendly warning messages for duplicate attempts - Add comprehensive error handling to catch duplicate errors from saveNewIdentity - Create Playwright tests to verify duplicate prevention functionality - Add documentation for duplicate prevention implementation The system now prevents users from importing the same account multiple times by checking for existing DIDs both before import (pre-check) and during save (post-check). Users receive clear warning messages instead of technical errors when attempting to import duplicate accounts. Files modified: - src/views/ImportAccountView.vue: Add duplicate check and error handling - src/views/ImportDerivedAccountView.vue: Add duplicate check for derived accounts - src/libs/util.ts: Add duplicate prevention in saveNewIdentity - test-playwright/duplicate-import-test.spec.ts: Add comprehensive tests - doc/duplicate-account-import-implementation.md: Add implementation docs Resolves: Prevent duplicate account imports in IdentitySwitcherView
This commit is contained in:
@@ -87,7 +87,11 @@ import { Component, Vue } from "vue-facing-decorator";
|
||||
import { Router } from "vue-router";
|
||||
|
||||
import { AppString, NotificationIface } from "../constants/app";
|
||||
import { DEFAULT_ROOT_DERIVATION_PATH } from "../libs/crypto";
|
||||
import {
|
||||
DEFAULT_ROOT_DERIVATION_PATH,
|
||||
deriveAddress,
|
||||
newIdentifier,
|
||||
} from "../libs/crypto";
|
||||
import { retrieveAccountCount, importFromMnemonic } from "../libs/util";
|
||||
import { PlatformServiceMixin } from "@/utils/PlatformServiceMixin";
|
||||
import { createNotifyHelpers, TIMEOUTS } from "@/utils/notify";
|
||||
@@ -198,6 +202,16 @@ export default class ImportAccountView extends Vue {
|
||||
}
|
||||
|
||||
try {
|
||||
// Check for duplicate account before importing
|
||||
const isDuplicate = await this.checkForDuplicateAccount();
|
||||
if (isDuplicate) {
|
||||
this.notify.warning(
|
||||
"This account has already been imported. Please use a different seed phrase or check your existing accounts.",
|
||||
TIMEOUTS.LONG,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await importFromMnemonic(
|
||||
this.mnemonic,
|
||||
this.derivationPath,
|
||||
@@ -223,12 +237,64 @@ export default class ImportAccountView extends Vue {
|
||||
this.$router.push({ name: "account" });
|
||||
} catch (error: unknown) {
|
||||
this.$logError("Import failed: " + error);
|
||||
|
||||
// Check if this is a duplicate account error from saveNewIdentity
|
||||
const errorMessage =
|
||||
error instanceof Error ? error.message : String(error);
|
||||
if (
|
||||
errorMessage.includes("already exists") &&
|
||||
errorMessage.includes("Cannot import duplicate account")
|
||||
) {
|
||||
this.notify.warning(
|
||||
"This account has already been imported. Please use a different seed phrase or check your existing accounts.",
|
||||
TIMEOUTS.LONG,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
this.notify.error(
|
||||
(error instanceof Error ? error.message : String(error)) ||
|
||||
"Failed to import account.",
|
||||
errorMessage || "Failed to import account.",
|
||||
TIMEOUTS.LONG,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the account to be imported already exists
|
||||
*
|
||||
* Derives the DID from the mnemonic and checks if it exists in the database
|
||||
* @returns Promise<boolean> - True if account already exists, false otherwise
|
||||
*/
|
||||
private async checkForDuplicateAccount(): Promise<boolean> {
|
||||
try {
|
||||
// Derive the address and create the identifier to get the DID
|
||||
const [address, privateHex, publicHex] = deriveAddress(
|
||||
this.mnemonic.trim().toLowerCase(),
|
||||
this.derivationPath,
|
||||
);
|
||||
|
||||
const newId = newIdentifier(
|
||||
address,
|
||||
publicHex,
|
||||
privateHex,
|
||||
this.derivationPath,
|
||||
);
|
||||
const didToCheck = newId.did;
|
||||
|
||||
// Check if an account with this DID already exists
|
||||
const existingAccount = await this.$query(
|
||||
"SELECT did FROM accounts WHERE did = ?",
|
||||
[didToCheck],
|
||||
);
|
||||
|
||||
return existingAccount?.values?.length > 0;
|
||||
} catch (error) {
|
||||
// If we can't check for duplicates (e.g., invalid mnemonic),
|
||||
// let the import process handle the error
|
||||
this.$logError("Error checking for duplicate account: " + error);
|
||||
// Return false to let the import process continue and handle the error
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -171,6 +171,16 @@ export default class ImportAccountView extends Vue {
|
||||
const newId = newIdentifier(address, publicHex, privateHex, newDerivPath);
|
||||
|
||||
try {
|
||||
// Check for duplicate account before creating
|
||||
const isDuplicate = await this.checkForDuplicateAccount(newId.did);
|
||||
if (isDuplicate) {
|
||||
this.notify.warning(
|
||||
"This derived account already exists. Please try a different derivation path.",
|
||||
TIMEOUTS.LONG,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await saveNewIdentity(newId, mne, newDerivPath);
|
||||
|
||||
// record that as the active DID
|
||||
@@ -192,5 +202,27 @@ export default class ImportAccountView extends Vue {
|
||||
this.notify.error(NOTIFY_ACCOUNT_DERIVATION_ERROR.message, TIMEOUTS.LONG);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the account to be created already exists
|
||||
*
|
||||
* @param did - The DID to check for duplicates
|
||||
* @returns Promise<boolean> - True if account already exists, false otherwise
|
||||
*/
|
||||
private async checkForDuplicateAccount(did: string): Promise<boolean> {
|
||||
try {
|
||||
// Check if an account with this DID already exists
|
||||
const existingAccount = await this.$query(
|
||||
"SELECT did FROM accounts WHERE did = ?",
|
||||
[did],
|
||||
);
|
||||
|
||||
return existingAccount?.values?.length > 0;
|
||||
} catch (error) {
|
||||
// If we can't check for duplicates, let the save process handle the error
|
||||
this.$logError("Error checking for duplicate account: " + error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user