import { NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'; import { getPublicKey, nip19 } from 'nostr-tools'; import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { toast } from 'sonner'; import { useNDK } from '@libs/ndk/provider'; import { useStorage } from '@libs/storage/provider'; export function NewPrivkeyScreen() { const { db } = useStorage(); const { ndk } = useNDK(); const [nsec, setNsec] = useState(''); const navigate = useNavigate(); const save = async (content: string) => { return await db.secureSave(db.account.pubkey, content); }; const submit = async (isSave?: boolean) => { try { if (!nsec.startsWith('nsec1')) return toast.info('You must enter a private key starts with nsec'); const decoded = nip19.decode(nsec); if (decoded.type !== 'nsec') return toast.info('You must enter a valid nsec'); const privkey = decoded.data; const pubkey = getPublicKey(privkey); if (pubkey !== db.account.pubkey) return toast.info( 'Your nsec is not match your current public key, please make sure you enter right nsec' ); const signer = new NDKPrivateKeySigner(privkey); ndk.signer = signer; if (isSave) await save(privkey); navigate(-1); } catch (e) { toast.error(e); } }; return (