github.com/ethereum/go-ethereum@v1.16.1/accounts/abi/pack.go (about) 1 // Copyright 2016 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 "errors" 21 "fmt" 22 "math/big" 23 "reflect" 24 25 "github.com/ethereum/go-ethereum/common" 26 "github.com/ethereum/go-ethereum/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 UintTy: 41 // make sure to not pack a negative value into a uint type. 42 if reflectValue.Kind() == reflect.Ptr { 43 val := new(big.Int).Set(reflectValue.Interface().(*big.Int)) 44 if val.Sign() == -1 { 45 return nil, errInvalidSign 46 } 47 } 48 return packNum(reflectValue), nil 49 case IntTy: 50 return packNum(reflectValue), nil 51 case StringTy: 52 return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len()), nil 53 case AddressTy: 54 if reflectValue.Kind() == reflect.Array { 55 reflectValue = mustArrayToByteSlice(reflectValue) 56 } 57 58 return common.LeftPadBytes(reflectValue.Bytes(), 32), nil 59 case BoolTy: 60 if reflectValue.Bool() { 61 return math.PaddedBigBytes(common.Big1, 32), nil 62 } 63 return math.PaddedBigBytes(common.Big0, 32), nil 64 case BytesTy: 65 if reflectValue.Kind() == reflect.Array { 66 reflectValue = mustArrayToByteSlice(reflectValue) 67 } 68 if reflectValue.Type() != reflect.TypeOf([]byte{}) { 69 return []byte{}, errors.New("bytes type is neither slice nor array") 70 } 71 return packBytesSlice(reflectValue.Bytes(), reflectValue.Len()), nil 72 case FixedBytesTy, FunctionTy: 73 if reflectValue.Kind() == reflect.Array { 74 reflectValue = mustArrayToByteSlice(reflectValue) 75 } 76 return common.RightPadBytes(reflectValue.Bytes(), 32), nil 77 default: 78 return []byte{}, fmt.Errorf("could not pack element, unknown type: %v", t.T) 79 } 80 } 81 82 // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation. 83 func packNum(value reflect.Value) []byte { 84 switch kind := value.Kind(); kind { 85 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: 86 return math.U256Bytes(new(big.Int).SetUint64(value.Uint())) 87 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 88 return math.U256Bytes(big.NewInt(value.Int())) 89 case reflect.Ptr: 90 return math.U256Bytes(new(big.Int).Set(value.Interface().(*big.Int))) 91 default: 92 panic("abi: fatal error") 93 } 94 }