ICAP.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. *
  18. * Ethereum-specific data structures & algorithms.
  19. */
  20. #pragma once
  21. #include <string>
  22. #include <functional>
  23. #include <boost/algorithm/string/case_conv.hpp>
  24. #include <libdevcore/Common.h>
  25. #include <libdevcore/Exceptions.h>
  26. #include <libdevcore/FixedHash.h>
  27. #include "Common.h"
  28. namespace dev
  29. {
  30. namespace eth
  31. {
  32. DEV_SIMPLE_EXCEPTION(InvalidICAP);
  33. /**
  34. * @brief Encapsulation of an ICAP address.
  35. * Can be encoded, decoded, looked-up and inspected.
  36. */
  37. class ICAP
  38. {
  39. public:
  40. /// Construct null ICAP object.
  41. ICAP() = default;
  42. /// Construct a direct ICAP object for given target address. Must have a zero first byte.
  43. ICAP(Address const& _target): m_type(Direct), m_direct(_target) {}
  44. /// Construct an indirect ICAP object for given client and institution names.
  45. ICAP(std::string const& _client, std::string const& _inst): m_type(Indirect), m_client(boost::algorithm::to_upper_copy(_client)), m_institution(boost::algorithm::to_upper_copy(_inst)), m_asset("XET") {}
  46. /// Construct an indirect ICAP object for given client, institution and asset names. You generally don't want to use this.
  47. ICAP(std::string const& _c, std::string const& _i, std::string const& _a): m_type(Indirect), m_client(boost::algorithm::to_upper_copy(_c)), m_institution(boost::algorithm::to_upper_copy(_i)), m_asset(boost::algorithm::to_upper_copy(_a)) {}
  48. /// Type of ICAP address.
  49. enum Type
  50. {
  51. Invalid,
  52. Direct,
  53. Indirect
  54. };
  55. /// Create a direct address for ICAP.
  56. static Secret createDirect();
  57. /// @returns IBAN encoding of client and data.
  58. static std::string iban(std::string _c, std::string _d);
  59. /// @returns Client and data from given IBAN address.
  60. static std::pair<std::string, std::string> fromIBAN(std::string _iban);
  61. /// @returns the ICAP object for the ICAP address given.
  62. static ICAP decoded(std::string const& _encoded);
  63. /// @returns the encoded ICAP address.
  64. std::string encoded() const;
  65. /// @returns type of ICAP.
  66. Type type() const { return m_type; }
  67. /// @returns target address. Only valid when type() == Direct.
  68. Address const& direct() const { return m_type == Direct ? m_direct : ZeroAddress; }
  69. /// @returns asset. Only valid when type() == Indirect.
  70. std::string const& asset() const { return m_type == Indirect ? m_asset : EmptyString; }
  71. /// @returns target name. Only valid when type() == Indirect and asset() == "ETH".
  72. std::string const& target() const { return m_type == Indirect && m_asset == "ETH" ? m_client : EmptyString; }
  73. /// @returns institution name. Only valid when type() == Indirect and asset() == "XET".
  74. std::string const& institution() const { return m_type == Indirect && m_asset == "XET" ? m_institution : EmptyString; }
  75. /// @returns client name. Only valid when type() == Indirect and asset() == "XET".
  76. std::string const& client() const { return m_type == Indirect && m_asset == "XET" ? m_client : EmptyString; }
  77. /// @returns target address. Always valid, but requires the Registry address and a function to make calls.
  78. std::pair<Address, bytes> address(std::function<bytes(Address, bytes)> const& _call, Address const& _reg) const { return m_type == Direct ? make_pair(direct(), bytes()) : m_type == Indirect ? lookup(_call, _reg) : make_pair(Address(), bytes()); }
  79. /// @returns target address. Looks up through the given Registry and call function. Only valid when type() == Indirect.
  80. std::pair<Address, bytes> lookup(std::function<bytes(Address, bytes)> const& _call, Address const& _reg) const;
  81. private:
  82. Type m_type = Invalid;
  83. Address m_direct;
  84. std::string m_client;
  85. std::string m_institution;
  86. std::string m_asset;
  87. };
  88. }
  89. }