ABI.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file ABI.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <libdevcore/Common.h>
  20. #include <libdevcore/FixedHash.h>
  21. #include <libdevcore/CommonData.h>
  22. #include <libdevcore/SHA3.h>
  23. namespace dev
  24. {
  25. namespace eth
  26. {
  27. inline string32 toString32(std::string const& _s)
  28. {
  29. string32 ret;
  30. for (unsigned i = 0; i < 32; ++i)
  31. ret[i] = i < _s.size() ? _s[i] : 0;
  32. return ret;
  33. }
  34. template <class T> struct ABISerialiser {};
  35. template <unsigned N> struct ABISerialiser<FixedHash<N>> { static bytes serialise(FixedHash<N> const& _t) { static_assert(N <= 32, "Cannot serialise hash > 32 bytes."); static_assert(N > 0, "Cannot serialise zero-length hash."); return bytes(32 - N, 0) + _t.asBytes(); } };
  36. template <> struct ABISerialiser<u256> { static bytes serialise(u256 const& _t) { return h256(_t).asBytes(); } };
  37. template <> struct ABISerialiser<u160> { static bytes serialise(u160 const& _t) { return bytes(12, 0) + h160(_t).asBytes(); } };
  38. template <> struct ABISerialiser<string32> { static bytes serialise(string32 const& _t) { bytes ret; bytesConstRef((byte const*)_t.data(), 32).populate(bytesRef(&ret)); return ret; } };
  39. template <> struct ABISerialiser<std::string>
  40. {
  41. static bytes serialise(std::string const& _t)
  42. {
  43. bytes ret = h256(u256(32)).asBytes() + h256(u256(_t.size())).asBytes();
  44. ret.resize(ret.size() + (_t.size() + 31) / 32 * 32);
  45. bytesConstRef(&_t).populate(bytesRef(&ret).cropped(64));
  46. return ret;
  47. }
  48. };
  49. inline bytes abiInAux() { return {}; }
  50. template <class T, class ... U> bytes abiInAux(T const& _t, U const& ... _u)
  51. {
  52. return ABISerialiser<T>::serialise(_t) + abiInAux(_u ...);
  53. }
  54. template <class ... T> bytes abiIn(std::string _id, T const& ... _t)
  55. {
  56. return sha3(_id).ref().cropped(0, 4).toBytes() + abiInAux(_t ...);
  57. }
  58. template <class T> struct ABIDeserialiser {};
  59. template <unsigned N> struct ABIDeserialiser<FixedHash<N>> { static FixedHash<N> deserialise(bytesConstRef& io_t) { static_assert(N <= 32, "Parameter sizes must be at most 32 bytes."); FixedHash<N> ret; io_t.cropped(32 - N, N).populate(ret.ref()); io_t = io_t.cropped(32); return ret; } };
  60. template <> struct ABIDeserialiser<u256> { static u256 deserialise(bytesConstRef& io_t) { u256 ret = fromBigEndian<u256>(io_t.cropped(0, 32)); io_t = io_t.cropped(32); return ret; } };
  61. template <> struct ABIDeserialiser<u160> { static u160 deserialise(bytesConstRef& io_t) { u160 ret = fromBigEndian<u160>(io_t.cropped(12, 20)); io_t = io_t.cropped(32); return ret; } };
  62. template <> struct ABIDeserialiser<string32> { static string32 deserialise(bytesConstRef& io_t) { string32 ret; io_t.cropped(0, 32).populate(bytesRef((byte*)ret.data(), 32)); io_t = io_t.cropped(32); return ret; } };
  63. template <> struct ABIDeserialiser<std::string>
  64. {
  65. static std::string deserialise(bytesConstRef& io_t)
  66. {
  67. unsigned o = (uint16_t)u256(h256(io_t.cropped(0, 32)));
  68. unsigned s = (uint16_t)u256(h256(io_t.cropped(o, 32)));
  69. std::string ret;
  70. ret.resize(s);
  71. io_t.cropped(o + 32, s).populate(bytesRef((byte*)ret.data(), s));
  72. io_t = io_t.cropped(32);
  73. return ret;
  74. }
  75. };
  76. template <class T> T abiOut(bytes const& _data)
  77. {
  78. bytesConstRef o(&_data);
  79. return ABIDeserialiser<T>::deserialise(o);
  80. }
  81. template <class T> T abiOut(bytesConstRef& _data)
  82. {
  83. return ABIDeserialiser<T>::deserialise(_data);
  84. }
  85. }
  86. }