2016-11-04 23:08:50 +01:00
|
|
|
const inherits = require('util').inherits
|
|
|
|
const Component = require('react').Component
|
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const connect = require('react-redux').connect
|
2017-01-19 01:09:21 +01:00
|
|
|
const actions = require('../../actions')
|
|
|
|
const FileInput = require('react-simple-file-input').default
|
2018-01-24 21:10:28 +01:00
|
|
|
const t = require('../../../i18n')
|
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
|
|
|
|
2016-11-04 23:08:50 +01:00
|
|
|
module.exports = connect(mapStateToProps)(JsonImportSubview)
|
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
2017-01-19 01:09:21 +01:00
|
|
|
return {
|
|
|
|
error: state.appState.warning,
|
|
|
|
}
|
2016-11-04 23:08:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
inherits(JsonImportSubview, Component)
|
|
|
|
function JsonImportSubview () {
|
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonImportSubview.prototype.render = function () {
|
2017-01-19 01:09:21 +01:00
|
|
|
const { error } = this.props
|
|
|
|
|
2016-11-04 23:08:50 +01:00
|
|
|
return (
|
2018-01-10 19:25:38 +01:00
|
|
|
h('div.new-account-import-form__json', [
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2018-01-24 09:14:47 +01:00
|
|
|
h('p', t('usedByClients')),
|
|
|
|
h('a.warning', { href: HELP_LINK, target: '_blank' }, t('fileImportFail')),
|
2017-01-19 01:09:21 +01:00
|
|
|
|
|
|
|
h(FileInput, {
|
|
|
|
readAs: 'text',
|
|
|
|
onLoad: this.onLoad.bind(this),
|
|
|
|
style: {
|
2018-01-10 19:25:38 +01:00
|
|
|
margin: '20px 0px 12px 34%',
|
2017-01-19 01:09:21 +01:00
|
|
|
fontSize: '15px',
|
2018-01-10 19:25:38 +01:00
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'center',
|
2017-01-19 01:09:21 +01:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
h('input.new-account-import-form__input-password', {
|
2017-01-19 01:09:21 +01:00
|
|
|
type: 'password',
|
2018-01-24 09:14:47 +01:00
|
|
|
placeholder: t('enterPassword'),
|
2017-01-19 01:09:21 +01:00
|
|
|
id: 'json-password-box',
|
|
|
|
onKeyPress: this.createKeyringOnEnter.bind(this),
|
|
|
|
}),
|
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
h('div.new-account-create-form__buttons', {}, [
|
|
|
|
|
2018-01-29 21:29:01 +01:00
|
|
|
h('button.new-account-create-form__button-cancel.allcaps', {
|
2018-01-10 19:25:38 +01:00
|
|
|
onClick: () => this.props.goHome(),
|
|
|
|
}, [
|
2018-01-29 21:29:01 +01:00
|
|
|
t('cancel'),
|
2018-01-10 19:25:38 +01:00
|
|
|
]),
|
|
|
|
|
2018-01-29 21:29:01 +01:00
|
|
|
h('button.new-account-create-form__button-create.allcaps', {
|
2018-01-10 19:25:38 +01:00
|
|
|
onClick: () => this.createNewKeychain.bind(this),
|
|
|
|
}, [
|
2018-01-29 21:29:01 +01:00
|
|
|
t('import'),
|
2018-01-10 19:25:38 +01:00
|
|
|
]),
|
|
|
|
|
|
|
|
]),
|
2017-01-19 01:09:21 +01:00
|
|
|
|
2017-03-22 21:54:10 +01:00
|
|
|
error ? h('span.error', error) : null,
|
2016-11-04 23:08:50 +01:00
|
|
|
])
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-01-19 01:09:21 +01:00
|
|
|
JsonImportSubview.prototype.onLoad = function (event, file) {
|
|
|
|
this.setState({file: file, fileContents: event.target.result})
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonImportSubview.prototype.createKeyringOnEnter = function (event) {
|
|
|
|
if (event.key === 'Enter') {
|
|
|
|
event.preventDefault()
|
|
|
|
this.createNewKeychain()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonImportSubview.prototype.createNewKeychain = function () {
|
|
|
|
const state = this.state
|
2018-01-30 22:23:59 +01:00
|
|
|
|
|
|
|
if (!state) {
|
|
|
|
const message = 'You must select a valid file to import.'
|
|
|
|
return this.props.dispatch(actions.displayWarning(message))
|
|
|
|
}
|
|
|
|
|
2017-01-19 01:09:21 +01:00
|
|
|
const { fileContents } = state
|
|
|
|
|
|
|
|
if (!fileContents) {
|
2018-01-24 09:14:47 +01:00
|
|
|
const message = t('needImportFile')
|
2017-01-19 01:09:21 +01:00
|
|
|
return this.props.dispatch(actions.displayWarning(message))
|
|
|
|
}
|
|
|
|
|
|
|
|
const passwordInput = document.getElementById('json-password-box')
|
|
|
|
const password = passwordInput.value
|
|
|
|
|
|
|
|
if (!password) {
|
2018-01-24 09:14:47 +01:00
|
|
|
const message = t('needImportPassword')
|
2017-01-19 01:09:21 +01:00
|
|
|
return this.props.dispatch(actions.displayWarning(message))
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.dispatch(actions.importNewAccount('JSON File', [ fileContents, password ]))
|
|
|
|
}
|