forked from stake-house/wagyu-key-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyConfigurationWizard.tsx
230 lines (210 loc) · 6.93 KB
/
KeyConfigurationWizard.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { Grid, Typography } from '@material-ui/core';
import React, { FC, ReactElement, useState, Dispatch, SetStateAction } from 'react';
import styled from 'styled-components';
import KeyInputs from './KeyGeneratioinFlow/0-KeyInputs';
import VerifyKeysPassword from './KeyGeneratioinFlow/1-VerifyKeysPassword';
import StepNavigation from './StepNavigation';
const ContentGrid = styled(Grid)`
height: 320px;
margin-top: 16px;
`;
type Props = {
onStepBack: () => void,
onStepForward: () => void,
keyGenerationStartIndex: number,
initialKeyGenerationStartIndex: number,
setKeyGenerationStartIndex: Dispatch<SetStateAction<number>>,
showKeyGenerationStartIndexInput: boolean,
numberOfKeys: number,
setNumberOfKeys: Dispatch<SetStateAction<number>>,
withdrawalAddress: string,
setWithdrawalAddress: Dispatch<SetStateAction<string>>,
password: string,
setPassword: Dispatch<SetStateAction<string>>,
}
/**
* This is the wizard the user will navigate to configure their keys.
* It uses the notion of a 'step' to render specific pages within the flow.
*
* @param props.onStepBack function to execute when stepping back
* @param props.onStepForward function to execute when stepping forward
* @param props.keyGenerationStartIndex the index at which to start generating keys for the user
* @param props.initialKeyGenerationStartIndex the default value for starting index
* @param props.setKeyGenerationStartIndex function to set the starting index
* @param props.showKeyGenerationStartIndexInput toggle to control whether we will prompt the user for starting index.
* Prompting the user for starting index is only required when importing a mnemonic.
* @param props.numberOfKeys the total number of keys to generate for the user
* @param props.setNumberOfKeys function to set the number of keys to generate
* @param props.withdrawalAddress the optional wallet address for the withdrawal credentials
* @param props.setWithdrawalAddress function to set the wallet address for the withdrawal credentials
* @param props.password the password to use to protect the keys for the user
* @param props.setPassword function to set the password
* @returns the react element to render
*/
const KeyConfigurationWizard: FC<Props> = (props): ReactElement => {
const [step, setStep] = useState(0);
const [verifyPassword, setVerifyPassword] = useState("");
const [numberOfKeysError, setNumberOfKeysError] = useState(false);
const [withdrawalAddressFormatError, setWithdrawalAddressFormatError] = useState(false);
const [passwordStrengthError, setPasswordStrengthError] = useState(false);
const [passwordVerifyError, setPasswordVerifyError] = useState(false);
const [startingIndexError, setStartingIndexError] = useState(false);
const prevLabel = () => {
switch (step) {
case 0:
return "Back";
case 1:
return "Back";
}
}
const prevClicked = () => {
switch (step) {
case 0: {
setNumberOfKeysError(false);
setWithdrawalAddressFormatError(false);
setPasswordStrengthError(false);
setStartingIndexError(false);
props.setPassword("");
props.setKeyGenerationStartIndex(props.initialKeyGenerationStartIndex);
props.setNumberOfKeys(1);
props.setWithdrawalAddress("");
props.onStepBack();
break;
}
case 1: {
setPasswordVerifyError(false);
props.setPassword("");
setVerifyPassword("");
setStep(step - 1);
break;
}
default: {
console.log("Key configuration step is greater than 1 when prev was clicked. This should never happen.");
break;
}
}
}
const nextLabel = () => {
switch (step) {
case 0:
return "Next";
case 1:
return "Next";
}
}
const nextClicked = () => {
switch (step) {
// Inputs
case 0: {
validateInputs();
break;
}
// Verify Password
case 1: {
confirmPassword();
break;
}
default: {
console.log("Key configuration step is greater than 1 when next was clicked. This should never happen.");
break;
}
}
}
const validateInputs = async () => {
let isError = false;
if (props.numberOfKeys < 1 || props.numberOfKeys > 1000) {
setNumberOfKeysError(true);
isError = true;
} else {
setNumberOfKeysError(false);
}
if (props.password.length < 8) {
setPasswordStrengthError(true);
isError = true;
} else {
setPasswordStrengthError(false);
}
if (props.keyGenerationStartIndex < 0) {
setStartingIndexError(true);
isError = true;
} else {
setStartingIndexError(false);
}
if (props.withdrawalAddress != "") {
const isValidAddress = await window.web3Utils.isAddress(props.withdrawalAddress);
if (!isValidAddress) {
setWithdrawalAddressFormatError(true);
isError = true;
} else {
setWithdrawalAddressFormatError(false);
}
} else {
setWithdrawalAddressFormatError(false);
}
if (!isError) {
setStep(step + 1);
}
}
const confirmPassword = () => {
if (props.password.localeCompare(verifyPassword) == 0) {
setPasswordVerifyError(false);
props.onStepForward();
} else {
setPasswordVerifyError(true);
}
}
const content = () => {
switch(step) {
case 0: return (
<KeyInputs
numberOfKeys={props.numberOfKeys}
setNumberOfKeys={props.setNumberOfKeys}
withdrawalAddress={props.withdrawalAddress}
setWithdrawalAddress={props.setWithdrawalAddress}
index={props.keyGenerationStartIndex}
setIndex={props.setKeyGenerationStartIndex}
showIndexInput={props.showKeyGenerationStartIndexInput}
password={props.password}
setPassword={props.setPassword}
numberOfKeysError={numberOfKeysError}
withdrawalAddressFormatError={withdrawalAddressFormatError}
setWithdrawalAddressFormatError={setWithdrawalAddressFormatError}
passwordStrengthError={passwordStrengthError}
startingIndexError={startingIndexError}
onFinish={validateInputs}
/>
);
case 1: return (
<VerifyKeysPassword
setVerifyPassword={setVerifyPassword}
passwordVerifyError={passwordVerifyError}
onFinish={confirmPassword}
/>
);
default:
return null;
}
}
return (
<Grid container direction="column" spacing={2}>
<Grid item>
<Typography variant="h1">
Create Keys
</Typography>
</Grid>
<ContentGrid item container>
<Grid item xs={12}>
{content()}
</Grid>
</ContentGrid>
{props.children}
<StepNavigation
onPrev={prevClicked}
onNext={nextClicked}
backLabel={prevLabel()}
nextLabel={nextLabel()}
/>
</Grid>
);
}
export default KeyConfigurationWizard;