Skip to content

Commit 5552ada

Browse files
authored
signer/core: fix encoding of bytes nested within array (#31049)
Fixes an incorrect encoding of recursive bytes types. closes #30979
1 parent 53e8e1f commit 5552ada

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

signer/core/apitypes/types.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,16 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
501501
for _, item := range arrayValue {
502502
if reflect.TypeOf(item).Kind() == reflect.Slice ||
503503
reflect.TypeOf(item).Kind() == reflect.Array {
504-
encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1)
504+
var (
505+
encodedData hexutil.Bytes
506+
err error
507+
)
508+
if reflect.TypeOf(item).Elem().Kind() == reflect.Uint8 {
509+
// the item type is bytes. encode the bytes array directly instead of recursing.
510+
encodedData, err = typedData.EncodePrimitiveValue(parsedType, item, depth+1)
511+
} else {
512+
encodedData, err = typedData.encodeArrayValue(item, parsedType, depth+1)
513+
}
505514
if err != nil {
506515
return nil, err
507516
}

signer/core/signed_data_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,3 +1014,49 @@ func TestComplexTypedDataWithLowercaseReftype(t *testing.T) {
10141014
t.Fatalf("Error, got %x, wanted %x", sighash, expSigHash)
10151015
}
10161016
}
1017+
1018+
var recursiveBytesTypesStandard = apitypes.Types{
1019+
"EIP712Domain": {
1020+
{
1021+
Name: "name",
1022+
Type: "string",
1023+
},
1024+
{
1025+
Name: "version",
1026+
Type: "string",
1027+
},
1028+
{
1029+
Name: "chainId",
1030+
Type: "uint256",
1031+
},
1032+
{
1033+
Name: "verifyingContract",
1034+
Type: "address",
1035+
},
1036+
},
1037+
"Val": {
1038+
{
1039+
Name: "field",
1040+
Type: "bytes[][]",
1041+
},
1042+
},
1043+
}
1044+
1045+
var recursiveBytesMessageStandard = map[string]interface{}{
1046+
"field": [][][]byte{{{1}, {2}}, {{3}, {4}}},
1047+
}
1048+
1049+
var recursiveBytesTypedData = apitypes.TypedData{
1050+
Types: recursiveBytesTypesStandard,
1051+
PrimaryType: "Val",
1052+
Domain: domainStandard,
1053+
Message: recursiveBytesMessageStandard,
1054+
}
1055+
1056+
func TestEncodeDataRecursiveBytes(t *testing.T) {
1057+
typedData := recursiveBytesTypedData
1058+
_, err := typedData.EncodeData(typedData.PrimaryType, typedData.Message, 0)
1059+
if err != nil {
1060+
t.Fatalf("got err %v", err)
1061+
}
1062+
}

0 commit comments

Comments
 (0)