2021-02-04 19:15:23 +01:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { withRouter } from 'react-router-dom';
|
|
|
|
import { compose } from 'redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import * as actions from '../../../store/actions';
|
|
|
|
import { getMetaMaskAccounts } from '../../../selectors';
|
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import { getMostRecentOverviewPage } from '../../../ducks/history/history';
|
2017-01-18 01:22:22 +01:00
|
|
|
|
2020-01-13 16:12:53 +01:00
|
|
|
class PrivateKeyImportView extends Component {
|
|
|
|
static contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2022-03-29 15:46:24 +02:00
|
|
|
trackEvent: PropTypes.func,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-13 16:12:53 +01:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
importNewAccount: PropTypes.func.isRequired,
|
|
|
|
history: PropTypes.object.isRequired,
|
|
|
|
displayWarning: PropTypes.func.isRequired,
|
|
|
|
setSelectedAddress: PropTypes.func.isRequired,
|
|
|
|
firstAddress: PropTypes.string.isRequired,
|
|
|
|
error: PropTypes.node,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-13 16:12:53 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
inputRef = React.createRef();
|
2020-01-28 14:40:03 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
state = { isEmpty: true };
|
2020-01-13 16:12:53 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
createNewKeychain() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const privateKey = this.inputRef.current.value;
|
2020-11-03 00:41:28 +01:00
|
|
|
const {
|
|
|
|
importNewAccount,
|
|
|
|
history,
|
|
|
|
displayWarning,
|
|
|
|
mostRecentOverviewPage,
|
|
|
|
setSelectedAddress,
|
|
|
|
firstAddress,
|
2021-02-04 19:15:23 +01:00
|
|
|
} = this.props;
|
2021-08-25 18:14:42 +02:00
|
|
|
const { t } = this.context;
|
2020-01-13 16:12:53 +01:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
importNewAccount('Private Key', [privateKey])
|
2020-01-13 16:12:53 +01:00
|
|
|
.then(({ selectedAddress }) => {
|
|
|
|
if (selectedAddress) {
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent({
|
|
|
|
category: 'Accounts',
|
|
|
|
event: 'Imported Account with Private Key',
|
|
|
|
properties: {
|
2020-01-13 16:12:53 +01:00
|
|
|
action: 'Import Account',
|
2022-03-29 15:46:24 +02:00
|
|
|
legacy_event: true,
|
2020-01-13 16:12:53 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
history.push(mostRecentOverviewPage);
|
|
|
|
displayWarning(null);
|
2020-01-13 16:12:53 +01:00
|
|
|
} else {
|
2021-08-25 18:14:42 +02:00
|
|
|
displayWarning(t('importAccountError'));
|
2022-03-29 15:46:24 +02:00
|
|
|
this.context.trackEvent({
|
|
|
|
category: 'Accounts',
|
|
|
|
event: 'Error importing with Private Key',
|
|
|
|
properties: {
|
2020-01-13 16:12:53 +01:00
|
|
|
action: 'Import Account',
|
2022-03-29 15:46:24 +02:00
|
|
|
legacy_event: true,
|
2020-01-13 16:12:53 +01:00
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
setSelectedAddress(firstAddress);
|
2020-01-13 16:12:53 +01:00
|
|
|
}
|
|
|
|
})
|
2021-02-04 19:15:23 +01:00
|
|
|
.catch((err) => err && displayWarning(err.message || err));
|
2020-01-13 16:12:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createKeyringOnEnter = (event) => {
|
|
|
|
if (event.key === 'Enter') {
|
2021-02-04 19:15:23 +01:00
|
|
|
event.preventDefault();
|
|
|
|
this.createNewKeychain();
|
2020-01-13 16:12:53 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-13 16:12:53 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
checkInputEmpty() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const privateKey = this.inputRef.current.value;
|
|
|
|
let isEmpty = true;
|
2020-01-28 14:40:03 +01:00
|
|
|
if (privateKey !== '') {
|
2021-02-04 19:15:23 +01:00
|
|
|
isEmpty = false;
|
2020-01-28 14:40:03 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
this.setState({ isEmpty });
|
2020-01-28 14:40:03 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { error, displayWarning } = this.props;
|
2020-01-13 16:12:53 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="new-account-import-form__private-key">
|
2022-03-24 15:44:40 +01:00
|
|
|
<span className="new-account-import-form__instruction">
|
2020-01-13 16:12:53 +01:00
|
|
|
{this.context.t('pastePrivateKey')}
|
|
|
|
</span>
|
|
|
|
<div className="new-account-import-form__private-key-password-container">
|
|
|
|
<input
|
|
|
|
className="new-account-import-form__input-password"
|
|
|
|
type="password"
|
|
|
|
id="private-key-box"
|
2020-02-15 21:34:12 +01:00
|
|
|
onKeyPress={(e) => this.createKeyringOnEnter(e)}
|
2020-01-28 14:40:03 +01:00
|
|
|
onChange={() => this.checkInputEmpty()}
|
|
|
|
ref={this.inputRef}
|
2020-10-14 18:43:26 +02:00
|
|
|
autoFocus
|
2020-01-13 16:12:53 +01:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="new-account-import-form__buttons">
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="secondary"
|
2020-01-13 16:12:53 +01:00
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { history, mostRecentOverviewPage } = this.props;
|
|
|
|
displayWarning(null);
|
|
|
|
history.push(mostRecentOverviewPage);
|
2020-01-13 16:12:53 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{this.context.t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
2021-10-05 21:20:42 +02:00
|
|
|
type="primary"
|
2020-01-13 16:12:53 +01:00
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => this.createNewKeychain()}
|
2020-01-28 14:40:03 +01:00
|
|
|
disabled={this.state.isEmpty}
|
2020-01-13 16:12:53 +01:00
|
|
|
>
|
|
|
|
{this.context.t('import')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{error ? <span className="error">{error}</span> : null}
|
2020-01-13 16:12:53 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-13 16:12:53 +01:00
|
|
|
}
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default compose(
|
2018-02-01 03:08:49 +01:00
|
|
|
withRouter,
|
2020-07-14 17:20:41 +02:00
|
|
|
connect(mapStateToProps, mapDispatchToProps),
|
2021-02-04 19:15:23 +01:00
|
|
|
)(PrivateKeyImportView);
|
2017-01-18 01:22:22 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function mapStateToProps(state) {
|
2017-01-18 01:22:22 +01:00
|
|
|
return {
|
|
|
|
error: state.appState.warning,
|
2018-11-30 23:51:24 +01:00
|
|
|
firstAddress: Object.keys(getMetaMaskAccounts(state))[0],
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: getMostRecentOverviewPage(state),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-01-18 01:22:22 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function mapDispatchToProps(dispatch) {
|
2018-01-10 19:25:38 +01:00
|
|
|
return {
|
2020-08-19 18:27:05 +02:00
|
|
|
importNewAccount: (strategy, [privateKey]) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return dispatch(actions.importNewAccount(strategy, [privateKey]));
|
2018-01-10 19:25:38 +01:00
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
displayWarning: (message) =>
|
|
|
|
dispatch(actions.displayWarning(message || null)),
|
|
|
|
setSelectedAddress: (address) =>
|
|
|
|
dispatch(actions.setSelectedAddress(address)),
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|