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,
|
|
|
|
TYPOGRAPHY,
|
|
|
|
JUSTIFY_CONTENT,
|
|
|
|
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';
|
|
|
|
import { EVENT_NAMES, EVENT } 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
|
|
|
|
justifyContent={JUSTIFY_CONTENT.CENTER}
|
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
|
|
|
<Typography variant={TYPOGRAPHY.H2} fontWeight={FONT_WEIGHT.BOLD}>
|
|
|
|
{t('seedPhraseConfirm')}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
|
|
|
<Box
|
|
|
|
justifyContent={JUSTIFY_CONTENT.CENTER}
|
|
|
|
textAlign={TEXT_ALIGN.CENTER}
|
|
|
|
marginBottom={4}
|
|
|
|
>
|
|
|
|
<Typography variant={TYPOGRAPHY.H4}>
|
|
|
|
{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({
|
|
|
|
category: EVENT.CATEGORIES.ONBOARDING,
|
|
|
|
event: EVENT_NAMES.ONBOARDING_WALLET_SECURITY_PHRASE_CONFIRMED,
|
|
|
|
});
|
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
|
|
|
};
|