-
Notifications
You must be signed in to change notification settings - Fork 2
feat(iframe): add request popup for eth_signTypedData_v4 requests #705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ultraviolet10
wants to merge
3
commits into
master
Choose a base branch
from
aritra/eth_signTypedData_v4
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@happy.tech/iframe": minor | ||
| --- | ||
|
|
||
| Add request popup handler for eth_signTypedData_v4. |
243 changes: 243 additions & 0 deletions
243
apps/iframe/src/components/requests/EthSignTypedDatav4.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,243 @@ | ||||||
| import { isAddress, stringify } from "@happy.tech/common" | ||||||
| import type { TypedData, TypedDataDomain } from "viem" | ||||||
| import { getAppURL } from "#src/utils/appURL.ts" | ||||||
| import DisclosureSection from "./common/DisclosureSection" | ||||||
| import { | ||||||
| FormattedDetailsLine, | ||||||
| Layout, | ||||||
| LinkToAddress, | ||||||
| SectionBlock, | ||||||
| SubsectionBlock, | ||||||
| SubsectionContent, | ||||||
| SubsectionTitle, | ||||||
| } from "./common/Layout" | ||||||
| import type { RequestConfirmationProps } from "./props" | ||||||
|
|
||||||
| /** | ||||||
| * Utility guard to determine if a given value is a non-null object (excluding arrays). | ||||||
| * | ||||||
| * This is used to safely recurse into nested structs defined in EIP-712 typed data. | ||||||
| */ | ||||||
| function isObject(value: unknown): value is Record<string, unknown> { | ||||||
| return typeof value === "object" && value !== null && !Array.isArray(value) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Renders an individual field in the EIP-712 message. | ||||||
| * | ||||||
| * If the field is a valid Ethereum address, it is rendered with a clickable explorer link. | ||||||
| * Boolean values are color-coded. Arrays are iterated. | ||||||
| * Otherwise, it is displayed as a plain string. | ||||||
| */ | ||||||
| function renderField(label: string, value: unknown) { | ||||||
| if (Array.isArray(value)) { | ||||||
| return ( | ||||||
| <> | ||||||
| <SubsectionTitle>{label}</SubsectionTitle> | ||||||
| <FormattedDetailsLine> | ||||||
| {value.map((item) => ( | ||||||
| <div key={stringify(item)} className="py-1"> | ||||||
| {typeof item === "string" && isAddress(item) ? ( | ||||||
| <LinkToAddress address={item}>{item}</LinkToAddress> | ||||||
| ) : ( | ||||||
| stringify(item) | ||||||
| )} | ||||||
| </div> | ||||||
| ))} | ||||||
| </FormattedDetailsLine> | ||||||
| </> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| if (typeof value === "boolean") { | ||||||
| return ( | ||||||
| <> | ||||||
| <SubsectionTitle>{label}</SubsectionTitle> | ||||||
| <FormattedDetailsLine> | ||||||
| <span className={value ? "text-success" : "text-error"}>{stringify(value)}</span> | ||||||
| </FormattedDetailsLine> | ||||||
| </> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| if (typeof value === "string" && isAddress(value)) { | ||||||
| return ( | ||||||
| <> | ||||||
| <SubsectionTitle>{label}</SubsectionTitle> | ||||||
| <FormattedDetailsLine> | ||||||
| <LinkToAddress address={value}>{value}</LinkToAddress> | ||||||
| </FormattedDetailsLine> | ||||||
| </> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| return ( | ||||||
| <> | ||||||
| <SubsectionTitle>{label}</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{stringify(value)}</FormattedDetailsLine> | ||||||
| </> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Format and display important domain information from the EIP-712 data. | ||||||
| */ | ||||||
| function renderDomainInfo(domain: TypedDataDomain) { | ||||||
| return ( | ||||||
| <SubsectionBlock> | ||||||
| <SubsectionTitle>Domain Information</SubsectionTitle> | ||||||
| <SubsectionContent> | ||||||
| {typeof domain.name === "string" && ( | ||||||
| <div> | ||||||
| <SubsectionTitle>Application</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{domain.name}</FormattedDetailsLine> | ||||||
| </div> | ||||||
| )} | ||||||
| {typeof domain.version === "string" && ( | ||||||
| <div> | ||||||
| <SubsectionTitle>Version</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{domain.version}</FormattedDetailsLine> | ||||||
| </div> | ||||||
| )} | ||||||
| {(typeof domain.chainId === "number" || typeof domain.chainId === "bigint") && ( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nipick/suggestion/preference 😄
Suggested change
|
||||||
| <div> | ||||||
| <SubsectionTitle>Chain ID</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{stringify(domain.chainId)}</FormattedDetailsLine> | ||||||
| </div> | ||||||
| )} | ||||||
| {typeof domain.verifyingContract === "string" && isAddress(domain.verifyingContract) && ( | ||||||
| <div> | ||||||
| <SubsectionTitle>Contract</SubsectionTitle> | ||||||
| <FormattedDetailsLine> | ||||||
| <LinkToAddress address={domain.verifyingContract}>{domain.verifyingContract}</LinkToAddress> | ||||||
| </FormattedDetailsLine> | ||||||
| </div> | ||||||
| )} | ||||||
| {domain.salt && ( | ||||||
| <div> | ||||||
| <SubsectionTitle>Salt</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{domain.salt}</FormattedDetailsLine> | ||||||
| </div> | ||||||
| )} | ||||||
| </SubsectionContent> | ||||||
| </SubsectionBlock> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Recursively renders an EIP-712 structured data message. | ||||||
| * | ||||||
| * This function walks the `types` definition provided in the typed data schema and | ||||||
| * matches it against the corresponding values in the `message` field. | ||||||
| */ | ||||||
| function renderTypedMessage(types: TypedData, typeName: string, message: Record<string, unknown>) { | ||||||
| const fields = types[typeName] | ||||||
| if (!fields) return null | ||||||
|
|
||||||
| return fields.map(({ name, type }) => { | ||||||
| const value = message[name] | ||||||
|
|
||||||
| if (type.endsWith("[]") && Array.isArray(value)) { | ||||||
| const baseType = type.slice(0, -2) | ||||||
| if (types[baseType as keyof typeof types]) { | ||||||
| return ( | ||||||
| <SubsectionBlock key={name}> | ||||||
| <SubsectionTitle>{name}</SubsectionTitle> | ||||||
| {value.map((item, index) => | ||||||
| isObject(item) ? ( | ||||||
| <div key={String(item)} className="pl-2 border-l-2 border-neutral/20 mt-2"> | ||||||
| <div className="text-xs opacity-50 mb-1">Item {index + 1}</div> | ||||||
| {renderTypedMessage(types, baseType, item)} | ||||||
| </div> | ||||||
| ) : null, | ||||||
| )} | ||||||
| </SubsectionBlock> | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Check if this is a custom type defined in the types object | ||||||
| const customType = type.split("[")[0] // Remove array notation if present | ||||||
| if (types[customType as keyof typeof types] && isObject(value)) { | ||||||
| return ( | ||||||
| <SubsectionBlock key={name}> | ||||||
| <SubsectionTitle>{name}</SubsectionTitle> | ||||||
| {renderTypedMessage(types, customType, value)} | ||||||
| </SubsectionBlock> | ||||||
| ) | ||||||
| } | ||||||
|
|
||||||
| return ( | ||||||
| <SubsectionBlock key={name}> | ||||||
| <SubsectionContent>{renderField(name, value)}</SubsectionContent> | ||||||
| </SubsectionBlock> | ||||||
| ) | ||||||
| }) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Component to render a confirmation UI for an `eth_signTypedData_v4` request. | ||||||
| * | ||||||
| * This request type is used to sign EIP-712 structured data on Ethereum. | ||||||
| * The component breaks down and visualizes domain details, message fields, and provides raw JSON. | ||||||
| */ | ||||||
| export const EthSignTypedDataV4 = ({ | ||||||
| method, | ||||||
| params, | ||||||
| reject, | ||||||
| accept, | ||||||
| }: RequestConfirmationProps<"eth_signTypedData_v4">) => { | ||||||
| const [, rawMessage] = params | ||||||
| const parsed = typeof rawMessage === "string" ? JSON.parse(rawMessage) : rawMessage | ||||||
|
|
||||||
| const origin = getAppURL() | ||||||
|
|
||||||
| return ( | ||||||
| <Layout | ||||||
| headline={<>Signature Request</>} | ||||||
| description={<>Please review the message you are about to sign.</>} | ||||||
| actions={{ | ||||||
| accept: { | ||||||
| children: "Sign", | ||||||
| onClick: () => accept({ method, params }), | ||||||
| }, | ||||||
| reject: { | ||||||
| children: "Go back", | ||||||
| onClick: reject, | ||||||
| }, | ||||||
| }} | ||||||
| > | ||||||
| <SectionBlock> | ||||||
| <SubsectionBlock> | ||||||
| <SubsectionTitle>Origin</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{origin}</FormattedDetailsLine> | ||||||
| </SubsectionBlock> | ||||||
|
|
||||||
| <SubsectionBlock> | ||||||
| <SubsectionTitle>Interacting with</SubsectionTitle> | ||||||
| <FormattedDetailsLine> | ||||||
| <LinkToAddress address={parsed.domain.verifyingContract} /> | ||||||
| </FormattedDetailsLine> | ||||||
| </SubsectionBlock> | ||||||
|
|
||||||
| {renderDomainInfo(parsed.domain)} | ||||||
|
|
||||||
| <SubsectionBlock> | ||||||
| <SubsectionTitle>Message Type</SubsectionTitle> | ||||||
| <FormattedDetailsLine>{parsed.primaryType}</FormattedDetailsLine> | ||||||
| </SubsectionBlock> | ||||||
|
|
||||||
| <SubsectionBlock> | ||||||
| <SubsectionTitle>Message</SubsectionTitle> | ||||||
| {renderTypedMessage(parsed.types, parsed.primaryType, parsed.message)} | ||||||
| </SubsectionBlock> | ||||||
| </SectionBlock> | ||||||
|
|
||||||
| <DisclosureSection title="Raw Request"> | ||||||
| <div className="grid gap-4 p-2"> | ||||||
| <FormattedDetailsLine isCode>{JSON.stringify(params, null, 2)}</FormattedDetailsLine> | ||||||
| </div> | ||||||
| </DisclosureSection> | ||||||
| </Layout> | ||||||
| ) | ||||||
| } | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably live in common utils i think 🫡