-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathImportScreen.tsx
More file actions
490 lines (457 loc) · 16.9 KB
/
ImportScreen.tsx
File metadata and controls
490 lines (457 loc) · 16.9 KB
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import FileDrop from "./components/FileDrop/FileDrop";
import { SecondaryInfoTypography } from "./Styles/Typographies";
import {
Box,
Button,
Card,
Switch,
TextField,
Select,
Typography,
FormGroup,
FormControlLabel,
MenuItem,
FormControl,
FormHelperText,
Alert
} from "@mui/material";
import { Link } from "react-router-dom";
import { DropEvent } from "react-dropzone";
import { useEffect, useState } from "react";
import BackupIcon from "@mui/icons-material/Backup";
import { ImportStatus, KeystoreInfo, TagSelectOption } from "./types";
import FileCardList from "./components/FileCards/FileCardList";
import ImportDialog from "./components/Dialogs/ImportDialog";
import {
Tag,
isValidEcdsaPubkey,
BURN_ADDRESS,
isFeeRecipientEditable,
areAllFeeRecipientsEditable,
Network
} from "@stakingbrain/common";
import CloseIcon from "@mui/icons-material/Close";
import { rpcClient } from "./socket";
import ArrowCircleLeftOutlinedIcon from "@mui/icons-material/ArrowCircleLeftOutlined";
import { extractPubkey } from "./utils/dataUtils";
import type { CustomImportRequest, Web3signerPostResponse } from "@stakingbrain/brain";
export default function ImportScreen({
network,
isMevBoostSet
}: {
network: Network;
isMevBoostSet: boolean;
}): JSX.Element {
const [keystoresPostResponse, setKeystoresPostResponse] = useState<Web3signerPostResponse>();
const [keystoresPostError, setKeystoresPostError] = useState<string>();
const [openDialog, setOpenDialog] = useState(false);
const [acceptedFiles, setAcceptedFiles] = useState<KeystoreInfo[]>([]);
const [passwords, setPasswords] = useState<string[]>([]);
const [useSamePassword, setUseSamePassword] = useState(false);
const [tags, setTags] = useState<Tag[]>([]);
const [useSameTag, setUseSameTag] = useState(false);
const [feeRecipients, setFeeRecipients] = useState<string[]>([]);
const [useSameFeerecipient, setUseSameFeerecipient] = useState(false);
const [importStatus, setImportStatus] = useState(ImportStatus.NotImported);
const [slashingFile, setSlashingFile] = useState<File>();
const [showMevWarning, setShowMevWarning] = useState(false);
const [isButtonDisabled, setIsButtonDisabled] = useState(true);
// If the user has selected the 'lido' tag and MEV-Boost is not set, show the warning
useEffect(() => {
const lidoSelected = tags.includes("lido");
setShowMevWarning(lidoSelected && !isMevBoostSet);
}, [tags, isMevBoostSet]);
// Function to handle "use same password/fee recipient/tag" switches toggling
const handleSwitchToggle = (switchType: "password" | "tag" | "feerecipient") => {
switch (switchType) {
case "password":
setUseSamePassword(!useSamePassword); // Directly toggle the boolean state
setPasswords([]); // Reset passwords to empty
break;
case "tag":
setUseSameTag(!useSameTag); // Directly toggle the boolean state
setTags([]); // Reset tags to empty
break;
case "feerecipient":
setUseSameFeerecipient(!useSameFeerecipient); // Directly toggle the boolean state
setFeeRecipients([]); // Reset fee recipients to empty
break;
}
};
// This use effect sets the Lido warning when one of the keystores has the 'lido' tag and MEV-Boost is not set
useEffect(() => {
// Check if any of the selected tags is 'lido' and MEV-Boost is not set
const lidoSelected = tags.includes("lido");
setShowMevWarning(lidoSelected && !isMevBoostSet);
}, [tags, isMevBoostSet]);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const keystoreFilesCallback = async (files: File[], event: DropEvent) => {
const keystoresToAdd: KeystoreInfo[] = [];
const passwordsToAdd: string[] = [];
for (const file of files) {
const pubkey = await extractPubkey(file);
if (pubkey) {
if (acceptedFiles.some((e) => e.pubkey === pubkey) === false) {
keystoresToAdd.push({ file: file, pubkey: pubkey });
passwordsToAdd.push("");
}
}
}
setAcceptedFiles([...acceptedFiles].concat(keystoresToAdd));
setPasswords([...passwords].concat(passwordsToAdd));
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const slashingFilesCallback = (files: File[], event: DropEvent) => {
setSlashingFile(files[0]);
};
// SLASHING PROTECTION SWITCH
const [slashingProtectionIncluded, setSlashingProtectionIncluded] = useState(true);
const onSlashingChecked = (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => {
setSlashingProtectionIncluded(checked);
};
const handleClickOpenDialog = () => {
setOpenDialog(true);
};
// This useEffect updates the "submit keystores" button state based on various conditions,
// such as if any password is empty, if any tag is empty, if any fee recipient is invalid, etc.
useEffect(() => {
const isAnyPasswordEmpty = () => {
return passwords.some((password) => password === "") || passwords.length === 0;
};
const hasInvalidTagOrFeeRecipient = () => {
if (tags.length !== acceptedFiles.length) return true;
return tags.some((tag, index) => {
if (tag.length === 0) return true;
if (isFeeRecipientEditable(tag)) {
return !isValidEcdsaPubkey(feeRecipients[index]);
}
return false;
});
};
const hasInvalidKeystoreData = () => {
return isAnyPasswordEmpty() || hasInvalidTagOrFeeRecipient();
};
const disable =
acceptedFiles.length === 0 ||
(!slashingFile && slashingProtectionIncluded) ||
hasInvalidKeystoreData() ||
showMevWarning;
setIsButtonDisabled(disable); // Set the state based on the conditions
}, [passwords, tags, feeRecipients, acceptedFiles, slashingFile, slashingProtectionIncluded, showMevWarning]);
async function importKeystores() {
try {
setImportStatus(ImportStatus.Importing);
handleClickOpenDialog();
const importRequest: CustomImportRequest = {
importFrom: "ui",
slashing_protection: slashingFile,
validatorsImportRequest: acceptedFiles.map((f, i) => {
return {
keystore: f.file,
password: passwords[i],
tag: tags[i],
feeRecipient: feeRecipients[i]
};
})
};
const response = await rpcClient.call("importValidators", importRequest);
setKeystoresPostResponse(response);
setKeystoresPostError(undefined);
setImportStatus(ImportStatus.Imported);
} catch (e) {
console.error(e);
setKeystoresPostError(e.message);
setImportStatus(ImportStatus.NotImported);
}
}
function getFeeRecipientFieldHelperText(index: number): string {
const feeRecipient = feeRecipients[index];
if (feeRecipient === "" || feeRecipient === undefined) {
return "The address you wish to receive the transaction fees";
}
if (!isValidEcdsaPubkey(feeRecipient)) {
return "Invalid address";
}
if (feeRecipient === BURN_ADDRESS) {
return "It is not possible to set the fee recipient to the burn address";
}
return "Address is valid";
}
function isFeeRecipientFieldWrong(index: number): boolean {
const feeRecipient = feeRecipients[index];
if (feeRecipient === "" || feeRecipient === undefined) {
return false;
}
if (!isValidEcdsaPubkey(feeRecipient) || feeRecipient === BURN_ADDRESS) {
return true;
}
return false;
}
const tagSelectOptions: TagSelectOption[] = ["gnosis", "lukso"].includes(network)
? [{ value: "solo", label: "Solo" }]
: ["holesky", "hoodi"].includes(network)
? [
{ value: "solo", label: "Solo" },
{ value: "rocketpool", label: "Rocketpool" },
{ value: "stakehouse", label: "StakeHouse" },
{ value: "lido", label: "Lido" }
]
: [
{ value: "solo", label: "Solo" },
{ value: "rocketpool", label: "Rocketpool" },
{ value: "stakehouse", label: "StakeHouse" },
{ value: "stakewise", label: "Stakewise" },
{ value: "stader", label: "Stader" },
{ value: "lido", label: "Lido" }
];
return (
<div>
<Box
sx={{
margin: { xs: 2, sm: 3, md: 5 },
display: "flex",
flexDirection: "column",
alignItems: "left"
}}
>
<Typography variant="h5" sx={{ marginBottom: 2, fontSize: { xs: 20, sm: 24 } }}>
<b>Import Validator Keystore(s)</b>
</Typography>
<Card
sx={{
padding: { xs: 2, sm: 4 },
borderRadius: 2
}}
>
<Typography>Upload any keystore JSON file(s).</Typography>
<SecondaryInfoTypography
sx={{ marginBottom: 4 }}
text="Keystores files are usually named keystore-xxxxxxxx.json and were
created in the Ethereum launchpad deposit CLI. Do not upload the
deposit_data.json file."
/>
<FileDrop callback={keystoreFilesCallback} />
<SecondaryInfoTypography
sx={{ marginTop: 4 }}
text="Remember you need to introduce the password you set during
creation of the keystore files."
/>
</Card>
{acceptedFiles.length > 1 && (
<>
<FormGroup sx={{ marginTop: "6px" }}>
<FormControlLabel
control={<Switch onChange={() => handleSwitchToggle("password")} />}
label="Use same password for every file"
/>
<FormControlLabel
control={<Switch onChange={() => handleSwitchToggle("feerecipient")} />}
label="Use same fee recipient for every file"
/>
<FormControlLabel
control={<Switch onChange={() => handleSwitchToggle("tag")} />}
label="Use same tag for every file"
/>
</FormGroup>
{(useSameTag || useSameFeerecipient || useSamePassword) && (
<FormControl sx={{ marginTop: 2, width: "100%" }}>
{useSamePassword && (
<>
<TextField
id={`outlined-password-input`}
label="Keystore Password"
type="password"
sx={{ marginTop: 2 }}
onChange={(e) => setPasswords(Array(acceptedFiles.length).fill(e.target.value))}
helperText={"Password to decrypt the keystore(s)"}
/>
</>
)}
{useSameTag && (
<>
<Select
id="outlined-tag-input"
label="Tag"
value={tags[0]}
type="text"
sx={{ marginTop: 2 }}
onChange={(e) => {
setTags(Array(acceptedFiles.length).fill(e.target.value));
if (!isFeeRecipientEditable(tags[0])) {
setFeeRecipients(Array(acceptedFiles.length).fill(""));
}
}}
>
{tagSelectOptions.map((option) => (
<MenuItem value={option.value}>{option.label}</MenuItem>
))}
</Select>
<FormHelperText>Staking protocol</FormHelperText>
</>
)}
{useSameFeerecipient && (
<>
<TextField
id={`outlined-fee-recipient-input`}
label={
tags[0] === undefined || isFeeRecipientEditable(tags[0])
? "Fee Recipient"
: "For this protocol, fee recipient will be set automatically"
}
type="text"
sx={{ marginTop: 2 }}
onChange={(e) => {
setFeeRecipients(Array(acceptedFiles.length).fill(e.target.value));
}}
error={isFeeRecipientFieldWrong(0)}
helperText={getFeeRecipientFieldHelperText(0)}
value={feeRecipients[0]}
disabled={!isFeeRecipientEditable(tags[0])}
/>
{!areAllFeeRecipientsEditable(tags) && !useSameTag && (
<Alert severity="info">This field will only apply to the editable fee recipients</Alert>
)}
</>
)}
</FormControl>
)}
</>
)}
{FileCardList(
acceptedFiles,
setAcceptedFiles,
passwords,
setPasswords,
useSamePassword,
tags,
setTags,
useSameTag,
feeRecipients,
setFeeRecipients,
useSameFeerecipient,
getFeeRecipientFieldHelperText,
isFeeRecipientFieldWrong,
tagSelectOptions
)}
<Box
sx={{
marginTop: 8,
display: "flex",
flexDirection: "row",
alignItems: "left"
}}
>
<Typography variant="h5" sx={{ marginBottom: 2, fontSize: { xs: 20, sm: 24 } }}>
<b>Import slashing protection data? (recommended)</b>
</Typography>
<Switch onChange={onSlashingChecked} />
</Box>
{slashingProtectionIncluded ? (
<Card
sx={{
padding: { xs: 2, sm: 4 },
borderRadius: 2
}}
>
<Typography>Upload your slashing protection file to protect your keystore(s).</Typography>
<SecondaryInfoTypography sx={{ marginBottom: 4 }} text="Only for previously-used keystores" />
{slashingFile ? (
<Card
key={slashingFile.name}
raised
sx={{
padding: 2,
marginTop: 4,
borderRadius: 2
}}
>
<Box
sx={{
display: "flex",
flexDirection: "row",
alignItems: "left",
justifyContent: "space-between"
}}
>
<Typography variant="h6">
<b>✅ {slashingFile.name}</b>
<br />
</Typography>
<button onClick={() => setSlashingFile(undefined)}>
<CloseIcon color="action" />
</button>
</Box>
</Card>
) : (
<FileDrop callback={slashingFilesCallback} />
)}
</Card>
) : null}
{showMevWarning && (
<Alert
severity="warning"
sx={{
marginTop: 4,
display: "flex"
}}
>
You are importing one or more "Lido" validators, but don't have the MEV Boost package up & running. As a
Lido Node Operator, it is your responsibility to ensure that your validators use MEV boost. <br /> Please
install the MEV Boost package from{" "}
<Link
to={
network === "holesky"
? "http://my.dappnode/stakers/holesky"
: network === "hoodi"
? "http://my.dappnode/stakers/hoodi"
: "http://my.dappnode/stakers/ethereum"
}
>
your stakers tab
</Link>{" "}
before importing your Lido validator. Visit{" "}
<Link to="https://docs.dappnode.io/docs/user/staking/ethereum/lsd-pools/lido">our docs</Link> for more
details.
</Alert>
)}
<Box
sx={{
marginTop: 4,
display: "flex",
justifyContent: "space-between",
gap: 2
}}
>
<Link to={{ pathname: "/", search: window.location.search }} style={{ textDecoration: "none" }}>
<Button
variant="outlined"
size="medium"
sx={{ borderRadius: 2 }}
startIcon={<ArrowCircleLeftOutlinedIcon />}
>
Back to Accounts
</Button>
</Link>
<Button
variant="contained"
size="medium"
endIcon={<BackupIcon />}
disabled={isButtonDisabled}
onClick={importKeystores}
sx={{ borderRadius: 2 }}
>
Submit Keystores
</Button>
</Box>
</Box>
<ImportDialog
open={openDialog}
setOpen={setOpenDialog}
keystoresPostResponse={keystoresPostResponse}
keystoresPostError={keystoresPostError}
importStatus={importStatus}
acceptedFiles={acceptedFiles}
/>
</div>
);
}