|
| 1 | +import type { OpenfortEmbeddedEthereumWalletProvider } from '../types/wallet' |
| 2 | + |
| 3 | +/** |
| 4 | + * EIP-712 domain fields in canonical order. |
| 5 | + * |
| 6 | + * `eth_signTypedData_v4` requires an explicit `EIP712Domain` entry in `types`, but |
| 7 | + * the typed data produced by most contract libraries (ethers, viem, Safe/Zodiac, |
| 8 | + * GnosisPay account-kit, ...) omits it. We re-add only the fields that are present, |
| 9 | + * in canonical order, so the digest matches what the verifying contract computes. |
| 10 | + */ |
| 11 | +const EIP712_DOMAIN_FIELDS: { name: string; type: string }[] = [ |
| 12 | + { name: 'name', type: 'string' }, |
| 13 | + { name: 'version', type: 'string' }, |
| 14 | + { name: 'chainId', type: 'uint256' }, |
| 15 | + { name: 'verifyingContract', type: 'address' }, |
| 16 | + { name: 'salt', type: 'bytes32' }, |
| 17 | +] |
| 18 | + |
| 19 | +/** |
| 20 | + * Ethers-style EIP-712 typed data (i.e. without an `EIP712Domain` entry in `types`). |
| 21 | + */ |
| 22 | +export interface TypedDataInput { |
| 23 | + /** EIP-712 domain separator fields (e.g. `chainId`, `verifyingContract`). */ |
| 24 | + domain: Record<string, unknown> |
| 25 | + /** Struct definitions, keyed by type name (no `EIP712Domain` needed). */ |
| 26 | + types: Record<string, { name: string; type: string }[]> |
| 27 | + /** The primary struct being signed. */ |
| 28 | + primaryType: string |
| 29 | + /** The values for the primary struct. */ |
| 30 | + message: Record<string, unknown> |
| 31 | +} |
| 32 | + |
| 33 | +function domainTypes(domain: Record<string, unknown>): { name: string; type: string }[] { |
| 34 | + return EIP712_DOMAIN_FIELDS.filter((field) => domain[field.name] !== undefined) |
| 35 | +} |
| 36 | + |
| 37 | +function bigintReplacer(_key: string, value: unknown): unknown { |
| 38 | + return typeof value === 'bigint' ? value.toString() : value |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Sign EIP-712 typed data with an embedded wallet via `eth_signTypedData_v4`. |
| 43 | + * |
| 44 | + * Pass typed data in the ethers/viem shape (`{ domain, types, primaryType, message }`) |
| 45 | + * exactly as produced by contract SDKs. This helper re-adds the required `EIP712Domain` |
| 46 | + * type (in canonical field order) and serializes any `bigint` values, so the resulting |
| 47 | + * signature recovers correctly on-chain. |
| 48 | + * |
| 49 | + * The wallet should be an EOA (`AccountTypeEnum.EOA`) so the signature is plain ECDSA; |
| 50 | + * smart-account owners require ERC-1271 contract signatures, which this does not produce. |
| 51 | + * |
| 52 | + * @param provider - EIP-1193 provider from a connected embedded Ethereum wallet |
| 53 | + * @param address - The signer address (the active embedded wallet) |
| 54 | + * @param typedData - Ethers-style typed data: `{ domain, types, primaryType, message }` |
| 55 | + * @returns The hex signature |
| 56 | + * |
| 57 | + * @example |
| 58 | + * ```ts |
| 59 | + * import { signTypedData, useEmbeddedEthereumWallet } from '@openfort/react-native' |
| 60 | + * |
| 61 | + * const wallet = useEmbeddedEthereumWallet() |
| 62 | + * if (wallet.status === 'connected') { |
| 63 | + * const signature = await signTypedData(wallet.provider, wallet.activeWallet.address, { |
| 64 | + * domain: { name: 'MyApp', version: '1', chainId: 1, verifyingContract: '0xCcCC...' }, |
| 65 | + * primaryType: 'Mail', |
| 66 | + * types: { Mail: [{ name: 'contents', type: 'string' }] }, |
| 67 | + * message: { contents: 'Hello, Openfort' }, |
| 68 | + * }) |
| 69 | + * } |
| 70 | + * ``` |
| 71 | + */ |
| 72 | +export async function signTypedData( |
| 73 | + provider: OpenfortEmbeddedEthereumWalletProvider, |
| 74 | + address: string, |
| 75 | + typedData: TypedDataInput |
| 76 | +): Promise<string> { |
| 77 | + const payload = JSON.stringify( |
| 78 | + { |
| 79 | + domain: typedData.domain, |
| 80 | + primaryType: typedData.primaryType, |
| 81 | + types: { EIP712Domain: domainTypes(typedData.domain), ...typedData.types }, |
| 82 | + message: typedData.message, |
| 83 | + }, |
| 84 | + bigintReplacer |
| 85 | + ) |
| 86 | + |
| 87 | + const signature = await provider.request({ |
| 88 | + method: 'eth_signTypedData_v4', |
| 89 | + params: [address.toLowerCase(), payload], |
| 90 | + }) |
| 91 | + |
| 92 | + return signature as string |
| 93 | +} |
0 commit comments