Skip to content

Commit

Permalink
chore: signer: add support for encoding fixed sized arrays for primit…
Browse files Browse the repository at this point in the history
…ive and custom types. (#2)
  • Loading branch information
naveen-imtb authored Jul 30, 2024
1 parent 7fe4ee0 commit b6e735d
Show file tree
Hide file tree
Showing 4 changed files with 2,693 additions and 78 deletions.
71 changes: 37 additions & 34 deletions signer/core/apitypes/signed_data_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ package apitypes

import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"os"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBytesPadding(t *testing.T) {
Expand Down Expand Up @@ -244,45 +250,42 @@ func TestConvertAddressDataToSlice(t *testing.T) {
func TestTypedDataArrayValidate(t *testing.T) {
t.Parallel()

typedData := TypedData{
Types: Types{
"BulkOrder": []Type{
// Should be able to accept fixed size arrays
{Name: "tree", Type: "OrderComponents[2][2]"},
},
"OrderComponents": []Type{
{Name: "offerer", Type: "address"},
{Name: "amount", Type: "uint8"},
},
"EIP712Domain": []Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint8"},
{Name: "verifyingContract", Type: "address"},
},
},
PrimaryType: "BulkOrder",
Domain: TypedDataDomain{
VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
Message: TypedDataMessage{},
type TestDataInput struct {
Name string `json:"name"`
TypedData TypedData `json:"typedData"`
DomainHash string `json:"domainHash"`
MessageHash string `json:"messageHash"`
Digest string `json:"digest"`
}

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

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

if err := typedData.validate(); err != nil {
t.Errorf("expected typed data to pass validation, got: %v", err)
}
for _, tt := range tests {
tc := tt

t.Run(tc.Name, func(t *testing.T) {
t.Parallel()

td := tc.TypedData

domainSeparator, tErr := td.HashStruct("EIP712Domain", td.Domain.Map())
assert.NoError(t, tErr, "failed to hash domain separator: %v", tErr)

messageHash, tErr := td.HashStruct(td.PrimaryType, td.Message)
assert.NoError(t, tErr, "failed to hash message: %v", tErr)

digest := crypto.Keccak256Hash([]byte(fmt.Sprintf("%s%s%s", "\x19\x01", string(domainSeparator), string(messageHash))))

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

if err := typedData.validate(); err != nil {
t.Errorf("expected typed data to pass validation, got: %v", err)
assert.NoError(t, td.validate(), "expected typed data to pass validation, got: %v", tErr)
})
}
}
Loading

0 comments on commit b6e735d

Please sign in to comment.