From dcaa033985300151fa41d0413a394a5fead6df04 Mon Sep 17 00:00:00 2001 From: Jagoda Berry Rybacka Date: Fri, 1 Sep 2023 13:34:23 +0200 Subject: [PATCH 1/2] Show different private key import errors --- ui/_locales/en/messages.json | 5 +++-- ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ui/_locales/en/messages.json b/ui/_locales/en/messages.json index 97da9d778..3a0f1cf73 100644 --- a/ui/_locales/en/messages.json +++ b/ui/_locales/en/messages.json @@ -411,7 +411,8 @@ "subtitle": "Importing a private key does not associate it to a secret recovery phrase, but it’s still protected by the same password", "inputLabel": "Paste private key string", "submit": "Import account", - "error": "Invalid private key", + "errorFormat": "Invalid private key format", + "errorImport": "Importing private key failed", "privateKey": "Private key", "password": "JSON file password", "json": "JSON", @@ -1030,4 +1031,4 @@ "earn": "Earn", "settings": "Settings" } -} +} \ No newline at end of file diff --git a/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx b/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx index 6ecc06254..c697edb0e 100644 --- a/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx +++ b/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx @@ -48,9 +48,10 @@ export default function ImportPrivateKey(props: Props): ReactElement { finalize() } else { setIsImporting(false) + setErrorMessage(t("errorImport")) } } else { - setErrorMessage(t("error")) + setErrorMessage(t("errorFormat")) } }, [dispatch, privateKey, setIsImporting, finalize, t]) From f0be4cf5199d9b5ce83ea93da8edb899aaee67b4 Mon Sep 17 00:00:00 2001 From: Jagoda Berry Rybacka Date: Fri, 1 Sep 2023 15:16:21 +0200 Subject: [PATCH 2/2] Attempt private key import to validate input --- background/utils/internal-signer.ts | 16 ---------- ui/_locales/en/messages.json | 1 - .../Onboarding/Tabbed/ImportPrivateKey.tsx | 31 +++++++++---------- 3 files changed, 14 insertions(+), 34 deletions(-) delete mode 100644 background/utils/internal-signer.ts diff --git a/background/utils/internal-signer.ts b/background/utils/internal-signer.ts deleted file mode 100644 index 89ac445cd..000000000 --- a/background/utils/internal-signer.ts +++ /dev/null @@ -1,16 +0,0 @@ -/* eslint-disable import/prefer-default-export */ -import { isHexString } from "ethers/lib/utils" - -export function validatePrivateKey(privateKey = ""): boolean { - try { - const paddedKey = privateKey.startsWith("0x") - ? privateKey - : `0x${privateKey}` - // valid pk has 32 bytes -> 64 hex characters - return ( - isHexString(paddedKey) && BigInt(paddedKey).toString(16).length === 64 - ) - } catch (e) { - return false - } -} diff --git a/ui/_locales/en/messages.json b/ui/_locales/en/messages.json index 3a0f1cf73..b9cada0d7 100644 --- a/ui/_locales/en/messages.json +++ b/ui/_locales/en/messages.json @@ -411,7 +411,6 @@ "subtitle": "Importing a private key does not associate it to a secret recovery phrase, but it’s still protected by the same password", "inputLabel": "Paste private key string", "submit": "Import account", - "errorFormat": "Invalid private key format", "errorImport": "Importing private key failed", "privateKey": "Private key", "password": "JSON file password", diff --git a/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx b/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx index c697edb0e..4cb850192 100644 --- a/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx +++ b/ui/pages/Onboarding/Tabbed/ImportPrivateKey.tsx @@ -5,7 +5,6 @@ import { SignerSourceTypes } from "@tallyho/tally-background/services/internal-s import React, { ReactElement, useCallback, useState } from "react" import { useTranslation } from "react-i18next" import { AsyncThunkFulfillmentType } from "@tallyho/tally-background/redux-slices/utils" -import { validatePrivateKey } from "@tallyho/tally-background/utils/internal-signer" import SharedButton from "../../../components/Shared/SharedButton" import SharedSeedInput from "../../../components/Shared/SharedSeedInput" import { useBackgroundDispatch } from "../../../hooks" @@ -34,24 +33,22 @@ export default function ImportPrivateKey(props: Props): ReactElement { const importWallet = useCallback(async () => { const trimmedPrivateKey = privateKey.toLowerCase().trim() - if (validatePrivateKey(trimmedPrivateKey)) { - setIsImporting(true) - const { success } = (await dispatch( - importSigner({ - type: SignerSourceTypes.privateKey, - privateKey: trimmedPrivateKey, - }), - )) as unknown as AsyncThunkFulfillmentType - if (success) { - dispatch(sendEvent(OneTimeAnalyticsEvent.ONBOARDING_FINISHED)) - finalize() - } else { - setIsImporting(false) - setErrorMessage(t("errorImport")) - } + setIsImporting(true) + + const { success } = (await dispatch( + importSigner({ + type: SignerSourceTypes.privateKey, + privateKey: trimmedPrivateKey, + }), + )) as unknown as AsyncThunkFulfillmentType + + if (success) { + dispatch(sendEvent(OneTimeAnalyticsEvent.ONBOARDING_FINISHED)) + finalize() } else { - setErrorMessage(t("errorFormat")) + setIsImporting(false) + setErrorMessage(t("errorImport")) } }, [dispatch, privateKey, setIsImporting, finalize, t])