import useSWR from "swr"; import { useState, useMemo } from "react"; import { Box, Divider, Link, Skeleton, Typography } from "@mui/material"; import { keccak256, hexToBytes, getAddress, toHex } from 'viem'; import CloseIcon from '@mui/icons-material/Close'; import CheckIcon from '@mui/icons-material/Check'; import ContentPasteIcon from '@mui/icons-material/ContentPaste'; import Modal from "../../components/Modal/Modal"; import DataRow from "../../components/DataRow/DataRow"; import GhostStyledIcon from "../../components/Icon/GhostIcon"; import { PrimaryButton } from "../../components/Button"; import { formatCurrency, shorten } from "../../helpers"; import { verifySignature, useGatekeeperAddress } from "../../hooks/staking"; import { useUnstableProvider } from "../../hooks/ghost"; import { useLocalStorage } from "../../hooks/localstorage"; export const ExodusVerifyModal = ({ chainId, isOpen, onClose, nativeMetadata, address, casperApi, item, ftsoSymbol, gasPrice, gasUsed, currentTwapPrice, }) => { const [isPending, setIsPending] = useState(false); const [copiedIndex, setCopiedIndex] = useState(null); const { tryStoreInHistory, CONSTANTS } = useLocalStorage(); const { client } = useUnstableProvider(); const copyToClipboard = (text, index) => { navigator.clipboard.writeText(text).then(() => { setCopiedIndex(index); setTimeout(() => setCopiedIndex(null) , 800); }); }; const { gatekeeperAddress, gatekeeperIsEmpty } = useGatekeeperAddress(Number(item?.exodusContext.network_id)); const { data: evmMessageMetadata, isLoading: evmMessageMetadataLoading } = useSWR( client && item ? ['evmMessage', client, item] : null, async ([_, client, it]) => client.getUnsafeApi().apis.ExodusApi.get_evm_rotation_metadata( BigInt(it.dkgIndex), { type: "Evm" } ) ); const upcomingGroupKey = useMemo(() => { if (!evmMessageMetadata) return null; const callHex = toHex(evmMessageMetadata.at(0)); const extractedPxHex = `0x${callHex.slice(74, 138)}`; const cleanHex = extractedPxHex.startsWith('0x04') && extractedPxHex.length === 130 ? `0x${extractedPxHex.slice(4)}` : extractedPxHex.startsWith('0x') ? extractedPxHex : `0x${extractedPxHex}`; const hash = keccak256(cleanHex); return getAddress(`0x${hash.slice(-40)}`); }, [evmMessageMetadata]); const verifySignatureModal = async () => { setIsPending(true); try { const txHash = await verifySignature({ account: address, gatekeeper: gatekeeperAddress, chainId: Number(item.exodusContext.network_id), calldata: toHex(evmMessageMetadata.at(0)), nonce: toHex(evmMessageMetadata.at(1)), signature: toHex(evmMessageMetadata.at(2)), }); if (txHash) tryStoreInHistory( Number(item.exodusContext.network_id), address, CONSTANTS.bond.key, txHash, CONSTANTS.bond.bond ); onClose(); } finally { setIsPending(false); } } return ( Verify {item ? item.type : ""}} onClose={!isPending && onClose} topRight={ } > New Group Key} tooltip="Some cool description here." balance={ { upcomingGroupKey ? shorten(upcomingGroupKey, 12, -8) : "" } } /> Verification Reward} tooltip="Some cool description here." balance={{ formatCurrency( currentTwapPrice((gasPrice ? gasPrice : 0n) * (gasUsed ? gasUsed : 0n)).toString(), 5, ftsoSymbol ) }} /> Raw Message} tooltip="Some cool description here." balance={evmMessageMetadata?.at(0) ? copyToClipboard(toHex(evmMessageMetadata.at(0)), 0)} > {shorten(toHex(evmMessageMetadata.at(0)), 12, -10)} : } /> Nonce} tooltip="Some cool description here." balance={evmMessageMetadata?.at(1) ? copyToClipboard(toHex(evmMessageMetadata.at(1)), 1)} > {shorten(toHex(evmMessageMetadata.at(1)), 12, -10)} : } /> Signture} tooltip="Some cool description here." balance={evmMessageMetadata?.at(2) ? copyToClipboard(toHex(evmMessageMetadata.at(2)), 2)} > {shorten(toHex(evmMessageMetadata.at(2)), 12, -10)} : } /> {isPending ? "Pending..." : "Verify"} ) }