abi.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. )
  23. // The ABI holds information about a contract's context and available
  24. // invokable methods. It will allow you to type check function calls and
  25. // packs data accordingly.
  26. type ABI struct {
  27. Constructor Method
  28. Methods map[string]Method
  29. Events map[string]Event
  30. }
  31. // JSON returns a parsed ABI interface and error if it failed.
  32. func JSON(reader io.Reader) (ABI, error) {
  33. dec := json.NewDecoder(reader)
  34. var abi ABI
  35. if err := dec.Decode(&abi); err != nil {
  36. return ABI{}, err
  37. }
  38. return abi, nil
  39. }
  40. // Pack the given method name to conform the ABI. Method call's data
  41. // will consist of method_id, args0, arg1, ... argN. Method id consists
  42. // of 4 bytes and arguments are all 32 bytes.
  43. // Method ids are created from the first 4 bytes of the hash of the
  44. // methods string signature. (signature = baz(uint32,string32))
  45. func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
  46. // Fetch the ABI of the requested method
  47. if name == "" {
  48. // constructor
  49. arguments, err := abi.Constructor.Inputs.Pack(args...)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return arguments, nil
  54. }
  55. method, exist := abi.Methods[name]
  56. if !exist {
  57. return nil, fmt.Errorf("method '%s' not found", name)
  58. }
  59. arguments, err := method.Inputs.Pack(args...)
  60. if err != nil {
  61. return nil, err
  62. }
  63. // Pack up the method ID too if not a constructor and return
  64. return append(method.Id(), arguments...), nil
  65. }
  66. // Unpack output in v according to the abi specification
  67. func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
  68. if len(output) == 0 {
  69. return fmt.Errorf("abi: unmarshalling empty output")
  70. }
  71. // since there can't be naming collisions with contracts and events,
  72. // we need to decide whether we're calling a method or an event
  73. if method, ok := abi.Methods[name]; ok {
  74. if len(output)%32 != 0 {
  75. return fmt.Errorf("abi: improperly formatted output")
  76. }
  77. return method.Outputs.Unpack(v, output)
  78. } else if event, ok := abi.Events[name]; ok {
  79. return event.Inputs.Unpack(v, output)
  80. }
  81. return fmt.Errorf("abi: could not locate named method or event")
  82. }
  83. // UnmarshalJSON implements json.Unmarshaler interface
  84. func (abi *ABI) UnmarshalJSON(data []byte) error {
  85. var fields []struct {
  86. Type string
  87. Name string
  88. Constant bool
  89. Anonymous bool
  90. Inputs []Argument
  91. Outputs []Argument
  92. }
  93. if err := json.Unmarshal(data, &fields); err != nil {
  94. return err
  95. }
  96. abi.Methods = make(map[string]Method)
  97. abi.Events = make(map[string]Event)
  98. for _, field := range fields {
  99. switch field.Type {
  100. case "constructor":
  101. abi.Constructor = Method{
  102. Inputs: field.Inputs,
  103. }
  104. // empty defaults to function according to the abi spec
  105. case "function", "":
  106. abi.Methods[field.Name] = Method{
  107. Name: field.Name,
  108. Const: field.Constant,
  109. Inputs: field.Inputs,
  110. Outputs: field.Outputs,
  111. }
  112. case "event":
  113. abi.Events[field.Name] = Event{
  114. Name: field.Name,
  115. Anonymous: field.Anonymous,
  116. Inputs: field.Inputs,
  117. }
  118. }
  119. }
  120. return nil
  121. }
  122. // MethodById looks up a method by the 4-byte id
  123. // returns nil if none found
  124. func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
  125. for _, method := range abi.Methods {
  126. if bytes.Equal(method.Id(), sigdata[:4]) {
  127. return &method, nil
  128. }
  129. }
  130. return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
  131. }