You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.7 KiB
129 lines
3.7 KiB
8 years ago
|
const inherits = require('util').inherits
|
||
|
const Component = require('react').Component
|
||
|
const h = require('react-hyperscript')
|
||
7 years ago
|
const { withRouter } = require('react-router-dom')
|
||
|
const { compose } = require('recompose')
|
||
7 years ago
|
const PropTypes = require('prop-types')
|
||
|
const connect = require('react-redux').connect
|
||
6 years ago
|
const actions = require('../../../store/actions')
|
||
|
const { DEFAULT_ROUTE } = require('../../../helpers/constants/routes')
|
||
|
const { getMetaMaskAccounts } = require('../../../selectors/selectors')
|
||
|
import Button from '../../../components/ui/button'
|
||
8 years ago
|
|
||
7 years ago
|
PrivateKeyImportView.contextTypes = {
|
||
|
t: PropTypes.func,
|
||
6 years ago
|
metricsEvent: PropTypes.func,
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
module.exports = compose(
|
||
|
withRouter,
|
||
|
connect(mapStateToProps, mapDispatchToProps)
|
||
|
)(PrivateKeyImportView)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
function mapStateToProps (state) {
|
||
|
return {
|
||
|
error: state.appState.warning,
|
||
6 years ago
|
firstAddress: Object.keys(getMetaMaskAccounts(state))[0],
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
function mapDispatchToProps (dispatch) {
|
||
|
return {
|
||
|
importNewAccount: (strategy, [ privateKey ]) => {
|
||
7 years ago
|
return dispatch(actions.importNewAccount(strategy, [ privateKey ]))
|
||
7 years ago
|
},
|
||
7 years ago
|
displayWarning: (message) => dispatch(actions.displayWarning(message || null)),
|
||
|
setSelectedAddress: (address) => dispatch(actions.setSelectedAddress(address)),
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
inherits(PrivateKeyImportView, Component)
|
||
|
function PrivateKeyImportView () {
|
||
7 years ago
|
this.createKeyringOnEnter = this.createKeyringOnEnter.bind(this)
|
||
8 years ago
|
Component.call(this)
|
||
|
}
|
||
|
|
||
|
PrivateKeyImportView.prototype.render = function () {
|
||
7 years ago
|
const { error, displayWarning } = this.props
|
||
8 years ago
|
|
||
|
return (
|
||
7 years ago
|
h('div.new-account-import-form__private-key', [
|
||
8 years ago
|
|
||
7 years ago
|
h('span.new-account-create-form__instruction', this.context.t('pastePrivateKey')),
|
||
8 years ago
|
|
||
7 years ago
|
h('div.new-account-import-form__private-key-password-container', [
|
||
|
|
||
|
h('input.new-account-import-form__input-password', {
|
||
|
type: 'password',
|
||
|
id: 'private-key-box',
|
||
7 years ago
|
onKeyPress: e => this.createKeyringOnEnter(e),
|
||
7 years ago
|
}),
|
||
|
|
||
|
]),
|
||
|
|
||
|
h('div.new-account-import-form__buttons', {}, [
|
||
7 years ago
|
|
||
6 years ago
|
h(Button, {
|
||
|
type: 'default',
|
||
|
large: true,
|
||
|
className: 'new-account-create-form__button',
|
||
7 years ago
|
onClick: () => {
|
||
|
displayWarning(null)
|
||
|
this.props.history.push(DEFAULT_ROUTE)
|
||
|
},
|
||
6 years ago
|
}, [this.context.t('cancel')]),
|
||
7 years ago
|
|
||
6 years ago
|
h(Button, {
|
||
|
type: 'primary',
|
||
|
large: true,
|
||
|
className: 'new-account-create-form__button',
|
||
7 years ago
|
onClick: () => this.createNewKeychain(),
|
||
6 years ago
|
}, [this.context.t('import')]),
|
||
7 years ago
|
|
||
|
]),
|
||
8 years ago
|
|
||
8 years ago
|
error ? h('span.error', error) : null,
|
||
8 years ago
|
])
|
||
|
)
|
||
|
}
|
||
|
|
||
|
PrivateKeyImportView.prototype.createKeyringOnEnter = function (event) {
|
||
|
if (event.key === 'Enter') {
|
||
|
event.preventDefault()
|
||
|
this.createNewKeychain()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
PrivateKeyImportView.prototype.createNewKeychain = function () {
|
||
|
const input = document.getElementById('private-key-box')
|
||
|
const privateKey = input.value
|
||
7 years ago
|
const { importNewAccount, history, displayWarning, setSelectedAddress, firstAddress } = this.props
|
||
7 years ago
|
|
||
7 years ago
|
importNewAccount('Private Key', [ privateKey ])
|
||
7 years ago
|
.then(({ selectedAddress }) => {
|
||
|
if (selectedAddress) {
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Import Account',
|
||
|
name: 'Imported Account with Private Key',
|
||
|
},
|
||
|
})
|
||
7 years ago
|
history.push(DEFAULT_ROUTE)
|
||
7 years ago
|
displayWarning(null)
|
||
7 years ago
|
} else {
|
||
|
displayWarning('Error importing account.')
|
||
6 years ago
|
this.context.metricsEvent({
|
||
|
eventOpts: {
|
||
|
category: 'Accounts',
|
||
|
action: 'Import Account',
|
||
|
name: 'Error importing with Private Key',
|
||
|
},
|
||
|
})
|
||
7 years ago
|
setSelectedAddress(firstAddress)
|
||
|
}
|
||
|
})
|
||
7 years ago
|
.catch(err => err && displayWarning(err.message || err))
|
||
8 years ago
|
}
|