Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "Error: unreachable" message when importing short private key #720

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,31 @@ export const SeedPhraseImport: React.FC<Props> = ({ onConfirm }) => {
Array.from(mnemonicsRange)
);

const privateKeyError = (() => {
const validation = validatePrivateKey(filterPrivateKeyPrefix(privateKey));
const validatePkAndFormatErrorMessage = (key: string): string => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to call it validatePk. We can also return {valid: boolean, msg: string} or something, as it seems a bit more explicit :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. updated function name and return type 👍

const validation = validatePrivateKey(filterPrivateKeyPrefix(key));
if (validation.ok) {
return "";
} else {
switch (validation.error.t) {
case "TooLong":
return `Private key must be no more than
${validation.error.maxLength} characters long`;
case "WrongLength":
return (
`Private key must be ${validation.error.length} characters long. ` +
`You provided a key of length ${key.length}.`
);
case "BadCharacter":
return "Private key may only contain characters 0-9, a-f";
default:
return assertNever(validation.error);
}
}
})();
};

const privateKeyError = validatePkAndFormatErrorMessage(privateKey);

const isSubmitButtonDisabled =
mnemonicType === MnemonicTypes.PrivateKey
? privateKey === "" || privateKeyError !== ""
: mnemonics.slice(0, mnemonicType).some((mnemonic) => !mnemonic);
mnemonicType === MnemonicTypes.PrivateKey ?
privateKey === "" || privateKeyError !== ""
: mnemonics.slice(0, mnemonicType).some((mnemonic) => !mnemonic);

const onPaste = useCallback(
(index: number, e: React.ClipboardEvent<HTMLInputElement>) => {
Expand Down Expand Up @@ -118,11 +122,16 @@ export const SeedPhraseImport: React.FC<Props> = ({ onConfirm }) => {

const onSubmit = useCallback(async () => {
if (mnemonicType === MnemonicTypes.PrivateKey) {
// TODO: validate here
onConfirm({
t: "PrivateKey",
privateKey: filterPrivateKeyPrefix(privateKey),
});
const privateKeyError = validatePkAndFormatErrorMessage(privateKey);
if (privateKeyError) {
setMnemonicError(privateKeyError);
} else {
setMnemonicError(undefined);
onConfirm({
t: "PrivateKey",
privateKey: filterPrivateKeyPrefix(privateKey),
});
}
} else {
const actualMnemonics = mnemonics.slice(0, mnemonicType);
const phrase = actualMnemonics.join(" ");
Expand Down
25 changes: 14 additions & 11 deletions apps/extension/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,27 @@ export const validateProps = <T>(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<null, PrivateKeyError> =>
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<null, PrivateKeyError> => {
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;