github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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/atheioschain/go-atheios/common"
    24  )
    25  
    26  var (
    27  	big_t     = reflect.TypeOf(big.Int{})
    28  	ubig_t    = reflect.TypeOf(big.Int{})
    29  	byte_t    = reflect.TypeOf(byte(0))
    30  	byte_ts   = reflect.TypeOf([]byte(nil))
    31  	uint_t    = reflect.TypeOf(uint(0))
    32  	uint8_t   = reflect.TypeOf(uint8(0))
    33  	uint16_t  = reflect.TypeOf(uint16(0))
    34  	uint32_t  = reflect.TypeOf(uint32(0))
    35  	uint64_t  = reflect.TypeOf(uint64(0))
    36  	int_t     = reflect.TypeOf(int(0))
    37  	int8_t    = reflect.TypeOf(int8(0))
    38  	int16_t   = reflect.TypeOf(int16(0))
    39  	int32_t   = reflect.TypeOf(int32(0))
    40  	int64_t   = reflect.TypeOf(int64(0))
    41  	hash_t    = reflect.TypeOf(common.Hash{})
    42  	address_t = reflect.TypeOf(common.Address{})
    43  
    44  	uint_ts   = reflect.TypeOf([]uint(nil))
    45  	uint8_ts  = reflect.TypeOf([]uint8(nil))
    46  	uint16_ts = reflect.TypeOf([]uint16(nil))
    47  	uint32_ts = reflect.TypeOf([]uint32(nil))
    48  	uint64_ts = reflect.TypeOf([]uint64(nil))
    49  	ubig_ts   = reflect.TypeOf([]*big.Int(nil))
    50  
    51  	int_ts   = reflect.TypeOf([]int(nil))
    52  	int8_ts  = reflect.TypeOf([]int8(nil))
    53  	int16_ts = reflect.TypeOf([]int16(nil))
    54  	int32_ts = reflect.TypeOf([]int32(nil))
    55  	int64_ts = reflect.TypeOf([]int64(nil))
    56  	big_ts   = reflect.TypeOf([]*big.Int(nil))
    57  )
    58  
    59  // U256 converts a big Int into a 256bit EVM number.
    60  func U256(n *big.Int) []byte {
    61  	return common.LeftPadBytes(common.U256(n).Bytes(), 32)
    62  }
    63  
    64  // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
    65  func packNum(value reflect.Value) []byte {
    66  	switch kind := value.Kind(); kind {
    67  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    68  		return U256(new(big.Int).SetUint64(value.Uint()))
    69  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    70  		return U256(big.NewInt(value.Int()))
    71  	case reflect.Ptr:
    72  		return U256(value.Interface().(*big.Int))
    73  	}
    74  	return nil
    75  }
    76  
    77  // checks whether the given reflect value is signed. This also works for slices with a number type
    78  func isSigned(v reflect.Value) bool {
    79  	switch v.Type() {
    80  	case int_ts, int8_ts, int16_ts, int32_ts, int64_ts, int_t, int8_t, int16_t, int32_t, int64_t:
    81  		return true
    82  	}
    83  	return false
    84  }