2023-01-13 17:11:27 +01:00
|
|
|
import React, { useState, useMemo, useContext } from 'react';
|
2021-07-08 02:10:12 +02:00
|
|
|
import { useHistory } from 'react-router-dom';
|
2022-12-15 15:38:15 +01:00
|
|
|
import { useDispatch } from 'react-redux';
|
2021-07-08 02:10:12 +02:00
|
|
|
import { debounce } from 'lodash';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import Box from '../../../components/ui/box';
|
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import Typography from '../../../components/ui/typography';
|
|
|
|
import {
|
|
|
|
TEXT_ALIGN,
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
|
|
|
JustifyContent,
|
2021-07-08 02:10:12 +02:00
|
|
|
FONT_WEIGHT,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
2021-10-13 19:41:24 +02:00
|
|
|
import {
|
|
|
|
ThreeStepProgressBar,
|
|
|
|
threeStepStages,
|
|
|
|
} from '../../../components/app/step-progress-bar';
|
2021-10-14 19:46:43 +02:00
|
|
|
import { ONBOARDING_COMPLETION_ROUTE } from '../../../helpers/constants/routes';
|
2021-07-08 02:10:12 +02:00
|
|
|
import { useI18nContext } from '../../../hooks/useI18nContext';
|
2022-12-15 15:38:15 +01:00
|
|
|
import { setSeedPhraseBackedUp } from '../../../store/actions';
|
2023-01-13 17:11:27 +01:00
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
2023-04-03 17:31:04 +02:00
|
|
|
import {
|
|
|
|
MetaMetricsEventCategory,
|
|
|
|
MetaMetricsEventName,
|
|
|
|
} from '../../../../shared/constants/metametrics';
|
2021-07-08 02:10:12 +02:00
|
|
|
import RecoveryPhraseChips from './recovery-phrase-chips';
|
|
|
|
|
2021-10-13 19:41:24 +02:00
|
|
|
export default function ConfirmRecoveryPhrase({ secretRecoveryPhrase = '' }) {
|
2021-07-08 02:10:12 +02:00
|
|
|
const history = useHistory();
|
|
|
|
const t = useI18nContext();
|
2022-12-15 15:38:15 +01:00
|
|
|
const dispatch = useDispatch();
|
2021-10-13 19:41:24 +02:00
|
|
|
const splitSecretRecoveryPhrase = secretRecoveryPhrase.split(' ');
|
2021-07-08 02:10:12 +02:00
|
|
|
const indicesToCheck = [2, 3, 7];
|
|
|
|
const [matching, setMatching] = useState(false);
|
2023-01-13 17:11:27 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
2021-07-08 02:10:12 +02:00
|
|
|
|
|
|
|
// Removes seed phrase words from chips corresponding to the
|
|
|
|
// indicesToCheck so that user has to complete the phrase and confirm
|
|
|
|
// they have saved it.
|
|
|
|
const initializePhraseElements = () => {
|
2021-10-13 19:41:24 +02:00
|
|
|
const phraseElements = { ...splitSecretRecoveryPhrase };
|
2021-07-08 02:10:12 +02:00
|
|
|
indicesToCheck.forEach((i) => {
|
|
|
|
phraseElements[i] = '';
|
|
|
|
});
|
|
|
|
return phraseElements;
|
|
|
|
};
|
|
|
|
const [phraseElements, setPhraseElements] = useState(
|
|
|
|
initializePhraseElements(),
|
|
|
|
);
|
|
|
|
|
|
|
|
const validate = useMemo(
|
|
|
|
() =>
|
|
|
|
debounce((elements) => {
|
2021-10-13 19:41:24 +02:00
|
|
|
setMatching(Object.values(elements).join(' ') === secretRecoveryPhrase);
|
2021-07-08 02:10:12 +02:00
|
|
|
}, 500),
|
2021-10-13 19:41:24 +02:00
|
|
|
[setMatching, secretRecoveryPhrase],
|
2021-07-08 02:10:12 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleSetPhraseElements = (values) => {
|
|
|
|
setPhraseElements(values);
|
|
|
|
validate(values);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-08-31 01:53:24 +02:00
|
|
|
<div
|
|
|
|
className="recovery-phrase__confirm"
|
|
|
|
data-testid="confirm-recovery-phrase"
|
|
|
|
>
|
2022-05-16 20:38:04 +02:00
|
|
|
<ThreeStepProgressBar
|
|
|
|
stage={threeStepStages.RECOVERY_PHRASE_CONFIRM}
|
|
|
|
marginBottom={4}
|
|
|
|
/>
|
2021-07-08 02:10:12 +02:00
|
|
|
<Box
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent={JustifyContent.center}
|
2021-07-08 02:10:12 +02:00
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography
|
|
|
|
variant={TypographyVariant.H2}
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
>
|
2021-07-08 02:10:12 +02:00
|
|
|
{t('seedPhraseConfirm')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
<Box
|
2023-02-02 21:15:26 +01:00
|
|
|
justifyContent={JustifyContent.center}
|
2021-07-08 02:10:12 +02:00
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H4}>
|
2021-07-08 02:10:12 +02:00
|
|
|
{t('seedPhraseEnterMissingWords')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
<RecoveryPhraseChips
|
2021-10-13 19:41:24 +02:00
|
|
|
secretRecoveryPhrase={splitSecretRecoveryPhrase}
|
2021-07-08 02:10:12 +02:00
|
|
|
confirmPhase
|
|
|
|
setInputValue={handleSetPhraseElements}
|
|
|
|
inputValue={phraseElements}
|
|
|
|
indicesToCheck={indicesToCheck}
|
|
|
|
/>
|
2022-05-16 20:38:04 +02:00
|
|
|
<div className="recovery-phrase__footer__confirm">
|
2021-07-08 02:10:12 +02:00
|
|
|
<Button
|
2021-11-10 18:27:10 +01:00
|
|
|
data-testid="recovery-phrase-confirm"
|
2021-07-08 02:10:12 +02:00
|
|
|
type="primary"
|
2022-05-16 20:38:04 +02:00
|
|
|
large
|
|
|
|
className="recovery-phrase__footer__confirm--button"
|
2022-12-15 15:38:15 +01:00
|
|
|
onClick={async () => {
|
|
|
|
await dispatch(setSeedPhraseBackedUp(true));
|
2023-01-13 17:11:27 +01:00
|
|
|
trackEvent({
|
2023-04-03 17:31:04 +02:00
|
|
|
category: MetaMetricsEventCategory.Onboarding,
|
|
|
|
event:
|
|
|
|
MetaMetricsEventName.OnboardingWalletSecurityPhraseConfirmed,
|
2023-01-13 17:11:27 +01:00
|
|
|
});
|
2021-10-14 19:46:43 +02:00
|
|
|
history.push(ONBOARDING_COMPLETION_ROUTE);
|
2021-07-08 02:10:12 +02:00
|
|
|
}}
|
|
|
|
disabled={!matching}
|
|
|
|
>
|
|
|
|
{t('confirm')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConfirmRecoveryPhrase.propTypes = {
|
2021-10-13 19:41:24 +02:00
|
|
|
secretRecoveryPhrase: PropTypes.string,
|
2021-07-08 02:10:12 +02:00
|
|
|
};
|