Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

signer/core/apitypes: support fixed size arrays for EIP-712 typed data #30175

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions signer/core/apitypes/signed_data_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,49 @@ func TestConvertAddressDataToSlice(t *testing.T) {
t.Fatal(err)
}
}

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{},
}

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

// Should be able to accept dynamic arrays
typedData.Types["BulkOrder"][0].Type = "OrderComponents[]"

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

// Should be able to accept standard types
typedData.Types["BulkOrder"][0].Type = "OrderComponents"

if err := typedData.validate(); err != nil {
t.Errorf("expected typed data to pass validation, got: %v", err)
}
}
7 changes: 4 additions & 3 deletions signer/core/apitypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
)

var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Za-z](\w*)(\[\])?$`)
var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Za-z](\w*)(\[\d*\])*$`)

type ValidationInfo struct {
Typ string `json:"type"`
Expand Down Expand Up @@ -216,8 +216,9 @@ func (t *Type) isArray() bool {
// typeName returns the canonical name of the type. If the type is 'Person[]', then
// this method returns 'Person'
func (t *Type) typeName() string {
if strings.HasSuffix(t.Type, "[]") {
return strings.TrimSuffix(t.Type, "[]")
if strings.Contains(t.Type, "[") {
re := regexp.MustCompile(`\[\d*\]`)
return re.ReplaceAllString(t.Type, "")
}
return t.Type
}
Expand Down