Skip to content

Commit f538d15

Browse files
stevenleegholiman
authored andcommitted
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
1 parent 5b947c5 commit f538d15

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

‎signer/core/signed_data.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -894,8 +894,10 @@ func (domain *TypedDataDomain) validate() error {
894894

895895
// Map is a helper function to generate a map version of the domain
896896
func (domain *TypedDataDomain) Map() map[string]interface{} {
897-
dataMap := map[string]interface{}{
898-
"chainId": domain.ChainId,
897+
dataMap := map[string]interface{}{}
898+
899+
if domain.ChainId != nil {
900+
dataMap["chainId"] = domain.ChainId
899901
}
900902

901903
if len(domain.Name) > 0 {

‎signer/core/signed_data_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,40 @@ func TestSignData(t *testing.T) {
225225
}
226226
}
227227

228+
func TestDomainChainId(t *testing.T) {
229+
withoutChainID := TypedData{
230+
Types: Types{
231+
"EIP712Domain": []Type{
232+
{Name: "name", Type: "string"},
233+
},
234+
},
235+
Domain: TypedDataDomain{
236+
Name: "test",
237+
},
238+
}
239+
240+
if _, ok := withoutChainID.Domain.Map()["chainId"]; ok {
241+
t.Errorf("Expected the chainId key to not be present in the domain map")
242+
}
243+
244+
withChainID := TypedData{
245+
Types: Types{
246+
"EIP712Domain": []Type{
247+
{Name: "name", Type: "string"},
248+
{Name: "chainId", Type: "uint256"},
249+
},
250+
},
251+
Domain: TypedDataDomain{
252+
Name: "test",
253+
ChainId: big.NewInt(1),
254+
},
255+
}
256+
257+
if _, ok := withChainID.Domain.Map()["chainId"]; !ok {
258+
t.Errorf("Expected the chainId key be present in the domain map")
259+
}
260+
}
261+
228262
func TestHashStruct(t *testing.T) {
229263
hash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
230264
if err != nil {

0 commit comments

Comments
 (0)