-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Seedless-Onboarding] export account (#2610)
- Loading branch information
Showing
9 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/components/settings/ExportMPCAccount/ExportMPCAccountModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { MpcWalletContext } from '@/components/common/ConnectWallet/MPCWalletProvider' | ||
import CopyButton from '@/components/common/CopyButton' | ||
import ModalDialog from '@/components/common/ModalDialog' | ||
import { Box, Button, DialogContent, DialogTitle, IconButton, TextField, Typography } from '@mui/material' | ||
import { useContext, useState } from 'react' | ||
import { useForm } from 'react-hook-form' | ||
import { Visibility, VisibilityOff, Close } from '@mui/icons-material' | ||
import css from './styles.module.css' | ||
import ErrorCodes from '@/services/exceptions/ErrorCodes' | ||
import { logError } from '@/services/exceptions' | ||
import ErrorMessage from '@/components/tx/ErrorMessage' | ||
import { asError } from '@/services/exceptions/utils' | ||
|
||
enum ExportFieldNames { | ||
password = 'password', | ||
pk = 'pk', | ||
} | ||
|
||
type ExportFormData = { | ||
[ExportFieldNames.password]: string | ||
[ExportFieldNames.pk]: string | undefined | ||
} | ||
|
||
const ExportMPCAccountModal = ({ onClose, open }: { onClose: () => void; open: boolean }) => { | ||
const { exportPk } = useContext(MpcWalletContext) | ||
const [error, setError] = useState<string>() | ||
|
||
const [showPassword, setShowPassword] = useState(false) | ||
const formMethods = useForm<ExportFormData>({ | ||
mode: 'all', | ||
defaultValues: { | ||
[ExportFieldNames.password]: '', | ||
}, | ||
}) | ||
const { register, formState, handleSubmit, setValue, watch, reset } = formMethods | ||
|
||
const exportedKey = watch(ExportFieldNames.pk) | ||
|
||
const onSubmit = async (data: ExportFormData) => { | ||
try { | ||
setError(undefined) | ||
const pk = await exportPk(data[ExportFieldNames.password]) | ||
setValue(ExportFieldNames.pk, pk) | ||
} catch (err) { | ||
logError(ErrorCodes._305, err) | ||
setError(asError(err).message) | ||
} | ||
} | ||
|
||
const handleClose = () => { | ||
setError(undefined) | ||
reset() | ||
onClose() | ||
} | ||
return ( | ||
<ModalDialog open={open} onClose={handleClose}> | ||
<DialogTitle> | ||
<Typography variant="h6" fontWeight={700}> | ||
Export your account | ||
</Typography> | ||
</DialogTitle> | ||
<IconButton className={css.close} aria-label="close" onClick={handleClose} size="small"> | ||
<Close fontSize="large" /> | ||
</IconButton> | ||
|
||
<DialogContent> | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<Box display="flex" flexDirection="column" gap={2} alignItems="flex-start" sx={{ width: '100%' }}> | ||
<Typography>For security reasons you have to enter your password to reveal your account key.</Typography> | ||
|
||
{exportedKey ? ( | ||
<Box display="flex" flexDirection="row" alignItems="center" gap={1} width="100%"> | ||
<TextField | ||
fullWidth | ||
multiline={showPassword} | ||
maxRows={3} | ||
label="Private key" | ||
type="password" | ||
InputProps={{ | ||
readOnly: true, | ||
endAdornment: ( | ||
<> | ||
<IconButton size="small" onClick={() => setShowPassword((prev) => !prev)}> | ||
{showPassword ? <VisibilityOff fontSize="small" /> : <Visibility fontSize="small" />} | ||
</IconButton> | ||
<CopyButton text={exportedKey} /> | ||
</> | ||
), | ||
}} | ||
{...register(ExportFieldNames.pk)} | ||
/> | ||
</Box> | ||
) : ( | ||
<> | ||
<TextField | ||
placeholder="Password" | ||
label="Password" | ||
type="password" | ||
fullWidth | ||
error={!!formState.errors[ExportFieldNames.password]} | ||
helperText={formState.errors[ExportFieldNames.password]?.message} | ||
{...register(ExportFieldNames.password, { | ||
required: true, | ||
})} | ||
/> | ||
</> | ||
)} | ||
{error && <ErrorMessage className={css.modalError}>{error}</ErrorMessage>} | ||
|
||
<Box display="flex" flexDirection="row" justifyContent="space-between" alignItems="center" width="100%"> | ||
<Button variant="outlined" onClick={handleClose}> | ||
Close | ||
</Button> | ||
{exportedKey === undefined && ( | ||
<Button color="primary" variant="contained" disabled={formState.isSubmitting} type="submit"> | ||
Export | ||
</Button> | ||
)} | ||
</Box> | ||
</Box> | ||
</form> | ||
</DialogContent> | ||
</ModalDialog> | ||
) | ||
} | ||
|
||
export default ExportMPCAccountModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Alert, Box, Button, Typography } from '@mui/material' | ||
import { useState } from 'react' | ||
import ExportMPCAccountModal from './ExportMPCAccountModal' | ||
|
||
const ExportMPCAccount = () => { | ||
const [isModalOpen, setIsModalOpen] = useState(false) | ||
|
||
return ( | ||
<> | ||
<Box display="flex" flexDirection="column" gap={2} alignItems="flex-start"> | ||
<Typography> | ||
Accounts created via Google can be exported and imported to any non-custodial wallet outside of Safe. | ||
</Typography> | ||
<Alert severity="warning"> | ||
Never disclose your keys or seed phrase to anyone. If someone gains access to them, they have full access over | ||
your signer account. | ||
</Alert> | ||
<Button color="primary" variant="contained" disabled={isModalOpen} onClick={() => setIsModalOpen(true)}> | ||
Reveal private key | ||
</Button> | ||
</Box> | ||
<ExportMPCAccountModal onClose={() => setIsModalOpen(false)} open={isModalOpen} /> | ||
</> | ||
) | ||
} | ||
|
||
export default ExportMPCAccount |
10 changes: 10 additions & 0 deletions
10
src/components/settings/ExportMPCAccount/styles.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.close { | ||
position: absolute; | ||
right: var(--space-1); | ||
top: var(--space-1); | ||
} | ||
|
||
.modalError { | ||
width: 100%; | ||
margin: 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters