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

TypedData.EncodeData should not reject a Domain without a ChainId #21306

Merged
merged 2 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 1 addition & 5 deletions signer/core/signed_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,7 @@ func isPrimitiveTypeValid(primitiveType string) bool {
// validate checks if the given domain is valid, i.e. contains at least
// the minimum viable keys and values
func (domain *TypedDataDomain) validate() error {
if domain.ChainId == nil {
return errors.New("chainId must be specified according to EIP-155")
}

if len(domain.Name) == 0 && len(domain.Version) == 0 && len(domain.VerifyingContract) == 0 && len(domain.Salt) == 0 {
if domain.ChainId == nil && len(domain.Name) == 0 && len(domain.Version) == 0 && len(domain.VerifyingContract) == 0 && len(domain.Salt) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why you moved domain.ChainId == nil into this clause? I don't understand

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EIP-712 says the domain has to have at least one of the fields (chain ID, name, version, verifying contract or salt) (https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md#definition-of-domainseparator)
I supposed this check was responsibile for enforcing that condition.

return errors.New("domain is undefined")
}

Expand Down
8 changes: 8 additions & 0 deletions signer/core/signed_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ func TestDomainChainId(t *testing.T) {
if _, ok := withoutChainID.Domain.Map()["chainId"]; ok {
t.Errorf("Expected the chainId key to not be present in the domain map")
}
// should encode successfully
if _, err := withoutChainID.HashStruct("EIP712Domain", withoutChainID.Domain.Map()); err != nil {
t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
}
withChainID := core.TypedData{
Types: core.Types{
"EIP712Domain": []core.Type{
Expand All @@ -261,6 +265,10 @@ func TestDomainChainId(t *testing.T) {
if _, ok := withChainID.Domain.Map()["chainId"]; !ok {
t.Errorf("Expected the chainId key be present in the domain map")
}
// should encode successfully
if _, err := withChainID.HashStruct("EIP712Domain", withChainID.Domain.Map()); err != nil {
t.Errorf("Expected the typedData to encode the domain successfully, got %v", err)
}
}

func TestHashStruct(t *testing.T) {
Expand Down