github.com/tuotoo/go-ethereum@v1.7.4-0.20171121184211-049797d40a24/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 "encoding/json" 21 "fmt" 22 "io" 23 ) 24 25 // The ABI holds information about a contract's context and available 26 // invokable methods. It will allow you to type check function calls and 27 // packs data accordingly. 28 type ABI struct { 29 Constructor Method 30 Methods map[string]Method 31 Events map[string]Event 32 } 33 34 // JSON returns a parsed ABI interface and error if it failed. 35 func JSON(reader io.Reader) (ABI, error) { 36 dec := json.NewDecoder(reader) 37 38 var abi ABI 39 if err := dec.Decode(&abi); err != nil { 40 return ABI{}, err 41 } 42 43 return abi, nil 44 } 45 46 // Pack the given method name to conform the ABI. Method call's data 47 // will consist of method_id, args0, arg1, ... argN. Method id consists 48 // of 4 bytes and arguments are all 32 bytes. 49 // Method ids are created from the first 4 bytes of the hash of the 50 // methods string signature. (signature = baz(uint32,string32)) 51 func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) { 52 // Fetch the ABI of the requested method 53 var method Method 54 55 if name == "" { 56 method = abi.Constructor 57 } else { 58 m, exist := abi.Methods[name] 59 if !exist { 60 return nil, fmt.Errorf("method '%s' not found", name) 61 } 62 method = m 63 } 64 arguments, err := method.pack(args...) 65 if err != nil { 66 return nil, err 67 } 68 // Pack up the method ID too if not a constructor and return 69 if name == "" { 70 return arguments, nil 71 } 72 return append(method.Id(), arguments...), nil 73 } 74 75 // Unpack output in v according to the abi specification 76 func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) { 77 if err = bytesAreProper(output); err != nil { 78 return err 79 } 80 // since there can't be naming collisions with contracts and events, 81 // we need to decide whether we're calling a method or an event 82 var unpack unpacker 83 if method, ok := abi.Methods[name]; ok { 84 unpack = method 85 } else if event, ok := abi.Events[name]; ok { 86 unpack = event 87 } else { 88 return fmt.Errorf("abi: could not locate named method or event.") 89 } 90 91 // requires a struct to unpack into for a tuple return... 92 if unpack.isTupleReturn() { 93 return unpack.tupleUnpack(v, output) 94 } 95 return unpack.singleUnpack(v, output) 96 } 97 98 func (abi *ABI) UnmarshalJSON(data []byte) error { 99 var fields []struct { 100 Type string 101 Name string 102 Constant bool 103 Indexed 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 }