import { useArk } from "@lume/ark"; import { CheckIcon, EyeOffIcon, EyeOnIcon, LoaderIcon } from "@lume/icons"; import { useStorage } from "@lume/storage"; import { onboardingAtom } from "@lume/utils"; import { NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import * as Checkbox from "@radix-ui/react-checkbox"; import { desktopDir } from "@tauri-apps/api/path"; import { save } from "@tauri-apps/plugin-dialog"; import { writeTextFile } from "@tauri-apps/plugin-fs"; import { useSetAtom } from "jotai"; import { nanoid } from "nanoid"; import { getPublicKey, nip19 } from "nostr-tools"; import { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; import { toast } from "sonner"; export function CreateAccountKeys() { const ark = useArk(); const storage = useStorage(); const setOnboarding = useSetAtom(onboardingAtom); const navigate = useNavigate(); const [key, setKey] = useState(""); const [loading, setLoading] = useState(false); const [showKey, setShowKey] = useState(false); const [confirm, setConfirm] = useState({ c1: false, c2: false, c3: false }); const submit = async () => { try { setLoading(true); const privkey = nip19.decode(key).data as string; const signer = new NDKPrivateKeySigner(privkey); const pubkey = getPublicKey(privkey); ark.updateNostrSigner({ signer }); const downloadPath = await desktopDir(); const fileName = `nostr_keys_${nanoid(4)}.txt`; const filePath = await save({ defaultPath: `${downloadPath}/${fileName}`, }); if (!filePath) { return toast.info("You need to save account keys before continue."); } await writeTextFile( filePath, `Nostr Account\nGenerated by Lume (lume.nu)\n---\nPrivate key: ${key}`, ); const newAccount = await storage.createAccount({ pubkey: pubkey, privkey: privkey, }); ark.account = newAccount; setLoading(false); setOnboarding({ open: true, newUser: true }); return navigate("/auth/onboarding", { replace: true }); } catch (e) { setLoading(false); toast.error(String(e)); } }; useEffect(() => { const privkey = NDKPrivateKeySigner.generate().privateKey; setKey(nip19.nsecEncode(privkey)); }, []); return (
Keep your key in safe place. If you lose this key, you will lose access to your account.