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: fix incorrect EIP-712 recursive encoding of nested bytes arrays #30987

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion signer/core/apitypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,15 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin

arrayBuffer := new(bytes.Buffer)
parsedType := strings.Split(encType, "[")[0]

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 {
Expand Down
34 changes: 34 additions & 0 deletions signer/core/apitypes/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ 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"
Expand Down Expand Up @@ -233,3 +234,36 @@ func TestType_TypeName(t *testing.T) {
}
}
}

func TestEncodeNestedBytesArray(t *testing.T) {
typedData := TypedData{
Types: Types{
"EIP712Domain": []Type{
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
},
"Test": []Type{
{Name: "data", Type: "bytes[]"},
},
},
PrimaryType: "Test",
Domain: TypedDataDomain{
Name: "TestDomain",
Version: "1",
},
Message: map[string]interface{}{
"data": []interface{}{
hexutil.MustDecode("0x1234"),
hexutil.MustDecode("0x5678"),
},
},
}

hash, _, err := TypedDataAndHash(typedData)
if err != nil {
t.Fatalf("Failed to hash struct with nested bytes array: %v", err)
}
if hash == nil {
t.Error("Hash is nil")
}
}