@@ -124,12 +124,12 @@ const domainChecks: Record<string, (value: any) => any> = {
124124function getBaseEncoder ( type : string ) : null | ( ( value : any ) => string ) {
125125 // intXX and uintXX
126126 {
127- const match = type . match ( / ^ ( u ? ) i n t ( \d * ) $ / ) ;
127+ const match = type . match ( / ^ ( u ? ) i n t ( \d + ) $ / ) ;
128128 if ( match ) {
129129 const signed = ( match [ 1 ] === "" ) ;
130130
131- const width = parseInt ( match [ 2 ] || "256" ) ;
132- assertArgument ( width % 8 === 0 && width !== 0 && width <= 256 && ( match [ 2 ] == null || match [ 2 ] === String ( width ) ) , "invalid numeric width" , "type" , type ) ;
131+ const width = parseInt ( match [ 2 ] ) ;
132+ assertArgument ( width % 8 === 0 && width !== 0 && width <= 256 && match [ 2 ] === String ( width ) , "invalid numeric width" , "type" , type ) ;
133133
134134 const boundsUpper = mask ( BN_MAX_UINT256 , signed ? ( width - 1 ) : width ) ;
135135 const boundsLower = signed ? ( ( boundsUpper + BN_1 ) * BN__1 ) : BN_0 ;
@@ -220,8 +220,7 @@ export class TypedDataEncoder {
220220 * do not violate the [[link-eip-712]] structural constraints as
221221 * well as computes the [[primaryType]].
222222 */
223- constructor ( types : Record < string , Array < TypedDataField > > ) {
224- this . #types = JSON . stringify ( types ) ;
223+ constructor ( _types : Record < string , Array < TypedDataField > > ) {
225224 this . #fullTypes = new Map ( ) ;
226225 this . #encoderCache = new Map ( ) ;
227226
@@ -234,30 +233,39 @@ export class TypedDataEncoder {
234233 // Link all subtypes within a given struct
235234 const subtypes : Map < string , Set < string > > = new Map ( ) ;
236235
237- Object . keys ( types ) . forEach ( ( type ) => {
236+ const types : Record < string , Array < TypedDataField > > = { } ;
237+ Object . keys ( _types ) . forEach ( ( type ) => {
238+ // Normalize int/uint unless they are a complex type themselves
239+ types [ type ] = _types [ type ] . map ( ( { name, type } ) => {
240+ if ( type === "int" && ! _types [ "int" ] ) { type = "int256" ; }
241+ if ( type === "uint" && ! _types [ "uint" ] ) { type = "uint256" ; }
242+ return { name, type } ;
243+ } ) ;
244+
238245 links . set ( type , new Set ( ) ) ;
239246 parents . set ( type , [ ] ) ;
240247 subtypes . set ( type , new Set ( ) ) ;
241248 } ) ;
249+ this . #types = JSON . stringify ( types ) ;
242250
243251 for ( const name in types ) {
244252 const uniqueNames : Set < string > = new Set ( ) ;
245253
246254 for ( const field of types [ name ] ) {
247255
248256 // Check each field has a unique name
249- assertArgument ( ! uniqueNames . has ( field . name ) , `duplicate variable name ${ JSON . stringify ( field . name ) } in ${ JSON . stringify ( name ) } ` , "types" , types ) ;
257+ assertArgument ( ! uniqueNames . has ( field . name ) , `duplicate variable name ${ JSON . stringify ( field . name ) } in ${ JSON . stringify ( name ) } ` , "types" , _types ) ;
250258 uniqueNames . add ( field . name ) ;
251259
252260 // Get the base type (drop any array specifiers)
253261 const baseType = ( < any > ( field . type . match ( / ^ ( [ ^ \x5b ] * ) ( \x5b | $ ) / ) ) ) [ 1 ] || null ;
254- assertArgument ( baseType !== name , `circular type reference to ${ JSON . stringify ( baseType ) } ` , "types" , types ) ;
262+ assertArgument ( baseType !== name , `circular type reference to ${ JSON . stringify ( baseType ) } ` , "types" , _types ) ;
255263
256264 // Is this a base encoding type?
257265 const encoder = getBaseEncoder ( baseType ) ;
258266 if ( encoder ) { continue ; }
259267
260- assertArgument ( parents . has ( baseType ) , `unknown type ${ JSON . stringify ( baseType ) } ` , "types" , types ) ;
268+ assertArgument ( parents . has ( baseType ) , `unknown type ${ JSON . stringify ( baseType ) } ` , "types" , _types ) ;
261269
262270 // Add linkage
263271 ( parents . get ( baseType ) as Array < string > ) . push ( name ) ;
@@ -267,14 +275,14 @@ export class TypedDataEncoder {
267275
268276 // Deduce the primary type
269277 const primaryTypes = Array . from ( parents . keys ( ) ) . filter ( ( n ) => ( ( parents . get ( n ) as Array < string > ) . length === 0 ) ) ;
270- assertArgument ( primaryTypes . length !== 0 , "missing primary type" , "types" , types ) ;
271- assertArgument ( primaryTypes . length === 1 , `ambiguous primary types or unused types: ${ primaryTypes . map ( ( t ) => ( JSON . stringify ( t ) ) ) . join ( ", " ) } ` , "types" , types ) ;
278+ assertArgument ( primaryTypes . length !== 0 , "missing primary type" , "types" , _types ) ;
279+ assertArgument ( primaryTypes . length === 1 , `ambiguous primary types or unused types: ${ primaryTypes . map ( ( t ) => ( JSON . stringify ( t ) ) ) . join ( ", " ) } ` , "types" , _types ) ;
272280
273281 defineProperties < TypedDataEncoder > ( this , { primaryType : primaryTypes [ 0 ] } ) ;
274282
275283 // Check for circular type references
276284 function checkCircular ( type : string , found : Set < string > ) {
277- assertArgument ( ! found . has ( type ) , `circular type reference to ${ JSON . stringify ( type ) } ` , "types" , types ) ;
285+ assertArgument ( ! found . has ( type ) , `circular type reference to ${ JSON . stringify ( type ) } ` , "types" , _types ) ;
278286
279287 found . add ( type ) ;
280288
0 commit comments