import { appConfigDir } from '@tauri-apps/api/path'; import { Stronghold } from '@tauri-apps/plugin-stronghold'; import { getPublicKey, nip19 } from 'nostr-tools'; import { useState } from 'react'; import { Resolver, useForm } from 'react-hook-form'; import { Link, useNavigate } from 'react-router-dom'; import { useStorage } from '@libs/storage/provider'; import { EyeOffIcon, EyeOnIcon, LoaderIcon } from '@shared/icons'; import { useStronghold } from '@stores/stronghold'; type FormValues = { password: string; privkey: string; }; const resolver: Resolver = async (values) => { return { values: values.password ? values : {}, errors: !values.password ? { password: { type: 'required', message: 'This is required.', }, } : {}, }; }; export function ResetScreen() { const navigate = useNavigate(); const setPrivkey = useStronghold((state) => state.setPrivkey); const [passwordInput, setPasswordInput] = useState('password'); const [loading, setLoading] = useState(false); const { db } = useStorage(); // toggle private key const showPassword = () => { if (passwordInput === 'password') { setPasswordInput('text'); } else { setPasswordInput('password'); } }; const { register, setError, handleSubmit, formState: { errors, isDirty, isValid }, } = useForm({ resolver }); const onSubmit = async (data: { [x: string]: string }) => { setLoading(true); if (data.password.length > 3) { try { let privkey = data.privkey; if (privkey.startsWith('nsec')) { privkey = nip19.decode(privkey).data as string; } const tmpPubkey = getPublicKey(privkey); if (tmpPubkey !== db.account.pubkey) { setLoading(false); setError('password', { type: 'custom', message: "Private key don't match current account store in database, please check again", }); } else { // remove old stronghold await db.secureReset(); // save privkey to secure storage const dir = await appConfigDir(); const stronghold = await Stronghold.load( `${dir}/lume.stronghold`, data.password ); if (!db.secureDB) db.secureDB = stronghold; await db.secureSave(db.account.pubkey, db.account.privkey); // add privkey to state setPrivkey(db.account.privkey); // redirect to home navigate('/auth/unlock', { replace: true }); } } catch { setLoading(false); setError('password', { type: 'custom', message: 'Invalid private key', }); } } else { setLoading(false); setError('password', { type: 'custom', message: 'Password is required and must be greater than 3', }); } }; return (

Reset unlock password

{errors.password &&

{errors.password.message}

}
Back
); }