Skip to content

Commit f9bf58c

Browse files
authored
fix signature handling (#169)
### TL;DR Improved signature handling for logs and transactions by moving ABI construction into the main handler functions. ### What changed? - Consolidated log and transaction handler logic by moving signature processing into the main handler functions - Updated signature hash calculation to use ABI-generated IDs instead of manual Keccak256 hashing - Simplified function signatures by removing unnecessary parameter passing - Improved error handling for ABI construction ### How to test? 1. Query event logs with a valid event signature - `GET /{chainId}/events/{contract}/{signature}` 2. Query transactions with a valid function signature - `GET /{chainId}/transactions/{to}/{signature}` 3. Verify that signature hashes are correctly generated from ABIs 4. Confirm that error handling works for invalid signatures ### Why make this change? The previous implementation had redundant signature processing and used a less reliable method for generating signature hashes. This change makes the code more maintainable and uses the more accurate ABI-generated IDs for signature matching, reducing potential edge cases and improving reliability.
2 parents f33e3df + 9eb1bc0 commit f9bf58c

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

internal/common/abi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GetABIForContract(chainId string, contract string) (*abi.ABI, error) {
4848

4949
func ConstructEventABI(signature string) (*abi.Event, error) {
5050
// Regex to extract the event name and parameters
51-
regex := regexp.MustCompile(`^(\w+)\((.*)\)$`)
51+
regex := regexp.MustCompile(`^(\w+)\s*\((.*)\)$`)
5252
matches := regex.FindStringSubmatch(strings.TrimSpace(signature))
5353
if len(matches) != 3 {
5454
return nil, fmt.Errorf("invalid event signature format")
@@ -68,7 +68,7 @@ func ConstructEventABI(signature string) (*abi.Event, error) {
6868
}
6969

7070
func ConstructFunctionABI(signature string) (*abi.Method, error) {
71-
regex := regexp.MustCompile(`^(\w+)\((.*)\)$`)
71+
regex := regexp.MustCompile(`^(\w+)\s*\((.*)\)$`)
7272
matches := regex.FindStringSubmatch(strings.TrimSpace(signature))
7373
if len(matches) != 3 {
7474
return nil, fmt.Errorf("invalid function signature format")

internal/handlers/logs_handlers.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"sync"
66

77
"github.com/ethereum/go-ethereum/accounts/abi"
8-
"github.com/ethereum/go-ethereum/crypto"
98
"github.com/gin-gonic/gin"
109
"github.com/rs/zerolog/log"
1110
"github.com/thirdweb-dev/indexer/api"
@@ -42,7 +41,7 @@ var (
4241
// @Failure 500 {object} api.Error
4342
// @Router /{chainId}/events [get]
4443
func GetLogs(c *gin.Context) {
45-
handleLogsRequest(c, "", "", nil)
44+
handleLogsRequest(c)
4645
}
4746

4847
// @Summary Get logs by contract
@@ -67,8 +66,7 @@ func GetLogs(c *gin.Context) {
6766
// @Failure 500 {object} api.Error
6867
// @Router /{chainId}/events/{contract} [get]
6968
func GetLogsByContract(c *gin.Context) {
70-
contractAddress := c.Param("contract")
71-
handleLogsRequest(c, contractAddress, "", nil)
69+
handleLogsRequest(c)
7270
}
7371

7472
// @Summary Get logs by contract and event signature
@@ -94,32 +92,33 @@ func GetLogsByContract(c *gin.Context) {
9492
// @Failure 500 {object} api.Error
9593
// @Router /{chainId}/events/{contract}/{signature} [get]
9694
func GetLogsByContractAndSignature(c *gin.Context) {
97-
contractAddress := c.Param("contract")
98-
eventSignature := c.Param("signature")
99-
strippedSignature := common.StripPayload(eventSignature)
100-
eventABI, err := common.ConstructEventABI(eventSignature)
101-
if err != nil {
102-
log.Debug().Err(err).Msgf("Unable to construct event ABI for %s", eventSignature)
103-
}
104-
handleLogsRequest(c, contractAddress, strippedSignature, eventABI)
95+
handleLogsRequest(c)
10596
}
10697

107-
func handleLogsRequest(c *gin.Context, contractAddress, signature string, eventABI *abi.Event) {
98+
func handleLogsRequest(c *gin.Context) {
10899
chainId, err := api.GetChainId(c)
109100
if err != nil {
110101
api.BadRequestErrorHandler(c, err)
111102
return
112103
}
113104

105+
contractAddress := c.Param("contract")
106+
signature := c.Param("signature")
107+
114108
queryParams, err := api.ParseQueryParams(c.Request)
115109
if err != nil {
116110
api.BadRequestErrorHandler(c, err)
117111
return
118112
}
119113

114+
var eventABI *abi.Event
120115
signatureHash := ""
121116
if signature != "" {
122-
signatureHash = crypto.Keccak256Hash([]byte(signature)).Hex()
117+
eventABI, err = common.ConstructEventABI(signature)
118+
if err != nil {
119+
log.Debug().Err(err).Msgf("Unable to construct event ABI for %s", signature)
120+
}
121+
signatureHash = eventABI.ID.Hex()
123122
}
124123

125124
mainStorage, err := getMainStorage()

internal/handlers/transactions_handlers.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"net/http"
55

66
"github.com/ethereum/go-ethereum/accounts/abi"
7-
"github.com/ethereum/go-ethereum/crypto"
7+
gethCommon "github.com/ethereum/go-ethereum/common"
88
"github.com/gin-gonic/gin"
99
"github.com/rs/zerolog/log"
1010
"github.com/thirdweb-dev/indexer/api"
1111
config "github.com/thirdweb-dev/indexer/configs"
1212
"github.com/thirdweb-dev/indexer/internal/common"
13-
"github.com/thirdweb-dev/indexer/internal/rpc"
1413
"github.com/thirdweb-dev/indexer/internal/storage"
1514
)
1615

@@ -35,7 +34,7 @@ import (
3534
// @Failure 500 {object} api.Error
3635
// @Router /{chainId}/transactions [get]
3736
func GetTransactions(c *gin.Context) {
38-
handleTransactionsRequest(c, "", "", nil)
37+
handleTransactionsRequest(c)
3938
}
4039

4140
// @Summary Get transactions by contract
@@ -60,8 +59,7 @@ func GetTransactions(c *gin.Context) {
6059
// @Failure 500 {object} api.Error
6160
// @Router /{chainId}/transactions/{to} [get]
6261
func GetTransactionsByContract(c *gin.Context) {
63-
to := c.Param("to")
64-
handleTransactionsRequest(c, to, "", nil)
62+
handleTransactionsRequest(c)
6563
}
6664

6765
// @Summary Get transactions by contract and signature
@@ -87,32 +85,33 @@ func GetTransactionsByContract(c *gin.Context) {
8785
// @Failure 500 {object} api.Error
8886
// @Router /{chainId}/transactions/{to}/{signature} [get]
8987
func GetTransactionsByContractAndSignature(c *gin.Context) {
90-
to := c.Param("to")
91-
signature := c.Param("signature")
92-
strippedSignature := common.StripPayload(signature)
93-
functionABI, err := common.ConstructFunctionABI(signature)
94-
if err != nil {
95-
log.Debug().Err(err).Msgf("Unable to construct function ABI for %s", signature)
96-
}
97-
handleTransactionsRequest(c, to, strippedSignature, functionABI)
88+
handleTransactionsRequest(c)
9889
}
9990

100-
func handleTransactionsRequest(c *gin.Context, contractAddress, signature string, functionABI *abi.Method) {
91+
func handleTransactionsRequest(c *gin.Context) {
10192
chainId, err := api.GetChainId(c)
10293
if err != nil {
10394
api.BadRequestErrorHandler(c, err)
10495
return
10596
}
10697

98+
contractAddress := c.Param("to")
99+
signature := c.Param("signature")
100+
107101
queryParams, err := api.ParseQueryParams(c.Request)
108102
if err != nil {
109103
api.BadRequestErrorHandler(c, err)
110104
return
111105
}
112106

107+
var functionABI *abi.Method
113108
signatureHash := ""
114109
if signature != "" {
115-
signatureHash = rpc.ExtractFunctionSelector(crypto.Keccak256Hash([]byte(signature)).Hex())
110+
functionABI, err = common.ConstructFunctionABI(signature)
111+
if err != nil {
112+
log.Debug().Err(err).Msgf("Unable to construct function ABI for %s", signature)
113+
}
114+
signatureHash = "0x" + gethCommon.Bytes2Hex(functionABI.ID)
116115
}
117116

118117
mainStorage, err := getMainStorage()

0 commit comments

Comments
 (0)