import { useArk } from "@lume/ark"; import { LoaderIcon } from "@lume/icons"; import { useStorage } from "@lume/storage"; import NDK, { NDKNip46Signer, NDKPrivateKeySigner } from "@nostr-dev-kit/ndk"; import { nip19 } from "nostr-tools"; import { useState } from "react"; import { useForm } from "react-hook-form"; import { useNavigate } from "react-router-dom"; import { toast } from "sonner"; export function LoginWithNsecbunker() { const ark = useArk(); const storage = useStorage(); const navigate = useNavigate(); const [loading, setLoading] = useState(false); const { register, handleSubmit, setError, formState: { errors, isValid }, } = useForm(); const onSubmit = async (data: { npub: string }) => { try { if (!data.npub.startsWith("npub1")) return toast.info("You need to enter a token start with npub1"); if (!data.npub.includes("#")) return toast.info("Token must include #secret"); setLoading(true); const bunker = new NDK({ explicitRelayUrls: [ "wss://relay.nsecbunker.com", "wss://nostr.vulpem.com", ], }); await bunker.connect(2000); const pubkey = nip19.decode(data.npub.split("#")[0]).data as string; const localSigner = NDKPrivateKeySigner.generate(); const remoteSigner = new NDKNip46Signer(bunker, data.npub, localSigner); await remoteSigner.blockUntilReady(); ark.updateNostrSigner({ signer: remoteSigner }); await storage.createSetting("nsecbunker", "1"); const account = await storage.createAccount({ pubkey: pubkey, privkey: localSigner.privateKey, }); ark.account = account; return navigate("/auth/onboarding", { replace: true }); } catch (e) { setLoading(false); setError("npub", { type: "manual", message: String(e), }); } }; return (

Enter your nsecbunker token

{errors.npub && (

{errors.npub.message as string}

)}
); }