github.com/core-coin/go-core/v2@v2.1.9/accounts/abi/pack.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"errors"
    21  	"fmt"
    22  	"math/big"
    23  	"reflect"
    24  
    25  	"github.com/core-coin/go-core/v2/common"
    26  	"github.com/core-coin/go-core/v2/common/math"
    27  )
    28  
    29  // packBytesSlice packs the given bytes as [L, V] as the canonical representation
    30  // bytes slice.
    31  func packBytesSlice(bytes []byte, l int) []byte {
    32  	len := packNum(reflect.ValueOf(l))
    33  	return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
    34  }
    35  
    36  // packElement packs the given reflect value according to the abi specification in
    37  // t.
    38  func packElement(t Type, reflectValue reflect.Value) ([]byte, error) {
    39  	switch t.T {
    40  	case IntTy, UintTy:
    41  		return packNum(reflectValue), nil
    42  	case StringTy:
    43  		return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil
    44  	case AddressTy:
    45  		if reflectValue.Kind() == reflect.Array {
    46  			reflectValue = mustArrayToByteSlice(reflectValue)
    47  		}
    48  
    49  		return common.LeftPadBytes(reflectValue.Bytes(), 32), nil
    50  	case BoolTy:
    51  		if reflectValue.Bool() {
    52  			return math.PaddedBigBytes(common.Big1, 32), nil
    53  		}
    54  		return math.PaddedBigBytes(common.Big0, 32), nil
    55  	case BytesTy:
    56  		if reflectValue.Kind() == reflect.Array {
    57  			reflectValue = mustArrayToByteSlice(reflectValue)
    58  		}
    59  		if reflectValue.Type() != reflect.TypeOf([]byte{}) {
    60  			return []byte{}, errors.New("Bytes type is neither slice nor array")
    61  		}
    62  		return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil
    63  	case FixedBytesTy, FunctionTy:
    64  		if reflectValue.Kind() == reflect.Array {
    65  			reflectValue = mustArrayToByteSlice(reflectValue)
    66  		}
    67  		return common.RightPadBytes(reflectValue.Bytes(), 32), nil
    68  	default:
    69  		return []byte{}, fmt.Errorf("Could not pack element, unknown type: %v", t.T)
    70  	}
    71  }
    72  
    73  // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation.
    74  func packNum(value reflect.Value) []byte {
    75  	switch kind := value.Kind(); kind {
    76  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
    77  		return math.U256Bytes(new(big.Int).SetUint64(value.Uint()))
    78  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
    79  		return math.U256Bytes(big.NewInt(value.Int()))
    80  	case reflect.Ptr:
    81  		return math.U256Bytes(new(big.Int).Set(value.Interface().(*big.Int)))
    82  	default:
    83  		panic("abi: fatal error")
    84  	}
    85  }