abi.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, data []byte) (err error) {
  68. if len(data) == 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(data)%32 != 0 {
  75. return fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data)
  76. }
  77. return method.Outputs.Unpack(v, data)
  78. }
  79. if event, ok := abi.Events[name]; ok {
  80. return event.Inputs.Unpack(v, data)
  81. }
  82. return fmt.Errorf("abi: could not locate named method or event")
  83. }
  84. // UnpackIntoMap unpacks a log into the provided map[string]interface{}
  85. func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) {
  86. if len(data) == 0 {
  87. return fmt.Errorf("abi: unmarshalling empty output")
  88. }
  89. // since there can't be naming collisions with contracts and events,
  90. // we need to decide whether we're calling a method or an event
  91. if method, ok := abi.Methods[name]; ok {
  92. if len(data)%32 != 0 {
  93. return fmt.Errorf("abi: improperly formatted output")
  94. }
  95. return method.Outputs.UnpackIntoMap(v, data)
  96. }
  97. if event, ok := abi.Events[name]; ok {
  98. return event.Inputs.UnpackIntoMap(v, data)
  99. }
  100. return fmt.Errorf("abi: could not locate named method or event")
  101. }
  102. // UnmarshalJSON implements json.Unmarshaler interface
  103. func (abi *ABI) UnmarshalJSON(data []byte) error {
  104. var fields []struct {
  105. Type string
  106. Name string
  107. Constant bool
  108. Anonymous bool
  109. Inputs []Argument
  110. Outputs []Argument
  111. }
  112. if err := json.Unmarshal(data, &fields); err != nil {
  113. return err
  114. }
  115. abi.Methods = make(map[string]Method)
  116. abi.Events = make(map[string]Event)
  117. for _, field := range fields {
  118. switch field.Type {
  119. case "constructor":
  120. abi.Constructor = Method{
  121. Inputs: field.Inputs,
  122. }
  123. // empty defaults to function according to the abi spec
  124. case "function", "":
  125. abi.Methods[field.Name] = Method{
  126. Name: field.Name,
  127. Const: field.Constant,
  128. Inputs: field.Inputs,
  129. Outputs: field.Outputs,
  130. }
  131. case "event":
  132. abi.Events[field.Name] = Event{
  133. Name: field.Name,
  134. Anonymous: field.Anonymous,
  135. Inputs: field.Inputs,
  136. }
  137. }
  138. }
  139. return nil
  140. }
  141. // MethodById looks up a method by the 4-byte id
  142. // returns nil if none found
  143. func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
  144. if len(sigdata) < 4 {
  145. return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata))
  146. }
  147. for _, method := range abi.Methods {
  148. if bytes.Equal(method.Id(), sigdata[:4]) {
  149. return &method, nil
  150. }
  151. }
  152. return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
  153. }