import React, { PureComponent } from 'react' import PropTypes from 'prop-types' import classnames from 'classnames' import shuffle from 'lodash.shuffle' import Identicon from '../../../../identicon' import Button from '../../../../button' import Breadcrumbs from '../../../../breadcrumbs' import { DEFAULT_ROUTE, INITIALIZE_SEED_PHRASE_ROUTE } from '../../../../../routes' import { exportAsFile } from '../../../../../../app/util' import { selectSeedWord, deselectSeedWord } from './confirm-seed-phrase.state' export default class ConfirmSeedPhrase extends PureComponent { static contextTypes = { t: PropTypes.func, } static defaultProps = { seedPhrase: '', } static propTypes = { address: PropTypes.string, completeOnboarding: PropTypes.func, history: PropTypes.object, onSubmit: PropTypes.func, openBuyEtherModal: PropTypes.func, seedPhrase: PropTypes.string, } state = { selectedSeedWords: [], shuffledSeedWords: [], // Hash of shuffledSeedWords index {Number} to selectedSeedWords index {Number} selectedSeedWordsHash: {}, } componentDidMount () { const { seedPhrase = '' } = this.props const shuffledSeedWords = shuffle(seedPhrase.split(' ')) || [] this.setState({ shuffledSeedWords }) } handleExport = () => { exportAsFile('MetaMask Secret Backup Phrase', this.props.seedPhrase, 'text/plain') } handleSubmit = async () => { const { completeOnboarding, history, openBuyEtherModal } = this.props if (!this.isValid()) { return } try { await completeOnboarding() history.push(DEFAULT_ROUTE) openBuyEtherModal() } catch (error) { console.error(error.message) } } handleSelectSeedWord = (word, shuffledIndex) => { this.setState(selectSeedWord(word, shuffledIndex)) } handleDeselectSeedWord = shuffledIndex => { this.setState(deselectSeedWord(shuffledIndex)) } isValid () { const { seedPhrase } = this.props const { selectedSeedWords } = this.state return seedPhrase === selectedSeedWords.join(' ') } render () { const { t } = this.context const { address, history } = this.props const { selectedSeedWords, shuffledSeedWords, selectedSeedWordsHash } = this.state return (
{ e.preventDefault() history.push(INITIALIZE_SEED_PHRASE_ROUTE) }} href="#" > {`< Back`}
{ t('confirmSecretBackupPhrase') }
{ t('selectEachPhrase') }
{ selectedSeedWords.map((word, index) => (
{ word }
)) }
{ shuffledSeedWords.map((word, index) => { const isSelected = index in selectedSeedWordsHash return (
{ if (!isSelected) { this.handleSelectSeedWord(word, index) } else { this.handleDeselectSeedWord(index) } }} > { word }
) }) }
) } }