-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
BTECConfigurationWizard.tsx
274 lines (247 loc) · 8.54 KB
/
BTECConfigurationWizard.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { Grid, Typography } from '@material-ui/core';
import React, { FC, ReactElement, useState, Dispatch, SetStateAction } from 'react';
import styled from 'styled-components';
import { errors } from '../constants';
import MainInputs from './BTECGenerationFlow/0-MainInputs';
import ValidatingBLSCredentials from './BTECGenerationFlow/1-ValidatingBLSCredentials';
import StepNavigation from './StepNavigation';
const ContentGrid = styled(Grid)`
height: 320px;
margin-top: 16px;
`;
type Props = {
onStepBack: () => void,
onStepForward: () => void,
network: string,
mnemonic: string,
startIndex: number,
setStartIndex: Dispatch<SetStateAction<number>>,
btecIndices: string,
setBtecIndices: Dispatch<SetStateAction<string>>,
btecCredentials: string,
setBtecCredentials: Dispatch<SetStateAction<string>>,
withdrawalAddress: string,
setWithdrawalAddress: Dispatch<SetStateAction<string>>,
}
/**
* This is the wizard the user will navigate to configure their BLS to execution change.
* 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.network the network for which to generate this BTEC
* @param props.mnemonic the current mnemonic for which to generate this BTEC
* @param props.startIndex the index for the keys to start generating withdrawal credentials
* @param props.setStartIndex function to set the starting index
* @param props.btecIndices a list of the chosen validator index number(s) as identified on the beacon chain
* @param props.setBtecIndices function to set the list of validator index number(s)
* @param props.btecCredentials a list of the old BLS withdrawal credentials of the given validator(s)
* @param props.setBtecCredentials function to set the list of old BLS withdrawal credentials
* @param props.withdrawalAddress the wallet address for the withdrawal credentials
* @param props.setWithdrawalAddress function to set the wallet address for the withdrawal credentials
* @returns the react element to render
*/
const BTECConfigurationWizard: FC<Props> = (props): ReactElement => {
const [step, setStep] = useState(0);
const [withdrawalAddressError, setWithdrawalAddressError] = useState(false);
const [withdrawalAddressErrorMsg, setWithdrawalAddressErrorMsg] = useState("");
const [startingIndexError, setStartingIndexError] = useState(false);
const [indicesError, setIndicesError] = useState(false);
const [btecCredentialsError, setBtecCredentialsError] = useState(false);
const [indicesErrorMsg, setIndicesErrorMsg] = useState("");
const [btecCredentialsErrorMsg, setBtecCredentialsErrorMsg] = useState("");
const prevLabel = () => {
switch (step) {
case 0:
return "Back";
case 1:
return "Back";
}
}
const prevClicked = () => {
switch (step) {
case 0: {
setWithdrawalAddressError(false);
setWithdrawalAddressErrorMsg("");
setStartingIndexError(false);
setIndicesError(false);
setIndicesErrorMsg("");
setBtecCredentialsError(false);
setBtecCredentialsErrorMsg("");
props.setStartIndex(0);
props.setBtecIndices("");
props.setBtecCredentials("");
props.setWithdrawalAddress("");
props.onStepBack();
break;
}
default: {
console.log("BTEC configuration step is greater than 0 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;
}
default: {
console.log("BTEC configuration step is greater than 1 when next was clicked. This should never happen.");
break;
}
}
}
const validateInputs = async () => {
let isError = false;
if (props.startIndex < 0) {
setStartingIndexError(true);
isError = true;
} else {
setStartingIndexError(false);
}
const splitIndices = props.btecIndices.split(',');
const splitBLSCredentials = props.btecCredentials.split(',');
if (props.btecIndices == "") {
setIndicesError(true);
setIndicesErrorMsg(errors.INDICES);
isError = true;
} else {
// Validate if all integers
let indiceFormatError = false;
splitIndices.forEach( (indice) => {
if (!/^\d+$/.test(indice)) {
indiceFormatError = true;
}
});
if (indiceFormatError) {
setIndicesError(true);
setIndicesErrorMsg(errors.INDICES_FORMAT);
isError = true;
} else {
setIndicesError(false);
}
}
if (props.btecCredentials == "") {
setBtecCredentialsError(true);
setBtecCredentialsErrorMsg(errors.BLS_CREDENTIALS);
isError = true;
} else {
// Validate if all credentials match format
let credentialFormatError = false;
splitBLSCredentials.forEach( (credential) => {
if (!/^(0x)?00[0-9a-fA-F]{62}$/.test(credential)) {
credentialFormatError = true;
}
});
if (credentialFormatError) {
setBtecCredentialsError(true);
setBtecCredentialsErrorMsg(errors.BLS_CREDENTIALS_FORMAT);
isError = true;
} else {
setBtecCredentialsError(false);
}
}
// Validate indices length matches credentials length
if (splitIndices.length != splitBLSCredentials.length) {
setIndicesError(true);
setIndicesErrorMsg(errors.INDICES_LENGTH);
isError = true;
}
if (props.withdrawalAddress != "") {
const isValidAddress = await window.web3Utils.isAddress(props.withdrawalAddress);
if (!isValidAddress) {
setWithdrawalAddressError(true);
setWithdrawalAddressErrorMsg(errors.ADDRESS_FORMAT_ERROR);
isError = true;
} else {
setWithdrawalAddressError(false);
}
} else {
setWithdrawalAddressError(true);
setWithdrawalAddressErrorMsg(errors.WITHDRAW_ADDRESS_REQUIRED);
isError = true;
}
if (!isError) {
setStep(step + 1);
window.eth2Deposit.validateBLSCredentials(props.network, props.mnemonic, props.startIndex, props.btecCredentials).then(() => {
props.onStepForward();
}).catch((error) => {
setStep(0);
setBtecCredentialsError(true);
setBtecCredentialsErrorMsg(errors.BLS_CREDENTIALS_NO_MATCH);
})
}
}
const content = () => {
switch(step) {
case 0: return (
<MainInputs
index={props.startIndex}
setIndex={props.setStartIndex}
btecIndices={props.btecIndices}
setBtecIndices={props.setBtecIndices}
btecCredentials={props.btecCredentials}
setBtecCredentials={props.setBtecCredentials}
withdrawalAddress={props.withdrawalAddress}
setWithdrawalAddress={props.setWithdrawalAddress}
withdrawalAddressError={withdrawalAddressError}
setWithdrawalAddressError={setWithdrawalAddressError}
withdrawalAddressErrorMsg={withdrawalAddressErrorMsg}
setWithdrawalAddressErrorMsg={setWithdrawalAddressErrorMsg}
startingIndexError={startingIndexError}
setStartingIndexError={setStartingIndexError}
indicesError={indicesError}
setIndicesError={setIndicesError}
indicesErrorMsg={indicesErrorMsg}
setIndicesErrorMsg={setIndicesErrorMsg}
btecCredentialsError={btecCredentialsError}
setBtecCredentialsError={setBtecCredentialsError}
btecCredentialsErrorMsg={btecCredentialsErrorMsg}
setBtecCredentialsErrorMsg={setBtecCredentialsErrorMsg}
onFinish={validateInputs}
/>
);
case 1: return (
<ValidatingBLSCredentials
/>
);
default:
return null;
}
}
return (
<Grid container direction="column" spacing={2}>
<Grid item>
<Typography variant="h1">
Generate BLS to execution change
</Typography>
</Grid>
<ContentGrid item container>
<Grid item xs={12}>
{content()}
</Grid>
</ContentGrid>
{props.children}
<StepNavigation
onPrev={prevClicked}
onNext={nextClicked}
backLabel={prevLabel()}
nextLabel={nextLabel()}
disableBack={step === 1}
disableNext={step === 1}
/>
</Grid>
);
}
export default BTECConfigurationWizard;