-
Notifications
You must be signed in to change notification settings - Fork 32
/
main.go
78 lines (66 loc) · 1.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/ethereum/go-ethereum/crypto"
)
func extractRevertStrings(line string) []string {
re := regexp.MustCompile(`\brevert\s+([\w\d_]+)\s*\(\s*\)`)
matches := re.FindAllStringSubmatch(line, -1)
var result []string
for _, match := range matches {
result = append(result, match[1])
}
return result
}
func keccak256Hash(input string) string {
hash := crypto.Keccak256Hash([]byte(input + "()"))
return hash.Hex()
}
func processFile(file string, filter string) {
//nolint: gosec
content, err := os.ReadFile(file)
if err != nil {
fmt.Printf("Error reading file %s: %s\n", file, err)
return
}
lines := strings.Split(string(content), "\n")
for i, line := range lines {
revertStrings := extractRevertStrings(line)
for _, revertString := range revertStrings {
hashedString := keccak256Hash(revertString)
if filter == "" || strings.HasPrefix(hashedString, filter) {
fmt.Printf("%s(): %s (File: %s, Line: %d)\n", revertString, hashedString, file, i+1)
}
}
}
}
func findAndPrintReverts(path, filter string) {
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && filepath.Ext(filePath) == ".sol" {
processFile(filePath, filter)
}
return nil
})
if err != nil {
fmt.Println("Error walking the path:", err)
}
}
func main() {
var path string
var filter string
flag.StringVar(&path, "p", "", "path to search recursively")
flag.StringVar(&filter, "f", "", "revert hash filter [optional]")
flag.Parse()
if len(path) == 0 {
panic("expected path to be set (use -p flag)")
}
findAndPrintReverts(path, filter)
}