2019-11-23 17:22:27 +01:00
|
|
|
import React, { Component } from 'react'
|
2017-01-18 01:22:22 +01:00
|
|
|
const inherits = require('util').inherits
|
2018-02-01 03:08:49 +01:00
|
|
|
const { withRouter } = require('react-router-dom')
|
|
|
|
const { compose } = require('recompose')
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
|
|
|
const connect = require('react-redux').connect
|
2019-03-22 00:03:30 +01:00
|
|
|
const actions = require('../../../store/actions')
|
|
|
|
const { DEFAULT_ROUTE } = require('../../../helpers/constants/routes')
|
|
|
|
const { getMetaMaskAccounts } = require('../../../selectors/selectors')
|
|
|
|
import Button from '../../../components/ui/button'
|
2017-01-18 01:22:22 +01:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
PrivateKeyImportView.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
2019-03-05 16:45:01 +01:00
|
|
|
metricsEvent: PropTypes.func,
|
2018-03-29 17:00:44 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 03:08:49 +01:00
|
|
|
module.exports = compose(
|
|
|
|
withRouter,
|
|
|
|
connect(mapStateToProps, mapDispatchToProps)
|
|
|
|
)(PrivateKeyImportView)
|
2017-01-18 01:22:22 +01:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
|
2017-01-18 01:22:22 +01:00
|
|
|
function mapStateToProps (state) {
|
|
|
|
return {
|
|
|
|
error: state.appState.warning,
|
2018-11-30 23:51:24 +01:00
|
|
|
firstAddress: Object.keys(getMetaMaskAccounts(state))[0],
|
2017-01-18 01:22:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:25:38 +01:00
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
|
|
|
importNewAccount: (strategy, [ privateKey ]) => {
|
2018-02-01 03:08:49 +01:00
|
|
|
return dispatch(actions.importNewAccount(strategy, [ privateKey ]))
|
2018-01-10 19:25:38 +01:00
|
|
|
},
|
2018-05-30 21:13:45 +02:00
|
|
|
displayWarning: (message) => dispatch(actions.displayWarning(message || null)),
|
|
|
|
setSelectedAddress: (address) => dispatch(actions.setSelectedAddress(address)),
|
2018-01-10 19:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 01:22:22 +01:00
|
|
|
inherits(PrivateKeyImportView, Component)
|
|
|
|
function PrivateKeyImportView () {
|
2018-04-05 01:48:12 +02:00
|
|
|
this.createKeyringOnEnter = this.createKeyringOnEnter.bind(this)
|
2017-01-18 01:22:22 +01:00
|
|
|
Component.call(this)
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:22:27 +01:00
|
|
|
PrivateKeyImportView.prototype.render = function PrivateKeyImportView () {
|
2018-05-30 21:13:45 +02:00
|
|
|
const { error, displayWarning } = this.props
|
2017-01-18 01:22:22 +01:00
|
|
|
|
|
|
|
return (
|
2019-11-23 17:22:27 +01:00
|
|
|
<div className="new-account-import-form__private-key">
|
|
|
|
<span className="new-account-create-form__instruction">
|
|
|
|
{this.context.t('pastePrivateKey')}
|
|
|
|
</span>
|
|
|
|
<div className="new-account-import-form__private-key-password-container">
|
|
|
|
<input
|
|
|
|
className="new-account-import-form__input-password"
|
|
|
|
type="password"
|
|
|
|
id="private-key-box"
|
|
|
|
onKeyPress={e => this.createKeyringOnEnter(e)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="new-account-import-form__buttons">
|
|
|
|
<Button
|
|
|
|
type="default"
|
|
|
|
large
|
|
|
|
className="new-account-create-form__button"
|
|
|
|
onClick={() => {
|
2018-05-30 21:13:45 +02:00
|
|
|
displayWarning(null)
|
|
|
|
this.props.history.push(DEFAULT_ROUTE)
|
2019-11-23 17:22:27 +01:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
{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>
|
2017-01-18 01:22:22 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2018-05-30 21:13:45 +02:00
|
|
|
const { importNewAccount, history, displayWarning, setSelectedAddress, firstAddress } = this.props
|
2018-01-10 19:25:38 +01:00
|
|
|
|
2018-02-01 03:08:49 +01:00
|
|
|
importNewAccount('Private Key', [ privateKey ])
|
2018-05-30 21:13:45 +02:00
|
|
|
.then(({ selectedAddress }) => {
|
|
|
|
if (selectedAddress) {
|
2019-03-05 16:45:01 +01:00
|
|
|
this.context.metricsEvent({
|
|
|
|
eventOpts: {
|
|
|
|
category: 'Accounts',
|
|
|
|
action: 'Import Account',
|
|
|
|
name: 'Imported Account with Private Key',
|
|
|
|
},
|
|
|
|
})
|
2018-05-30 21:13:45 +02:00
|
|
|
history.push(DEFAULT_ROUTE)
|
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 with Private Key',
|
|
|
|
},
|
|
|
|
})
|
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-18 01:22:22 +01:00
|
|
|
}
|