signer/core/apitypes: minor nits

This commit is contained in:
Martin Holst Swende 2024-11-07 15:25:11 +01:00
parent ceea63bc19
commit 5a52487ab2
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 20 additions and 21 deletions

View file

@ -250,7 +250,7 @@ func TestConvertAddressDataToSlice(t *testing.T) {
func TestTypedDataArrayValidate(t *testing.T) { func TestTypedDataArrayValidate(t *testing.T) {
t.Parallel() t.Parallel()
type TestDataInput struct { type testDataInput struct {
Name string `json:"name"` Name string `json:"name"`
Domain TypedDataDomain `json:"domain"` Domain TypedDataDomain `json:"domain"`
PrimaryType string `json:"primaryType"` PrimaryType string `json:"primaryType"`
@ -258,17 +258,14 @@ func TestTypedDataArrayValidate(t *testing.T) {
Message TypedDataMessage `json:"data"` Message TypedDataMessage `json:"data"`
Digest string `json:"digest"` Digest string `json:"digest"`
} }
fc, err := os.ReadFile("./testdata/typed-data.json") fc, err := os.ReadFile("./testdata/typed-data.json")
require.NoError(t, err, "error reading test data file") require.NoError(t, err, "error reading test data file")
var tests []TestDataInput var tests []testDataInput
err = json.Unmarshal(fc, &tests) err = json.Unmarshal(fc, &tests)
require.NoError(t, err, "error unmarshalling test data file contents") require.NoError(t, err, "error unmarshalling test data file contents")
for _, tt := range tests { for _, tc := range tests {
tc := tt
t.Run(tc.Name, func(t *testing.T) { t.Run(tc.Name, func(t *testing.T) {
t.Parallel() t.Parallel()

View file

@ -325,9 +325,11 @@ type Type struct {
Type string `json:"type"` Type string `json:"type"`
} }
// isArray returns true if the type is a fixed or variable sized array // isArray returns true if the type is a fixed or variable sized array.
// This method may return false positives, in case the Type is not a valid
// expression, e.g. "fooo[[[[".
func (t *Type) isArray() bool { func (t *Type) isArray() bool {
return len(strings.Split(t.Type, "[")) > 1 return strings.IndexByte(t.Type, '[') > 0
} }
// typeName returns the canonical name of the type. If the type is 'Person[]' or 'Person[2]', then // typeName returns the canonical name of the type. If the type is 'Person[]' or 'Person[2]', then
@ -462,9 +464,9 @@ func (typedData *TypedData) EncodeData(primaryType string, data map[string]inter
encType := field.Type encType := field.Type
encValue := data[field.Name] encValue := data[field.Name]
if encType[len(encType)-1:] == "]" { if encType[len(encType)-1:] == "]" {
encodedData, encErr := typedData.encodeArrayValue(encValue, encType, depth) encodedData, err := typedData.encodeArrayValue(encValue, encType, depth)
if encErr != nil { if err != nil {
return nil, encErr return nil, err
} }
buffer.Write(encodedData) buffer.Write(encodedData)
} else if typedData.Types[field.Type] != nil { } else if typedData.Types[field.Type] != nil {
@ -494,14 +496,14 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
return nil, dataMismatchError(encType, encValue) return nil, dataMismatchError(encType, encValue)
} }
arrayBuffer := bytes.Buffer{} arrayBuffer := new(bytes.Buffer)
parsedType := strings.Split(encType, "[")[0] parsedType := strings.Split(encType, "[")[0]
for _, item := range arrayValue { for _, item := range arrayValue {
if reflect.TypeOf(item).Kind() == reflect.Slice || if reflect.TypeOf(item).Kind() == reflect.Slice ||
reflect.TypeOf(item).Kind() == reflect.Array { reflect.TypeOf(item).Kind() == reflect.Array {
encodedData, encErr := typedData.encodeArrayValue(item, parsedType, depth+1) encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1)
if encErr != nil { if err != nil {
return nil, encErr return nil, err
} }
arrayBuffer.Write(encodedData) arrayBuffer.Write(encodedData)
} else { } else {
@ -510,16 +512,16 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
if !ok { if !ok {
return nil, dataMismatchError(parsedType, item) return nil, dataMismatchError(parsedType, item)
} }
encodedData, encErr := typedData.EncodeData(parsedType, mapValue, depth+1) encodedData, err := typedData.EncodeData(parsedType, mapValue, depth+1)
if encErr != nil { if err != nil {
return nil, encErr return nil, err
} }
digest := crypto.Keccak256(encodedData) digest := crypto.Keccak256(encodedData)
arrayBuffer.Write(digest) arrayBuffer.Write(digest)
} else { } else {
bytesValue, encErr := typedData.EncodePrimitiveValue(parsedType, item, depth) bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
if encErr != nil { if err != nil {
return nil, encErr return nil, err
} }
arrayBuffer.Write(bytesValue) arrayBuffer.Write(bytesValue)
} }