2016-11-04 23:08:50 +01:00
|
|
|
const Component = require('react').Component
|
2018-03-09 16:30:31 +01:00
|
|
|
const PropTypes = require('prop-types')
|
2016-11-04 23:08:50 +01:00
|
|
|
const h = require('react-hyperscript')
|
2018-03-16 01:29:45 +01:00
|
|
|
const connect = require('../../metamask-connect')
|
2017-01-19 01:09:21 +01:00
|
|
|
const actions = require('../../actions')
|
|
|
|
const FileInput = require('react-simple-file-input').default
|
2018-03-16 01:29:45 +01:00
|
|
|
const t = require('../../../i18n-helper').getMessage
|
2018-03-15 00:31:45 +01:00
|
|
|
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2017-10-25 09:38:20 +02:00
|
|
|
const HELP_LINK = 'https://support.metamask.io/kb/article/7-importing-accounts'
|
2017-06-14 19:13:07 +02:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
class JsonImportSubview extends Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
this.state = {
|
|
|
|
file: null,
|
|
|
|
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 (
|
|
|
|
h('div.new-account-import-form__json', [
|
|
|
|
|
2018-03-16 01:29:45 +01:00
|
|
|
h('p', t(this.props.localeMessages, 'usedByClients')),
|
2018-03-09 16:30:31 +01:00
|
|
|
h('a.warning', {
|
|
|
|
href: HELP_LINK,
|
|
|
|
target: '_blank',
|
2018-03-16 01:29:45 +01:00
|
|
|
}, t(this.props.localeMessages, 'fileImportFail')),
|
2018-03-09 16:30:31 +01:00
|
|
|
|
|
|
|
h(FileInput, {
|
|
|
|
readAs: 'text',
|
|
|
|
onLoad: this.onLoad.bind(this),
|
|
|
|
style: {
|
|
|
|
margin: '20px 0px 12px 34%',
|
|
|
|
fontSize: '15px',
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
|
|
|
h('input.new-account-import-form__input-password', {
|
|
|
|
type: 'password',
|
2018-03-16 01:29:45 +01:00
|
|
|
placeholder: t(this.props.localeMessages, 'enterPassword'),
|
2018-03-09 16:30:31 +01:00
|
|
|
id: 'json-password-box',
|
|
|
|
onKeyPress: this.createKeyringOnEnter.bind(this),
|
|
|
|
}),
|
|
|
|
|
|
|
|
h('div.new-account-create-form__buttons', {}, [
|
|
|
|
|
|
|
|
h('button.new-account-create-form__button-cancel', {
|
|
|
|
onClick: () => this.props.goHome(),
|
|
|
|
}, [
|
2018-03-16 01:29:45 +01:00
|
|
|
t(this.props.localeMessages, 'cancel'),
|
2018-03-09 16:30:31 +01:00
|
|
|
]),
|
|
|
|
|
|
|
|
h('button.new-account-create-form__button-create', {
|
|
|
|
onClick: () => this.createNewKeychain(),
|
|
|
|
}, [
|
2018-03-16 01:29:45 +01:00
|
|
|
t(this.props.localeMessages, 'import'),
|
2018-03-09 16:30:31 +01:00
|
|
|
]),
|
2016-11-04 23:08:50 +01:00
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
]),
|
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
error ? h('span.error', error) : null,
|
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
2018-01-10 19:25:38 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
onLoad (event, file) {
|
|
|
|
this.setState({file: file, fileContents: event.target.result})
|
|
|
|
}
|
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 () {
|
|
|
|
const state = this.state
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
if (!state) {
|
|
|
|
const message = 'You must select a valid file to import.'
|
|
|
|
return this.props.displayWarning(message)
|
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
const { fileContents } = state
|
2018-01-30 22:23:59 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
if (!fileContents) {
|
2018-03-16 01:29:45 +01:00
|
|
|
const message = t(this.props.localeMessages, 'needImportFile')
|
2018-03-09 16:30:31 +01:00
|
|
|
return this.props.displayWarning(message)
|
|
|
|
}
|
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-03-09 16:30:31 +01:00
|
|
|
if (!password) {
|
2018-03-16 01:29:45 +01:00
|
|
|
const message = t(this.props.localeMessages, 'needImportPassword')
|
2018-03-09 16:30:31 +01:00
|
|
|
return this.props.displayWarning(message)
|
|
|
|
}
|
2018-03-15 00:31:45 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
this.props.importNewJsonAccount([ fileContents, password ])
|
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,
|
|
|
|
goHome: PropTypes.func,
|
|
|
|
displayWarning: PropTypes.func,
|
|
|
|
importNewJsonAccount: PropTypes.func,
|
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-03-09 16:30:31 +01:00
|
|
|
const mapStateToProps = state => {
|
|
|
|
return {
|
|
|
|
error: state.appState.warning,
|
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 {
|
|
|
|
goHome: () => dispatch(actions.goHome()),
|
|
|
|
displayWarning: warning => dispatch(actions.displayWarning(warning)),
|
|
|
|
importNewJsonAccount: options => dispatch(actions.importNewAccount('JSON File', options)),
|
|
|
|
}
|
2017-01-19 01:09:21 +01:00
|
|
|
}
|
2018-03-09 16:30:31 +01:00
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(JsonImportSubview)
|