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