2017-09-20 10:05:27 +02:00
|
|
|
const Component = require('react').Component
|
2018-03-29 17:00:44 +02:00
|
|
|
const PropTypes = require('prop-types')
|
2017-09-20 10:05:27 +02:00
|
|
|
const h = require('react-hyperscript')
|
|
|
|
const inherits = require('util').inherits
|
2018-03-29 17:00:44 +02:00
|
|
|
const connect = require('react-redux').connect
|
2018-04-17 21:57:55 +02:00
|
|
|
const { stripHexPrefix } = require('ethereumjs-util')
|
2017-09-20 10:05:27 +02:00
|
|
|
const actions = require('../../actions')
|
|
|
|
const AccountModalContainer = require('./account-modal-container')
|
|
|
|
const { getSelectedIdentity } = require('../../selectors')
|
|
|
|
const ReadOnlyInput = require('../readonly-input')
|
2017-10-13 22:23:10 +02:00
|
|
|
const copyToClipboard = require('copy-to-clipboard')
|
2018-04-17 21:57:55 +02:00
|
|
|
const { checksumAddress } = require('../../util')
|
2017-09-20 10:05:27 +02:00
|
|
|
|
|
|
|
function mapStateToProps (state) {
|
|
|
|
return {
|
2017-09-20 14:32:18 +02:00
|
|
|
warning: state.appState.warning,
|
|
|
|
privateKey: state.appState.accountDetail.privateKey,
|
2017-09-20 10:05:27 +02:00
|
|
|
network: state.metamask.network,
|
|
|
|
selectedIdentity: getSelectedIdentity(state),
|
2017-09-22 21:57:18 +02:00
|
|
|
previousModalState: state.appState.modal.previousModalState.name,
|
2017-09-20 10:05:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:32:18 +02:00
|
|
|
function mapDispatchToProps (dispatch) {
|
|
|
|
return {
|
|
|
|
exportAccount: (password, address) => dispatch(actions.exportAccount(password, address)),
|
2017-09-22 21:57:18 +02:00
|
|
|
showAccountDetailModal: () => dispatch(actions.showModal({ name: 'ACCOUNT_DETAILS' })),
|
2017-09-20 14:32:18 +02:00
|
|
|
hideModal: () => dispatch(actions.hideModal()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-20 10:05:27 +02:00
|
|
|
inherits(ExportPrivateKeyModal, Component)
|
|
|
|
function ExportPrivateKeyModal () {
|
|
|
|
Component.call(this)
|
2017-09-20 14:32:18 +02:00
|
|
|
|
|
|
|
this.state = {
|
2017-09-27 01:03:33 +02:00
|
|
|
password: '',
|
|
|
|
privateKey: null,
|
2017-09-20 14:32:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
ExportPrivateKeyModal.contextTypes = {
|
|
|
|
t: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:32:18 +02:00
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(ExportPrivateKeyModal)
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
|
2017-09-27 01:03:33 +02:00
|
|
|
ExportPrivateKeyModal.prototype.exportAccountAndGetPrivateKey = function (password, address) {
|
|
|
|
const { exportAccount } = this.props
|
|
|
|
|
|
|
|
exportAccount(password, address)
|
|
|
|
.then(privateKey => this.setState({ privateKey }))
|
|
|
|
}
|
|
|
|
|
2017-09-20 14:32:18 +02:00
|
|
|
ExportPrivateKeyModal.prototype.renderPasswordLabel = function (privateKey) {
|
|
|
|
return h('span.private-key-password-label', privateKey
|
2018-03-29 17:00:44 +02:00
|
|
|
? this.context.t('copyPrivateKey')
|
|
|
|
: this.context.t('typePassword')
|
2017-09-20 14:32:18 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportPrivateKeyModal.prototype.renderPasswordInput = function (privateKey) {
|
2018-04-17 21:57:55 +02:00
|
|
|
const plainKey = privateKey && stripHexPrefix(privateKey)
|
2017-09-20 14:32:18 +02:00
|
|
|
|
|
|
|
return privateKey
|
|
|
|
? h(ReadOnlyInput, {
|
|
|
|
wrapperClass: 'private-key-password-display-wrapper',
|
|
|
|
inputClass: 'private-key-password-display-textarea',
|
|
|
|
textarea: true,
|
|
|
|
value: plainKey,
|
2017-10-13 22:23:10 +02:00
|
|
|
onClick: () => copyToClipboard(plainKey),
|
2017-09-20 14:32:18 +02:00
|
|
|
})
|
|
|
|
: h('input.private-key-password-input', {
|
|
|
|
type: 'password',
|
2017-10-13 22:23:10 +02:00
|
|
|
onChange: event => this.setState({ password: event.target.value }),
|
2017-09-20 14:32:18 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
ExportPrivateKeyModal.prototype.renderButton = function (className, onClick, label) {
|
|
|
|
return h('button', {
|
|
|
|
className,
|
|
|
|
onClick,
|
|
|
|
}, label)
|
2017-09-20 10:05:27 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 01:03:33 +02:00
|
|
|
ExportPrivateKeyModal.prototype.renderButtons = function (privateKey, password, address, hideModal) {
|
2017-09-20 14:32:18 +02:00
|
|
|
return h('div.export-private-key-buttons', {}, [
|
2018-01-12 01:30:07 +01:00
|
|
|
!privateKey && this.renderButton(
|
2018-05-29 23:33:29 +02:00
|
|
|
'btn-default btn--large export-private-key__button export-private-key__button--cancel',
|
2018-01-12 01:30:07 +01:00
|
|
|
() => hideModal(),
|
|
|
|
'Cancel'
|
|
|
|
),
|
2017-09-20 14:32:18 +02:00
|
|
|
|
|
|
|
(privateKey
|
2018-05-29 23:33:29 +02:00
|
|
|
? this.renderButton('btn-primary btn--large export-private-key__button', () => hideModal(), this.context.t('done'))
|
|
|
|
: this.renderButton('btn-primary btn--large export-private-key__button', () => this.exportAccountAndGetPrivateKey(this.state.password, address), this.context.t('confirm'))
|
2017-09-20 14:32:18 +02:00
|
|
|
),
|
|
|
|
|
|
|
|
])
|
|
|
|
}
|
2017-09-20 10:05:27 +02:00
|
|
|
|
|
|
|
ExportPrivateKeyModal.prototype.render = function () {
|
2017-09-20 14:32:18 +02:00
|
|
|
const {
|
|
|
|
selectedIdentity,
|
|
|
|
warning,
|
2017-09-22 21:57:18 +02:00
|
|
|
showAccountDetailModal,
|
|
|
|
hideModal,
|
|
|
|
previousModalState,
|
2017-09-20 14:32:18 +02:00
|
|
|
} = this.props
|
2017-09-20 10:05:27 +02:00
|
|
|
const { name, address } = selectedIdentity
|
|
|
|
|
2017-09-27 01:03:33 +02:00
|
|
|
const { privateKey } = this.state
|
|
|
|
|
2017-09-22 21:57:18 +02:00
|
|
|
return h(AccountModalContainer, {
|
|
|
|
showBackButton: previousModalState === 'ACCOUNT_DETAILS',
|
|
|
|
backButtonAction: () => showAccountDetailModal(),
|
|
|
|
}, [
|
2017-09-20 10:05:27 +02:00
|
|
|
|
|
|
|
h('span.account-name', name),
|
|
|
|
|
|
|
|
h(ReadOnlyInput, {
|
|
|
|
wrapperClass: 'ellip-address-wrapper',
|
2017-09-20 14:32:18 +02:00
|
|
|
inputClass: 'qr-ellip-address ellip-address',
|
2018-04-17 21:57:55 +02:00
|
|
|
value: checksumAddress(address),
|
2017-09-20 10:05:27 +02:00
|
|
|
}),
|
|
|
|
|
|
|
|
h('div.account-modal-divider'),
|
2017-10-13 22:23:10 +02:00
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('span.modal-body-title', this.context.t('showPrivateKeys')),
|
2017-09-20 10:06:31 +02:00
|
|
|
|
|
|
|
h('div.private-key-password', {}, [
|
2017-09-20 14:32:18 +02:00
|
|
|
this.renderPasswordLabel(privateKey),
|
|
|
|
|
|
|
|
this.renderPasswordInput(privateKey),
|
2017-09-20 10:06:31 +02:00
|
|
|
|
2017-09-20 14:32:18 +02:00
|
|
|
!warning ? null : h('span.private-key-password-error', warning),
|
2017-09-20 10:06:31 +02:00
|
|
|
]),
|
|
|
|
|
2018-03-29 17:00:44 +02:00
|
|
|
h('div.private-key-password-warning', this.context.t('privateKeyWarning')),
|
2017-09-20 10:06:31 +02:00
|
|
|
|
2017-09-27 01:03:33 +02:00
|
|
|
this.renderButtons(privateKey, this.state.password, address, hideModal),
|
2017-10-13 22:23:10 +02:00
|
|
|
|
2017-09-20 10:05:27 +02:00
|
|
|
])
|
|
|
|
}
|