aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/modals/export-private-key-modal.js
blob: 447e43b7abf337336313a2fcb9ce796c23bdc641 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const Component = require('react').Component
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const inherits = require('util').inherits
const connect = require('react-redux').connect
const { stripHexPrefix } = require('ethereumjs-util')
const actions = require('../../actions')
const AccountModalContainer = require('./account-modal-container')
const { getSelectedIdentity } = require('../../selectors')
const ReadOnlyInput = require('../readonly-input')
const copyToClipboard = require('copy-to-clipboard')
const { checksumAddress } = require('../../util')

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,
  }
}

ExportPrivateKeyModal.contextTypes = {
  t: PropTypes.func,
}

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
    ? this.context.t('copyPrivateKey')
    : this.context.t('typePassword')
  )
}

ExportPrivateKeyModal.prototype.renderPasswordInput = function (privateKey) {
  const plainKey = privateKey && 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-secondary--lg export-private-key__button export-private-key__button--cancel',
      () => hideModal(),
      'Cancel'
    ),

    (privateKey
      ? this.renderButton('btn-primary--lg export-private-key__button', () => hideModal(), this.context.t('done'))
      : this.renderButton('btn-primary--lg export-private-key__button', () => this.exportAccountAndGetPrivateKey(this.state.password, address), this.context.t('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: checksumAddress(address),
      }),

      h('div.account-modal-divider'),

      h('span.modal-body-title', this.context.t('showPrivateKeys')),

      h('div.private-key-password', {}, [
        this.renderPasswordLabel(privateKey),

        this.renderPasswordInput(privateKey),

        !warning ? null : h('span.private-key-password-error', warning),
      ]),

      h('div.private-key-password-warning', this.context.t('privateKeyWarning')),

      this.renderButtons(privateKey, this.state.password, address, hideModal),

  ])
}