import * as yup from "yup" import {useFormik} from "formik" import {MdCheckCircle, MdChevronRight, MdLock} from "react-icons/md" import {generateKey} from "openpgp" import React, {ReactElement, useMemo} from "react" import passwordGenerator from "secure-random-password" import {LoadingButton} from "@mui/lab" import {Box, Grid, InputAdornment, Typography} from "@mui/material" import {useMutation} from "@tanstack/react-query" import {PasswordField} from "~/components" import {buildEncryptionPassword, encryptString} from "~/utils" import {isDev} from "~/constants/development" import {useUser} from "~/hooks" import {MASTER_PASSWORD_LENGTH} from "~/constants/values" interface Form { password: string passwordConfirmation: string detail?: string } const schema = yup.object().shape({ password: yup.string().required(), passwordConfirmation: yup .string() .required() .oneOf([yup.ref("password"), null], "Passwords must match"), }) export default function PasswordForm(): ReactElement { const user = useUser() const {} = useMutation<>() const awaitGenerateKey = useMemo( () => generateKey({ type: "rsa", format: "armored", curve: "curve25519", userIDs: [{name: "John Smith", email: "john@example.com"}], passphrase: "", rsaBits: isDev ? 2048 : 4096, }), [], ) const formik = useFormik
({ validationSchema: schema, initialValues: { password: "", passwordConfirmation: "", }, onSubmit: async (values, {setErrors}) => { try { const keyPair = await awaitGenerateKey const masterPassword = passwordGenerator.randomPassword({ length: MASTER_PASSWORD_LENGTH, }) const encryptionPassword = buildEncryptionPassword( values.password, user.email.address, ) const encryptedMasterPassword = encryptString( masterPassword, `${values.password}-${user.email.address}`, ) const encryptedPrivateKey = encryptString( keyPair.privateKey, masterPassword, ) } catch (error) { setErrors({detail: "An error occurred"}) } }, }) return ( Set up your password Please enter a safe password so that we can encrypt your data. ), }} /> ), }} /> } > Continue
) }