import React, { useState, useEffect } from 'react'; import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { generateSecretKey, getPublicKey } from 'nostr-tools/pure' import { nip19 } from "nostr-tools" import { Label } from "../ui/label" import { bytesToHex, hexToBytes } from '@noble/hashes/utils' import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Shield, Key, Copy, RefreshCw, ArrowRight } from 'lucide-react' import { Card, CardContent } from "@/components/ui/card" export function CreateSecretKeyForm() { const [nsec, setNsec] = useState(''); const [npub, setNpub] = useState(''); const [nsecCopied, setNsecCopied] = useState(false); const [npubCopied, setNpubCopied] = useState(false); const [isGenerating, setIsGenerating] = useState(true); const regenerateKey = () => { setIsGenerating(true); // Add a small delay to show the loading state setTimeout(() => { let sk = generateSecretKey(); // `sk` is a Uint8Array let pk = getPublicKey(sk); // `pk` is a hex string let newNpub = nip19.npubEncode(pk); // `npub` is a string let newNsec = nip19.nsecEncode(sk); // `nsec` is a string setNsec(newNsec); setNpub(newNpub); setIsGenerating(false); }, 500); } const copyToClipboard = (text: string, setStateFunc: React.Dispatch>) => { navigator.clipboard.writeText(text); setStateFunc(true); setTimeout(() => setStateFunc(false), 2000); }; // Generate keys when the component is first rendered useEffect(() => { if(localStorage.getItem("nsec")) { const nsecString = localStorage.getItem('nsec') || ''; const nsecBytes = hexToBytes(nsecString); setNsec(nip19.nsecEncode(nsecBytes)); setNpub(nip19.npubEncode(getPublicKey(nsecBytes))); } else { regenerateKey(); } }, []); return (
Important security notice Your secret key gives full control of your account. It's stored locally in your browser and never sent to any server. Make sure to back it up securely.
) }