From f7486f0490c529592567100e6bbad6b785ff6239 Mon Sep 17 00:00:00 2001 From: fmtabbara Date: Fri, 13 May 2022 12:22:19 +0100 Subject: [PATCH] add field validation for mnemonic and account name --- .../components/Accounts/AddAccountModal.tsx | 122 +++++++++++++----- nym-wallet/src/context/accounts.tsx | 2 +- nym-wallet/src/requests/account.ts | 2 +- 3 files changed, 89 insertions(+), 37 deletions(-) diff --git a/nym-wallet/src/components/Accounts/AddAccountModal.tsx b/nym-wallet/src/components/Accounts/AddAccountModal.tsx index df9b4c25ed..3f3aec7b62 100644 --- a/nym-wallet/src/components/Accounts/AddAccountModal.tsx +++ b/nym-wallet/src/components/Accounts/AddAccountModal.tsx @@ -14,10 +14,12 @@ import { } from '@mui/material'; import { ArrowBackSharp } from '@mui/icons-material'; import { useClipboard } from 'use-clipboard-copy'; -import { createMnemonic } from 'src/requests'; +import { createMnemonic, validateMnemonic } from 'src/requests'; import { AccountsContext } from 'src/context'; import { PasswordInput } from 'src/components'; import { Mnemonic } from '../Mnemonic'; +import { Console } from 'src/utils/console'; +import { match } from 'assert'; const createAccountSteps = [ 'Copy and save mnemonic for your new account', @@ -54,41 +56,81 @@ const ImportMnemonic = ({ value: string; onChange: (value: string) => void; onNext: () => void; -}) => ( - - - - onChange(e.target.value)} - fullWidth - type="password" - /> - - - - - - -); +}) => { + const [error, setError] = useState(); + + const handleOnNext = async () => { + const isValid = await validateMnemonic(value); + if (!isValid) setError('Please enter a valid mnemonic. Mnemonic must have a word count that is a multiple of 6.'); + else onNext(); + }; -const NameAccount = ({ onNext }: { onNext: (value: string) => void }) => { - const [value, setValue] = useState(''); return ( - setValue(e.target.value)} fullWidth /> + + {error && ( + + {error} + + )} + { + onChange(e.target.value); + setError(undefined); + }} + fullWidth + type="password" + /> + + + + + + + ); +}; + +const NameAccount = ({ onNext }: { onNext: (value: string) => void }) => { + const [value, setValue] = useState(''); + const [error, setError] = useState(); + + const nameValidation = /^([a-zA-Z0-9\s]){1,20}$/; + + const handleNext = (value: string) => { + if (!nameValidation.test(value)) + [setError('Account name must contain only letters and numbers and be between 1 and 20 characters')]; + else onNext(value); + }; + + return ( + + + + {error} + + { + setValue(e.target.value); + setError(undefined); + }} + fullWidth + /> @@ -158,6 +200,10 @@ export const AddAccountModal = () => { const handleClose = () => { setDialogToDisplay('Accounts'); + resetState(); + }; + + const resetState = () => { setData({ mnemonic: '', accountName: '' }); setStep(0); setError(undefined); @@ -165,7 +211,7 @@ export const AddAccountModal = () => { useEffect(() => { if (dialogToDisplay === 'Add') generateMnemonic(); - if (dialogToDisplay === 'Accounts') setStep(0); + if (dialogToDisplay === 'Accounts') resetState(); }, [dialogToDisplay]); useEffect(() => { @@ -216,7 +262,13 @@ export const AddAccountModal = () => { { if (data.accountName && data.mnemonic) { - await handleAddAccount({ accountName: data.accountName, mnemonic: data.mnemonic, password }); + try { + await handleAddAccount({ accountName: data.accountName, mnemonic: data.mnemonic, password }); + setStep(0); + setDialogToDisplay('Accounts'); + } catch (e) { + Console.error(e as string); + } } }} /> diff --git a/nym-wallet/src/context/accounts.tsx b/nym-wallet/src/context/accounts.tsx index 53e7127124..51794db671 100644 --- a/nym-wallet/src/context/accounts.tsx +++ b/nym-wallet/src/context/accounts.tsx @@ -60,9 +60,9 @@ export const AccountsProvider: React.FC = ({ children }) => { }); setAccounts((accs) => [...accs, newAccount]); enqueueSnackbar('New account created', { variant: 'success' }); - setDialogToDisplay('Accounts'); } catch (e) { setError(`Error adding account: ${e}`); + throw new Error(); } finally { setIsLoading(false); } diff --git a/nym-wallet/src/requests/account.ts b/nym-wallet/src/requests/account.ts index bc85fe30ad..171776aef5 100644 --- a/nym-wallet/src/requests/account.ts +++ b/nym-wallet/src/requests/account.ts @@ -44,7 +44,7 @@ export const addAccount = async ({ password: string; accountName: string; }) => { - const res: AccountEntry = await invoke('add_account_for_password', { mnemonic, password, innerId: accountName }); + const res: AccountEntry = await invoke('add_account_for_password', { mnemonic, password, accountId: accountName }); return res; };