ghost-extension-wallet/src/containers/WalletPopup/pages/SignRequest.tsx
Uncle Fatso d3274222f9
rewrite extension around new UX
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
2026-10-08 18:25:56 +03:00

196 lines
8.3 KiB
TypeScript

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<boolean>(false)
const [userSignedExtensions, setUserSignedExtensions] = useState<Partial<UserSignedExtensionsTy>>({})
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 (
<Layout2>
<h1 className="text-3xl font-bold mb-6 px-6">Loading Sign Request #{signRequestId}</h1>
</Layout2>
)
}
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 (
<Layout2>
<div className="flex flex-col justify-between">
<div className="">
<div className="py-2 px-6">
{activeAddress && (
<div className="mt-1 text-xs text-white flex items-center justify-center gap-1.5 font-mono">
<span className="inline-block w-1.5 h-1.5 rounded-full bg-emerald-400 animate-pulse"></span>
{`Active Account: ${activeAddress.slice(0, 8)}...${activeAddress.slice(-8)}`}
</div>
)}
{isExecutionBlocked && (
<div className="mt-4 p-3.5 rounded-xl border border-destructive/30 bg-destructive/15 text-destructive-foreground text-xs space-y-1">
<span className="font-bold flex items-center gap-1 text-red-400">
Signing Request Blocked
</span>
<p className="text-white/80 leading-relaxed">
{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'}".`}
</p>
</div>
)}
</div>
{!isRequestNotFound && (
<div className="w-full flex flex-col gap-1 pt-2 px-6">
<div className="w-full flex flex-row justify-between items-center">
<span className="text-xs font-semibold text-white">Origin</span>
<span className="text-xs font-medium text-primary truncate max-w-[280px] select-all font-mono" title={request.url}>
{request.url}
</span>
</div>
<div className="w-full flex flex-row justify-between items-center">
<span className="text-xs font-semibold text-white">Chain</span>
<span className="text-xs font-medium text-primary font-mono">
{wellKnownChainIdByGenesisHash[request.chainId] || "Unknown Chain Spec"}
</span>
</div>
<div className="w-full flex flex-col min-w-0 mt-6">
<div className="text-xs font-semibold mb-2 text-white uppercase tracking-wider">
Transaction Details
</div>
<div className="w-full min-w-0">
<DecodedCallData chainId={request.chainId} callData={request.callData} />
</div>
</div>
<div className="my-2">
{request.userSignedExtensions.type === "names" ? (
<UserSignedExtensionInputs
userSignedExtensionNames={request.userSignedExtensions.names}
onChange={setUserSignedExtensions}
/>
) : (
<UserSignedExtensions
userSignedExtensions={request.userSignedExtensions.values}
/>
)}
</div>
</div>
)}
</div>
<div className="w-full flex gap-4 justify-center space-x-4 pt-6 px-6">
<Button
variant="secondary"
disabled={inAction}
size="full"
onClick={() => rpc.client.cancelSignRequest(signRequestId)}
>
Cancel
</Button>
<Button
variant="primary"
size="full"
disabled={isExecutionBlocked || inAction}
onClick={() => handleSigning()}
>
Approve
</Button>
</div>
</div>
</Layout2>
)
}