diff --git a/apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx b/apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx index 8b8b7e066..c8ee0c7af 100644 --- a/apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx +++ b/apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx @@ -47,27 +47,35 @@ export const SeedPhraseImport: React.FC = ({ onConfirm }) => { Array.from(mnemonicsRange) ); - const privateKeyError = (() => { - const validation = validatePrivateKey(filterPrivateKeyPrefix(privateKey)); + const validatePk = (key: string): { isValid: boolean; msg: string } => { + const validation = validatePrivateKey(filterPrivateKeyPrefix(key)); if (validation.ok) { - return ""; + return { isValid: true, msg: "" }; } else { + let msg = ""; switch (validation.error.t) { - case "TooLong": - return `Private key must be no more than - ${validation.error.maxLength} characters long`; + case "WrongLength": + msg = + `Private key must be ${validation.error.length} characters long. ` + + `You provided a key of length ${key.length}.`; + break; case "BadCharacter": - return "Private key may only contain characters 0-9, a-f"; + msg = "Private key may only contain characters 0-9, a-f"; + break; default: - return assertNever(validation.error); + msg = assertNever(validation.error); + break; } + return { isValid: false, msg: msg }; } - })(); + }; + + const pkValidationResult = validatePk(privateKey); const isSubmitButtonDisabled = - mnemonicType === MnemonicTypes.PrivateKey - ? privateKey === "" || privateKeyError !== "" - : mnemonics.slice(0, mnemonicType).some((mnemonic) => !mnemonic); + mnemonicType === MnemonicTypes.PrivateKey ? + privateKey === "" || !pkValidationResult.isValid + : mnemonics.slice(0, mnemonicType).some((mnemonic) => !mnemonic); const onPaste = useCallback( (index: number, e: React.ClipboardEvent) => { @@ -118,11 +126,16 @@ export const SeedPhraseImport: React.FC = ({ onConfirm }) => { const onSubmit = useCallback(async () => { if (mnemonicType === MnemonicTypes.PrivateKey) { - // TODO: validate here - onConfirm({ - t: "PrivateKey", - privateKey: filterPrivateKeyPrefix(privateKey), - }); + const pkValidationResult = validatePk(privateKey); + if (!pkValidationResult.isValid) { + setMnemonicError(pkValidationResult.msg); + } else { + setMnemonicError(undefined); + onConfirm({ + t: "PrivateKey", + privateKey: filterPrivateKeyPrefix(privateKey), + }); + } } else { const actualMnemonics = mnemonics.slice(0, mnemonicType); const phrase = actualMnemonics.join(" "); @@ -226,7 +239,7 @@ export const SeedPhraseImport: React.FC = ({ onConfirm }) => { value={privateKey} placeholder="Enter your private key" onChange={(e) => setPrivateKey(e.target.value)} - error={privateKeyError} + error={pkValidationResult.msg} /> )} diff --git a/apps/extension/src/utils/index.ts b/apps/extension/src/utils/index.ts index 960379731..407c08144 100644 --- a/apps/extension/src/utils/index.ts +++ b/apps/extension/src/utils/index.ts @@ -81,24 +81,27 @@ export const validateProps = (object: T, props: (keyof T)[]): void => { }); }; -const PRIVATE_KEY_MAX_LENGTH = 64; +const PRIVATE_KEY_LENGTH = 64; export type PrivateKeyError = - | { t: "TooLong"; maxLength: number } + | { t: "WrongLength"; length: number } | { t: "BadCharacter" }; // Very basic private key validation export const validatePrivateKey = ( privateKey: string -): Result => - privateKey.length > PRIVATE_KEY_MAX_LENGTH - ? Result.err({ t: "TooLong", maxLength: PRIVATE_KEY_MAX_LENGTH }) - : !/^[0-9a-f]*$/.test(privateKey) - ? Result.err({ t: "BadCharacter" }) - : Result.ok(null); +): Result => { + if (privateKey.length != PRIVATE_KEY_LENGTH) { + return Result.err({ t: "WrongLength", length: PRIVATE_KEY_LENGTH }); + } else if (!/^[0-9a-f]*$/.test(privateKey)) { + return Result.err({ t: "BadCharacter" }); + } else { + return Result.ok(null); + } +}; // Remove prefix from private key, which may be present when exporting keys from CLI export const filterPrivateKeyPrefix = (privateKey: string): string => - privateKey.length === PRIVATE_KEY_MAX_LENGTH + 2 - ? privateKey.replace(/^00/, "") - : privateKey; + privateKey.length === PRIVATE_KEY_LENGTH + 2 ? + privateKey.replace(/^00/, "") + : privateKey;