Skip to content

Commit 92c5d10

Browse files
authored
accounts/abi/bind: check event signature before parsing (ethereum#23230)
* accounts/abi/bind: check event signature before parsing * remove redundant break line
1 parent 783e97e commit 92c5d10

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

accounts/abi/bind/base.go

+6
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]inter
431431

432432
// UnpackLog unpacks a retrieved log into the provided output structure.
433433
func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log) error {
434+
if log.Topics[0] != c.abi.Events[event].ID {
435+
return fmt.Errorf("event signature mismatch")
436+
}
434437
if len(log.Data) > 0 {
435438
if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil {
436439
return err
@@ -447,6 +450,9 @@ func (c *BoundContract) UnpackLog(out interface{}, event string, log types.Log)
447450

448451
// UnpackLogIntoMap unpacks a retrieved log into the provided map.
449452
func (c *BoundContract) UnpackLogIntoMap(out map[string]interface{}, event string, log types.Log) error {
453+
if log.Topics[0] != c.abi.Events[event].ID {
454+
return fmt.Errorf("event signature mismatch")
455+
}
450456
if len(log.Data) > 0 {
451457
if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil {
452458
return err

accounts/abi/bind/base_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const hexData = "0x000000000000000000000000376c47978271565f56deb45495afa69e59c16
110110
func TestUnpackIndexedStringTyLogIntoMap(t *testing.T) {
111111
hash := crypto.Keccak256Hash([]byte("testName"))
112112
topics := []common.Hash{
113-
common.HexToHash("0x0"),
113+
crypto.Keccak256Hash([]byte("received(string,address,uint256,bytes)")),
114114
hash,
115115
}
116116
mockLog := newMockLog(topics, common.HexToHash("0x0"))
@@ -135,7 +135,7 @@ func TestUnpackIndexedSliceTyLogIntoMap(t *testing.T) {
135135
}
136136
hash := crypto.Keccak256Hash(sliceBytes)
137137
topics := []common.Hash{
138-
common.HexToHash("0x0"),
138+
crypto.Keccak256Hash([]byte("received(string[],address,uint256,bytes)")),
139139
hash,
140140
}
141141
mockLog := newMockLog(topics, common.HexToHash("0x0"))
@@ -160,7 +160,7 @@ func TestUnpackIndexedArrayTyLogIntoMap(t *testing.T) {
160160
}
161161
hash := crypto.Keccak256Hash(arrBytes)
162162
topics := []common.Hash{
163-
common.HexToHash("0x0"),
163+
crypto.Keccak256Hash([]byte("received(address[2],address,uint256,bytes)")),
164164
hash,
165165
}
166166
mockLog := newMockLog(topics, common.HexToHash("0x0"))
@@ -187,7 +187,7 @@ func TestUnpackIndexedFuncTyLogIntoMap(t *testing.T) {
187187
var functionTy [24]byte
188188
copy(functionTy[:], functionTyBytes[0:24])
189189
topics := []common.Hash{
190-
common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
190+
crypto.Keccak256Hash([]byte("received(function,address,uint256,bytes)")),
191191
common.BytesToHash(functionTyBytes),
192192
}
193193
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))
@@ -208,7 +208,7 @@ func TestUnpackIndexedBytesTyLogIntoMap(t *testing.T) {
208208
bytes := []byte{1, 2, 3, 4, 5}
209209
hash := crypto.Keccak256Hash(bytes)
210210
topics := []common.Hash{
211-
common.HexToHash("0x99b5620489b6ef926d4518936cfec15d305452712b88bd59da2d9c10fb0953e8"),
211+
crypto.Keccak256Hash([]byte("received(bytes,address,uint256,bytes)")),
212212
hash,
213213
}
214214
mockLog := newMockLog(topics, common.HexToHash("0x5c698f13940a2153440c6d19660878bc90219d9298fdcf37365aa8d88d40fc42"))

0 commit comments

Comments
 (0)