|
| 1 | +import type { |
| 2 | + Account, |
| 3 | + Address, |
| 4 | + Chain, |
| 5 | + Client, |
| 6 | + Transport, |
| 7 | + WalletClient, |
| 8 | +} from 'viem'; |
| 9 | +import { BaseError } from 'viem'; |
| 10 | +import { parseAccount } from 'viem/accounts'; |
| 11 | + |
| 12 | +import { prepareSignUserOperationTypedData } from '../userOp'; |
| 13 | +import type { UserOperationV07 } from '../userOp'; |
| 14 | + |
| 15 | +export type SignUserOperationParameters = { |
| 16 | + /** Account to sign with */ |
| 17 | + account?: Account | Address; |
| 18 | + /** The user operation to sign */ |
| 19 | + userOperation: Omit<UserOperationV07, 'signature'>; |
| 20 | + /** The entry point contract address */ |
| 21 | + entryPoint: { address: Address }; |
| 22 | + /** The chain ID that the entry point is deployed on */ |
| 23 | + chainId: number; |
| 24 | + /** The address of the smart account */ |
| 25 | + address: Address; |
| 26 | + /** The name of the domain of the implementation contract */ |
| 27 | + name: 'HybridDeleGator' | 'MultiSigDeleGator'; |
| 28 | + /** The version of the domain of the implementation contract */ |
| 29 | + version?: string; |
| 30 | +}; |
| 31 | + |
| 32 | +export type SignUserOperationReturnType = `0x${string}`; |
| 33 | + |
| 34 | +/** |
| 35 | + * Signs a user operation using a wallet client. |
| 36 | + * @param client - The wallet client to use for signing. |
| 37 | + * @param parameters - The parameters for signing the user operation. |
| 38 | + * @returns The signature of the user operation. |
| 39 | + * @example |
| 40 | + * ```ts |
| 41 | + * const signature = await signUserOperation(walletClient, { |
| 42 | + * userOperation: { |
| 43 | + * sender: '0x...', |
| 44 | + * nonce: 0n, |
| 45 | + * callData: '0x', |
| 46 | + * callGasLimit: 1000000n, |
| 47 | + * verificationGasLimit: 1000000n, |
| 48 | + * preVerificationGas: 21000n, |
| 49 | + * maxFeePerGas: 1000000000n, |
| 50 | + * maxPriorityFeePerGas: 1000000000n |
| 51 | + * }, |
| 52 | + * entryPoint: { address: '0x...' }, |
| 53 | + * chainId: 1, |
| 54 | + * address: '0x...', |
| 55 | + * name: 'HybridDeleGator' |
| 56 | + * }); |
| 57 | + * ``` |
| 58 | + */ |
| 59 | +export async function signUserOperation< |
| 60 | + TChain extends Chain | undefined, |
| 61 | + TAccount extends Account | undefined, |
| 62 | +>( |
| 63 | + client: Client<Transport, TChain, TAccount> & { |
| 64 | + signTypedData: WalletClient['signTypedData']; |
| 65 | + }, |
| 66 | + parameters: SignUserOperationParameters, |
| 67 | +): Promise<SignUserOperationReturnType> { |
| 68 | + const { |
| 69 | + account: accountParam = client.account, |
| 70 | + userOperation, |
| 71 | + entryPoint, |
| 72 | + chainId, |
| 73 | + name, |
| 74 | + address, |
| 75 | + version = '1', |
| 76 | + } = parameters; |
| 77 | + |
| 78 | + if (!accountParam) { |
| 79 | + throw new BaseError('Account not found. Please provide an account.'); |
| 80 | + } |
| 81 | + |
| 82 | + const account = parseAccount(accountParam); |
| 83 | + |
| 84 | + const typedData = prepareSignUserOperationTypedData({ |
| 85 | + userOperation, |
| 86 | + entryPoint, |
| 87 | + chainId, |
| 88 | + name, |
| 89 | + address, |
| 90 | + version, |
| 91 | + }); |
| 92 | + |
| 93 | + return client.signTypedData({ |
| 94 | + account, |
| 95 | + ...typedData, |
| 96 | + }); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Creates a sign user operation action that can be used to extend a wallet client. |
| 101 | + * @returns A function that can be used with wallet client extend method. |
| 102 | + * @example |
| 103 | + * ```ts |
| 104 | + * const walletClient = createWalletClient({ |
| 105 | + * chain: mainnet, |
| 106 | + * transport: http() |
| 107 | + * }).extend(signUserOperationActions()); |
| 108 | + * ``` |
| 109 | + */ |
| 110 | +export function signUserOperationActions() { |
| 111 | + return < |
| 112 | + TChain extends Chain | undefined, |
| 113 | + TAccount extends Account | undefined, |
| 114 | + >( |
| 115 | + client: Client<Transport, TChain, TAccount> & { |
| 116 | + signTypedData: WalletClient['signTypedData']; |
| 117 | + }, |
| 118 | + ) => ({ |
| 119 | + signUserOperation: async ( |
| 120 | + parameters: Omit<SignUserOperationParameters, 'chainId'> & { |
| 121 | + chainId?: number; |
| 122 | + }, |
| 123 | + ) => |
| 124 | + signUserOperation(client, { |
| 125 | + chainId: |
| 126 | + parameters.chainId ?? |
| 127 | + (() => { |
| 128 | + if (!client.chain?.id) { |
| 129 | + throw new BaseError( |
| 130 | + 'Chain ID is required. Either provide it in parameters or configure the client with a chain.', |
| 131 | + ); |
| 132 | + } |
| 133 | + return client.chain.id; |
| 134 | + })(), |
| 135 | + ...parameters, |
| 136 | + }), |
| 137 | + }); |
| 138 | +} |
0 commit comments