@@ -238,19 +238,18 @@ func TypedDataFromJSON(typedDataJSON string) (*TypedData, error) {
238238}
239239
240240func (t * TypedData ) UnmarshalJSON (data []byte ) error {
241- // Create an intermediate structure using json.Number
241+ // Intermediary structure to decode message field
242242 type TypedDataRaw struct {
243243 Types TypedDataTypes `json:"types"`
244244 PrimaryType string `json:"primaryType"`
245245 Domain TypedDataDomain `json:"domain"`
246246 Message map [string ]interface {} `json:"message"`
247247 }
248248
249- // Create a decoder that will preserve number strings
249+ // Json decoder with json.Number support
250250 dec := json .NewDecoder (bytes .NewReader (data ))
251251 dec .UseNumber ()
252252
253- // First unmarshal into the intermediate structure
254253 var raw TypedDataRaw
255254 if err := dec .Decode (& raw ); err != nil {
256255 return err
@@ -278,15 +277,18 @@ func (t *TypedData) UnmarshalJSON(data []byte) error {
278277 }
279278 }
280279
281- // ..
280+ // Check primary type is defined
281+ if raw .PrimaryType == "" {
282+ return fmt .Errorf ("primary type is required" )
283+ }
282284 primaryDomainType , ok := raw .Types [raw .PrimaryType ]
283285 if ! ok {
284- return fmt .Errorf ("primary type %s is not defined" , raw .PrimaryType )
286+ return fmt .Errorf ("primary type '%s' is not defined" , raw .PrimaryType )
285287 }
286288 primaryDomainTypeMap := typedDataTypeMap (primaryDomainType )
287289 fmt .Println ("===> primaryDomainType" , primaryDomainTypeMap )
288290
289- // Process the Message map to convert values to desired types
291+ // Decode the message map into the typedData struct
290292 processedMessage := make (map [string ]interface {})
291293 for k , v := range raw .Message {
292294 fmt .Println ("===> k" , k , "v" , v )
@@ -297,45 +299,23 @@ func (t *TypedData) UnmarshalJSON(data []byte) error {
297299 }
298300 fmt .Println ("===> typ" , k , typ )
299301
300- // TODO: its possible that the type is a struct, and we need to do another call to get the typedData map, etc
301-
302- switch val := v .(type ) {
303- case json.Number :
304- // TODO: we will check the domain, etc.........
305-
306- if typ == "uint8" {
307- num , err := val .Int64 ()
308- if err != nil {
309- return fmt .Errorf ("failed to parse uint8 value %s, because %w" , val , err )
310- }
311- // TODO: is this okay ... int64 to uint8 ..???...
312- processedMessage [k ] = uint8 (num )
313- } else {
314- // Try parsing as big.Int first
315- if n , ok := new (big.Int ).SetString (string (val ), 10 ); ok {
316- processedMessage [k ] = n
317- } else {
318- // If it's not a valid integer, keep the original value
319- processedMessage [k ] = v
320- }
321- }
322-
323- case string :
324- if typ == "address" {
325- addr := common .HexToAddress (val )
326- processedMessage [k ] = addr
327- } else if len (val ) > 2 && (val [:2 ] == "0x" || val [:2 ] == "0X" ) {
328- // Convert hex strings to *big.Int
329- n := new (big.Int )
330- n .SetString (val [2 :], 16 )
331- processedMessage [k ] = n
332- } else {
333- processedMessage [k ] = val
302+ // ...
303+ customType , ok := raw .Types [typ ]
304+ if ok {
305+ val := fmt .Sprintf ("%v" , v )
306+ fmt .Println ("===> customType" , customType , val )
307+ // processedMessage[k] = val
308+
309+ // ............
310+ // ..
311+
312+ } else {
313+ val := fmt .Sprintf ("%v" , v )
314+ out , err := ABIUnmarshalStringValuesAny ([]string {typ }, []any {val })
315+ if err != nil {
316+ return fmt .Errorf ("failed to unmarshal string value for type %s with argument name %s, because %w" , typ , k , err )
334317 }
335-
336- default :
337- // TODO: prob needs to be recursive.. cuz might be some array or object ..
338- return fmt .Errorf ("unsupported type %T for value %v" , v , v )
318+ processedMessage [k ] = out [0 ]
339319 }
340320 }
341321
0 commit comments