diff options
author | Steve Gattuso <steve@stevegattuso.me> | 2019-04-09 16:17:09 +0800 |
---|---|---|
committer | Martin Holst Swende <martin@swende.se> | 2019-04-09 16:17:09 +0800 |
commit | f538d15241d65a2fa1a12bdfb5209f32cecf0353 (patch) | |
tree | 6b3e3056c30a6e83829637a7d75b17e747a424bb /signer/core | |
parent | 5b947c50046f794dcbbc73367e77c4605d3f596d (diff) | |
download | go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar.gz go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar.bz2 go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar.lz go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar.xz go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.tar.zst go-tangerine-f538d15241d65a2fa1a12bdfb5209f32cecf0353.zip |
clef: fix chainId key being present in domain map (#19303)
This PR fixes this, moving domain.ChainId from the map's initializer down to a separate if statement which checks the existance of ChainId's value, similar to the rest of the fields, before adding it. I've also included a new test to demonstrate the issue
Diffstat (limited to 'signer/core')
-rw-r--r-- | signer/core/signed_data.go | 6 | ||||
-rw-r--r-- | signer/core/signed_data_test.go | 34 |
2 files changed, 38 insertions, 2 deletions
diff --git a/signer/core/signed_data.go b/signer/core/signed_data.go index 475a8837e..d264cbaa0 100644 --- a/signer/core/signed_data.go +++ b/signer/core/signed_data.go @@ -894,8 +894,10 @@ func (domain *TypedDataDomain) validate() error { // Map is a helper function to generate a map version of the domain func (domain *TypedDataDomain) Map() map[string]interface{} { - dataMap := map[string]interface{}{ - "chainId": domain.ChainId, + dataMap := map[string]interface{}{} + + if domain.ChainId != nil { + dataMap["chainId"] = domain.ChainId } if len(domain.Name) > 0 { diff --git a/signer/core/signed_data_test.go b/signer/core/signed_data_test.go index 76e1b72ee..b1d893221 100644 --- a/signer/core/signed_data_test.go +++ b/signer/core/signed_data_test.go @@ -225,6 +225,40 @@ func TestSignData(t *testing.T) { } } +func TestDomainChainId(t *testing.T) { + withoutChainID := TypedData{ + Types: Types{ + "EIP712Domain": []Type{ + {Name: "name", Type: "string"}, + }, + }, + Domain: TypedDataDomain{ + Name: "test", + }, + } + + if _, ok := withoutChainID.Domain.Map()["chainId"]; ok { + t.Errorf("Expected the chainId key to not be present in the domain map") + } + + withChainID := TypedData{ + Types: Types{ + "EIP712Domain": []Type{ + {Name: "name", Type: "string"}, + {Name: "chainId", Type: "uint256"}, + }, + }, + Domain: TypedDataDomain{ + Name: "test", + ChainId: big.NewInt(1), + }, + } + + if _, ok := withChainID.Domain.Map()["chainId"]; !ok { + t.Errorf("Expected the chainId key be present in the domain map") + } +} + func TestHashStruct(t *testing.T) { hash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message) if err != nil { |