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'
|
|
|
|
import { compose } from 'recompose'
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import * as actions from '../../../store/actions'
|
|
|
|
import FileInput from 'react-simple-file-input'
|
|
|
|
import { DEFAULT_ROUTE } from '../../../helpers/constants/routes'
|
|
|
|
import { getMetaMaskAccounts } from '../../../selectors/selectors'
|
2019-03-22 00:03:30 +01:00
|
|
|
import Button from '../../../components/ui/button'
|
2017-06-14 19:13:07 +02:00
|
|
|
|
2019-06-17 13:07:34 +02:00
|
|
|
const HELP_LINK = 'https://metamask.zendesk.com/hc/en-us/articles/360015489351-Importing-Accounts'
|
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
class JsonImportSubview extends Component {
|
2019-12-03 16:30:03 +01:00
|
|
|
state = {
|
|
|
|
fileContents: '',
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
render () {
|
|
|
|
const { error } = this.props
|
|
|
|
|
|
|
|
return (
|
2019-11-23 05:13:48 +01:00
|
|
|
<div className="new-account-import-form__json">
|
|
|
|
<p>{this.context.t('usedByClients')}</p>
|
|
|
|
<a className="warning" href={HELP_LINK} target="_blank">{this.context.t('fileImportFail')}</a>
|
|
|
|
<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)}
|
|
|
|
/>
|
|
|
|
<div className="new-account-create-form__buttons">
|
|
|
|
<Button
|
|
|
|
type="default"
|
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => this.props.history.push(DEFAULT_ROUTE)}
|
|
|
|
>
|
|
|
|
{this.context.t('cancel')}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="secondary"
|
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => this.createNewKeychain()}
|
|
|
|
>
|
|
|
|
{this.context.t('import')}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
{
|
|
|
|
error
|
|
|
|
? <span className="error">{error}</span>
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
</div>
|
2018-03-09 16:30:31 +01:00
|
|
|
)
|
|
|
|
}
|
2018-01-10 19:25:38 +01:00
|
|
|
|
2019-12-03 16:30:03 +01:00
|
|
|
onLoad (event) {
|
|
|
|
this.setState({
|
|
|
|
fileContents: event.target.result,
|
|
|
|
})
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
createKeyringOnEnter (event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault()
|
|
|
|
this.createNewKeychain()
|
|
|
|
}
|
|
|
|
}
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
createNewKeychain () {
|
2018-09-14 02:18:58 +02:00
|
|
|
const { firstAddress, displayWarning, importNewJsonAccount, setSelectedAddress, history } = 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')
|
2018-05-30 21:13:45 +02:00
|
|
|
return displayWarning(message)
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2018-01-30 22:23:59 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
const passwordInput = document.getElementById('json-password-box')
|
|
|
|
const password = passwordInput.value
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-05-30 21:13:45 +02:00
|
|
|
importNewJsonAccount([ fileContents, password ])
|
|
|
|
.then(({ selectedAddress }) => {
|
|
|
|
if (selectedAddress) {
|
|
|
|
history.push(DEFAULT_ROUTE)
|
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)
|
|
|
|
}
|
|
|
|
})
|
2018-06-21 20:23:26 +02:00
|
|
|
.catch(err => err && displayWarning(err.message || err))
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
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,
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
const mapStateToProps = state => {
|
|
|
|
return {
|
|
|
|
error: state.appState.warning,
|
2018-11-30 23:51:24 +01:00
|
|
|
firstAddress: Object.keys(getMetaMaskAccounts(state))[0],
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2018-03-09 16:30:31 +01:00
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
const mapDispatchToProps = dispatch => {
|
|
|
|
return {
|
|
|
|
displayWarning: warning => dispatch(actions.displayWarning(warning)),
|
|
|
|
importNewJsonAccount: options => dispatch(actions.importNewAccount('JSON File', options)),
|
2018-05-30 21:13:45 +02:00
|
|
|
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,
|
|
|
|
connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
)(JsonImportSubview)
|