github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/accounts/abi/numbers.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 "math/big" 21 "reflect" 22 23 "github.com/ethereumproject/go-ethereum/common" 24 ) 25 26 var ( 27 big_t = reflect.TypeOf(big.Int{}) 28 ubig_t = reflect.TypeOf(big.Int{}) 29 int_t = reflect.TypeOf(int(0)) 30 int8_t = reflect.TypeOf(int8(0)) 31 int16_t = reflect.TypeOf(int16(0)) 32 int32_t = reflect.TypeOf(int32(0)) 33 int64_t = reflect.TypeOf(int64(0)) 34 address_t = reflect.TypeOf(common.Address{}) 35 36 int_ts = reflect.TypeOf([]int(nil)) 37 int8_ts = reflect.TypeOf([]int8(nil)) 38 int16_ts = reflect.TypeOf([]int16(nil)) 39 int32_ts = reflect.TypeOf([]int32(nil)) 40 int64_ts = reflect.TypeOf([]int64(nil)) 41 ) 42 43 // U256 converts a big Int into a 256bit EVM number. 44 func U256(n *big.Int) []byte { 45 return common.LeftPadBytes(common.U256(n).Bytes(), 32) 46 } 47 48 // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation 49 func packNum(value reflect.Value) []byte { 50 switch kind := value.Kind(); kind { 51 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 52 return U256(new(big.Int).SetUint64(value.Uint())) 53 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 54 return U256(big.NewInt(value.Int())) 55 case reflect.Ptr: 56 return U256(value.Interface().(*big.Int)) 57 } 58 return nil 59 } 60 61 // checks whether the given reflect value is signed. This also works for slices with a number type 62 func isSigned(v reflect.Value) bool { 63 switch v.Type() { 64 case int_ts, int8_ts, int16_ts, int32_ts, int64_ts, int_t, int8_t, int16_t, int32_t, int64_t: 65 return true 66 } 67 return false 68 }