From 079f4f61075143105f5024930b3eda26c38057fd Mon Sep 17 00:00:00 2001 From: Nino Serec Date: Thu, 5 Jun 2025 17:48:37 +0200 Subject: [PATCH] Added typed data example --- .../agw-client/actions/signTypedData.mdx | 159 ++++++++++++++++++ docs.json | 1 + 2 files changed, 160 insertions(+) create mode 100644 abstract-global-wallet/agw-client/actions/signTypedData.mdx diff --git a/abstract-global-wallet/agw-client/actions/signTypedData.mdx b/abstract-global-wallet/agw-client/actions/signTypedData.mdx new file mode 100644 index 0000000..9e2175e --- /dev/null +++ b/abstract-global-wallet/agw-client/actions/signTypedData.mdx @@ -0,0 +1,159 @@ +--- +title: "signTypedData" +description: Function to sign structured data using the connected Abstract Global Wallet. +--- + +The [AbstractClient](/abstract-global-wallet/agw-client/createAbstractClient) +includes a `signTypedData` method that can be used to sign structured data using the connected Abstract Global Wallet. +The method follows the [EIP-712](https://eips.ethereum.org/EIPS/eip-712) standard for typed structured data hashing and signing. + + + View an example implementation of signing and verifying structured data with AGW in a + Next.js app. + + +## Usage + + + +```tsx Signing (client-side) +import { useAbstractClient } from "@abstract-foundation/agw-react"; + +export default function SignTypedData() { + const { data: agwClient } = useAbstractClient(); + + async function signTypedData() { + if (!agwClient) return; + + const signature = await agwClient.signTypedData({ + domain: { + name: "Ether Mail", + version: "1", + chainId: 11124, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + }, + types: { + Person: [ + { name: "name", type: "string" }, + { name: "wallet", type: "address" }, + ], + Mail: [ + { name: "from", type: "Person" }, + { name: "to", type: "Person" }, + { name: "contents", type: "string" }, + ], + }, + primaryType: "Mail", + message: { + from: { + name: "Cow", + wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", + }, + to: { + name: "Bob", + wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", + }, + contents: "Hello, Bob!", + }, + }); + } + + return ; +} +``` + +```tsx Verifying (server-side) +import { createPublicClient, http } from "viem"; +import { abstractTestnet } from "viem/chains"; + +// Create a public client to verify the typed data +const publicClient = createPublicClient({ + chain: abstractTestnet, + transport: http(), +}); + +// Verify the typed data signature +const isValid = await publicClient.verifyTypedData({ + address: walletAddress, // The AGW address you expect to have signed the data + domain: { + name: "Ether Mail", + version: "1", + chainId: 11124, + verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC", + }, + types: { + Person: [ + { name: "name", type: "string" }, + { name: "wallet", type: "address" }, + ], + Mail: [ + { name: "from", type: "Person" }, + { name: "to", type: "Person" }, + { name: "contents", type: "string" }, + ], + }, + primaryType: "Mail", + message: { + from: { + name: "Cow", + wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826", + }, + to: { + name: "Bob", + wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB", + }, + contents: "Hello, Bob!", + }, + signature, +}); +``` + + + + + **Alternative**: You can also use Wagmi's [useSignTypedData](https://wagmi.sh/react/api/hooks/useSignTypedData) hook for signing typed data with any connected wallet, not just Abstract Global Wallet. + + +## Parameters + + + The EIP-712 domain separator containing information about the signing domain. + + + + The user-readable name of the signing domain (e.g., dApp name). + + + The current major version of the signing domain. + + + The chain ID of the network (e.g., 11124 for Abstract testnet). + + + The address of the contract that will verify the signature. + + + An optional disambiguating salt for the protocol. + + + + + + The type definitions for the structured data. Each type is defined as an array of fields with names and types. + + + + The primary type to hash. Must be a key in the types object. + + + + The structured data to sign. Must conform to the structure defined in types[primaryType]. + + +## Returns + +Returns a `Promise` containing the signature of the structured data. \ No newline at end of file diff --git a/docs.json b/docs.json index 799a1e0..b974256 100644 --- a/docs.json +++ b/docs.json @@ -179,6 +179,7 @@ "abstract-global-wallet/agw-client/actions/sendTransaction", "abstract-global-wallet/agw-client/actions/sendTransactionBatch", "abstract-global-wallet/agw-client/actions/signMessage", + "abstract-global-wallet/agw-client/actions/signTypedData", "abstract-global-wallet/agw-client/actions/writeContract", "abstract-global-wallet/agw-react/hooks/useWriteContractSponsored" ]