Added extra regexp for reference types

This commit is contained in:
Paul Berg 2018-12-06 08:51:12 +00:00 committed by Martin Holst Swende
parent 159677a873
commit 7bbe49e8d0
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0
2 changed files with 13 additions and 8 deletions

View file

@ -96,6 +96,7 @@ type TypedDataDomain struct {
} }
var typedDataRegexp = regexp.MustCompile(`^((address|bool|bytes|string)|((bytes)([1-9]|[1-2][0-9]|3[0-2]))|((int|uint)(8|16|32|64|128|256)))(\[])?$`) var typedDataRegexp = regexp.MustCompile(`^((address|bool|bytes|string)|((bytes)([1-9]|[1-2][0-9]|3[0-2]))|((int|uint)(8|16|32|64|128|256)))(\[])?$`)
var typedDataReferenceTypeRegexp = regexp.MustCompile(`^[A-Z](\w*)(\[])?$`)
// Sign receives a request and produces a signature // Sign receives a request and produces a signature
@ -747,15 +748,19 @@ func (types *Types) Validate() error {
} }
firstChar := []rune(typeVal)[0] firstChar := []rune(typeVal)[0]
if unicode.IsUpper(firstChar) { if unicode.IsUpper(firstChar) {
if (*types)[typeVal] == nil { if (*types)[typeVal] != nil {
return fmt.Errorf("referenced type '%s' is undefined", typeVal) if !typedDataReferenceTypeRegexp.MatchString(typeVal) {
return fmt.Errorf("unknown reference type '%s", typeVal)
}
} else {
return fmt.Errorf("reference type '%s' is undefined", typeVal)
} }
} else { } else {
if !typedDataRegexp.MatchString(typeVal) { if !typedDataRegexp.MatchString(typeVal) {
if (*types)[typeVal] != nil { if (*types)[typeVal] != nil {
return fmt.Errorf("referenced type '%s' must be capitalized", typeVal) return fmt.Errorf("reference type '%s' must be capitalized", typeVal)
} else { } else {
return fmt.Errorf("unknown atomic type '%s'", typeVal) return fmt.Errorf("unknown type '%s'", typeVal)
} }
} }
} }

View file

@ -463,8 +463,8 @@ func TestMalformedData2(t *testing.T) {
malformedTypedData.Types["Mail"][2]["type"] = "Blahonga" malformedTypedData.Types["Mail"][2]["type"] = "Blahonga"
err = malformedTypedData.Validate() err = malformedTypedData.Validate()
if err == nil || err.Error() != "referenced type 'Blahonga' is undefined" { if err == nil || err.Error() != "reference type 'Blahonga' is undefined" {
t.Fatalf("Expected `referenced type 'Blahonga' is undefined`, got %v", err) t.Fatalf("Expected `reference type 'Blahonga' is undefined`, got %v", err)
} }
_, err = malformedTypedData.HashStruct(malformedTypedData.PrimaryType, malformedTypedData.Message) _, err = malformedTypedData.HashStruct(malformedTypedData.PrimaryType, malformedTypedData.Message)
if err == nil || err.Error() != "unrecognized type 'Blahonga'" { if err == nil || err.Error() != "unrecognized type 'Blahonga'" {
@ -555,8 +555,8 @@ func TestMalformedData3(t *testing.T) {
t.Fatalf("unmarshalling failed %v", err) t.Fatalf("unmarshalling failed %v", err)
} }
err = malformedTypedData.Validate() err = malformedTypedData.Validate()
if err == nil || err.Error() != "unknown atomic type 'uint256 ... and now for something completely different'" { if err == nil || err.Error() != "unknown type 'uint256 ... and now for something completely different'" {
t.Fatalf("Expected `unknown atomic type 'uint256 ... and now for something completely different'`, got %v", err) t.Fatalf("Expected `unknown type 'uint256 ... and now for something completely different'`, got %v", err)
} }
malformedTypedData.Types["EIP712Domain"][2]["type"] = "uint256" malformedTypedData.Types["EIP712Domain"][2]["type"] = "uint256"