aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/pages/keychains/reveal-seed.js
blob: edc9db5a08fe9964d49a823d9362f4b8b6262e15 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const { Component } = require('react')
const { connect } = require('react-redux')
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const classnames = require('classnames')

const { requestRevealSeedWords } = require('../../store/actions')
const { DEFAULT_ROUTE } = require('../../helpers/constants/routes')
const ExportTextContainer = require('../../components/ui/export-text-container')

import Button from '../../components/ui/button'

const PASSWORD_PROMPT_SCREEN = 'PASSWORD_PROMPT_SCREEN'
const REVEAL_SEED_SCREEN = 'REVEAL_SEED_SCREEN'

class RevealSeedPage extends Component {
  constructor (props) {
    super(props)

    this.state = {
      screen: PASSWORD_PROMPT_SCREEN,
      password: '',
      seedWords: null,
      error: null,
    }
  }

  componentDidMount () {
    const passwordBox = document.getElementById('password-box')
    if (passwordBox) {
      passwordBox.focus()
    }
  }

  handleSubmit (event) {
    event.preventDefault()
    this.setState({ seedWords: null, error: null })
    this.props.requestRevealSeedWords(this.state.password)
      .then(seedWords => this.setState({ seedWords, screen: REVEAL_SEED_SCREEN }))
      .catch(error => this.setState({ error: error.message }))
  }

  renderWarning () {
    return (
      h('.page-container__warning-container', [
        h('img.page-container__warning-icon', {
          src: 'images/warning.svg',
        }),
        h('.page-container__warning-message', [
          h('.page-container__warning-title', [this.context.t('revealSeedWordsWarningTitle')]),
          h('div', [this.context.t('revealSeedWordsWarning')]),
        ]),
      ])
    )
  }

  renderContent () {
    return this.state.screen === PASSWORD_PROMPT_SCREEN
      ? this.renderPasswordPromptContent()
      : this.renderRevealSeedContent()
  }

  renderPasswordPromptContent () {
    const { t } = this.context

    return (
      h('form', {
        onSubmit: event => this.handleSubmit(event),
      }, [
        h('label.input-label', {
          htmlFor: 'password-box',
        }, t('enterPasswordContinue')),
        h('.input-group', [
          h('input.form-control', {
            type: 'password',
            placeholder: t('password'),
            id: 'password-box',
            value: this.state.password,
            onChange: event => this.setState({ password: event.target.value }),
            className: classnames({ 'form-control--error': this.state.error }),
          }),
        ]),
        this.state.error && h('.reveal-seed__error', this.state.error),
      ])
    )
  }

  renderRevealSeedContent () {
    const { t } = this.context

    return (
      h('div', [
        h('label.reveal-seed__label', t('yourPrivateSeedPhrase')),
        h(ExportTextContainer, {
          text: this.state.seedWords,
          filename: t('metamaskSeedWords'),
        }),
      ])
    )
  }

  renderFooter () {
    return this.state.screen === PASSWORD_PROMPT_SCREEN
      ? this.renderPasswordPromptFooter()
      : this.renderRevealSeedFooter()
  }

  renderPasswordPromptFooter () {
    return (
      h('.page-container__footer', [
        h('header', [
          h(Button, {
            type: 'default',
            large: true,
            className: 'page-container__footer-button',
            onClick: () => this.props.history.push(DEFAULT_ROUTE),
          }, this.context.t('cancel')),
          h(Button, {
            type: 'primary',
            large: true,
            className: 'page-container__footer-button',
            onClick: event => this.handleSubmit(event),
            disabled: this.state.password === '',
          }, this.context.t('next')),
        ]),
      ])
    )
  }

  renderRevealSeedFooter () {
    return (
      h('.page-container__footer', [
        h(Button, {
          type: 'default',
          large: true,
          className: 'page-container__footer-button',
          onClick: () => this.props.history.push(DEFAULT_ROUTE),
        }, this.context.t('close')),
      ])
    )
  }

  render () {
    return (
      h('.page-container', [
        h('.page-container__header', [
          h('.page-container__title', this.context.t('revealSeedWordsTitle')),
          h('.page-container__subtitle', this.context.t('revealSeedWordsDescription')),
        ]),
        h('.page-container__content', [
          this.renderWarning(),
          h('.reveal-seed__content', [
            this.renderContent(),
          ]),
        ]),
        this.renderFooter(),
      ])
    )
  }
}

RevealSeedPage.propTypes = {
  requestRevealSeedWords: PropTypes.func,
  history: PropTypes.object,
}

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

const mapDispatchToProps = dispatch => {
  return {
    requestRevealSeedWords: password => dispatch(requestRevealSeedWords(password)),
  }
}

module.exports = connect(null, mapDispatchToProps)(RevealSeedPage)