2019-11-23 05:13:48 +01:00
|
|
|
import React, { Component } from 'react'
|
2020-01-09 04:34:58 +01:00
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { withRouter } from 'react-router-dom'
|
2020-02-24 23:58:26 +01:00
|
|
|
import { compose } from 'redux'
|
2020-01-09 04:34:58 +01:00
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import FileInput from 'react-simple-file-input'
|
2020-08-18 21:18:25 +02:00
|
|
|
import * as actions from '../../../store/actions'
|
2020-05-02 21:41:17 +02:00
|
|
|
import { getMetaMaskAccounts } from '../../../selectors'
|
2019-03-22 00:03:30 +01:00
|
|
|
import Button from '../../../components/ui/button'
|
2020-06-01 19:54:32 +02:00
|
|
|
import { getMostRecentOverviewPage } from '../../../ducks/history/history'
|
2017-06-14 19:13:07 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const HELP_LINK =
|
|
|
|
'https://metamask.zendesk.com/hc/en-us/articles/360015489331-Importing-an-Account'
|
2019-06-17 13:07:34 +02:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
class JsonImportSubview extends Component {
|
2019-12-03 16:30:03 +01:00
|
|
|
state = {
|
|
|
|
fileContents: '',
|
2020-01-28 14:40:03 +01:00
|
|
|
isEmpty: true,
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2020-01-28 14:40:03 +01:00
|
|
|
inputRef = React.createRef()
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2020-06-01 19:54:32 +02:00
|
|
|
const { error, history, mostRecentOverviewPage } = this.props
|
2020-01-28 14:40:03 +01:00
|
|
|
const enabled = !this.state.isEmpty && this.state.fileContents !== ''
|
2018-03-09 16:30:31 +01:00
|
|
|
|
|
|
|
return (
|
2019-11-23 05:13:48 +01:00
|
|
|
<div className="new-account-import-form__json">
|
|
|
|
<p>{this.context.t('usedByClients')}</p>
|
2020-11-03 00:41:28 +01:00
|
|
|
<a
|
|
|
|
className="warning"
|
|
|
|
href={HELP_LINK}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{this.context.t('fileImportFail')}
|
|
|
|
</a>
|
2019-11-23 05:13:48 +01:00
|
|
|
<FileInput
|
|
|
|
readAs="text"
|
|
|
|
onLoad={this.onLoad.bind(this)}
|
|
|
|
style={{
|
2019-06-17 13:07:34 +02:00
|
|
|
padding: '20px 0px 12px 15%',
|
2018-03-09 16:30:31 +01:00
|
|
|
fontSize: '15px',
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
2019-06-17 13:07:34 +02:00
|
|
|
width: '100%',
|
2019-11-23 05:13:48 +01:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
<input
|
|
|
|
className="new-account-import-form__input-password"
|
|
|
|
type="password"
|
|
|
|
placeholder={this.context.t('enterPassword')}
|
|
|
|
id="json-password-box"
|
|
|
|
onKeyPress={this.createKeyringOnEnter.bind(this)}
|
2020-01-28 14:40:03 +01:00
|
|
|
onChange={() => this.checkInputEmpty()}
|
|
|
|
ref={this.inputRef}
|
2019-11-23 05:13:48 +01:00
|
|
|
/>
|
|
|
|
<div className="new-account-create-form__buttons">
|
|
|
|
<Button
|
|
|
|
type="default"
|
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
2020-06-01 19:54:32 +02:00
|
|
|
onClick={() => history.push(mostRecentOverviewPage)}
|
2019-11-23 05:13:48 +01:00
|
|
|
>
|
|
|
|
{this.context.t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="secondary"
|
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => this.createNewKeychain()}
|
2020-01-28 14:40:03 +01:00
|
|
|
disabled={!enabled}
|
2019-11-23 05:13:48 +01:00
|
|
|
>
|
|
|
|
{this.context.t('import')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
2020-11-03 00:41:28 +01:00
|
|
|
{error ? <span className="error">{error}</span> : null}
|
2019-11-23 05:13:48 +01:00
|
|
|
</div>
|
2018-03-09 16:30:31 +01:00
|
|
|
)
|
|
|
|
}
|
2018-01-10 19:25:38 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
onLoad(event) {
|
2019-12-03 16:30:03 +01:00
|
|
|
this.setState({
|
|
|
|
fileContents: event.target.result,
|
|
|
|
})
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
createKeyringOnEnter(event) {
|
2018-03-09 16:30:31 +01:00
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault()
|
|
|
|
this.createNewKeychain()
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
createNewKeychain() {
|
2020-06-01 19:54:32 +02:00
|
|
|
const {
|
|
|
|
firstAddress,
|
|
|
|
displayWarning,
|
|
|
|
history,
|
|
|
|
importNewJsonAccount,
|
|
|
|
mostRecentOverviewPage,
|
|
|
|
setSelectedAddress,
|
|
|
|
} = this.props
|
2019-12-03 16:30:03 +01:00
|
|
|
const { fileContents } = this.state
|
2018-01-30 22:23:59 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
if (!fileContents) {
|
2018-03-29 17:00:44 +02:00
|
|
|
const message = this.context.t('needImportFile')
|
2020-08-12 21:06:57 +02:00
|
|
|
displayWarning(message)
|
|
|
|
return
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2018-01-30 22:23:59 +01:00
|
|
|
|
2020-01-28 14:40:03 +01:00
|
|
|
const password = this.inputRef.current.value
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
importNewJsonAccount([fileContents, password])
|
2018-05-30 21:13:45 +02:00
|
|
|
.then(({ selectedAddress }) => {
|
|
|
|
if (selectedAddress) {
|
2020-06-01 19:54:32 +02:00
|
|
|
history.push(mostRecentOverviewPage)
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Accounts',
|
|
|
|
action: 'Import Account',
|
|
|
|
name: 'Imported Account with JSON',
|
|
|
|
},
|
|
|
|
})
|
2018-07-03 18:47:30 +02:00
|
|
|
displayWarning(null)
|
2018-05-30 21:13:45 +02:00
|
|
|
} else {
|
|
|
|
displayWarning('Error importing account.')
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Accounts',
|
|
|
|
action: 'Import Account',
|
|
|
|
name: 'Error importing JSON',
|
|
|
|
},
|
|
|
|
})
|
2018-05-30 21:13:45 +02:00
|
|
|
setSelectedAddress(firstAddress)
|
|
|
|
}
|
|
|
|
})
|
2020-02-15 21:34:12 +01:00
|
|
|
.catch((err) => err && displayWarning(err.message || err))
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2020-01-28 14:40:03 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
checkInputEmpty() {
|
2020-01-28 14:40:03 +01:00
|
|
|
const password = this.inputRef.current.value
|
|
|
|
let isEmpty = true
|
|
|
|
if (password !== '') {
|
|
|
|
isEmpty = false
|
|
|
|
}
|
|
|
|
this.setState({ isEmpty })
|
|
|
|
}
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
JsonImportSubview.propTypes = {
|
|
|
|
error: PropTypes.string,
|
|
|
|
displayWarning: PropTypes.func,
|
2018-05-30 21:13:45 +02:00
|
|
|
firstAddress: PropTypes.string,
|
2018-03-09 16:30:31 +01:00
|
|
|
importNewJsonAccount: PropTypes.func,
|
2018-03-27 09:20:35 +02:00
|
|
|
history: PropTypes.object,
|
2018-05-30 21:13:45 +02:00
|
|
|
setSelectedAddress: PropTypes.func,
|
2020-06-01 19:54:32 +02:00
|
|
|
mostRecentOverviewPage: PropTypes.string.isRequired,
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapStateToProps = (state) => {
|
2018-03-09 16:30:31 +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),
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2020-02-15 21:34:12 +01:00
|
|
|
const mapDispatchToProps = (dispatch) => {
|
2018-03-09 16:30:31 +01:00
|
|
|
return {
|
2020-02-15 21:34:12 +01:00
|
|
|
displayWarning: (warning) => dispatch(actions.displayWarning(warning)),
|
2020-11-03 00:41:28 +01:00
|
|
|
importNewJsonAccount: (options) =>
|
|
|
|
dispatch(actions.importNewAccount('JSON File', options)),
|
|
|
|
setSelectedAddress: (address) =>
|
|
|
|
dispatch(actions.setSelectedAddress(address)),
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2018-03-09 16:30:31 +01:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
JsonImportSubview.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
export default compose(
|
2018-03-27 09:20:35 +02:00
|
|
|
withRouter,
|
2020-07-14 17:20:41 +02:00
|
|
|
connect(mapStateToProps, mapDispatchToProps),
|
2018-03-27 09:20:35 +02:00
|
|
|
)(JsonImportSubview)
|