2021-02-04 19:15:23 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { Switch, Route } from 'react-router-dom';
|
|
|
|
import Unlock from '../unlock-page';
|
2019-01-23 16:25:34 +01:00
|
|
|
import {
|
|
|
|
DEFAULT_ROUTE,
|
|
|
|
INITIALIZE_WELCOME_ROUTE,
|
|
|
|
INITIALIZE_CREATE_PASSWORD_ROUTE,
|
|
|
|
INITIALIZE_SEED_PHRASE_ROUTE,
|
|
|
|
INITIALIZE_UNLOCK_ROUTE,
|
2019-02-27 15:46:41 +01:00
|
|
|
INITIALIZE_SELECT_ACTION_ROUTE,
|
|
|
|
INITIALIZE_END_OF_FLOW_ROUTE,
|
2019-03-05 16:45:01 +01:00
|
|
|
INITIALIZE_METAMETRICS_OPT_IN_ROUTE,
|
2019-08-06 05:24:19 +02:00
|
|
|
INITIALIZE_BACKUP_SEED_PHRASE_ROUTE,
|
2021-05-05 21:26:03 +02:00
|
|
|
INITIALIZE_SEED_PHRASE_INTRO_ROUTE,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../../helpers/constants/routes';
|
|
|
|
import FirstTimeFlowSwitch from './first-time-flow-switch';
|
|
|
|
import Welcome from './welcome';
|
|
|
|
import SelectAction from './select-action';
|
|
|
|
import EndOfFlow from './end-of-flow';
|
|
|
|
import CreatePassword from './create-password';
|
|
|
|
import SeedPhrase from './seed-phrase';
|
|
|
|
import MetaMetricsOptInScreen from './metametrics-opt-in';
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
export default class FirstTimeFlow extends PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
completedOnboarding: PropTypes.bool,
|
|
|
|
createNewAccount: PropTypes.func,
|
|
|
|
createNewAccountFromSeed: PropTypes.func,
|
|
|
|
history: PropTypes.object,
|
|
|
|
isInitialized: PropTypes.bool,
|
|
|
|
isUnlocked: PropTypes.bool,
|
|
|
|
unlockAccount: PropTypes.func,
|
2019-06-27 15:11:16 +02:00
|
|
|
nextRoute: PropTypes.string,
|
2019-08-02 05:57:26 +02:00
|
|
|
showingSeedPhraseBackupAfterOnboarding: PropTypes.bool,
|
|
|
|
seedPhraseBackedUp: PropTypes.bool,
|
|
|
|
verifySeedPhrase: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
state = {
|
|
|
|
seedPhrase: '',
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2019-08-02 05:57:26 +02:00
|
|
|
const {
|
|
|
|
completedOnboarding,
|
|
|
|
history,
|
|
|
|
isInitialized,
|
|
|
|
isUnlocked,
|
|
|
|
showingSeedPhraseBackupAfterOnboarding,
|
|
|
|
seedPhraseBackedUp,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2019-01-23 16:25:34 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
|
|
|
completedOnboarding &&
|
|
|
|
(!showingSeedPhraseBackupAfterOnboarding || seedPhraseBackedUp)
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(DEFAULT_ROUTE);
|
|
|
|
return;
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isInitialized && !isUnlocked) {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(INITIALIZE_UNLOCK_ROUTE);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
handleCreateNewAccount = async (password) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createNewAccount } = this.props;
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const seedPhrase = await createNewAccount(password);
|
|
|
|
this.setState({ seedPhrase });
|
2019-01-23 16:25:34 +01:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(error.message);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
handleImportWithSeedPhrase = async (password, seedPhrase) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { createNewAccountFromSeed } = this.props;
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const vault = await createNewAccountFromSeed(password, seedPhrase);
|
|
|
|
return vault;
|
2019-01-23 16:25:34 +01:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(error.message);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
handleUnlock = async (password) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { unlockAccount, history, nextRoute } = this.props;
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
const seedPhrase = await unlockAccount(password);
|
2019-01-23 16:25:34 +01:00
|
|
|
this.setState({ seedPhrase }, () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
history.push(nextRoute);
|
|
|
|
});
|
2019-01-23 16:25:34 +01:00
|
|
|
} catch (error) {
|
2021-02-04 19:15:23 +01:00
|
|
|
throw new Error(error.message);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2019-01-23 16:25:34 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { seedPhrase } = this.state;
|
|
|
|
const { verifySeedPhrase } = this.props;
|
2019-01-23 16:25:34 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="first-time-flow">
|
|
|
|
<Switch>
|
|
|
|
<Route
|
|
|
|
path={INITIALIZE_SEED_PHRASE_ROUTE}
|
2020-02-15 21:34:12 +01:00
|
|
|
render={(routeProps) => (
|
2019-01-23 16:25:34 +01:00
|
|
|
<SeedPhrase
|
2020-11-03 00:41:28 +01:00
|
|
|
{...routeProps}
|
2019-01-23 16:25:34 +01:00
|
|
|
seedPhrase={seedPhrase}
|
2019-08-02 05:57:26 +02:00
|
|
|
verifySeedPhrase={verifySeedPhrase}
|
2019-01-23 16:25:34 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2019-08-06 05:24:19 +02:00
|
|
|
<Route
|
|
|
|
path={INITIALIZE_BACKUP_SEED_PHRASE_ROUTE}
|
2020-02-15 21:34:12 +01:00
|
|
|
render={(routeProps) => (
|
2019-08-06 05:24:19 +02:00
|
|
|
<SeedPhrase
|
2020-11-03 00:41:28 +01:00
|
|
|
{...routeProps}
|
2019-08-06 05:24:19 +02:00
|
|
|
seedPhrase={seedPhrase}
|
|
|
|
verifySeedPhrase={verifySeedPhrase}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2021-05-05 21:26:03 +02:00
|
|
|
<Route
|
|
|
|
path={INITIALIZE_SEED_PHRASE_INTRO_ROUTE}
|
|
|
|
render={(routeProps) => (
|
|
|
|
<SeedPhrase
|
|
|
|
{...routeProps}
|
|
|
|
seedPhrase={seedPhrase}
|
|
|
|
verifySeedPhrase={verifySeedPhrase}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2019-01-23 16:25:34 +01:00
|
|
|
<Route
|
|
|
|
path={INITIALIZE_CREATE_PASSWORD_ROUTE}
|
2020-02-15 21:34:12 +01:00
|
|
|
render={(routeProps) => (
|
2019-01-23 16:25:34 +01:00
|
|
|
<CreatePassword
|
2020-11-03 00:41:28 +01:00
|
|
|
{...routeProps}
|
2019-01-23 16:25:34 +01:00
|
|
|
onCreateNewAccount={this.handleCreateNewAccount}
|
|
|
|
onCreateNewAccountFromSeed={this.handleImportWithSeedPhrase}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
/>
|
2019-02-27 15:46:41 +01:00
|
|
|
<Route
|
|
|
|
path={INITIALIZE_SELECT_ACTION_ROUTE}
|
|
|
|
component={SelectAction}
|
|
|
|
/>
|
2019-01-23 16:25:34 +01:00
|
|
|
<Route
|
|
|
|
path={INITIALIZE_UNLOCK_ROUTE}
|
2020-02-15 21:34:12 +01:00
|
|
|
render={(routeProps) => (
|
2020-11-03 00:41:28 +01:00
|
|
|
<Unlock {...routeProps} onSubmit={this.handleUnlock} />
|
2019-01-23 16:25:34 +01:00
|
|
|
)}
|
|
|
|
/>
|
2019-02-27 15:46:41 +01:00
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={INITIALIZE_END_OF_FLOW_ROUTE}
|
|
|
|
component={EndOfFlow}
|
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
<Route exact path={INITIALIZE_WELCOME_ROUTE} component={Welcome} />
|
2019-03-05 16:45:01 +01:00
|
|
|
<Route
|
|
|
|
exact
|
|
|
|
path={INITIALIZE_METAMETRICS_OPT_IN_ROUTE}
|
|
|
|
component={MetaMetricsOptInScreen}
|
|
|
|
/>
|
2020-11-03 00:41:28 +01:00
|
|
|
<Route exact path="*" component={FirstTimeFlowSwitch} />
|
2019-01-23 16:25:34 +01:00
|
|
|
</Switch>
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2019-01-23 16:25:34 +01:00
|
|
|
}
|
|
|
|
}
|