Fixed TestMalformedData4 errors and renamed IsValid to Validate

This commit is contained in:
Paul Berg 2018-11-13 18:10:24 +02:00
parent eb03047d42
commit 2308f12e00
2 changed files with 26 additions and 44 deletions

View file

@ -25,6 +25,7 @@ import (
"math/big" "math/big"
"mime" "mime"
"reflect" "reflect"
"regexp"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
@ -265,7 +266,7 @@ func SignTextPlain(data hexutil.Bytes) (hexutil.Bytes, string) {
// SignTypedData signs EIP-712 conformant typed data // SignTypedData signs EIP-712 conformant typed data
// hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}") // hash = keccak256("\x19${byteVersion}${domainSeparator}${hashStruct(message)}")
func (api *SignerAPI) SignTypedData(ctx context.Context, addr common.MixedcaseAddress, typedData TypedData) (hexutil.Bytes, error) { 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 return nil, err
} }
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map()) domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
@ -584,12 +585,12 @@ func UnmarshalValidatorData(data interface{}) (ValidatorData, error) {
}, nil }, nil
} }
// IsValid checks if the typed data is sound // Validate checks if the typed data is sound
func (typedData *TypedData) IsValid() error { func (typedData *TypedData) Validate() error {
if err := typedData.Types.IsValid(); err != nil { if err := typedData.Types.Validate(); err != nil {
return err return err
} }
if err := typedData.Domain.IsValid(); err != nil { if err := typedData.Domain.Validate(); err != nil {
return err return err
} }
return nil return nil
@ -612,8 +613,8 @@ func (typedData *TypedData) PrettyPrint() string {
return "" return ""
} }
// IsValid checks if the types object is conformant to the specs // Validate checks if the types object is conformant to the specs
func (types *EIP712Types) IsValid() error { func (types *EIP712Types) Validate() error {
for typeKey, typeArr := range *types { for typeKey, typeArr := range *types {
for _, typeObj := range typeArr { for _, typeObj := range typeArr {
typeVal := typeObj["type"] typeVal := typeObj["type"]
@ -642,35 +643,26 @@ func (types *EIP712Types) IsValid() error {
// isStandardType checks if the given type is a EIP712 conformant type // isStandardType checks if the given type is a EIP712 conformant type
func isStandardTypeStr(encType string) bool { func isStandardTypeStr(encType string) bool {
// Atomic types // Atomic types
for _, standardType := range []string{ exp, _ := regexp.Compile(`^(address|bool|bytes|string)$`)
TypeAddress, if (exp.MatchString(encType)) {
TypeBool,
TypeBytes,
TypeString,
} {
if standardType == encType {
return true return true
} }
}
// Dynamic types // Dynamic types
for _, standardType := range []string{ exp, _ = regexp.Compile(`^(bytes|int|uint)(\d+)$`)
TypeBytes, if (exp.MatchString(encType)) {
TypeInt,
TypeUint,
} {
if strings.HasPrefix(encType, standardType) {
return true return true
} }
}
// Reference types // Arrays
return encType[len(encType)-1] == ']' // 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 // the minimum viable keys and values
func (domain *EIP712Domain) IsValid() error { func (domain *EIP712Domain) Validate() error {
if domain.ChainId == big.NewInt(0) { if domain.ChainId == big.NewInt(0) {
return errors.New("chainId must be specified according to EIP-155") 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 { if err != nil {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
err = typedData.IsValid() err = typedData.Validate()
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
} }
@ -357,7 +357,7 @@ func TestMalformedDomainData(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
err = typedData.IsValid() err = typedData.Validate()
if err == nil { if err == nil {
t.Fatalf("Expected `referenced type 'Blahonga' is undefined`, got %v", err) t.Fatalf("Expected `referenced type 'Blahonga' is undefined`, got %v", err)
} }
@ -440,7 +440,7 @@ func TestMalformedData3(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
err = typedData.IsValid() err = typedData.Validate()
if err != nil { if err != nil {
t.Fatalf("Expected no error, got %v", err) t.Fatalf("Expected no error, got %v", err)
} }
@ -543,18 +543,8 @@ func TestMalformedData4(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
err = typedData.IsValid() err = typedData.Validate()
if err != nil { if err.Error() != "unknown atomic type 'uint256 ... and now for something completely different'" {
t.Fatalf("Expected no error, got %v", err) 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)
//}
} }