From c47598a9483358574fe6daea440e5447e82c8288 Mon Sep 17 00:00:00 2001 From: DMY <147dmy@gmail.com> Date: Fri, 22 Nov 2024 14:31:23 +0800 Subject: [PATCH] fix: seed phrase input --- .../component/WordsMatrix/MnemonicsInputs.tsx | 33 +++++++++------ .../views/NewUserImport/ImportSeedPhrase.tsx | 41 +++++++++++++------ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/ui/component/WordsMatrix/MnemonicsInputs.tsx b/src/ui/component/WordsMatrix/MnemonicsInputs.tsx index 30592a3520e..bde8356ca20 100644 --- a/src/ui/component/WordsMatrix/MnemonicsInputs.tsx +++ b/src/ui/component/WordsMatrix/MnemonicsInputs.tsx @@ -71,11 +71,26 @@ const MatrixWrapper = styled.div.withConfig<{ width: calc(calc(100% - 18px) / 3); border: 0.5px solid var(--r-neutral-line, #e0e5ec) !important; .mnemonics-input { + text-align: center; &:focus, &.ant-input-focused { box-shadow: none; } } + + ${styid(NumberFlag)} { + top: 8px; + left: 8px; + color: var(--r-neutral-body, #3e495e); + font-size: 11px; + font-style: normal; + font-weight: 400; + } + } + .matrix-word-item.invalid { + ${styid(NumberFlag)} { + color: var(--r-red-default, #e34935); + } } } @@ -604,7 +619,7 @@ function MnemonicsInputs({ key={`word-input-${ver}-${word}-${idx}`} className={clsx( 'mnemonics-input pr-10', - newUserImport ? 'pl-[30px]' : 'pl-[46px]', + newUserImport ? 'pl-[10px]' : 'pl-[46px]', isCurrentFocusing && 'ant-input-focused', { 'opacity-50': @@ -664,7 +679,7 @@ function MnemonicsInputs({ {errMsgs?.[0] || invalidWords.length > 0 ? (
{invalidWords.length > 0 && ( @@ -692,27 +707,19 @@ const SLIP39MnemonicsInput = ({ onTextChange: (text: string) => void; idx: number; error?: boolean; - onPaste?: (e: React.ClipboardEvent) => void; + onPaste?: (e: React.ClipboardEvent) => void; }) => { const { t } = useTranslation(); const [show, setShow] = useState(false); return (
- { ); const [errMsgs, setErrMsgs] = React.useState(); - const [checkPass, setCheckPass] = React.useState(false); const disabledButton = React.useMemo(() => { - if (!checkPass) return true; - if (!isSlip39) return false; + if (!isSlip39) return; return secretShares.length < slip39GroupNumber; - }, [isSlip39, secretShares, slip39GroupNumber, checkPass]); + }, [isSlip39, secretShares, slip39GroupNumber]); + + const validateMnemonic = React.useCallback( + async (mnemonics: string, skipSlip39?: boolean) => { + try { + if (skipSlip39 && isSlip39) return true; + if (!isSlip39) { + return bip39.validateMnemonic(mnemonics, wordlist); + } + const result = await wallet.validateMnemonic(mnemonics); + return result; + } catch (err) { + return false; + } + }, + [isSlip39] + ); const run = React.useCallback( async ({ @@ -133,6 +149,13 @@ export const ImportSeedPhrase = () => { }) => { try { await checkSubmitSlip39Mnemonics(mnemonics); + + if (!(await validateMnemonic(mnemonics))) { + throw new Error( + t('page.newAddress.theSeedPhraseIsInvalidPleaseCheck') + ); + } + setStore({ seedPhrase: mnemonics, passphrase }); history.push('/new-user/import/seed-phrase/set-password'); } catch (err) { @@ -148,7 +171,7 @@ export const ImportSeedPhrase = () => { ]); } }, - [checkSubmitSlip39Mnemonics, setStore, form, t] + [validateMnemonic, checkSubmitSlip39Mnemonics, setStore, form, t] ); return ( @@ -169,15 +192,9 @@ export const ImportSeedPhrase = () => { onValuesChange={async (states) => { setErrMsgs([]); setSlip39ErrorIndex(-1); - try { - const pass = await wallet.validateMnemonic(states.mnemonics); - setCheckPass(pass); - } catch (error) { - setCheckPass(false); - } }} > - +