abi.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package fourbyte
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "strings"
  22. "github.com/ethereum/go-ethereum/accounts/abi"
  23. "github.com/ethereum/go-ethereum/common"
  24. )
  25. // decodedCallData is an internal type to represent a method call parsed according
  26. // to an ABI method signature.
  27. type decodedCallData struct {
  28. signature string
  29. name string
  30. inputs []decodedArgument
  31. }
  32. // decodedArgument is an internal type to represent an argument parsed according
  33. // to an ABI method signature.
  34. type decodedArgument struct {
  35. soltype abi.Argument
  36. value interface{}
  37. }
  38. // String implements stringer interface, tries to use the underlying value-type
  39. func (arg decodedArgument) String() string {
  40. var value string
  41. switch val := arg.value.(type) {
  42. case fmt.Stringer:
  43. value = val.String()
  44. default:
  45. value = fmt.Sprintf("%v", val)
  46. }
  47. return fmt.Sprintf("%v: %v", arg.soltype.Type.String(), value)
  48. }
  49. // String implements stringer interface for decodedCallData
  50. func (cd decodedCallData) String() string {
  51. args := make([]string, len(cd.inputs))
  52. for i, arg := range cd.inputs {
  53. args[i] = arg.String()
  54. }
  55. return fmt.Sprintf("%s(%s)", cd.name, strings.Join(args, ","))
  56. }
  57. // verifySelector checks whether the ABI encoded data blob matches the requested
  58. // function signature.
  59. func verifySelector(selector string, calldata []byte) (*decodedCallData, error) {
  60. // Parse the selector into an ABI JSON spec
  61. abidata, err := parseSelector(selector)
  62. if err != nil {
  63. return nil, err
  64. }
  65. // Parse the call data according to the requested selector
  66. return parseCallData(calldata, string(abidata))
  67. }
  68. // parseSelector converts a method selector into an ABI JSON spec. The returned
  69. // data is a valid JSON string which can be consumed by the standard abi package.
  70. func parseSelector(unescapedSelector string) ([]byte, error) {
  71. selector, err := abi.ParseSelector(unescapedSelector)
  72. if err != nil {
  73. return nil, fmt.Errorf("failed to parse selector: %v", err)
  74. }
  75. return json.Marshal([]abi.SelectorMarshaling{selector})
  76. }
  77. // parseCallData matches the provided call data against the ABI definition and
  78. // returns a struct containing the actual go-typed values.
  79. func parseCallData(calldata []byte, unescapedAbidata string) (*decodedCallData, error) {
  80. // Validate the call data that it has the 4byte prefix and the rest divisible by 32 bytes
  81. if len(calldata) < 4 {
  82. return nil, fmt.Errorf("invalid call data, incomplete method signature (%d bytes < 4)", len(calldata))
  83. }
  84. sigdata := calldata[:4]
  85. argdata := calldata[4:]
  86. if len(argdata)%32 != 0 {
  87. return nil, fmt.Errorf("invalid call data; length should be a multiple of 32 bytes (was %d)", len(argdata))
  88. }
  89. // Validate the called method and upack the call data accordingly
  90. abispec, err := abi.JSON(strings.NewReader(unescapedAbidata))
  91. if err != nil {
  92. return nil, fmt.Errorf("invalid method signature (%q): %v", unescapedAbidata, err)
  93. }
  94. method, err := abispec.MethodById(sigdata)
  95. if err != nil {
  96. return nil, err
  97. }
  98. values, err := method.Inputs.UnpackValues(argdata)
  99. if err != nil {
  100. return nil, fmt.Errorf("signature %q matches, but arguments mismatch: %v", method.String(), err)
  101. }
  102. // Everything valid, assemble the call infos for the signer
  103. decoded := decodedCallData{signature: method.Sig, name: method.RawName}
  104. for i := 0; i < len(method.Inputs); i++ {
  105. decoded.inputs = append(decoded.inputs, decodedArgument{
  106. soltype: method.Inputs[i],
  107. value: values[i],
  108. })
  109. }
  110. // We're finished decoding the data. At this point, we encode the decoded data
  111. // to see if it matches with the original data. If we didn't do that, it would
  112. // be possible to stuff extra data into the arguments, which is not detected
  113. // by merely decoding the data.
  114. encoded, err := method.Inputs.PackValues(values)
  115. if err != nil {
  116. return nil, err
  117. }
  118. if !bytes.Equal(encoded, argdata) {
  119. was := common.Bytes2Hex(encoded)
  120. exp := common.Bytes2Hex(argdata)
  121. return nil, fmt.Errorf("WARNING: Supplied data is stuffed with extra data. \nWant %s\nHave %s\nfor method %v", exp, was, method.Sig)
  122. }
  123. return &decoded, nil
  124. }