Skip to content

Commit b6e735d

Browse files
authored
chore: signer: add support for encoding fixed sized arrays for primitive and custom types. (#2)
1 parent 7fe4ee0 commit b6e735d

4 files changed

Lines changed: 2693 additions & 78 deletions

File tree

signer/core/apitypes/signed_data_internal_test.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ package apitypes
1818

1919
import (
2020
"bytes"
21+
"encoding/json"
22+
"fmt"
2123
"math/big"
24+
"os"
2225
"testing"
2326

2427
"github.com/ethereum/go-ethereum/common"
2528
"github.com/ethereum/go-ethereum/common/hexutil"
2629
"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"
2733
)
2834

2935
func TestBytesPadding(t *testing.T) {
@@ -244,45 +250,42 @@ func TestConvertAddressDataToSlice(t *testing.T) {
244250
func TestTypedDataArrayValidate(t *testing.T) {
245251
t.Parallel()
246252

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+
TypedData TypedData `json:"typedData"`
256+
DomainHash string `json:"domainHash"`
257+
MessageHash string `json:"messageHash"`
258+
Digest string `json:"digest"`
269259
}
270260

271-
if err := typedData.validate(); err != nil {
272-
t.Errorf("expected typed data to pass validation, got: %v", err)
273-
}
261+
fc, err := os.ReadFile("./testdata/typed-data.json")
262+
require.NoError(t, err, "error reading test data file")
274263

275-
// Should be able to accept dynamic arrays
276-
typedData.Types["BulkOrder"][0].Type = "OrderComponents[]"
264+
var tests []TestDataInput
265+
err = json.Unmarshal(fc, &tests)
266+
require.NoError(t, err, "error unmarshalling test data file contents")
277267

278-
if err := typedData.validate(); err != nil {
279-
t.Errorf("expected typed data to pass validation, got: %v", err)
280-
}
268+
for _, tt := range tests {
269+
tc := tt
270+
271+
t.Run(tc.Name, func(t *testing.T) {
272+
t.Parallel()
273+
274+
td := tc.TypedData
275+
276+
domainSeparator, tErr := td.HashStruct("EIP712Domain", td.Domain.Map())
277+
assert.NoError(t, tErr, "failed to hash domain separator: %v", tErr)
278+
279+
messageHash, tErr := td.HashStruct(td.PrimaryType, td.Message)
280+
assert.NoError(t, tErr, "failed to hash message: %v", tErr)
281+
282+
digest := crypto.Keccak256Hash([]byte(fmt.Sprintf("%s%s%s", "\x19\x01", string(domainSeparator), string(messageHash))))
281283

282-
// Should be able to accept standard types
283-
typedData.Types["BulkOrder"][0].Type = "OrderComponents"
284+
assert.Equal(t, tc.Digest, digest.String(), "digest doesn't not match")
285+
assert.Equal(t, tc.DomainHash, domainSeparator.String(), "domain separator hashes do not match")
286+
assert.Equal(t, tc.MessageHash, messageHash.String(), "message hashes do not match")
284287

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(), "expected typed data to pass validation, got: %v", tErr)
289+
})
287290
}
288291
}

0 commit comments

Comments
 (0)