import { useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import * as secp from '@noble/secp256k1'; import { bech32 } from "bech32"; import { setPrivateKey, setNip07PubKey } from "../state/Login"; export default function LoginPage() { const dispatch = useDispatch(); const navigate = useNavigate(); const publicKey = useSelector(s => s.login.publicKey); const [key, setKey] = useState(""); function doLogin() { if (key.startsWith("nsec")) { let nKey = bech32.decode(key); let buff = bech32.fromWords(nKey.words); let hexKey = secp.utils.bytesToHex(Uint8Array.from(buff)); if (secp.utils.isValidPrivateKey(hexKey)) { dispatch(setPrivateKey(hexKey)); } else { throw "INVALID PRIVATE KEY"; } } else { if (secp.utils.isValidPrivateKey(key)) { dispatch(setPrivateKey(key)); } else { throw "INVALID PRIVATE KEY"; } } } async function doNip07Login() { let pubKey = await window.nostr.getPublicKey(); dispatch(setNip07PubKey(pubKey)); } function altLogins() { let nip07 = 'nostr' in window; if (!nip07) { return null; } return ( <>

Other Login Methods

doNip07Login()}>Login with Extension (NIP-07)
) } useEffect(() => { if (publicKey) { navigate("/"); } }, [publicKey]); return ( <>

Login

Enter your private key:

setKey(e.target.value)} />
doLogin()}>Login
{altLogins()} ); }