224 lines
9.2 KiB
JavaScript
224 lines
9.2 KiB
JavaScript
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 (
|
|
<Modal
|
|
maxWidth="426px"
|
|
minHeight="200px"
|
|
open={isOpen}
|
|
headerContent={<Typography variant="h4">Verify {item ? item.type : ""}</Typography>}
|
|
onClose={!isPending && onClose}
|
|
topRight={
|
|
<GhostStyledIcon
|
|
viewBox="0 0 23 23"
|
|
component={CloseIcon}
|
|
onClick={onClose}
|
|
/>
|
|
}
|
|
>
|
|
<Box display="flex" flexDirection="column">
|
|
<Box mb="15px" display="flex" flexDirection="column" gap="1px" sx={{ "& .DataRow-root": { margin: "0 !important" } }}>
|
|
<DataRow
|
|
title={<Typography variant="caption">New Group Key</Typography>}
|
|
tooltip="Some cool description here."
|
|
balance={<Link
|
|
href={`${nativeMetadata.url}/address/${upcomingGroupKey}`}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
>
|
|
<Typography variant="caption">{
|
|
upcomingGroupKey ? shorten(upcomingGroupKey, 12, -8) : ""
|
|
}</Typography>
|
|
</Link>}
|
|
/>
|
|
|
|
<DataRow
|
|
title={<Typography variant="caption">Verification Reward</Typography>}
|
|
tooltip="Some cool description here."
|
|
balance={<Typography variant="caption">{
|
|
formatCurrency(
|
|
currentTwapPrice((gasPrice ? gasPrice : 0n) * (gasUsed ? gasUsed : 0n)).toString(),
|
|
5,
|
|
ftsoSymbol
|
|
)
|
|
}</Typography>}
|
|
/>
|
|
|
|
<Divider sx={{ my: "15px", width: "100%" }} />
|
|
|
|
<DataRow
|
|
title={<Typography variant="caption">Raw Message</Typography>}
|
|
tooltip="Some cool description here."
|
|
balance={evmMessageMetadata?.at(0)
|
|
? <Link
|
|
style={{ display: "flex", flexDirection: "row", justifyContent: "center", alignItems: "center" }}
|
|
onClick={() => copyToClipboard(toHex(evmMessageMetadata.at(0)), 0)}
|
|
>
|
|
<Typography variant="caption">
|
|
{shorten(toHex(evmMessageMetadata.at(0)), 12, -10)}
|
|
</Typography>
|
|
<GhostStyledIcon
|
|
sx={{ marginLeft: "5px", width: "12px", height: "12px" }}
|
|
viewBox="0 0 25 25"
|
|
component={copiedIndex === 0 ? CheckIcon : ContentPasteIcon}
|
|
/>
|
|
</Link>
|
|
: <Skeleton width="185px" />
|
|
}
|
|
/>
|
|
|
|
<DataRow
|
|
title={<Typography variant="caption">Nonce</Typography>}
|
|
tooltip="Some cool description here."
|
|
balance={evmMessageMetadata?.at(1)
|
|
? <Link
|
|
style={{ display: "flex", flexDirection: "row", justifyContent: "center", alignItems: "center" }}
|
|
onClick={() => copyToClipboard(toHex(evmMessageMetadata.at(1)), 1)}
|
|
>
|
|
<Typography variant="caption">
|
|
{shorten(toHex(evmMessageMetadata.at(1)), 12, -10)}
|
|
</Typography>
|
|
<GhostStyledIcon
|
|
sx={{ marginLeft: "5px", width: "12px", height: "12px" }}
|
|
viewBox="0 0 25 25"
|
|
component={copiedIndex === 1 ? CheckIcon : ContentPasteIcon}
|
|
/>
|
|
</Link>
|
|
: <Skeleton width="185px" />
|
|
}
|
|
/>
|
|
|
|
<DataRow
|
|
title={<Typography variant="caption">Signture</Typography>}
|
|
tooltip="Some cool description here."
|
|
balance={evmMessageMetadata?.at(2)
|
|
? <Link
|
|
style={{ display: "flex", flexDirection: "row", justifyContent: "center", alignItems: "center" }}
|
|
onClick={() => copyToClipboard(toHex(evmMessageMetadata.at(2)), 2)}
|
|
>
|
|
<Typography variant="caption">
|
|
{shorten(toHex(evmMessageMetadata.at(2)), 12, -10)}
|
|
</Typography>
|
|
<GhostStyledIcon
|
|
sx={{ marginLeft: "5px", width: "12px", height: "12px" }}
|
|
viewBox="0 0 25 25"
|
|
component={copiedIndex === 2 ? CheckIcon : ContentPasteIcon}
|
|
/>
|
|
</Link>
|
|
: <Skeleton width="185px" />
|
|
}
|
|
/>
|
|
</Box>
|
|
|
|
<PrimaryButton
|
|
fullWidth
|
|
disabled={isPending || gatekeeperIsEmpty || evmMessageMetadataLoading || !evmMessageMetadata}
|
|
onClick={verifySignatureModal}
|
|
>
|
|
{isPending ? "Pending..." : "Verify"}
|
|
</PrimaryButton>
|
|
</Box>
|
|
</Modal>
|
|
)
|
|
}
|