From bc92df79d551a9b211ab50de384475508079b060 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Mon, 10 Jul 2023 11:35:11 +0200 Subject: [PATCH] fix: fourbyte integration no arg --- services/wallet/thirdparty/fourbyte/client.go | 51 ++++++++--------- .../wallet/thirdparty/fourbyte/client_test.go | 55 +++++++++++-------- .../thirdparty/fourbytegithub/client.go | 46 ++++++++-------- 3 files changed, 81 insertions(+), 71 deletions(-) diff --git a/services/wallet/thirdparty/fourbyte/client.go b/services/wallet/thirdparty/fourbyte/client.go index 32fa44df8..372ac8361 100644 --- a/services/wallet/thirdparty/fourbyte/client.go +++ b/services/wallet/thirdparty/fourbyte/client.go @@ -32,15 +32,16 @@ type SignatureList struct { } type Client struct { - client *http.Client + Client *http.Client + URL string } func NewClient() *Client { - return &Client{client: &http.Client{Timeout: time.Minute}} + return &Client{Client: &http.Client{Timeout: time.Minute}, URL: "https://www.4byte.directory"} } func (c *Client) DoQuery(url string) (*http.Response, error) { - resp, err := c.client.Get(url) + resp, err := c.Client.Get(url) if err != nil { return nil, err @@ -53,7 +54,7 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) { return nil, errors.New("input is badly formatted") } methodSigData := data[2:10] - url := fmt.Sprintf("https://www.4byte.directory/api/v1/signatures/?hex_signature=%s", methodSigData) + url := fmt.Sprintf("%s/api/v1/signatures/?hex_signature=%s", c.URL, methodSigData) resp, err := c.DoQuery(url) if err != nil { return nil, err @@ -79,30 +80,30 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) { id := fmt.Sprintf("0x%x", signature.ID) name := strings.Split(signature.Text, "(")[0] rs := rgx.FindStringSubmatch(signature.Text) - - inputs := make([]string, 0) - rawInputs := strings.Split(rs[1], ",") - for index, typ := range rawInputs { - if index == len(rawInputs)-1 && typ == "bytes" { + 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 { 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 { - fmt.Println(err) - continue - } - inputsMapString := make(map[string]string) - for key, value := range inputsMap { - inputsMapString[key] = fmt.Sprintf("%v", value) + for key, value := range inputsMap { + inputsMapString[key] = fmt.Sprintf("%v", value) + } } return &thirdparty.DataParsed{ diff --git a/services/wallet/thirdparty/fourbyte/client_test.go b/services/wallet/thirdparty/fourbyte/client_test.go index 7f500c8e3..486f421c6 100644 --- a/services/wallet/thirdparty/fourbyte/client_test.go +++ b/services/wallet/thirdparty/fourbyte/client_test.go @@ -1,27 +1,34 @@ package fourbyte -// TODO: use mocked data if it make sense, instead of testing the servers UP status -// -// func TestRun(t *testing.T) { -// client := NewClient() -// res, err := client.Run("0x40e8d703000000000000000000000000670dca62b3418bddd08cbc69cb4490a5a3382a9f0000000000000000000000000000000000000000000000000000000000000064") -// require.Nil(t, err) -// require.Equal(t, res.Signature, "processDepositQueue(address,uint256)") -// require.Equal(t, res.Name, "processDepositQueue") -// require.Equal(t, res.ID, "0xf94d2") -// require.Equal(t, res.Inputs, map[string]string{ -// "0": "0x3030303030303030303030303637306463613632", -// "1": "44417128579249187980157595307322491418158007948522794164811090501355597543782", -// }) +import ( + "net/http" + "net/http/httptest" + "testing" -// res, err = client.Run("0xb88d4fde000000000000000000000000af1857535160973f42c34cb20f5ed7cd29099ad9000000000000000000000000baa4b7858c3277da9cb9cdadf405f2017afea19a00000000000000000000000000000000000000000000000000000000000000cb00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000360c6ebe") -// require.Nil(t, err) -// require.Equal(t, res.Signature, "safeTransferFrom(address,address,uint256,bytes)") -// require.Equal(t, res.Name, "safeTransferFrom") -// require.Equal(t, res.ID, "0x73e0") -// require.Equal(t, res.Inputs, map[string]string{ -// "0": "0x3030303030303030303030306166313835373533", -// "1": "0x3463623230663565643763643239303939616439", -// "2": "21796157974083048550319244236929488537086114760591164995666220774418197985333", -// }) -// } + "github.com/stretchr/testify/require" +) + +func TestRun(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(200) + _, err := w.Write([]byte("{\"count\":1,\"next\":null,\"previous\":null,\"results\":[{\"id\":145,\"created_at\":\"2016-07-09T03:58:28.234977Z\",\"text_signature\":\"transfer(address,uint256)\",\"hex_signature\":\"0xa9059cbb\",\"bytes_signature\":\"0xa9059cbb\"}]}")) + if err != nil { + return + } + })) + defer srv.Close() + + fb := NewClient() + fb.Client = srv.Client() + fb.URL = srv.URL + + res, err := fb.Run("0xa9059cbb000000000000000000000000e0e40d81121d41a7d85d8d2462b475074f9df5ec0000000000000000000000000000000000000000000000000000000077359400") + require.Nil(t, err) + require.Equal(t, res.Signature, "transfer(address,uint256)") + require.Equal(t, res.ID, "0x91") + require.Equal(t, res.Name, "transfer") + require.Equal(t, res.Inputs, map[string]string{ + "0": "0x3030303030303030303030306530653430643831", + "1": "22252012820881184517742036120632151212095838186768864961872069019727748752739", + }) +} diff --git a/services/wallet/thirdparty/fourbytegithub/client.go b/services/wallet/thirdparty/fourbytegithub/client.go index f7f0150a0..f7cbec186 100644 --- a/services/wallet/thirdparty/fourbytegithub/client.go +++ b/services/wallet/thirdparty/fourbytegithub/client.go @@ -69,29 +69,31 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) { id := fmt.Sprintf("0x%s", methodSigData) name := strings.Split(signature, "(")[0] rs := rgx.FindStringSubmatch(signature) - - 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 { - return nil, err - } - method := contractABI.Methods[name] - inputsMap := make(map[string]interface{}) - if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil { - return nil, err - } inputsMapString := make(map[string]string) - for key, value := range inputsMap { - inputsMapString[key] = fmt.Sprintf("%v", value) + + 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 { + return nil, err + } + method := contractABI.Methods[name] + inputsMap := make(map[string]interface{}) + if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil { + return nil, err + } + for key, value := range inputsMap { + inputsMapString[key] = fmt.Sprintf("%v", value) + } } return &thirdparty.DataParsed{