ICAP.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 ICAP.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #include "ICAP.h"
  19. #include <boost/algorithm/string/case_conv.hpp>
  20. #include <boost/algorithm/string.hpp>
  21. #include <libdevcore/Base64.h>
  22. #include <libdevcore/SHA3.h>
  23. #include "Exceptions.h"
  24. #include "ABI.h"
  25. using namespace std;
  26. using namespace dev;
  27. using namespace dev::eth;
  28. namespace dev
  29. {
  30. namespace eth
  31. {
  32. string ICAP::iban(std::string _c, std::string _d)
  33. {
  34. boost::to_upper(_c);
  35. boost::to_upper(_d);
  36. auto totStr = _d + _c + "00";
  37. bigint tot = 0;
  38. for (char x: totStr)
  39. if (x >= 'A')
  40. tot = tot * 100 + x - 'A' + 10;
  41. else
  42. tot = tot * 10 + x - '0';
  43. unsigned check = (unsigned)(u256)(98 - tot % 97);
  44. ostringstream out;
  45. out << _c << setfill('0') << setw(2) << check << _d;
  46. return out.str();
  47. }
  48. std::pair<string, string> ICAP::fromIBAN(std::string _iban)
  49. {
  50. if (_iban.size() < 4)
  51. return std::make_pair(string(), string());
  52. boost::to_upper(_iban);
  53. std::string c = _iban.substr(0, 2);
  54. std::string d = _iban.substr(4);
  55. if (iban(c, d) != _iban)
  56. return std::make_pair(string(), string());
  57. return make_pair(c, d);
  58. }
  59. Secret ICAP::createDirect()
  60. {
  61. Secret ret;
  62. while (true)
  63. {
  64. ret = Secret::random();
  65. if (!toAddress(ret)[0])
  66. return ret;
  67. }
  68. }
  69. ICAP ICAP::decoded(std::string const& _encoded)
  70. {
  71. ICAP ret;
  72. std::string country;
  73. std::string data;
  74. std::tie(country, data) = fromIBAN(_encoded);
  75. if (country != "XE")
  76. BOOST_THROW_EXCEPTION(InvalidICAP());
  77. if (data.size() == 30 || data.size() == 31)
  78. {
  79. ret.m_type = Direct;
  80. // Direct ICAP
  81. ret.m_direct = fromBase36<Address::size>(data);
  82. }
  83. else if (data.size() == 16)
  84. {
  85. ret.m_type = Indirect;
  86. ret.m_asset = data.substr(0, 3);
  87. if (ret.m_asset == "XET" || ret.m_asset == "ETH")
  88. {
  89. ret.m_institution = data.substr(3, 4);
  90. ret.m_client = data.substr(7);
  91. }
  92. else
  93. BOOST_THROW_EXCEPTION(InvalidICAP());
  94. }
  95. else
  96. BOOST_THROW_EXCEPTION(InvalidICAP());
  97. return ret;
  98. }
  99. std::string ICAP::encoded() const
  100. {
  101. if (m_type == Direct)
  102. {
  103. std::string d = toBase36<Address::size>(m_direct);
  104. while (d.size() < 30) // always 34, sometimes 35.
  105. d = "0" + d;
  106. return iban("XE", d);
  107. }
  108. else if (m_type == Indirect)
  109. {
  110. if (
  111. m_asset.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
  112. m_institution.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
  113. m_client.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
  114. m_asset.size() != 3 ||
  115. (boost::to_upper_copy(m_asset) != "XET" && boost::to_upper_copy(m_asset) != "ETH") ||
  116. m_institution.size() != 4 ||
  117. m_client.size() != 9
  118. )
  119. BOOST_THROW_EXCEPTION(InvalidICAP());
  120. return iban("XE", m_asset + m_institution + m_client);
  121. }
  122. else
  123. BOOST_THROW_EXCEPTION(InvalidICAP());
  124. }
  125. pair<Address, bytes> ICAP::lookup(std::function<bytes(Address, bytes)> const& _call, Address const& _reg) const
  126. {
  127. auto resolve = [&](string const& s)
  128. {
  129. /* vector<string> ss;
  130. boost::algorithm::split(ss, s, boost::is_any_of("/"));
  131. Address r = _reg;
  132. for (unsigned i = 0; i < ss.size() - 1; ++i)
  133. r = abiOut<Address>(_call(r, abiIn("subRegistrar(bytes32)", ss[i])));*/
  134. return abiOut<Address>(_call(_reg, abiIn("addr(string)", s)));
  135. };
  136. if (m_asset == "XET")
  137. {
  138. Address a = resolve(m_institution);
  139. bytes d = abiIn("deposit(uint64)", fromBase36<8>(m_client));
  140. return make_pair(a, d);
  141. }
  142. /* else if (m_asset == "ETH")
  143. {
  144. if (m_institution[0] != 'X')
  145. return make_pair(resolve(m_institution + "/" + m_client), bytes());
  146. else if (m_institution == "XREG")
  147. return make_pair(resolve(m_client), bytes());
  148. else
  149. BOOST_THROW_EXCEPTION(InterfaceNotSupported("ICAP::lookup(), bad institution"));
  150. }*/
  151. BOOST_THROW_EXCEPTION(InterfaceNotSupported("ICAP::lookup(), bad asset"));
  152. }
  153. }
  154. }