import { ethers } from 'ethers'; import React, { useCallback, useState } from 'react'; import PropTypes from 'prop-types'; import { useI18nContext } from '../../../hooks/useI18nContext'; import TextField from '../../ui/text-field'; import { clearClipboard } from '../../../helpers/utils/util'; import CheckBox from '../../ui/check-box'; import Typography from '../../ui/typography'; import { COLORS } from '../../../helpers/constants/design-system'; import Textarea from '../../ui/textarea'; import { parseSecretRecoveryPhrase } from './parse-secret-recovery-phrase'; const { isValidMnemonic } = ethers.utils; export default function SrpInput({ onChange }) { const [srpError, setSrpError] = useState(''); const [draftSrp, setDraftSrp] = useState(''); const [showSrp, setShowSrp] = useState(false); const t = useI18nContext(); const onSrpChange = useCallback( (event) => { const rawSrp = event.target.value; let newSrpError = ''; const parsedSeedPhrase = parseSecretRecoveryPhrase(rawSrp); if (rawSrp) { const wordCount = parsedSeedPhrase.split(/\s/u).length; if (wordCount % 3 !== 0 || wordCount > 24 || wordCount < 12) { newSrpError = t('seedPhraseReq'); } else if (!isValidMnemonic(parsedSeedPhrase)) { newSrpError = t('invalidSeedPhrase'); } } setDraftSrp(rawSrp); setSrpError(newSrpError); onChange(newSrpError ? '' : parsedSeedPhrase); }, [setDraftSrp, setSrpError, t, onChange], ); const toggleShowSrp = useCallback(() => { setShowSrp((currentShowSrp) => !currentShowSrp); }, []); return ( <> {showSrp ? (