method.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2015 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 abi
  17. import (
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "github.com/ethereum/go-ethereum/crypto"
  22. )
  23. // Callable method given a `Name` and whether the method is a constant.
  24. // If the method is `Const` no transaction needs to be created for this
  25. // particular Method call. It can easily be simulated using a local VM.
  26. // For example a `Balance()` method only needs to retrieve something
  27. // from the storage and therefor requires no Tx to be send to the
  28. // network. A method such as `Transact` does require a Tx and thus will
  29. // be flagged `true`.
  30. // Input specifies the required input parameters for this gives method.
  31. type Method struct {
  32. Name string
  33. Const bool
  34. Inputs []Argument
  35. Outputs []Argument
  36. }
  37. func (m Method) pack(method Method, args ...interface{}) ([]byte, error) {
  38. // Make sure arguments match up and pack them
  39. if len(args) != len(method.Inputs) {
  40. return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs))
  41. }
  42. // variable input is the output appended at the end of packed
  43. // output. This is used for strings and bytes types input.
  44. var variableInput []byte
  45. var ret []byte
  46. for i, a := range args {
  47. input := method.Inputs[i]
  48. // pack the input
  49. packed, err := input.Type.pack(reflect.ValueOf(a))
  50. if err != nil {
  51. return nil, fmt.Errorf("`%s` %v", method.Name, err)
  52. }
  53. // check for a slice type (string, bytes, slice)
  54. if input.Type.requiresLengthPrefix() {
  55. // calculate the offset
  56. offset := len(method.Inputs)*32 + len(variableInput)
  57. // set the offset
  58. ret = append(ret, packNum(reflect.ValueOf(offset), UintTy)...)
  59. // Append the packed output to the variable input. The variable input
  60. // will be appended at the end of the input.
  61. variableInput = append(variableInput, packed...)
  62. } else {
  63. // append the packed value to the input
  64. ret = append(ret, packed...)
  65. }
  66. }
  67. // append the variable input at the end of the packed input
  68. ret = append(ret, variableInput...)
  69. return ret, nil
  70. }
  71. // Sig returns the methods string signature according to the ABI spec.
  72. //
  73. // Example
  74. //
  75. // function foo(uint32 a, int b) = "foo(uint32,int256)"
  76. //
  77. // Please note that "int" is substitute for its canonical representation "int256"
  78. func (m Method) Sig() string {
  79. types := make([]string, len(m.Inputs))
  80. i := 0
  81. for _, input := range m.Inputs {
  82. types[i] = input.Type.String()
  83. i++
  84. }
  85. return fmt.Sprintf("%v(%v)", m.Name, strings.Join(types, ","))
  86. }
  87. func (m Method) String() string {
  88. inputs := make([]string, len(m.Inputs))
  89. for i, input := range m.Inputs {
  90. inputs[i] = fmt.Sprintf("%v %v", input.Name, input.Type)
  91. }
  92. outputs := make([]string, len(m.Outputs))
  93. for i, output := range m.Outputs {
  94. if len(output.Name) > 0 {
  95. outputs[i] = fmt.Sprintf("%v ", output.Name)
  96. }
  97. outputs[i] += output.Type.String()
  98. }
  99. constant := ""
  100. if m.Const {
  101. constant = "constant "
  102. }
  103. return fmt.Sprintf("function %v(%v) %sreturns(%v)", m.Name, strings.Join(inputs, ", "), constant, strings.Join(outputs, ", "))
  104. }
  105. func (m Method) Id() []byte {
  106. return crypto.Keccak256([]byte(m.Sig()))[:4]
  107. }