-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtyped_data_json.go
More file actions
340 lines (301 loc) · 9.28 KB
/
Copy pathtyped_data_json.go
File metadata and controls
340 lines (301 loc) · 9.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package ethcoder
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"slices"
"sort"
"strings"
"github.com/0xsequence/ethkit/go-ethereum/common"
)
func TypedDataFromJSON(typedDataJSON string) (*TypedData, error) {
var typedData TypedData
err := json.Unmarshal([]byte(typedDataJSON), &typedData)
if err != nil {
return nil, err
}
return &typedData, nil
}
func (t *TypedData) MarshalJSON() ([]byte, error) {
type TypedDataJSON struct {
Types TypedDataTypes `json:"types"`
PrimaryType string `json:"primaryType"`
Domain TypedDataDomain `json:"domain"`
Message map[string]interface{} `json:"message"`
}
encodedMessage, err := t.jsonEncodeMessageValues(t.PrimaryType, t.Message)
if err != nil {
return nil, err
}
return json.Marshal(TypedDataJSON{
Types: t.Types,
PrimaryType: t.PrimaryType,
Domain: t.Domain,
Message: encodedMessage,
})
}
func (t *TypedData) jsonEncodeMessageValues(typeName string, message map[string]interface{}) (map[string]interface{}, error) {
typeFields, ok := t.Types[typeName]
if !ok {
return nil, fmt.Errorf("type '%s' not found in types", typeName)
}
encodedMessage := make(map[string]interface{})
for _, field := range typeFields {
val, exists := message[field.Name]
if !exists {
continue
}
// Handle arrays
if strings.HasSuffix(field.Type, "[]") {
baseType := field.Type[:len(field.Type)-2]
if arr, ok := val.([]interface{}); ok {
encodedArr := make([]interface{}, len(arr))
for i, item := range arr {
encoded, err := t.jsonEncodeValue(baseType, item)
if err != nil {
return nil, err
}
encodedArr[i] = encoded
}
encodedMessage[field.Name] = encodedArr
continue
}
}
// Handle single values
encoded, err := t.jsonEncodeValue(field.Type, val)
if err != nil {
return nil, err
}
encodedMessage[field.Name] = encoded
}
return encodedMessage, nil
}
func (t *TypedData) jsonEncodeValue(fieldType string, value interface{}) (interface{}, error) {
// Handle bytes/bytes32
if strings.HasPrefix(fieldType, "bytes") {
switch v := value.(type) {
case []byte:
return "0x" + common.Bytes2Hex(v), nil
case [8]byte:
return "0x" + common.Bytes2Hex(v[:]), nil
case [16]byte:
return "0x" + common.Bytes2Hex(v[:]), nil
case [24]byte:
return "0x" + common.Bytes2Hex(v[:]), nil
case [32]byte:
return "0x" + common.Bytes2Hex(v[:]), nil
}
return value, nil
}
// Handle nested custom types
if _, isCustomType := t.Types[fieldType]; isCustomType {
if nestedMsg, ok := value.(map[string]interface{}); ok {
return t.jsonEncodeMessageValues(fieldType, nestedMsg)
}
return nil, fmt.Errorf("value for custom type '%s' is not a map", fieldType)
}
// Return primitive values as-is
return value, nil
}
func (t *TypedData) UnmarshalJSON(data []byte) error {
// Intermediary structure to decode message field
type TypedDataRaw struct {
Types TypedDataTypes `json:"types"`
PrimaryType string `json:"primaryType"`
Domain struct {
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
ChainID interface{} `json:"chainId,omitempty"`
VerifyingContract *common.Address `json:"verifyingContract,omitempty"`
Salt *common.Hash `json:"salt,omitempty"`
} `json:"domain"`
Message map[string]interface{} `json:"message"`
}
// Json decoder with json.Number support, so that we can decode big.Int values
dec := json.NewDecoder(bytes.NewReader(data))
dec.UseNumber()
raw := TypedDataRaw{
Types: TypedDataTypes{},
Message: map[string]any{},
}
if err := dec.Decode(&raw); err != nil {
return err
}
// Ensure the "EIP712Domain" type is defined. In case it's not defined
// we will add it to the types map
_, ok := raw.Types["EIP712Domain"]
if !ok {
raw.Types["EIP712Domain"] = []TypedDataArgument{}
if raw.Domain.Name != "" {
raw.Types["EIP712Domain"] = append(raw.Types["EIP712Domain"], TypedDataArgument{Name: "name", Type: "string"})
}
if raw.Domain.Version != "" {
raw.Types["EIP712Domain"] = append(raw.Types["EIP712Domain"], TypedDataArgument{Name: "version", Type: "string"})
}
if raw.Domain.ChainID != nil {
raw.Types["EIP712Domain"] = append(raw.Types["EIP712Domain"], TypedDataArgument{Name: "chainId", Type: "uint256"})
}
if raw.Domain.VerifyingContract != nil {
raw.Types["EIP712Domain"] = append(raw.Types["EIP712Domain"], TypedDataArgument{Name: "verifyingContract", Type: "address"})
}
if raw.Domain.Salt != nil {
raw.Types["EIP712Domain"] = append(raw.Types["EIP712Domain"], TypedDataArgument{Name: "salt", Type: "bytes32"})
}
}
// Ensure primary type is defined
if raw.PrimaryType == "" {
// detect primary type if it's unspecified
primaryType, err := typedDataDetectPrimaryType(raw.Types.Map(), raw.Message)
if err != nil {
return err
}
raw.PrimaryType = primaryType
}
_, ok = raw.Types[raw.PrimaryType]
if !ok {
return fmt.Errorf("primary type '%s' is not defined", raw.PrimaryType)
}
// Decode the domain, which is mostly decooded except the chainId is an interface{} type
// because the value may be a number of a hex encoded number. We want it in a big.Int.
domain := TypedDataDomain{
Name: raw.Domain.Name,
Version: raw.Domain.Version,
ChainID: nil,
VerifyingContract: raw.Domain.VerifyingContract,
Salt: raw.Domain.Salt,
}
if raw.Domain.ChainID != nil {
chainID := big.NewInt(0)
if val, ok := raw.Domain.ChainID.(float64); ok {
chainID.SetInt64(int64(val))
} else if val, ok := raw.Domain.ChainID.(json.Number); ok {
chainID.SetString(val.String(), 10)
} else if val, ok := raw.Domain.ChainID.(string); ok {
if strings.HasPrefix(val, "0x") {
chainID.SetString(val[2:], 16)
} else {
chainID.SetString(val, 10)
}
}
domain.ChainID = chainID
}
// Decode the raw message into Go runtime types
message, err := typedDataDecodeRawMessageMap(raw.Types.Map(), raw.PrimaryType, raw.Message)
if err != nil {
return err
}
t.Types = raw.Types
t.PrimaryType = raw.PrimaryType
t.Domain = domain
m, ok := message.(map[string]interface{})
if !ok {
return fmt.Errorf("resulting message is not a map")
}
t.Message = m
return nil
}
func typedDataDetectPrimaryType(typesMap map[string]map[string]string, message map[string]interface{}) (string, error) {
// If there are only two types, and one is the EIP712Domain, then the other is the primary type
if len(typesMap) == 2 {
_, ok := typesMap["EIP712Domain"]
if ok {
for typ := range typesMap {
if typ == "EIP712Domain" {
continue
}
return typ, nil
}
}
}
// Otherwise search for the primary type by looking for the first type that has a message field keys
messageKeys := []string{}
for k := range message {
messageKeys = append(messageKeys, k)
}
sort.Strings(messageKeys)
for typ := range typesMap {
if typ == "EIP712Domain" {
continue
}
if len(typesMap[typ]) != len(messageKeys) {
continue
}
typKeys := []string{}
for k := range typesMap[typ] {
typKeys = append(typKeys, k)
}
sort.Strings(typKeys)
if !slices.Equal(messageKeys, typKeys) {
continue
}
return typ, nil
}
return "", fmt.Errorf("no primary type found")
}
func typedDataDecodeRawMessageMap(typesMap map[string]map[string]string, primaryType string, data interface{}) (interface{}, error) {
// Handle array types
if arr, ok := data.([]interface{}); ok {
results := make([]interface{}, len(arr))
for i, item := range arr {
decoded, err := typedDataDecodeRawMessageMap(typesMap, primaryType, item)
if err != nil {
return nil, err
}
results[i] = decoded
}
return results, nil
}
// Handle primitive directly
message, ok := data.(map[string]interface{})
if !ok {
return typedDataDecodePrimitiveValue(primaryType, data)
}
currentType, ok := typesMap[primaryType]
if !ok {
return nil, fmt.Errorf("type %s is not defined", primaryType)
}
processedMessage := make(map[string]interface{})
for k, v := range message {
typ, ok := currentType[k]
if !ok {
return nil, fmt.Errorf("message field '%s' is missing type definition on '%s'", k, primaryType)
}
// Extract base type and check if it's an array
baseType := typ
isArray := false
if idx := strings.Index(typ, "["); idx != -1 {
baseType = typ[:idx]
isArray = true
}
// Process value based on whether it's a custom or primitive type
if _, isCustomType := typesMap[baseType]; isCustomType {
decoded, err := typedDataDecodeRawMessageMap(typesMap, baseType, v)
if err != nil {
return nil, err
}
processedMessage[k] = decoded
} else {
var decoded interface{}
var err error
if isArray {
decoded, err = typedDataDecodeRawMessageMap(typesMap, baseType, v)
} else {
decoded, err = typedDataDecodePrimitiveValue(baseType, v)
}
if err != nil {
return nil, fmt.Errorf("failed to decode field '%s': %w", k, err)
}
processedMessage[k] = decoded
}
}
return processedMessage, nil
}
func typedDataDecodePrimitiveValue(typ string, value interface{}) (interface{}, error) {
val := fmt.Sprintf("%v", value)
out, err := ABIUnmarshalStringValuesAny([]string{typ}, []any{val})
if err != nil {
return nil, fmt.Errorf("typedDataDecodePrimitiveValue: %w", err)
}
return out[0], nil
}