@@ -18,12 +18,18 @@ package apitypes
18
18
19
19
import (
20
20
"bytes"
21
+ "encoding/json"
22
+ "fmt"
21
23
"math/big"
24
+ "os"
22
25
"testing"
23
26
24
27
"github.com/ethereum/go-ethereum/common"
25
28
"github.com/ethereum/go-ethereum/common/hexutil"
26
29
"github.com/ethereum/go-ethereum/common/math"
30
+ "github.com/ethereum/go-ethereum/crypto"
31
+ "github.com/stretchr/testify/assert"
32
+ "github.com/stretchr/testify/require"
27
33
)
28
34
29
35
func TestBytesPadding (t * testing.T ) {
@@ -244,45 +250,42 @@ func TestConvertAddressDataToSlice(t *testing.T) {
244
250
func TestTypedDataArrayValidate (t * testing.T ) {
245
251
t .Parallel ()
246
252
247
- typedData := TypedData {
248
- Types : Types {
249
- "BulkOrder" : []Type {
250
- // Should be able to accept fixed size arrays
251
- {Name : "tree" , Type : "OrderComponents[2][2]" },
252
- },
253
- "OrderComponents" : []Type {
254
- {Name : "offerer" , Type : "address" },
255
- {Name : "amount" , Type : "uint8" },
256
- },
257
- "EIP712Domain" : []Type {
258
- {Name : "name" , Type : "string" },
259
- {Name : "version" , Type : "string" },
260
- {Name : "chainId" , Type : "uint8" },
261
- {Name : "verifyingContract" , Type : "address" },
262
- },
263
- },
264
- PrimaryType : "BulkOrder" ,
265
- Domain : TypedDataDomain {
266
- VerifyingContract : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
267
- },
268
- Message : TypedDataMessage {},
253
+ type testDataInput struct {
254
+ Name string `json:"name"`
255
+ Domain TypedDataDomain `json:"domain"`
256
+ PrimaryType string `json:"primaryType"`
257
+ Types Types `json:"types"`
258
+ Message TypedDataMessage `json:"data"`
259
+ Digest string `json:"digest"`
269
260
}
261
+ fc , err := os .ReadFile ("./testdata/typed-data.json" )
262
+ require .NoError (t , err , "error reading test data file" )
270
263
271
- if err := typedData . validate (); err != nil {
272
- t . Errorf ( "expected typed data to pass validation, got: %v" , err )
273
- }
264
+ var tests [] testDataInput
265
+ err = json . Unmarshal ( fc , & tests )
266
+ require . NoError ( t , err , "error unmarshalling test data file contents" )
274
267
275
- // Should be able to accept dynamic arrays
276
- typedData .Types ["BulkOrder" ][0 ].Type = "OrderComponents[]"
268
+ for _ , tc := range tests {
269
+ t .Run (tc .Name , func (t * testing.T ) {
270
+ t .Parallel ()
277
271
278
- if err := typedData .validate (); err != nil {
279
- t .Errorf ("expected typed data to pass validation, got: %v" , err )
280
- }
272
+ td := TypedData {
273
+ Types : tc .Types ,
274
+ PrimaryType : tc .PrimaryType ,
275
+ Domain : tc .Domain ,
276
+ Message : tc .Message ,
277
+ }
278
+
279
+ domainSeparator , tErr := td .HashStruct ("EIP712Domain" , td .Domain .Map ())
280
+ assert .NoError (t , tErr , "failed to hash domain separator: %v" , tErr )
281
+
282
+ messageHash , tErr := td .HashStruct (td .PrimaryType , td .Message )
283
+ assert .NoError (t , tErr , "failed to hash message: %v" , tErr )
281
284
282
- // Should be able to accept standard types
283
- typedData . Types [ "BulkOrder" ][ 0 ]. Type = "OrderComponents"
285
+ digest := crypto . Keccak256Hash ([] byte ( fmt . Sprintf ( "%s%s%s" , " \x19 \x01 " , string ( domainSeparator ), string ( messageHash ))))
286
+ assert . Equal ( t , tc . Digest , digest . String (), "digest doesn't not match" )
284
287
285
- if err := typedData .validate (); err != nil {
286
- t . Errorf ( "expected typed data to pass validation, got: %v" , err )
288
+ assert . NoError ( t , td .validate (), "validation failed" , tErr )
289
+ } )
287
290
}
288
291
}
0 commit comments