You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
348 lines
9.5 KiB
348 lines
9.5 KiB
4 years ago
|
import { ethers } from 'ethers';
|
||
|
import React, { PureComponent } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import TextField from '../../../../components/ui/text-field';
|
||
|
import Button from '../../../../components/ui/button';
|
||
6 years ago
|
import {
|
||
6 years ago
|
INITIALIZE_SELECT_ACTION_ROUTE,
|
||
6 years ago
|
INITIALIZE_END_OF_FLOW_ROUTE,
|
||
4 years ago
|
} from '../../../../helpers/constants/routes';
|
||
6 years ago
|
|
||
4 years ago
|
const { isValidMnemonic } = ethers.utils;
|
||
4 years ago
|
|
||
6 years ago
|
export default class ImportWithSeedPhrase extends PureComponent {
|
||
|
static contextTypes = {
|
||
|
t: PropTypes.func,
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
static propTypes = {
|
||
|
history: PropTypes.object,
|
||
|
onSubmit: PropTypes.func.isRequired,
|
||
5 years ago
|
setSeedPhraseBackedUp: PropTypes.func,
|
||
5 years ago
|
initializeThreeBox: PropTypes.func,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
state = {
|
||
|
seedPhrase: '',
|
||
5 years ago
|
showSeedPhrase: false,
|
||
6 years ago
|
password: '',
|
||
|
confirmPassword: '',
|
||
|
seedPhraseError: '',
|
||
|
passwordError: '',
|
||
|
confirmPasswordError: '',
|
||
6 years ago
|
termsChecked: false,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
4 years ago
|
parseSeedPhrase = (seedPhrase) =>
|
||
4 years ago
|
(seedPhrase || '').trim().toLowerCase().match(/\w+/gu)?.join(' ') || '';
|
||
6 years ago
|
|
||
4 years ago
|
UNSAFE_componentWillMount() {
|
||
|
this._onBeforeUnload = () =>
|
||
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Onboarding',
|
||
|
action: 'Import Seed Phrase',
|
||
|
name: 'Close window on import screen',
|
||
|
},
|
||
|
customVariables: {
|
||
|
errorLabel: 'Seed Phrase Error',
|
||
|
errorMessage: this.state.seedPhraseError,
|
||
|
},
|
||
4 years ago
|
});
|
||
|
window.addEventListener('beforeunload', this._onBeforeUnload);
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
componentWillUnmount() {
|
||
4 years ago
|
window.removeEventListener('beforeunload', this._onBeforeUnload);
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
handleSeedPhraseChange(seedPhrase) {
|
||
4 years ago
|
let seedPhraseError = '';
|
||
6 years ago
|
|
||
|
if (seedPhrase) {
|
||
4 years ago
|
const parsedSeedPhrase = this.parseSeedPhrase(seedPhrase);
|
||
|
const wordCount = parsedSeedPhrase.split(/\s/u).length;
|
||
5 years ago
|
if (wordCount % 3 !== 0 || wordCount > 24 || wordCount < 12) {
|
||
4 years ago
|
seedPhraseError = this.context.t('seedPhraseReq');
|
||
4 years ago
|
} else if (!isValidMnemonic(parsedSeedPhrase)) {
|
||
4 years ago
|
seedPhraseError = this.context.t('invalidSeedPhrase');
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
this.setState({ seedPhrase, seedPhraseError });
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
handlePasswordChange(password) {
|
||
4 years ago
|
const { t } = this.context;
|
||
6 years ago
|
|
||
5 years ago
|
this.setState((state) => {
|
||
4 years ago
|
const { confirmPassword } = state;
|
||
|
let confirmPasswordError = '';
|
||
|
let passwordError = '';
|
||
6 years ago
|
|
||
|
if (password && password.length < 8) {
|
||
4 years ago
|
passwordError = t('passwordNotLongEnough');
|
||
6 years ago
|
}
|
||
|
|
||
|
if (confirmPassword && password !== confirmPassword) {
|
||
4 years ago
|
confirmPasswordError = t('passwordsDontMatch');
|
||
6 years ago
|
}
|
||
|
|
||
|
return {
|
||
|
password,
|
||
|
passwordError,
|
||
|
confirmPasswordError,
|
||
4 years ago
|
};
|
||
|
});
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
handleConfirmPasswordChange(confirmPassword) {
|
||
4 years ago
|
const { t } = this.context;
|
||
6 years ago
|
|
||
5 years ago
|
this.setState((state) => {
|
||
4 years ago
|
const { password } = state;
|
||
|
let confirmPasswordError = '';
|
||
6 years ago
|
|
||
|
if (password !== confirmPassword) {
|
||
4 years ago
|
confirmPasswordError = t('passwordsDontMatch');
|
||
6 years ago
|
}
|
||
|
|
||
|
return {
|
||
|
confirmPassword,
|
||
|
confirmPasswordError,
|
||
4 years ago
|
};
|
||
|
});
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
handleImport = async (event) => {
|
||
4 years ago
|
event.preventDefault();
|
||
6 years ago
|
|
||
|
if (!this.isValid()) {
|
||
4 years ago
|
return;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
const { password, seedPhrase } = this.state;
|
||
4 years ago
|
const {
|
||
|
history,
|
||
|
onSubmit,
|
||
|
setSeedPhraseBackedUp,
|
||
|
initializeThreeBox,
|
||
4 years ago
|
} = this.props;
|
||
6 years ago
|
|
||
|
try {
|
||
4 years ago
|
await onSubmit(password, this.parseSeedPhrase(seedPhrase));
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Onboarding',
|
||
|
action: 'Import Seed Phrase',
|
||
|
name: 'Import Complete',
|
||
|
},
|
||
4 years ago
|
});
|
||
5 years ago
|
|
||
4 years ago
|
setSeedPhraseBackedUp(true).then(async () => {
|
||
4 years ago
|
initializeThreeBox();
|
||
|
history.push(INITIALIZE_END_OF_FLOW_ROUTE);
|
||
|
});
|
||
6 years ago
|
} catch (error) {
|
||
4 years ago
|
this.setState({ seedPhraseError: error.message });
|
||
6 years ago
|
}
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
4 years ago
|
isValid() {
|
||
6 years ago
|
const {
|
||
|
seedPhrase,
|
||
|
password,
|
||
|
confirmPassword,
|
||
|
passwordError,
|
||
|
confirmPasswordError,
|
||
|
seedPhraseError,
|
||
4 years ago
|
} = this.state;
|
||
6 years ago
|
|
||
4 years ago
|
if (
|
||
|
!password ||
|
||
|
!confirmPassword ||
|
||
|
!seedPhrase ||
|
||
|
password !== confirmPassword
|
||
|
) {
|
||
4 years ago
|
return false;
|
||
6 years ago
|
}
|
||
|
|
||
|
if (password.length < 8) {
|
||
4 years ago
|
return false;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
return !passwordError && !confirmPasswordError && !seedPhraseError;
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
onTermsKeyPress = ({ key }) => {
|
||
5 years ago
|
if (key === ' ' || key === 'Enter') {
|
||
4 years ago
|
this.toggleTermsCheck();
|
||
5 years ago
|
}
|
||
4 years ago
|
};
|
||
5 years ago
|
|
||
6 years ago
|
toggleTermsCheck = () => {
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Onboarding',
|
||
|
action: 'Import Seed Phrase',
|
||
|
name: 'Check ToS',
|
||
|
},
|
||
4 years ago
|
});
|
||
6 years ago
|
this.setState((prevState) => ({
|
||
5 years ago
|
termsChecked: !prevState.termsChecked,
|
||
4 years ago
|
}));
|
||
|
};
|
||
6 years ago
|
|
||
5 years ago
|
toggleShowSeedPhrase = () => {
|
||
|
this.setState(({ showSeedPhrase }) => ({
|
||
|
showSeedPhrase: !showSeedPhrase,
|
||
4 years ago
|
}));
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
render() {
|
||
4 years ago
|
const { t } = this.context;
|
||
4 years ago
|
const {
|
||
|
seedPhraseError,
|
||
|
showSeedPhrase,
|
||
|
passwordError,
|
||
|
confirmPasswordError,
|
||
|
termsChecked,
|
||
4 years ago
|
} = this.state;
|
||
6 years ago
|
|
||
|
return (
|
||
4 years ago
|
<form className="first-time-flow__form" onSubmit={this.handleImport}>
|
||
6 years ago
|
<div className="first-time-flow__create-back">
|
||
6 years ago
|
<a
|
||
5 years ago
|
onClick={(e) => {
|
||
4 years ago
|
e.preventDefault();
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Onboarding',
|
||
|
action: 'Import Seed Phrase',
|
||
|
name: 'Go Back from Onboarding Import',
|
||
|
},
|
||
6 years ago
|
customVariables: {
|
||
|
errorLabel: 'Seed Phrase Error',
|
||
|
errorMessage: seedPhraseError,
|
||
|
},
|
||
4 years ago
|
});
|
||
|
this.props.history.push(INITIALIZE_SELECT_ACTION_ROUTE);
|
||
6 years ago
|
}}
|
||
|
href="#"
|
||
|
>
|
||
|
{`< Back`}
|
||
|
</a>
|
||
|
</div>
|
||
|
<div className="first-time-flow__header">
|
||
4 years ago
|
{t('importAccountSeedPhrase')}
|
||
6 years ago
|
</div>
|
||
4 years ago
|
<div className="first-time-flow__text-block">{t('secretPhrase')}</div>
|
||
6 years ago
|
<div className="first-time-flow__textarea-wrapper">
|
||
4 years ago
|
<label>{t('walletSeed')}</label>
|
||
5 years ago
|
{showSeedPhrase ? (
|
||
|
<textarea
|
||
|
className="first-time-flow__textarea"
|
||
|
onChange={(e) => this.handleSeedPhraseChange(e.target.value)}
|
||
|
value={this.state.seedPhrase}
|
||
|
placeholder={t('seedPhrasePlaceholder')}
|
||
|
/>
|
||
|
) : (
|
||
|
<TextField
|
||
|
className="first-time-flow__textarea first-time-flow__seedphrase"
|
||
|
type="password"
|
||
|
onChange={(e) => this.handleSeedPhraseChange(e.target.value)}
|
||
|
value={this.state.seedPhrase}
|
||
|
placeholder={t('seedPhrasePlaceholderPaste')}
|
||
|
/>
|
||
|
)}
|
||
4 years ago
|
{seedPhraseError && <span className="error">{seedPhraseError}</span>}
|
||
|
<div
|
||
|
className="first-time-flow__checkbox-container"
|
||
|
onClick={this.toggleShowSeedPhrase}
|
||
|
>
|
||
5 years ago
|
<div
|
||
|
className="first-time-flow__checkbox"
|
||
|
tabIndex="0"
|
||
|
role="checkbox"
|
||
|
onKeyPress={this.toggleShowSeedPhrase}
|
||
|
aria-checked={showSeedPhrase}
|
||
|
aria-labelledby="ftf-chk1-label"
|
||
|
>
|
||
|
{showSeedPhrase ? <i className="fa fa-check fa-2x" /> : null}
|
||
|
</div>
|
||
4 years ago
|
<span
|
||
|
id="ftf-chk1-label"
|
||
|
className="first-time-flow__checkbox-label"
|
||
|
>
|
||
|
{t('showSeedPhrase')}
|
||
5 years ago
|
</span>
|
||
|
</div>
|
||
6 years ago
|
</div>
|
||
|
<TextField
|
||
|
id="password"
|
||
|
label={t('newPassword')}
|
||
|
type="password"
|
||
|
className="first-time-flow__input"
|
||
|
value={this.state.password}
|
||
5 years ago
|
onChange={(event) => this.handlePasswordChange(event.target.value)}
|
||
6 years ago
|
error={passwordError}
|
||
|
autoComplete="new-password"
|
||
|
margin="normal"
|
||
|
largeLabel
|
||
|
/>
|
||
|
<TextField
|
||
|
id="confirm-password"
|
||
|
label={t('confirmPassword')}
|
||
|
type="password"
|
||
|
className="first-time-flow__input"
|
||
|
value={this.state.confirmPassword}
|
||
4 years ago
|
onChange={(event) =>
|
||
|
this.handleConfirmPasswordChange(event.target.value)
|
||
|
}
|
||
6 years ago
|
error={confirmPasswordError}
|
||
|
autoComplete="confirm-password"
|
||
|
margin="normal"
|
||
|
largeLabel
|
||
|
/>
|
||
4 years ago
|
<div
|
||
|
className="first-time-flow__checkbox-container"
|
||
|
onClick={this.toggleTermsCheck}
|
||
|
>
|
||
5 years ago
|
<div
|
||
5 years ago
|
className="first-time-flow__checkbox first-time-flow__terms"
|
||
5 years ago
|
tabIndex="0"
|
||
|
role="checkbox"
|
||
|
onKeyPress={this.onTermsKeyPress}
|
||
|
aria-checked={termsChecked}
|
||
|
aria-labelledby="ftf-chk1-label"
|
||
|
>
|
||
6 years ago
|
{termsChecked ? <i className="fa fa-check fa-2x" /> : null}
|
||
|
</div>
|
||
5 years ago
|
<span id="ftf-chk1-label" className="first-time-flow__checkbox-label">
|
||
4 years ago
|
{t('acceptTermsOfUse', [
|
||
5 years ago
|
<a
|
||
|
onClick={(e) => e.stopPropagation()}
|
||
|
key="first-time-flow__link-text"
|
||
|
href="https://metamask.io/terms.html"
|
||
|
target="_blank"
|
||
|
rel="noopener noreferrer"
|
||
|
>
|
||
4 years ago
|
<span className="first-time-flow__link-text">{t('terms')}</span>
|
||
|
</a>,
|
||
|
])}
|
||
6 years ago
|
</span>
|
||
|
</div>
|
||
6 years ago
|
<Button
|
||
6 years ago
|
type="primary"
|
||
5 years ago
|
submit
|
||
6 years ago
|
className="first-time-flow__button"
|
||
6 years ago
|
disabled={!this.isValid() || !termsChecked}
|
||
6 years ago
|
>
|
||
4 years ago
|
{t('import')}
|
||
6 years ago
|
</Button>
|
||
|
</form>
|
||
4 years ago
|
);
|
||
6 years ago
|
}
|
||
|
}
|