1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/ui/app/components/modals/export-private-key-modal.js

140 lines
4.3 KiB
JavaScript
Raw Normal View History

const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
2018-03-16 01:29:45 +01:00
const connect = require('../../metamask-connect')
const ethUtil = require('ethereumjs-util')
const actions = require('../../actions')
const AccountModalContainer = require('./account-modal-container')
const { getSelectedIdentity } = require('../../selectors')
const ReadOnlyInput = require('../readonly-input')
2018-03-16 01:29:45 +01:00
const t = require('../../../i18n-helper').getMessage
const copyToClipboard = require('copy-to-clipboard')
function mapStateToProps (state) {
return {
warning: state.appState.warning,
privateKey: state.appState.accountDetail.privateKey,
network: state.metamask.network,
selectedIdentity: getSelectedIdentity(state),
previousModalState: state.appState.modal.previousModalState.name,
}
}
function mapDispatchToProps (dispatch) {
return {
exportAccount: (password, address) => dispatch(actions.exportAccount(password, address)),
showAccountDetailModal: () => dispatch(actions.showModal({ name: 'ACCOUNT_DETAILS' })),
hideModal: () => dispatch(actions.hideModal()),
}
}
inherits(ExportPrivateKeyModal, Component)
function ExportPrivateKeyModal () {
Component.call(this)
this.state = {
password: '',
privateKey: null,
}
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(ExportPrivateKeyModal)
ExportPrivateKeyModal.prototype.exportAccountAndGetPrivateKey = function (password, address) {
const { exportAccount } = this.props
exportAccount(password, address)
.then(privateKey => this.setState({ privateKey }))
}
ExportPrivateKeyModal.prototype.renderPasswordLabel = function (privateKey) {
return h('span.private-key-password-label', privateKey
2018-03-16 01:29:45 +01:00
? t(this.props.localeMessages, 'copyPrivateKey')
: t(this.props.localeMessages, 'typePassword')
)
}
ExportPrivateKeyModal.prototype.renderPasswordInput = function (privateKey) {
const plainKey = privateKey && ethUtil.stripHexPrefix(privateKey)
return privateKey
? h(ReadOnlyInput, {
wrapperClass: 'private-key-password-display-wrapper',
inputClass: 'private-key-password-display-textarea',
textarea: true,
value: plainKey,
onClick: () => copyToClipboard(plainKey),
})
: h('input.private-key-password-input', {
type: 'password',
onChange: event => this.setState({ password: event.target.value }),
})
}
ExportPrivateKeyModal.prototype.renderButton = function (className, onClick, label) {
return h('button', {
className,
onClick,
}, label)
}
ExportPrivateKeyModal.prototype.renderButtons = function (privateKey, password, address, hideModal) {
return h('div.export-private-key-buttons', {}, [
!privateKey && this.renderButton(
'btn-cancel export-private-key__button export-private-key__button--cancel',
() => hideModal(),
'Cancel'
),
(privateKey
2018-03-16 01:29:45 +01:00
? this.renderButton('btn-clear export-private-key__button', () => hideModal(), t(this.props.localeMessages, 'done'))
: this.renderButton('btn-clear export-private-key__button', () => this.exportAccountAndGetPrivateKey(this.state.password, address), t(this.props.localeMessages, 'confirm'))
),
])
}
ExportPrivateKeyModal.prototype.render = function () {
const {
selectedIdentity,
warning,
showAccountDetailModal,
hideModal,
previousModalState,
} = this.props
const { name, address } = selectedIdentity
const { privateKey } = this.state
return h(AccountModalContainer, {
showBackButton: previousModalState === 'ACCOUNT_DETAILS',
backButtonAction: () => showAccountDetailModal(),
}, [
h('span.account-name', name),
h(ReadOnlyInput, {
wrapperClass: 'ellip-address-wrapper',
inputClass: 'qr-ellip-address ellip-address',
value: address,
}),
h('div.account-modal-divider'),
2018-03-16 01:29:45 +01:00
h('span.modal-body-title', t(this.props.localeMessages, 'showPrivateKeys')),
2017-09-20 10:06:31 +02:00
h('div.private-key-password', {}, [
this.renderPasswordLabel(privateKey),
this.renderPasswordInput(privateKey),
2017-09-20 10:06:31 +02:00
!warning ? null : h('span.private-key-password-error', warning),
2017-09-20 10:06:31 +02:00
]),
2018-03-16 01:29:45 +01:00
h('div.private-key-password-warning', t(this.props.localeMessages, 'privateKeyWarning')),
2017-09-20 10:06:31 +02:00
this.renderButtons(privateKey, this.state.password, address, hideModal),
])
}