-
Notifications
You must be signed in to change notification settings - Fork 295
/
WalletRecoveryPhraseStep2Dialog.tsx
197 lines (187 loc) · 6.38 KB
/
WalletRecoveryPhraseStep2Dialog.tsx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// @flow
import React, { Component } from 'react';
import { observer } from 'mobx-react';
import { defineMessages, intlShape } from 'react-intl';
import vjf from 'mobx-react-form/lib/validators/VJF';
import { Autocomplete } from 'react-polymorph/lib/components/Autocomplete';
import { AutocompleteSkin } from 'react-polymorph/lib/skins/simple/AutocompleteSkin';
import suggestedMnemonics from '../../../../../common/config/crypto/valid-words.en';
import { isValidMnemonic } from '../../../../../common/config/crypto/decrypt';
import ReactToolboxMobxForm from '../../../utils/ReactToolboxMobxForm';
import {
errorOrIncompleteMarker,
validateMnemonics,
} from '../../../utils/validations';
import DialogCloseButton from '../../widgets/DialogCloseButton';
import Dialog from '../../widgets/Dialog';
import styles from './WalletRecoveryPhraseStepDialogs.scss';
import globalMessages from '../../../i18n/global-messages';
export const messages = defineMessages({
recoveryPhraseStep2Title: {
id: 'wallet.settings.recoveryPhraseStep2Title',
defaultMessage: '!!!Wallet recovery phrase verification',
description: 'Label for the recoveryPhraseStep2Title on wallet settings.',
},
recoveryPhraseStep2Description: {
id: 'wallet.settings.recoveryPhraseStep2Description',
defaultMessage:
'!!!Please enter your wallet recovery phrase. Make sure you enter the words in the correct order.',
description:
'Label for the recoveryPhraseStep2Description on wallet settings.',
},
recoveryPhraseStep2Subtitle: {
id: 'wallet.settings.recoveryPhraseStep2Subtitle',
defaultMessage: '!!!Recovery phrase',
description:
'Label for the recoveryPhraseStep2Subtitle on wallet settings.',
},
recoveryPhraseStep2Button: {
id: 'wallet.settings.recoveryPhraseStep2Button',
defaultMessage: '!!!Verify',
description: 'Label for the recoveryPhraseStep2Button on wallet settings.',
},
recoveryPhraseInputPlaceholder: {
id: 'wallet.settings.recoveryPhraseInputPlaceholder',
defaultMessage: '!!!Enter word #{wordNumber}',
description:
'Placeholder "Enter word #{wordNumber}" for the recovery phrase input on the verification dialog.',
},
recoveryPhraseNoResults: {
id: 'wallet.settings.recoveryPhraseInputNoResults',
defaultMessage: '!!!No results',
description:
'"No results" message for the recovery phrase input search results.',
},
recoveryPhraseStep2InvalidMnemonics: {
id: 'wallet.settings.recoveryPhraseStep2InvalidMnemonics',
defaultMessage: '!!!Invalid recovery phrase',
description:
'Error message shown when invalid recovery phrase was entered.',
},
});
type Props = {
onContinue: Function,
onClose: Function,
expectedWordCount: number | Array<number>,
walletName: string,
};
type State = {
isVerifying: boolean,
};
@observer
export default class WalletRecoveryPhraseStep2Dialog extends Component<
Props,
State
> {
static contextTypes = {
intl: intlShape.isRequired,
};
state = {
isVerifying: false,
};
form = new ReactToolboxMobxForm(
{
fields: {
recoveryPhrase: {
value: [],
validators: ({ field }) =>
validateMnemonics({
requiredWords: this.props.expectedWordCount,
providedWords: field.value,
validator: (enteredWords) => [
isValidMnemonic(enteredWords.join(' '), enteredWords.length),
this.context.intl.formatMessage(
messages.recoveryPhraseStep2InvalidMnemonics
),
],
}),
},
},
},
{
plugins: { vjf: vjf() },
options: {
validateOnChange: true,
},
}
);
render() {
const { form } = this;
const { intl } = this.context;
const { onClose, onContinue, expectedWordCount, walletName } = this.props;
const { isVerifying } = this.state;
const recoveryPhraseField = form.$('recoveryPhrase');
const { length: enteredWordCount } = recoveryPhraseField.value;
const canSubmit =
!recoveryPhraseField.error &&
!isVerifying &&
(Array.isArray(expectedWordCount)
? expectedWordCount.includes(enteredWordCount)
: enteredWordCount === expectedWordCount);
const recoveryPhrase = recoveryPhraseField.value;
const actions = [
{
className: isVerifying ? styles.isVerifying : null,
label: intl.formatMessage(messages.recoveryPhraseStep2Button),
primary: true,
onClick: () => {
this.setState({ isVerifying: true });
onContinue({ recoveryPhrase });
},
disabled: !canSubmit,
},
];
const maxSelections = Array.isArray(expectedWordCount)
? Math.max(...expectedWordCount)
: expectedWordCount;
return (
<Dialog
className={styles.dialog}
title={intl.formatMessage(messages.recoveryPhraseStep2Title)}
subtitle={walletName}
actions={actions}
closeOnOverlayClick={false}
onClose={onClose}
closeButton={<DialogCloseButton />}
>
<div className={styles.subtitle}>
<p>{intl.formatMessage(messages.recoveryPhraseStep2Description)}</p>
</div>
<Autocomplete
{...recoveryPhraseField.bind()}
label={intl.formatMessage(messages.recoveryPhraseStep2Subtitle)}
placeholder={intl.formatMessage(
messages.recoveryPhraseInputPlaceholder,
{
wordNumber: enteredWordCount + 1,
}
)}
options={suggestedMnemonics}
requiredSelections={
Array.isArray(expectedWordCount)
? expectedWordCount
: [expectedWordCount]
}
requiredSelectionsInfo={(required, actual) =>
Array.isArray(expectedWordCount)
? intl.formatMessage(globalMessages.unknownMnemonicWordCount, {
actual,
})
: intl.formatMessage(globalMessages.knownMnemonicWordCount, {
actual,
required,
})
}
maxSelections={maxSelections}
error={errorOrIncompleteMarker(recoveryPhraseField.error)}
maxVisibleOptions={5}
noResultsMessage={intl.formatMessage(
messages.recoveryPhraseNoResults
)}
skin={AutocompleteSkin}
optionHeight={50}
/>
</Dialog>
);
}
}