fix: parse input data with bytes

This commit is contained in:
Anthony Laibe 2023-06-19 12:04:29 +02:00 committed by Anthony Laibe
parent fee033fadb
commit 8ef3c52f4d
2 changed files with 16 additions and 8 deletions

View File

@ -53,7 +53,6 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
return nil, errors.New("input is badly formatted") return nil, errors.New("input is badly formatted")
} }
methodSigData := data[2:10] methodSigData := data[2:10]
fmt.Println(methodSigData)
url := fmt.Sprintf("https://www.4byte.directory/api/v1/signatures/?hex_signature=%s", methodSigData) url := fmt.Sprintf("https://www.4byte.directory/api/v1/signatures/?hex_signature=%s", methodSigData)
resp, err := c.DoQuery(url) resp, err := c.DoQuery(url)
if err != nil { if err != nil {
@ -70,7 +69,6 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if signatures.Count == 0 { if signatures.Count == 0 {
return nil, err return nil, err
} }
@ -83,7 +81,11 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
rs := rgx.FindStringSubmatch(signature.Text) rs := rgx.FindStringSubmatch(signature.Text)
inputs := make([]string, 0) inputs := make([]string, 0)
for index, typ := range strings.Split(rs[1], ",") { 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)) inputs = append(inputs, fmt.Sprintf("{\"name\":\"%d\",\"type\":\"%s\"}", index, typ))
} }
@ -95,6 +97,7 @@ func (c *Client) Run(data string) (*thirdparty.DataParsed, error) {
method := contractABI.Methods[name] method := contractABI.Methods[name]
inputsMap := make(map[string]interface{}) inputsMap := make(map[string]interface{})
if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil { if err := method.Inputs.UnpackIntoMap(inputsMap, []byte(data[10:])); err != nil {
fmt.Println(err)
continue continue
} }
inputsMapString := make(map[string]string) inputsMapString := make(map[string]string)

View File

@ -18,9 +18,14 @@ func TestRun(t *testing.T) {
"1": "44417128579249187980157595307322491418158007948522794164811090501355597543782", "1": "44417128579249187980157595307322491418158007948522794164811090501355597543782",
}) })
_, err = client.Run("0x70a08231000") res, err = client.Run("0xb88d4fde000000000000000000000000af1857535160973f42c34cb20f5ed7cd29099ad9000000000000000000000000baa4b7858c3277da9cb9cdadf405f2017afea19a00000000000000000000000000000000000000000000000000000000000000cb00000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000360c6ebe")
require.NotNil(t, err) require.Nil(t, err)
require.Equal(t, res.Signature, "safeTransferFrom(address,address,uint256,bytes)")
_, err = client.Run("0x70a082310") require.Equal(t, res.Name, "safeTransferFrom")
require.NotNil(t, err) require.Equal(t, res.ID, "0x73e0")
require.Equal(t, res.Inputs, map[string]string{
"0": "0x3030303030303030303030306166313835373533",
"1": "0x3463623230663565643763643239303939616439",
"2": "21796157974083048550319244236929488537086114760591164995666220774418197985333",
})
} }