github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/pack.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package abi
    19  
    20  import (
    21  	"math/big"
    22  	"reflect"
    23  
    24  	"github.com/AigarNetwork/aigar/common"
    25  	"github.com/AigarNetwork/aigar/common/math"
    26  )
    27  
    28  // packBytesSlice packs the given bytes as [L, V] as the canonical representation
    29  // bytes slice
    30  func packBytesSlice(bytes []byte, l int) []byte {
    31  	len := packNum(reflect.ValueOf(l))
    32  	return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
    33  }
    34  
    35  // packElement packs the given reflect value according to the abi specification in
    36  // t.
    37  func packElement(t Type, reflectValue reflect.Value) []byte {
    38  	switch t.T {
    39  	case IntTy, UintTy:
    40  		return packNum(reflectValue)
    41  	case StringTy:
    42  		return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len())
    43  	case AddressTy:
    44  		if reflectValue.Kind() == reflect.Array {
    45  			reflectValue = mustArrayToByteSlice(reflectValue)
    46  		}
    47  
    48  		return common.LeftPadBytes(reflectValue.Bytes(), 32)
    49  	case BoolTy:
    50  		if reflectValue.Bool() {
    51  			return math.PaddedBigBytes(common.Big1, 32)
    52  		}
    53  		return math.PaddedBigBytes(common.Big0, 32)
    54  	case BytesTy:
    55  		if reflectValue.Kind() == reflect.Array {
    56  			reflectValue = mustArrayToByteSlice(reflectValue)
    57  		}
    58  		return packBytesSlice(reflectValue.Bytes(), reflectValue.Len())
    59  	case FixedBytesTy, FunctionTy:
    60  		if reflectValue.Kind() == reflect.Array {
    61  			reflectValue = mustArrayToByteSlice(reflectValue)
    62  		}
    63  		return common.RightPadBytes(reflectValue.Bytes(), 32)
    64  	default:
    65  		panic("abi: fatal error")
    66  	}
    67  }
    68  
    69  // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
    70  func packNum(value reflect.Value) []byte {
    71  	switch kind := value.Kind(); kind {
    72  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    73  		return U256(new(big.Int).SetUint64(value.Uint()))
    74  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    75  		return U256(big.NewInt(value.Int()))
    76  	case reflect.Ptr:
    77  		return U256(value.Interface().(*big.Int))
    78  	default:
    79  		panic("abi: fatal error")
    80  	}
    81  
    82  }