This commit is contained in:
crStiv 2025-01-25 21:54:25 -08:00 committed by GitHub
commit 461828c04a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 1 deletions

View file

@ -498,8 +498,15 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
arrayBuffer := new(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 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 { reflect.TypeOf(item).Kind() == reflect.Array {
encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1) encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1)
if err != nil { if err != nil {

View file

@ -22,6 +22,7 @@ import (
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "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/core/types"
"github.com/ethereum/go-ethereum/crypto/kzg4844" "github.com/ethereum/go-ethereum/crypto/kzg4844"
"github.com/holiman/uint256" "github.com/holiman/uint256"
@ -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")
}
}