mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
Fixed TestMalformedData4 errors and renamed IsValid to Validate
This commit is contained in:
parent
cb97813e04
commit
7de3f530b4
2 changed files with 26 additions and 44 deletions
|
|
@ -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,
|
return true
|
||||||
TypeBytes,
|
|
||||||
TypeString,
|
|
||||||
} {
|
|
||||||
if standardType == encType {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dynamic types
|
// Dynamic types
|
||||||
for _, standardType := range []string{
|
exp, _ = regexp.Compile(`^(bytes|int|uint)(\d+)$`)
|
||||||
TypeBytes,
|
if (exp.MatchString(encType)) {
|
||||||
TypeInt,
|
return true
|
||||||
TypeUint,
|
|
||||||
} {
|
|
||||||
if strings.HasPrefix(encType, standardType) {
|
|
||||||
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")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue