pack.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package abi
  17. import (
  18. "math/big"
  19. "reflect"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/common/math"
  22. )
  23. // packBytesSlice packs the given bytes as [L, V] as the canonical representation
  24. // bytes slice
  25. func packBytesSlice(bytes []byte, l int) []byte {
  26. len := packNum(reflect.ValueOf(l))
  27. return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
  28. }
  29. // packElement packs the given reflect value according to the abi specification in
  30. // t.
  31. func packElement(t Type, reflectValue reflect.Value) []byte {
  32. switch t.T {
  33. case IntTy, UintTy:
  34. return packNum(reflectValue)
  35. case StringTy:
  36. return packBytesSlice([]byte(reflectValue.String()), reflectValue.Len())
  37. case AddressTy:
  38. if reflectValue.Kind() == reflect.Array {
  39. reflectValue = mustArrayToByteSlice(reflectValue)
  40. }
  41. return common.LeftPadBytes(reflectValue.Bytes(), 32)
  42. case BoolTy:
  43. if reflectValue.Bool() {
  44. return math.PaddedBigBytes(common.Big1, 32)
  45. }
  46. return math.PaddedBigBytes(common.Big0, 32)
  47. case BytesTy:
  48. if reflectValue.Kind() == reflect.Array {
  49. reflectValue = mustArrayToByteSlice(reflectValue)
  50. }
  51. return packBytesSlice(reflectValue.Bytes(), reflectValue.Len())
  52. case FixedBytesTy, FunctionTy:
  53. if reflectValue.Kind() == reflect.Array {
  54. reflectValue = mustArrayToByteSlice(reflectValue)
  55. }
  56. return common.RightPadBytes(reflectValue.Bytes(), 32)
  57. default:
  58. panic("abi: fatal error")
  59. }
  60. }
  61. // packNum packs the given number (using the reflect value) and will cast it to appropriate number representation
  62. func packNum(value reflect.Value) []byte {
  63. switch kind := value.Kind(); kind {
  64. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  65. return U256(new(big.Int).SetUint64(value.Uint()))
  66. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  67. return U256(big.NewInt(value.Int()))
  68. case reflect.Ptr:
  69. return U256(value.Interface().(*big.Int))
  70. default:
  71. panic("abi: fatal error")
  72. }
  73. }