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 all 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
49 changes: 31 additions & 18 deletions apps/extension/src/Setup/ImportAccount/SeedPhraseImport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,35 @@ export const SeedPhraseImport: React.FC<Props> = ({ 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<HTMLInputElement>) => {
Expand Down Expand Up @@ -118,11 +126,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 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(" ");
Expand Down Expand Up @@ -226,7 +239,7 @@ export const SeedPhraseImport: React.FC<Props> = ({ onConfirm }) => {
value={privateKey}
placeholder="Enter your private key"
onChange={(e) => setPrivateKey(e.target.value)}
error={privateKeyError}
error={pkValidationResult.msg}
/>
)}
</Stack>
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;