pack.go 3.0 KB

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