Fixed TestMalformedData4 errors and renamed IsValid to Validate

This commit is contained in:
Paul Berg 2018-11-13 18:10:24 +02:00 committed by Martin Holst Swende
parent cb97813e04
commit 7de3f530b4
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 26 additions and 44 deletions

View file

@ -25,6 +25,7 @@ import (
"math/big"
"mime"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
@ -265,7 +266,7 @@ func SignTextPlain(data hexutil.Bytes) (hexutil.Bytes, string) {
// SignTypedData signs EIP-712 conformant typed data
// hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")
func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, typedData TypedData) (hexutil.Bytes, error) {
if err := typedData.IsValid(); err != nil {
if err := typedData.Validate(); err != nil {
return nil, err
}
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
@ -584,12 +585,12 @@ func UnmarshalValidatorData(data interface{}) (ValidatorData, error) {
}, nil
}
// IsValid checks if the typed data is sound
func (typedData *TypedData) IsValid() error {
if err := typedData.Types.IsValid(); err != nil {
// Validate checks if the typed data is sound
func (typedData *TypedData) Validate() error {
if err := typedData.Types.Validate(); err != nil {
return err
}
if err := typedData.Domain.IsValid(); err != nil {
if err := typedData.Domain.Validate(); err != nil {
return err
}
return nil
@ -612,8 +613,8 @@ func (typedData *TypedData) PrettyPrint() string {
return ""
}
// IsValid checks if the types object is conformant to the specs
func (types *EIP712Types) IsValid() error {
// Validate checks if the types object is conformant to the specs
func (types *EIP712Types) Validate() error {
for typeKey, typeArr := range *types {
for _, typeObj := range typeArr {
typeVal := typeObj["type"]
@ -642,35 +643,26 @@ func (types *EIP712Types) IsValid() error {
// isStandardType checks if the given type is a EIP712 conformant type
func isStandardTypeStr(encType string) bool {
// Atomic types
for _, standardType := range []string{
TypeAddress,
TypeBool,
TypeBytes,
TypeString,
} {
if standardType == encType {
return true
}
exp, _ := regexp.Compile(`^(address|bool|bytes|string)$`)
if (exp.MatchString(encType)) {
return true
}
// Dynamic types
for _, standardType := range []string{
TypeBytes,
TypeInt,
TypeUint,
} {
if strings.HasPrefix(encType, standardType) {
return true
}
exp, _ = regexp.Compile(`^(bytes|int|uint)(\d+)$`)
if (exp.MatchString(encType)) {
return true
}
// Reference types
return encType[len(encType)-1] == ']'
// Arrays
// TODO: add dynamic type arrays
exp, _ = regexp.Compile(`^(address|bool|bytes|string)\[]$`)
return exp.MatchString(encType)
}
// IsValid checks if the given domain is valid, i.e. contains at least
// Validate checks if the given domain is valid, i.e. contains at least
// the minimum viable keys and values
func (domain *EIP712Domain) IsValid() error {
func (domain *EIP712Domain) Validate() error {
if domain.ChainId == big.NewInt(0) {
return errors.New("chainId must be specified according to EIP-155")
}

View file

@ -276,7 +276,7 @@ func TestMalformedData1(t *testing.T) {
if err != nil {
t.Fatalf("unmarshalling failed %v", err)
}
err = typedData.IsValid()
err = typedData.Validate()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
@ -357,7 +357,7 @@ func TestMalformedDomainData(t *testing.T) {
if err != nil {
t.Fatalf("unmarshalling failed %v", err)
}
err = typedData.IsValid()
err = typedData.Validate()
if err == nil {
t.Fatalf("Expected `referenced type 'Blahonga' is undefined`, got %v", err)
}
@ -440,7 +440,7 @@ func TestMalformedData3(t *testing.T) {
if err != nil {
t.Fatalf("unmarshalling failed %v", err)
}
err = typedData.IsValid()
err = typedData.Validate()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
@ -543,18 +543,8 @@ func TestMalformedData4(t *testing.T) {
if err != nil {
t.Fatalf("unmarshalling failed %v", err)
}
err = typedData.IsValid()
if err != nil {
t.Fatalf("Expected no error, got %v", err)
err = typedData.Validate()
if err.Error() != "unknown atomic type 'uint256 ... and now for something completely different'" {
t.Fatalf("Expected `unknown atomic type 'uint256 ... and now for something completely different'`, got %v", err)
}
hash, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err == nil{
t.Errorf("Expected error, got hash %v", hash)
}else
{
fmt.Printf("err %v", err)
}
//if err.Error() != "provided data '<nil>' doesn't match type 'address'" {
// t.Errorf("Expected `provided data '<nil>' doesn't match type 'address'`, got %v", err)
//}
}