mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
signer/core/apitypes: minor nits
This commit is contained in:
parent
ceea63bc19
commit
5a52487ab2
2 changed files with 20 additions and 21 deletions
|
|
@ -250,7 +250,7 @@ func TestConvertAddressDataToSlice(t *testing.T) {
|
|||
func TestTypedDataArrayValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type TestDataInput struct {
|
||||
type testDataInput struct {
|
||||
Name string `json:"name"`
|
||||
Domain TypedDataDomain `json:"domain"`
|
||||
PrimaryType string `json:"primaryType"`
|
||||
|
|
@ -258,17 +258,14 @@ func TestTypedDataArrayValidate(t *testing.T) {
|
|||
Message TypedDataMessage `json:"data"`
|
||||
Digest string `json:"digest"`
|
||||
}
|
||||
|
||||
fc, err := os.ReadFile("./testdata/typed-data.json")
|
||||
require.NoError(t, err, "error reading test data file")
|
||||
|
||||
var tests []TestDataInput
|
||||
var tests []testDataInput
|
||||
err = json.Unmarshal(fc, &tests)
|
||||
require.NoError(t, err, "error unmarshalling test data file contents")
|
||||
|
||||
for _, tt := range tests {
|
||||
tc := tt
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
|||
|
|
@ -325,9 +325,11 @@ type Type struct {
|
|||
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 {
|
||||
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
|
||||
|
|
@ -462,9 +464,9 @@ func (typedData *TypedData) EncodeData(primaryType string, data map[string]inter
|
|||
encType := field.Type
|
||||
encValue := data[field.Name]
|
||||
if encType[len(encType)-1:] == "]" {
|
||||
encodedData, encErr := typedData.encodeArrayValue(encValue, encType, depth)
|
||||
if encErr != nil {
|
||||
return nil, encErr
|
||||
encodedData, err := typedData.encodeArrayValue(encValue, encType, depth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buffer.Write(encodedData)
|
||||
} else if typedData.Types[field.Type] != nil {
|
||||
|
|
@ -494,14 +496,14 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
|
|||
return nil, dataMismatchError(encType, encValue)
|
||||
}
|
||||
|
||||
arrayBuffer := bytes.Buffer{}
|
||||
arrayBuffer := new(bytes.Buffer)
|
||||
parsedType := strings.Split(encType, "[")[0]
|
||||
for _, item := range arrayValue {
|
||||
if reflect.TypeOf(item).Kind() == reflect.Slice ||
|
||||
reflect.TypeOf(item).Kind() == reflect.Array {
|
||||
encodedData, encErr := typedData.encodeArrayValue(item, parsedType, depth+1)
|
||||
if encErr != nil {
|
||||
return nil, encErr
|
||||
encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
arrayBuffer.Write(encodedData)
|
||||
} else {
|
||||
|
|
@ -510,16 +512,16 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
|
|||
if !ok {
|
||||
return nil, dataMismatchError(parsedType, item)
|
||||
}
|
||||
encodedData, encErr := typedData.EncodeData(parsedType, mapValue, depth+1)
|
||||
if encErr != nil {
|
||||
return nil, encErr
|
||||
encodedData, err := typedData.EncodeData(parsedType, mapValue, depth+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
digest := crypto.Keccak256(encodedData)
|
||||
arrayBuffer.Write(digest)
|
||||
} else {
|
||||
bytesValue, encErr := typedData.EncodePrimitiveValue(parsedType, item, depth)
|
||||
if encErr != nil {
|
||||
return nil, encErr
|
||||
bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
arrayBuffer.Write(bytesValue)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue