import { useState, useMemo } from "react" import { useParams } from "react-router-dom" import useSWR from "swr" import { rpc } from "../api" import { UserSignedExtensions as UserSignedExtensionsTy } from "@/types/UserSignedExtension" import { wellKnownChainIdByGenesisHash } from "@/constants" import { Layout2 } from "@/components/Layout2" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" import { Textarea } from "@/components/ui/textarea" import { DecodedCallData, UserSignedExtensionInputs, UserSignedExtensions, } from "../components" import { useAccounts, useChain } from "../hooks" export const SignRequest = () => { const { currentChain, isLoadingChain } = useChain() const { activeAccount, accountTree, requestPassword, isFetchingActiveAccounts, isFetchingAccountTree, } = useAccounts() const { signRequestId } = useParams<{ signRequestId: string }>() const [inAction, setInAction] = useState(false) const [userSignedExtensions, setUserSignedExtensions] = useState>({}) const { data: signRequests, error, isLoading, } = useSWR(signRequestId ? ["getSignRequest"] : null, () => rpc.client.getSignRequests(), ) const chainId = useMemo(() => currentChain?.chainId, [currentChain]) const activeAddress = useMemo(() => { if (!activeAccount || !accountTree) return undefined let targetAddress = accountTree.address if (activeAccount.path) { const matchingDerivation = accountTree.derivations.find( (d) => d.path === activeAccount.path ) if (matchingDerivation) { targetAddress = matchingDerivation.address } } return targetAddress }, [activeAccount, accountTree]) if (isLoading || isLoadingChain || isFetchingActiveAccounts || isFetchingAccountTree) { return (

Loading Sign Request #{signRequestId}

) } if (!signRequestId) { window.close() return null } const request = signRequests?.[signRequestId] const isRequestNotFound = !request || error const isAddressMismatched = !!(request && activeAddress && request.address !== activeAddress) const isChainMismatched = !!(request && chainId && request.chainId !== chainId) const isExecutionBlocked = isRequestNotFound || isAddressMismatched || isChainMismatched const handleSigning = async () => { setInAction(true) try { const cleanExtensions = JSON.parse( JSON.stringify(userSignedExtensions, (_, value) => typeof value === "bigint" ? value.toString() : value ) ) await requestPassword(async (password) => { await rpc.client.approveSignRequest( signRequestId, cleanExtensions, password ) }) } catch { setInAction(false) } } console.log(isExecutionBlocked, inAction) return (
{activeAddress && (
{`Active Account: ${activeAddress.slice(0, 8)}...${activeAddress.slice(-8)}`}
)} {isExecutionBlocked && (
Signing Request Blocked

{isRequestNotFound && "This transaction payload could not be recovered or found by the proxy client pipeline."} {isAddressMismatched && `Identity Mismatch! The DApp is requesting a signature for address but your wallet extension is currently does not expose it". Change your active profile inside the extension to proceed.`} {isChainMismatched && `Network Mismatch! This transaction was generated for chain context "${wellKnownChainIdByGenesisHash[request.chainId] || 'Unknown'} ", but your extension is currently connected to "${wellKnownChainIdByGenesisHash[chainId] || 'Unknown'}".`}

)}
{!isRequestNotFound && (
Origin {request.url}
Chain {wellKnownChainIdByGenesisHash[request.chainId] || "Unknown Chain Spec"}
Transaction Details
{request.userSignedExtensions.type === "names" ? ( ) : ( )}
)}
) }