From b2457f32edadda4d5789e803752efcf4efb0a937 Mon Sep 17 00:00:00 2001 From: crStiv Date: Sun, 5 Jan 2025 01:47:19 +0100 Subject: [PATCH 1/4] Update types.go --- signer/core/apitypes/types.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index b56931c1d18b..8f18db1fa019 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -498,6 +498,16 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin arrayBuffer := new(bytes.Buffer) parsedType := strings.Split(encType, "[")[0] + + // Special handling for bytes arrays + if parsedType == "bytes" { + bytesValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth) + if err != nil { + return nil, err + } + return bytesValue, nil + } + for _, item := range arrayValue { if reflect.TypeOf(item).Kind() == reflect.Slice || reflect.TypeOf(item).Kind() == reflect.Array { From b8628f0bcfcba99397aba19a0f65ebcced9aa359 Mon Sep 17 00:00:00 2001 From: crStiv Date: Sun, 5 Jan 2025 01:48:47 +0100 Subject: [PATCH 2/4] Update types_test.go --- signer/core/apitypes/types_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/signer/core/apitypes/types_test.go b/signer/core/apitypes/types_test.go index 22bbeba19ec4..11a28f04e268 100644 --- a/signer/core/apitypes/types_test.go +++ b/signer/core/apitypes/types_test.go @@ -25,6 +25,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/holiman/uint256" + "github.com/ethereum/go-ethereum/common/hexutil" ) func TestIsPrimitive(t *testing.T) { @@ -233,3 +234,27 @@ func TestType_TypeName(t *testing.T) { } } } + +func TestEncodeNestedBytesArray(t *testing.T) { + typedData := TypedData{ + Types: Types{ + "EIP712Domain": []Type{}, + "Test": []Type{ + {Name: "data", Type: "bytes[]"}, + }, + }, + PrimaryType: "Test", + Domain: TypedDataDomain{}, + Message: map[string]interface{}{ + "data": []interface{}{ + hexutil.MustDecode("0x1234"), + hexutil.MustDecode("0x5678"), + }, + }, + } + + _, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) + if err != nil { + t.Errorf("Failed to hash struct with nested bytes array: %v", err) + } +} From c78dcbccbe9322064be6fbf04410e3cb56893d6e Mon Sep 17 00:00:00 2001 From: crStiv Date: Tue, 7 Jan 2025 18:14:53 +0100 Subject: [PATCH 3/4] Update types_test.go --- signer/core/apitypes/types_test.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/signer/core/apitypes/types_test.go b/signer/core/apitypes/types_test.go index 11a28f04e268..81ecd6a14c2c 100644 --- a/signer/core/apitypes/types_test.go +++ b/signer/core/apitypes/types_test.go @@ -22,10 +22,10 @@ import ( "testing" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/holiman/uint256" - "github.com/ethereum/go-ethereum/common/hexutil" ) func TestIsPrimitive(t *testing.T) { @@ -238,13 +238,19 @@ func TestType_TypeName(t *testing.T) { func TestEncodeNestedBytesArray(t *testing.T) { typedData := TypedData{ Types: Types{ - "EIP712Domain": []Type{}, + "EIP712Domain": []Type{ + {Name: "name", Type: "string"}, + {Name: "version", Type: "string"}, + }, "Test": []Type{ {Name: "data", Type: "bytes[]"}, }, }, PrimaryType: "Test", - Domain: TypedDataDomain{}, + Domain: TypedDataDomain{ + Name: "TestDomain", + Version: "1", + }, Message: map[string]interface{}{ "data": []interface{}{ hexutil.MustDecode("0x1234"), @@ -253,8 +259,11 @@ func TestEncodeNestedBytesArray(t *testing.T) { }, } - _, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) + hash, _, err := TypedDataAndHash(typedData) if err != nil { - t.Errorf("Failed to hash struct with nested bytes array: %v", err) + t.Fatalf("Failed to hash struct with nested bytes array: %v", err) + } + if hash == nil { + t.Error("Hash is nil") } } From 1df93d5c6c0f1e9d5c87d0d911e5bb4698d0c1d3 Mon Sep 17 00:00:00 2001 From: crStiv Date: Sat, 11 Jan 2025 23:21:05 +0100 Subject: [PATCH 4/4] Update types.go --- signer/core/apitypes/types.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index 8f18db1fa019..bdf85c8ac323 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -498,18 +498,15 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin arrayBuffer := new(bytes.Buffer) parsedType := strings.Split(encType, "[")[0] - - // Special handling for bytes arrays - if parsedType == "bytes" { - bytesValue, err := typedData.EncodePrimitiveValue(encType, encValue, depth) - if err != nil { - return nil, err - } - return bytesValue, nil - } - + for _, item := range arrayValue { - if reflect.TypeOf(item).Kind() == reflect.Slice || + if parsedType == "bytes" { + bytesValue, err := typedData.EncodePrimitiveValue(parsedType, item, depth) + if err != nil { + return nil, err + } + arrayBuffer.Write(bytesValue) + } else if reflect.TypeOf(item).Kind() == reflect.Slice || reflect.TypeOf(item).Kind() == reflect.Array { encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1) if err != nil {