Skip to content

Commit f890194

Browse files
jwasingerbuddh0
authored andcommitted
signer/core: fix encoding of bytes nested within array (ethereum#31049)
Fixes an incorrect encoding of recursive bytes types. closes ethereum#30979
1 parent 7000aa1 commit f890194

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
@@ -505,7 +505,16 @@ func (typedData *TypedData) encodeArrayValue(encValue interface{}, encType strin
505505
for _, item := range arrayValue {
506506
if reflect.TypeOf(item).Kind() == reflect.Slice ||
507507
reflect.TypeOf(item).Kind() == reflect.Array {
508-
encodedData, err := typedData.encodeArrayValue(item, parsedType, depth+1)
508+
var (
509+
encodedData hexutil.Bytes
510+
err error
511+
)
512+
if reflect.TypeOf(item).Elem().Kind() == reflect.Uint8 {
513+
// the item type is bytes. encode the bytes array directly instead of recursing.
514+
encodedData, err = typedData.EncodePrimitiveValue(parsedType, item, depth+1)
515+
} else {
516+
encodedData, err = typedData.encodeArrayValue(item, parsedType, depth+1)
517+
}
509518
if err != nil {
510519
return nil, err
511520
}

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)