import React, { useState, useMemo, useEffect } from "react"; import { useNavigate } from "react-router-dom"; import { SnackbarProvider, enqueueSnackbar } from "notistack"; import canonicalStringify from "canonical-json"; import { Select, MenuItem, Box, Typography, } from "@mui/material"; import LoadingButton from '@mui/lab/LoadingButton'; import { utf8ToHex } from "@walletconnect/encoding"; import { useWalletConnectContext } from "../context/WalletConnectContext"; const SignWithNitroKey = () => { const { session, signClient, checkPersistedState } = useWalletConnectContext(); useEffect(() => { if (signClient && !session) { checkPersistedState(signClient); } }, [session, signClient, checkPersistedState]); const navigate = useNavigate(); const [ethAddress, setEthAddress] = useState(""); const [ethSignature, setEthSignature] = useState(""); const [cosmosAddress, setCosmosAddress] = useState(""); const [isLoading, setIsLoading] = useState(false); const message = useMemo(() => { return { msg: "Register my account as a participant on the Laconic network", address: ethAddress, }; }, [ethAddress]); const signEth = async () => { if (session && signClient) { try { setIsLoading(true) const jsonMessage = canonicalStringify(message); const hexMsg = utf8ToHex(jsonMessage, true); const receivedEthSig: string = await signClient!.request({ topic: session!.topic, chainId: "eip155:1", request: { method: "personal_sign", params: [hexMsg, ethAddress], }, }); setIsLoading(false) setEthSignature(ethSignature); navigate("/user-verification", { state: { message, cosmosAddress, receivedEthSig, }, }); } catch (error) { console.log("err in signing ", error); setIsLoading(false) enqueueSnackbar("Error signing message", { variant: "error" }); } } }; return (
{session ? ( Sign with Nitro key Select Cosmos account: Select Nitro account: {(Boolean(ethAddress) && Boolean(cosmosAddress)) && (
{canonicalStringify(message, null, 2)} 
)} Sign using Nitro key
) : ( <>Loading... )}
); }; export default SignWithNitroKey;