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