From 08a83f18bd46473dc36568992b0d8c7a6c635565 Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Thu, 21 Nov 2024 14:40:27 -0600 Subject: [PATCH 1/3] accounts/abi: add TestMakeTopics subtest for mutation of big.Int inputs --- accounts/abi/topics_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/accounts/abi/topics_test.go b/accounts/abi/topics_test.go index 6a4c50078af5..161867e2d962 100644 --- a/accounts/abi/topics_test.go +++ b/accounts/abi/topics_test.go @@ -149,6 +149,23 @@ func TestMakeTopics(t *testing.T) { } }) } + + t.Run("does not mutate big.Int", func(t *testing.T) { + t.Parallel() + want := [][]common.Hash{{common.HexToHash("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}} + + in := big.NewInt(-1) + got, err := MakeTopics([]interface{}{in}) + if err != nil { + t.Fatalf("makeTopics() error = %v", err) + } + if !reflect.DeepEqual(got, want) { + t.Fatalf("makeTopics() = %v, want %v", got, want) + } + if orig := big.NewInt(-1); in.Cmp(orig) != 0 { + t.Fatalf("makeTopics() mutated an input parameter from %v to %v", orig, in) + } + }) } type args struct { From 0a8e4835db01d0bb061339c176437b5b07bd602e Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Thu, 21 Nov 2024 14:41:31 -0600 Subject: [PATCH 2/3] accounts/abi: fix MakeTopics big.Int mutation --- accounts/abi/topics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/accounts/abi/topics.go b/accounts/abi/topics.go index 7ce9b7273c47..4819334ae6da 100644 --- a/accounts/abi/topics.go +++ b/accounts/abi/topics.go @@ -42,7 +42,7 @@ func MakeTopics(query ...[]interface{}) ([][]common.Hash, error) { case common.Address: copy(topic[common.HashLength-common.AddressLength:], rule[:]) case *big.Int: - copy(topic[:], math.U256Bytes(rule)) + copy(topic[:], math.U256Bytes(new(big.Int).Set(rule))) case bool: if rule { topic[common.HashLength-1] = 1 From 1e531eb20d8c09809146833635bdf1d66fcb6cf1 Mon Sep 17 00:00:00 2001 From: Jordan Krage Date: Mon, 25 Nov 2024 06:13:19 -0600 Subject: [PATCH 3/3] signer/core/apitypes: fix (*TypedData).EncodePrimitiveValue --- signer/core/apitypes/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index 2ae182279a84..b56931c1d18b 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -676,7 +676,7 @@ func (typedData *TypedData) EncodePrimitiveValue(encType string, encValue interf if err != nil { return nil, err } - return math.U256Bytes(b), nil + return math.U256Bytes(new(big.Int).Set(b)), nil } return nil, fmt.Errorf("unrecognized type '%s'", encType) }