Support.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 Support.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2015
  17. */
  18. #include "Support.h"
  19. #include <boost/algorithm/string.hpp>
  20. #include <libdevcore/Log.h>
  21. #include <libethcore/ICAP.h>
  22. #include <libethereum/Client.h>
  23. #include "WebThree.h"
  24. using namespace std;
  25. using namespace dev;
  26. using namespace eth;
  27. Support::Support(WebThreeDirect* _web3):
  28. m_web3(_web3)
  29. {
  30. }
  31. Support::~Support()
  32. {
  33. }
  34. strings Support::decomposed(std::string const& _name)
  35. {
  36. size_t i = _name.find_first_of('/');
  37. strings parts;
  38. string ts = _name.substr(0, i);
  39. boost::algorithm::split(parts, ts, boost::algorithm::is_any_of("."));
  40. std::reverse(parts.begin(), parts.end());
  41. if (i != string::npos)
  42. {
  43. strings pathParts;
  44. boost::algorithm::split(pathParts, ts = _name.substr(i + 1), boost::is_any_of("/"));
  45. parts += pathParts;
  46. }
  47. return parts;
  48. }
  49. bytes Support::call(Address const& _to, bytes const& _data) const
  50. {
  51. return m_web3->ethereum()->call(_to, _data).output;
  52. }
  53. bytes Support::auxLookup(strings const& _path, std::string const& _query) const
  54. {
  55. Address address = registrar();
  56. for (unsigned i = 0; i < _path.size() - 1; ++i)
  57. address = abiOut<Address>(call(address, abiIn("subRegistrar(string)", _path[i])));
  58. return call(address, abiIn(_query + "(string)", _path.back()));
  59. }
  60. Address Support::registrar() const
  61. {
  62. return Address(m_web3->ethereum()->chainParams().otherParams.at("registrar"));
  63. }
  64. Address Support::urlHint() const
  65. {
  66. return lookup<Address>({"urlhinter"}, "addr");
  67. }
  68. Address Support::sha256Hint() const
  69. {
  70. return lookup<Address>({"sha256hinter"}, "addr");
  71. }
  72. std::string Support::urlHint(h256 const& _content) const
  73. {
  74. return toString(abiOut<string32>(call(urlHint(), abiIn("url(bytes32)", _content))));
  75. }
  76. h256 Support::sha256Hint(h256 const& _content) const
  77. {
  78. return abiOut<h256>(call(sha256Hint(), abiIn("data(bytes32)", _content)));
  79. }
  80. void Support::hintURL(h256 const& _content, std::string const& _url, Secret const& _s) const
  81. {
  82. TransactionSkeleton t;
  83. t.data = abiIn("suggestUrl(bytes32,bytes32)", _content, toString32(_url));
  84. t.to = urlHint();
  85. m_web3->ethereum()->submitTransaction(t, _s);
  86. }
  87. void Support::hintSHA256(h256 const& _content, h256 const& _sha256, Secret const& _s) const
  88. {
  89. TransactionSkeleton t;
  90. t.data = abiIn("setData(bytes32,bytes32)", _content, _sha256);
  91. t.to = sha256Hint();
  92. m_web3->ethereum()->submitTransaction(t, _s);
  93. }
  94. Address Support::icapRegistrar() const
  95. {
  96. return Address("a1a111bc074c9cfa781f0c38e63bd51c91b8af00");
  97. return lookup<Address>({"icapregistrar"}, "addr");
  98. }
  99. std::pair<Address, bytes> Support::decodeICAP(std::string const& _icap) const
  100. {
  101. return ICAP::decoded(_icap).address([&](Address a, bytes b){ return call(a, b); }, icapRegistrar());
  102. }