2023-05-25 13:12:54 +00:00
|
|
|
package fourbyte
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/abi"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Signature struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Text string `json:"text_signature"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ByID []Signature
|
|
|
|
|
|
|
|
func (s ByID) Len() int { return len(s) }
|
2023-06-06 11:35:51 +00:00
|
|
|
func (s ByID) Less(i, j int) bool { return s[i].ID > s[j].ID }
|
2023-05-25 13:12:54 +00:00
|
|
|
func (s ByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
type SignatureList struct {
|
|
|
|
Count int `json:"count"`
|
|
|
|
Results []Signature `json:"results"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
2023-07-10 09:35:11 +00:00
|
|
|
Client *http.Client
|
|
|
|
URL string
|
2023-05-25 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() *Client {
|
2023-07-10 09:35:11 +00:00
|
|
|
return &Client{Client: &http.Client{Timeout: time.Minute}, URL: "https://www.4byte.directory"}
|
2023-05-25 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) DoQuery(url string) (*http.Response, error) {
|
2023-07-10 09:35:11 +00:00
|
|
|
resp, err := c.Client.Get(url)
|
2023-05-25 13:12:54 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
|
|
|
|
if len(data) < 10 || !strings.HasPrefix(data, "0x") {
|
|
|
|
return nil, errors.New("input is badly formatted")
|
|
|
|
}
|
|
|
|
methodSigData := data[2:10]
|
2023-07-10 09:35:11 +00:00
|
|
|
url := fmt.Sprintf("%s/api/v1/signatures/?hex_signature=%s", c.URL, methodSigData)
|
2023-05-25 13:12:54 +00:00
|
|
|
resp, err := c.DoQuery(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var signatures SignatureList
|
|
|
|
err = json.Unmarshal(body, &signatures)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if signatures.Count == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rgx := regexp.MustCompile(`\((.*?)\)`)
|
|
|
|
results := signatures.Results
|
|
|
|
sort.Sort(ByID(results))
|
|
|
|
for _, signature := range results {
|
2023-06-06 11:35:51 +00:00
|
|
|
id := fmt.Sprintf("0x%x", signature.ID)
|
2023-05-25 13:12:54 +00:00
|
|
|
name := strings.Split(signature.Text, "(")[0]
|
|
|
|
rs := rgx.FindStringSubmatch(signature.Text)
|
2023-07-10 09:35:11 +00:00
|
|
|
inputsMapString := make(map[string]string)
|
|
|
|
if len(rs[1]) > 0 {
|
|
|
|
inputs := make([]string, 0)
|
|
|
|
rawInputs := strings.Split(rs[1], ",")
|
|
|
|
for index, typ := range rawInputs {
|
|
|
|
if index == len(rawInputs)-1 && typ == "bytes" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inputs = append(inputs, fmt.Sprintf("{\"name\":\"%d\",\"type\":\"%s\"}", index, typ))
|
|
|
|
}
|
|
|
|
functionABI := fmt.Sprintf("[{\"constant\":true,\"inputs\":[%s],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\", \"name\": \"%s\"}], ", strings.Join(inputs, ","), name)
|
|
|
|
contractABI, err := abi.JSON(strings.NewReader(functionABI))
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
method := contractABI.Methods[name]
|
|
|
|
inputsMap := make(map[string]interface{})
|
|
|
|
if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil {
|
2023-06-19 10:04:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-05-25 13:12:54 +00:00
|
|
|
|
2023-07-10 09:35:11 +00:00
|
|
|
for key, value := range inputsMap {
|
|
|
|
inputsMapString[key] = fmt.Sprintf("%v", value)
|
|
|
|
}
|
2023-05-25 13:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &thirdparty.DataParsed{
|
|
|
|
Name: name,
|
2023-06-06 11:35:51 +00:00
|
|
|
ID: id,
|
2023-05-25 13:12:54 +00:00
|
|
|
Signature: signature.Text,
|
|
|
|
Inputs: inputsMapString,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("couldn't find a corresponding signature")
|
|
|
|
}
|