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: EIP 712, parse bytes and bytesX as hex strings + correct padding #21307

Merged
merged 6 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 17 additions & 2 deletions signer/core/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,21 @@ func (typedData *TypedData) EncodeData(primaryType string, data map[string]inter
return buffer.Bytes(), nil
}

// Attempt to parse bytes in different formats: byte array, hex string, hexutil.Bytes.
func parseBytes(encType interface{}) ([]byte, bool) {
switch v := encType.(type) {
case []byte:
return v, true
case hexutil.Bytes:
return []byte(v), true
case string:
bytes, err := hexutil.Decode(v)
return bytes, err == nil
natsukagami marked this conversation as resolved.
Show resolved Hide resolved
default:
return nil, false
}
}

func parseInteger(encType string, encValue interface{}) (*big.Int, error) {
var (
length int
Expand Down Expand Up @@ -560,7 +575,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf
}
return crypto.Keccak256([]byte(strVal)), nil
case "bytes":
bytesValue, ok := encValue.([]byte)
bytesValue, ok := parseBytes(encValue)
if !ok {
return nil, dataMismatchError(encType, encValue)
}
Expand All @@ -575,7 +590,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf
if length < 0 || length > 32 {
return nil, fmt.Errorf("invalid size on bytes: %d", length)
}
if byteValue, ok := encValue.(hexutil.Bytes); !ok {
if byteValue, ok := parseBytes(encValue); !ok {
return nil, dataMismatchError(encType, encValue)
} else {
return math.PaddedBigBytes(new(big.Int).SetBytes(byteValue), 32), nil
Expand Down
32 changes: 32 additions & 0 deletions signer/core/signed_data_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,42 @@
package core

import (
"fmt"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common/hexutil"
)

func TestParseBytes(t *testing.T) {
for i, tt := range []struct {
v interface{}
exp []byte
}{
{"0x", []byte{}},
{"0x1234", []byte{0x12, 0x34}},
natsukagami marked this conversation as resolved.
Show resolved Hide resolved
{[]byte{12, 34}, []byte{12, 34}},
{hexutil.Bytes([]byte{12, 34}), []byte{12, 34}},
{"not a hex string", nil},
{15, nil},
{nil, nil},
} {
out, ok := parseBytes(tt.v)
if tt.exp == nil {
if ok {
t.Errorf("Case %d: expecting !ok, got ok with %v", i, out)
natsukagami marked this conversation as resolved.
Show resolved Hide resolved
}
continue
}
if !ok {
t.Errorf("Case %d: expecting ok got !ok", i)
}
if fmt.Sprintf("%#v", out) != fmt.Sprintf("%#v", tt.exp) {
natsukagami marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Case %d: expecting %v got %v", i, tt.exp, out)
natsukagami marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

func TestParseInteger(t *testing.T) {
for i, tt := range []struct {
t string
Expand Down