github.com/luckypickle/go-ethereum-vet@v1.14.2/accounts/abi/type.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 "fmt" 21 "reflect" 22 "regexp" 23 "strconv" 24 "strings" 25 ) 26 27 // Type enumerator 28 const ( 29 IntTy byte = iota 30 UintTy 31 BoolTy 32 StringTy 33 SliceTy 34 ArrayTy 35 AddressTy 36 FixedBytesTy 37 BytesTy 38 HashTy 39 FixedPointTy 40 FunctionTy 41 ) 42 43 // Type is the reflection of the supported argument type 44 type Type struct { 45 Elem *Type 46 47 Kind reflect.Kind 48 Type reflect.Type 49 Size int 50 T byte // Our own type checking 51 52 stringKind string // holds the unparsed string for deriving signatures 53 } 54 55 var ( 56 // typeRegex parses the abi sub types 57 typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?") 58 ) 59 60 // NewType creates a new reflection type of abi type given in t. 61 func NewType(t string) (typ Type, err error) { 62 // check that array brackets are equal if they exist 63 if strings.Count(t, "[") != strings.Count(t, "]") { 64 return Type{}, fmt.Errorf("invalid arg type in abi") 65 } 66 67 typ.stringKind = t 68 69 // if there are brackets, get ready to go into slice/array mode and 70 // recursively create the type 71 if strings.Count(t, "[") != 0 { 72 i := strings.LastIndex(t, "[") 73 // recursively embed the type 74 embeddedType, err := NewType(t[:i]) 75 if err != nil { 76 return Type{}, err 77 } 78 // grab the last cell and create a type from there 79 sliced := t[i:] 80 // grab the slice size with regexp 81 re := regexp.MustCompile("[0-9]+") 82 intz := re.FindAllString(sliced, -1) 83 84 if len(intz) == 0 { 85 // is a slice 86 typ.T = SliceTy 87 typ.Kind = reflect.Slice 88 typ.Elem = &embeddedType 89 typ.Type = reflect.SliceOf(embeddedType.Type) 90 } else if len(intz) == 1 { 91 // is a array 92 typ.T = ArrayTy 93 typ.Kind = reflect.Array 94 typ.Elem = &embeddedType 95 typ.Size, err = strconv.Atoi(intz[0]) 96 if err != nil { 97 return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) 98 } 99 typ.Type = reflect.ArrayOf(typ.Size, embeddedType.Type) 100 } else { 101 return Type{}, fmt.Errorf("invalid formatting of array type") 102 } 103 return typ, err 104 } 105 // parse the type and size of the abi-type. 106 parsedType := typeRegex.FindAllStringSubmatch(t, -1)[0] 107 // varSize is the size of the variable 108 var varSize int 109 if len(parsedType[3]) > 0 { 110 var err error 111 varSize, err = strconv.Atoi(parsedType[2]) 112 if err != nil { 113 return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err) 114 } 115 } else { 116 if parsedType[0] == "uint" || parsedType[0] == "int" { 117 // this should fail because it means that there's something wrong with 118 // the abi type (the compiler should always format it to the size...always) 119 return Type{}, fmt.Errorf("unsupported arg type: %s", t) 120 } 121 } 122 // varType is the parsed abi type 123 switch varType := parsedType[1]; varType { 124 case "int": 125 typ.Kind, typ.Type = reflectIntKindAndType(false, varSize) 126 typ.Size = varSize 127 typ.T = IntTy 128 case "uint": 129 typ.Kind, typ.Type = reflectIntKindAndType(true, varSize) 130 typ.Size = varSize 131 typ.T = UintTy 132 case "bool": 133 typ.Kind = reflect.Bool 134 typ.T = BoolTy 135 typ.Type = reflect.TypeOf(bool(false)) 136 case "address": 137 typ.Kind = reflect.Array 138 typ.Type = addressT 139 typ.Size = 20 140 typ.T = AddressTy 141 case "string": 142 typ.Kind = reflect.String 143 typ.Type = reflect.TypeOf("") 144 typ.T = StringTy 145 case "bytes": 146 if varSize == 0 { 147 typ.T = BytesTy 148 typ.Kind = reflect.Slice 149 typ.Type = reflect.SliceOf(reflect.TypeOf(byte(0))) 150 } else { 151 typ.T = FixedBytesTy 152 typ.Kind = reflect.Array 153 typ.Size = varSize 154 typ.Type = reflect.ArrayOf(varSize, reflect.TypeOf(byte(0))) 155 } 156 case "function": 157 typ.Kind = reflect.Array 158 typ.T = FunctionTy 159 typ.Size = 24 160 typ.Type = reflect.ArrayOf(24, reflect.TypeOf(byte(0))) 161 default: 162 return Type{}, fmt.Errorf("unsupported arg type: %s", t) 163 } 164 165 return 166 } 167 168 // String implements Stringer 169 func (t Type) String() (out string) { 170 return t.stringKind 171 } 172 173 func (t Type) pack(v reflect.Value) ([]byte, error) { 174 // dereference pointer first if it's a pointer 175 v = indirect(v) 176 177 if err := typeCheck(t, v); err != nil { 178 return nil, err 179 } 180 181 if t.T == SliceTy || t.T == ArrayTy { 182 var packed []byte 183 184 for i := 0; i < v.Len(); i++ { 185 val, err := t.Elem.pack(v.Index(i)) 186 if err != nil { 187 return nil, err 188 } 189 packed = append(packed, val...) 190 } 191 if t.T == SliceTy { 192 return packBytesSlice(packed, v.Len()), nil 193 } else if t.T == ArrayTy { 194 return packed, nil 195 } 196 } 197 return packElement(t, v), nil 198 } 199 200 // requireLengthPrefix returns whether the type requires any sort of length 201 // prefixing. 202 func (t Type) requiresLengthPrefix() bool { 203 return t.T == StringTy || t.T == BytesTy || t.T == SliceTy 204 }