1- import { type Hex , getAddress } from "viem" ;
1+ import { type Hex , getAddress , pad } from "viem" ;
22import { z , type infer as zodInfer } from "zod" ;
33
44export enum ValidationErrors {
@@ -60,6 +60,68 @@ export const hexDataSchema = z
6060 . regex ( hexDataPattern , { message : "not valid hex data" } )
6161 . transform ( ( val ) => val . toLowerCase ( ) as Hex ) ;
6262
63+ const signedAuthorizationSchema = z . union ( [
64+ z . object ( {
65+ contractAddress : addressSchema ,
66+ chainId : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
67+ nonce : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
68+ r : hexDataSchema . transform ( ( val ) => val as Hex ) ,
69+ s : hexDataSchema . transform ( ( val ) => val as Hex ) ,
70+ v : hexNumberSchema . optional ( ) ,
71+ yParity : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
72+ } ) ,
73+ z . object ( {
74+ address : addressSchema ,
75+ chainId : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
76+ nonce : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
77+ r : hexDataSchema . transform ( ( val ) => val as Hex ) ,
78+ s : hexDataSchema . transform ( ( val ) => val as Hex ) ,
79+ v : hexNumberSchema . optional ( ) ,
80+ yParity : hexNumberSchema . transform ( ( val ) => Number ( val ) ) ,
81+ } ) ,
82+ ] ) ;
83+
84+ const partialAuthorizationSchema = z . union ( [
85+ z . object ( {
86+ contractAddress : addressSchema ,
87+ chainId : hexNumberSchema
88+ . optional ( )
89+ . transform ( ( val ) => ( val ? Number ( val ) : 1 ) ) ,
90+ nonce : hexNumberSchema
91+ . optional ( )
92+ . transform ( ( val ) => ( val ? Number ( val ) : 0 ) ) ,
93+ r : hexDataSchema
94+ . optional ( )
95+ . transform ( ( val ) => ( val as Hex ) ?? pad ( "0x" , { size : 32 } ) ) ,
96+ s : hexDataSchema
97+ . optional ( )
98+ . transform ( ( val ) => ( val as Hex ) ?? pad ( "0x" , { size : 32 } ) ) ,
99+ v : hexNumberSchema . optional ( ) ,
100+ yParity : hexNumberSchema
101+ . optional ( )
102+ . transform ( ( val ) => ( val ? Number ( val ) : 0 ) ) ,
103+ } ) ,
104+ z . object ( {
105+ address : addressSchema ,
106+ chainId : hexNumberSchema
107+ . optional ( )
108+ . transform ( ( val ) => ( val ? Number ( val ) : 1 ) ) ,
109+ nonce : hexNumberSchema
110+ . optional ( )
111+ . transform ( ( val ) => ( val ? Number ( val ) : 0 ) ) ,
112+ r : hexDataSchema
113+ . optional ( )
114+ . transform ( ( val ) => ( val as Hex ) ?? pad ( "0x" , { size : 32 } ) ) ,
115+ s : hexDataSchema
116+ . optional ( )
117+ . transform ( ( val ) => ( val as Hex ) ?? pad ( "0x" , { size : 32 } ) ) ,
118+ v : hexNumberSchema . optional ( ) ,
119+ yParity : hexNumberSchema
120+ . optional ( )
121+ . transform ( ( val ) => ( val ? Number ( val ) : 0 ) ) ,
122+ } ) ,
123+ ] ) ;
124+
63125const userOperationSchemaPaymasterV6 = z
64126 . object ( {
65127 sender : addressSchema ,
@@ -71,7 +133,60 @@ const userOperationSchemaPaymasterV6 = z
71133 preVerificationGas : hexNumberSchema . default ( 1n ) ,
72134 maxPriorityFeePerGas : hexNumberSchema ,
73135 maxFeePerGas : hexNumberSchema ,
74- paymasterAndData : hexDataSchema
136+ paymasterAndData : z
137+ . union ( [ hexDataSchema , z . literal ( "" ) ] )
138+ . optional ( )
139+ . transform ( ( val ) => {
140+ if ( val === "" || val === undefined ) {
141+ return "0x" ;
142+ }
143+ return val ;
144+ } ) ,
145+ signature : z
146+ . union ( [ hexDataSchema , z . literal ( "" ) ] )
147+ . optional ( )
148+ . transform ( ( val ) => {
149+ if ( val === "" || val === undefined ) {
150+ return "0x" ;
151+ }
152+ return val ;
153+ } ) ,
154+ eip7702Auth : signedAuthorizationSchema . optional ( ) . nullable ( ) ,
155+ } )
156+ . strict ( )
157+ . transform ( ( val ) => val ) ;
158+
159+ const userOperationSchemaPaymasterV7 = z
160+ . object ( {
161+ sender : addressSchema ,
162+ nonce : hexNumberSchema ,
163+ factory : addressSchema
164+ . nullable ( )
165+ . optional ( )
166+ . transform ( ( val ) => val ?? undefined ) ,
167+ factoryData : hexDataSchema
168+ . nullable ( )
169+ . optional ( )
170+ . transform ( ( val ) => val ?? undefined ) ,
171+ callData : hexDataSchema ,
172+ callGasLimit : hexNumberSchema . default ( 1n ) ,
173+ verificationGasLimit : hexNumberSchema . default ( 1n ) ,
174+ preVerificationGas : hexNumberSchema . default ( 1n ) ,
175+ maxFeePerGas : hexNumberSchema ,
176+ maxPriorityFeePerGas : hexNumberSchema ,
177+ paymaster : addressSchema
178+ . nullable ( )
179+ . optional ( )
180+ . transform ( ( val ) => val ?? undefined ) ,
181+ paymasterVerificationGasLimit : hexNumberSchema
182+ . nullable ( )
183+ . optional ( )
184+ . transform ( ( val ) => val ?? undefined ) ,
185+ paymasterPostOpGasLimit : hexNumberSchema
186+ . nullable ( )
187+ . optional ( )
188+ . transform ( ( val ) => val ?? undefined ) ,
189+ paymasterData : hexDataSchema
75190 . nullable ( )
76191 . optional ( )
77192 . transform ( ( val ) => val ?? undefined ) ,
@@ -81,18 +196,24 @@ const userOperationSchemaPaymasterV6 = z
81196 }
82197 return val ;
83198 } ) ,
199+ eip7702Auth : signedAuthorizationSchema . optional ( ) . nullable ( ) ,
84200 } )
85201 . strict ( )
86- . transform ( ( val ) => {
87- return val ;
88- } ) ;
202+ . transform ( ( val ) => val ) ;
89203
90- const userOperationSchemaPaymasterV7 = z
204+ const userOperationSchemaPaymasterV8 = z
91205 . object ( {
92206 sender : addressSchema ,
93207 nonce : hexNumberSchema ,
94- factory : addressSchema . optional ( ) . transform ( ( val ) => val ?? undefined ) ,
95- factoryData : hexDataSchema . optional ( ) . transform ( ( val ) => val ?? undefined ) ,
208+ factory : z
209+ . union ( [ addressSchema , z . literal ( "0x7702" ) ] )
210+ . nullable ( )
211+ . optional ( )
212+ . transform ( ( val ) => val ?? undefined ) ,
213+ factoryData : hexDataSchema
214+ . nullable ( )
215+ . optional ( )
216+ . transform ( ( val ) => val ?? undefined ) ,
96217 callData : hexDataSchema ,
97218 callGasLimit : hexNumberSchema . default ( 1n ) ,
98219 verificationGasLimit : hexNumberSchema . default ( 1n ) ,
@@ -121,6 +242,7 @@ const userOperationSchemaPaymasterV7 = z
121242 }
122243 return val ;
123244 } ) ,
245+ eip7702Auth : signedAuthorizationSchema . optional ( ) . nullable ( ) ,
124246 } )
125247 . strict ( )
126248 . transform ( ( val ) => {
@@ -167,6 +289,7 @@ const eip7677UserOperationSchemaV6 = z
167289 . transform ( ( _ ) => {
168290 return "0x" as Hex ;
169291 } ) ,
292+ eip7702Auth : signedAuthorizationSchema . optional ( ) . nullable ( ) ,
170293 } )
171294 . strict ( )
172295 . transform ( ( val ) => {
@@ -213,15 +336,66 @@ const eip7677UserOperationSchemaV7 = z
213336 }
214337 return val ;
215338 } ) ,
339+ eip7702Auth : signedAuthorizationSchema . optional ( ) . nullable ( ) ,
216340 } )
217341 . strict ( )
218342 . transform ( ( val ) => {
219343 return val ;
220344 } ) ;
221345
346+ const eip7677UserOperationSchemaV8 = z
347+ . object ( {
348+ sender : addressSchema ,
349+ nonce : hexNumberSchema ,
350+ factory : z
351+ . union ( [ addressSchema , z . literal ( "0x7702" ) ] )
352+ . nullable ( )
353+ . optional ( )
354+ . transform ( ( val ) => val ?? null ) ,
355+ factoryData : hexDataSchema
356+ . nullable ( )
357+ . optional ( )
358+ . transform ( ( val ) => val ?? null ) ,
359+ callData : hexDataSchema ,
360+ callGasLimit : hexNumberSchema ,
361+ verificationGasLimit : hexNumberSchema ,
362+ preVerificationGas : hexNumberSchema ,
363+ maxFeePerGas : hexNumberSchema ,
364+ maxPriorityFeePerGas : hexNumberSchema ,
365+ paymaster : addressSchema
366+ . nullable ( )
367+ . optional ( )
368+ . transform ( ( val ) => val ?? null ) ,
369+ paymasterVerificationGasLimit : hexNumberSchema
370+ . nullable ( )
371+ . optional ( )
372+ . transform ( ( val ) => val ?? null ) ,
373+ paymasterPostOpGasLimit : hexNumberSchema
374+ . nullable ( )
375+ . optional ( )
376+ . transform ( ( val ) => val ?? null ) ,
377+ paymasterData : hexDataSchema
378+ . nullable ( )
379+ . optional ( )
380+ . transform ( ( val ) => val ?? null ) ,
381+ signature : hexDataSchema . optional ( ) . transform ( ( val ) => {
382+ return val ?? "0x" ;
383+ } ) ,
384+ eip7702Auth : partialAuthorizationSchema . optional ( ) . nullable ( ) ,
385+ } )
386+ . strict ( )
387+ . transform ( ( val ) => val ) ;
388+
222389const eip7677UserOperationSchema = z . union ( [
223390 eip7677UserOperationSchemaV6 ,
224391 eip7677UserOperationSchemaV7 ,
392+ eip7677UserOperationSchemaV8 ,
393+ ] ) ;
394+
395+ const userOperationSchemaPaymaster = z . union ( [
396+ userOperationSchemaPaymasterV6 ,
397+ userOperationSchemaPaymasterV7 ,
398+ userOperationSchemaPaymasterV8 ,
225399] ) ;
226400
227401const paymasterContextSchema = z . union ( [
@@ -236,12 +410,9 @@ const paymasterContextSchema = z.union([
236410
237411export const pmSponsorUserOperationParamsSchema = z
238412 . union ( [
413+ z . tuple ( [ userOperationSchemaPaymaster , addressSchema ] ) ,
239414 z . tuple ( [
240- z . union ( [ userOperationSchemaPaymasterV6 , userOperationSchemaPaymasterV7 ] ) ,
241- addressSchema ,
242- ] ) ,
243- z . tuple ( [
244- z . union ( [ userOperationSchemaPaymasterV6 , userOperationSchemaPaymasterV7 ] ) ,
415+ userOperationSchemaPaymaster ,
245416 addressSchema ,
246417 paymasterContextSchema . nullable ( ) ,
247418 ] ) ,
@@ -286,6 +457,4 @@ export const pimlicoGetTokenQuotesSchema = z.tuple([
286457 hexNumberSchema ,
287458] ) ;
288459
289- export type UserOperationV7 = zodInfer < typeof userOperationSchemaPaymasterV7 > ;
290- export type UserOperationV6 = zodInfer < typeof userOperationSchemaPaymasterV6 > ;
291460export type JsonRpcSchema = zodInfer < typeof jsonRpcSchema > ;
0 commit comments