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' export function CreateSecretKeyForm() { const [nsec, setNsec] = useState(''); const [npub, setNpub] = useState(''); const regenerateKey = () => { 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); } // Schlüssel generieren, wenn die Komponente zum ersten Mal gerendert wird 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 (