/
client.go
134 lines (103 loc) · 2.83 KB
/
client.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package etherface
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/samber/lo"
"github.com/tidwall/gjson"
)
var (
ErrorNoResults = errors.New("no results")
)
const (
EtherfaceEndpoint = "https://etherface.rss3.io/v1"
DefaultTimeout = 3 * time.Second
DefaultAttempts = 3
)
type Client interface {
Lookup(ctx context.Context, hash string) (string, error)
}
var _ Client = (*etherfaceClient)(nil)
type etherfaceClient struct {
httpClient *http.Client
attempts uint
}
func (h *etherfaceClient) Lookup(ctx context.Context, hash string) (functionName string, err error) {
retryableFunc := func() error {
functionName, err = h.fetch(ctx, hash)
return err
}
retryIfFunc := func(err error) bool {
return !errors.Is(err, ErrorNoResults)
}
if err := retry.Do(retryableFunc, retry.Attempts(h.attempts), retry.RetryIf(retryIfFunc)); err != nil {
return "", fmt.Errorf("retry attempts: %w", err)
}
return functionName, nil
}
func (h *etherfaceClient) fetch(ctx context.Context, hash string) (string, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/signatures/hash/function/%s/1", EtherfaceEndpoint, hash), nil)
if err != nil {
return "", fmt.Errorf("new request: %w", err)
}
// nolint:bodyclose // False positive
response, err := h.httpClient.Do(request)
if err != nil {
return "", fmt.Errorf("send request: %w", err)
}
if response.StatusCode != http.StatusOK {
defer lo.Try(response.Body.Close)
return "", fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
defer lo.Try(response.Body.Close)
respData, err := io.ReadAll(response.Body)
if err != nil {
return "", fmt.Errorf("read all: %w", err)
}
etherfaceData := gjson.ParseBytes(respData)
items := etherfaceData.Get("items").Array()
if len(items) > 0 {
return extractFunctionName(items[0].Get("text").String()), nil
}
return "", nil
}
// extractFunctionName extracts the function name from a string before the first bracket
func extractFunctionName(str string) string {
index := strings.Index(str, "(")
if index != -1 {
return str[:index]
}
return ""
}
func NewEtherfaceClient(options ...ClientOption) (Client, error) {
instance := etherfaceClient{
httpClient: &http.Client{
Timeout: DefaultTimeout,
},
attempts: DefaultAttempts,
}
for _, option := range options {
if err := option(&instance); err != nil {
return nil, fmt.Errorf("apply options: %w", err)
}
}
return &instance, nil
}
type ClientOption func(client *etherfaceClient) error
func WithAttempts(attempts uint) ClientOption {
return func(h *etherfaceClient) error {
h.attempts = attempts
return nil
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(h *etherfaceClient) error {
h.httpClient.Timeout = timeout
return nil
}
}