import { appConfigDir } from '@tauri-apps/api/path'; import { useState } from 'react'; import { Resolver, useForm } from 'react-hook-form'; import { Link, useNavigate } from 'react-router-dom'; import { Stronghold } from 'tauri-plugin-stronghold-api'; import { useStorage } from '@libs/storage/provider'; import { ArrowRightCircleIcon, EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons'; import { User } from '@shared/user'; import { useStronghold } from '@stores/stronghold'; type FormValues = { password: string; }; const resolver: Resolver = async (values) => { return { values: values.password ? values : {}, errors: !values.password ? { password: { type: 'required', message: 'This is required.', }, } : {}, }; }; export function UnlockScreen() { const navigate = useNavigate(); const setPrivkey = useStronghold((state) => state.setPrivkey); const setWalletConnectURL = useStronghold((state) => state.setWalletConnectURL); const resetStronghold = useStronghold((state) => state.reset); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const { db } = useStorage(); const { register, setError, handleSubmit, formState: { errors, isDirty, isValid }, } = useForm({ resolver }); const onSubmit = async (data: { [x: string]: string }) => { try { setLoading(true); const dir = await appConfigDir(); const stronghold = await Stronghold.load(`${dir}/lume.stronghold`, data.password); if (!db.secureDB) db.secureDB = stronghold; const privkey = await db.secureLoad(db.account.pubkey); const uri = await db.secureLoad('walletConnectURL', 'nwc'); if (privkey) setPrivkey(privkey); if (uri) setWalletConnectURL(uri); // redirect to home navigate('/', { replace: true }); } catch (e) { setLoading(false); setError('password', { type: 'custom', message: e, }); } }; const logout = async () => { // remove account db.accountLogout(); // reset stronghold resetStronghold(); // redirect to welcome screen navigate('/auth/welcome'); }; return (

Enter password to unlock

{errors.password &&

{errors.password.message}

}

Forgot password?

Reset password if you still have a private key
); }