forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4byte.go
51 lines (42 loc) · 964 Bytes
/
4byte.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
package fourbyte
import (
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
)
const (
fourByteURL = "https://www.4byte.directory"
)
// Resolve resolves a method/event signature
func Resolve(str string) (string, error) {
return get("/api/v1/signatures/?hex_signature=" + str)
}
// ResolveBytes resolves a method/event signature in bytes
func ResolveBytes(b []byte) (string, error) {
return Resolve(hex.EncodeToString(b))
}
func get(path string) (string, error) {
req, err := http.Get(fourByteURL + path)
if err != nil {
return "", err
}
defer req.Body.Close()
data, err := ioutil.ReadAll(req.Body)
if err != nil {
return "", err
}
var result struct {
Results []signatureResult
}
if err := json.Unmarshal(data, &result); err != nil {
return "", err
}
if len(result.Results) == 0 {
return "", nil
}
return result.Results[0].TextSignature, nil
}
type signatureResult struct {
TextSignature string `json:"text_signature"`
}