Fabcoin Core  0.16.2
P2P Digital Currency
ICAP.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU 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  cpp-ethereum 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 General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include "ICAP.h"
23 #include <boost/algorithm/string/case_conv.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <libdevcore/Base64.h>
26 #include <libdevcore/SHA3.h>
27 #include "Exceptions.h"
28 #include "ABI.h"
29 using namespace std;
30 using namespace dev;
31 using namespace dev::eth;
32 
33 namespace dev
34 {
35 namespace eth
36 {
37 
38 string ICAP::iban(std::string _c, std::string _d)
39 {
40  boost::to_upper(_c);
41  boost::to_upper(_d);
42  auto totStr = _d + _c + "00";
43  bigint tot = 0;
44  for (char x: totStr)
45  if (x >= 'A')
46  tot = tot * 100 + x - 'A' + 10;
47  else
48  tot = tot * 10 + x - '0';
49  unsigned check = (unsigned)(u256)(98 - tot % 97);
50  ostringstream out;
51  out << _c << setfill('0') << setw(2) << check << _d;
52  return out.str();
53 }
54 
55 std::pair<string, string> ICAP::fromIBAN(std::string _iban)
56 {
57  if (_iban.size() < 4)
58  return std::make_pair(string(), string());
59  boost::to_upper(_iban);
60  std::string c = _iban.substr(0, 2);
61  std::string d = _iban.substr(4);
62  if (iban(c, d) != _iban)
63  return std::make_pair(string(), string());
64  return make_pair(c, d);
65 }
66 
67 Secret ICAP::createDirect()
68 {
69  Secret ret;
70  while (true)
71  {
72  ret = Secret::random();
73  if (!toAddress(ret)[0])
74  return ret;
75  }
76 }
77 
78 ICAP ICAP::decoded(std::string const& _encoded)
79 {
80  ICAP ret;
81  std::string country;
82  std::string data;
83  std::tie(country, data) = fromIBAN(_encoded);
84  if (country != "XE")
85  BOOST_THROW_EXCEPTION(InvalidICAP());
86  if (data.size() == 30 || data.size() == 31)
87  {
88  ret.m_type = Direct;
89  // Direct ICAP
90  ret.m_direct = fromBase36<Address::size>(data);
91  }
92  else if (data.size() == 16)
93  {
94  ret.m_type = Indirect;
95  ret.m_asset = data.substr(0, 3);
96  if (ret.m_asset == "XET" || ret.m_asset == "ETH")
97  {
98  ret.m_institution = data.substr(3, 4);
99  ret.m_client = data.substr(7);
100  }
101  else
102  BOOST_THROW_EXCEPTION(InvalidICAP());
103  }
104  else
105  BOOST_THROW_EXCEPTION(InvalidICAP());
106 
107  return ret;
108 }
109 
110 std::string ICAP::encoded() const
111 {
112  if (m_type == Direct)
113  {
114  std::string d = toBase36<Address::size>(m_direct);
115  while (d.size() < 30) // always 34, sometimes 35.
116  d = "0" + d;
117  return iban("XE", d);
118  }
119  else if (m_type == Indirect)
120  {
121  if (
122  m_asset.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
123  m_institution.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
124  m_client.find_first_not_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890") != string::npos ||
125  m_asset.size() != 3 ||
126  (boost::to_upper_copy(m_asset) != "XET" && boost::to_upper_copy(m_asset) != "ETH") ||
127  m_institution.size() != 4 ||
128  m_client.size() != 9
129  )
130  BOOST_THROW_EXCEPTION(InvalidICAP());
131  return iban("XE", m_asset + m_institution + m_client);
132  }
133  else
134  BOOST_THROW_EXCEPTION(InvalidICAP());
135 }
136 
137 pair<Address, bytes> ICAP::lookup(std::function<bytes(Address, bytes)> const& _call, Address const& _reg) const
138 {
139  auto resolve = [&](string const& s)
140  {
141 /* vector<string> ss;
142  boost::algorithm::split(ss, s, boost::is_any_of("/"));
143  Address r = _reg;
144  for (unsigned i = 0; i < ss.size() - 1; ++i)
145  r = abiOut<Address>(_call(r, abiIn("subRegistrar(bytes32)", ss[i])));*/
146  return abiOut<Address>(_call(_reg, abiIn("addr(string)", s)));
147  };
148  if (m_asset == "XET")
149  {
150  Address a = resolve(m_institution);
151  bytes d = abiIn("deposit(uint64)", fromBase36<8>(m_client));
152  return make_pair(a, d);
153  }
154 /* else if (m_asset == "ETH")
155  {
156  if (m_institution[0] != 'X')
157  return make_pair(resolve(m_institution + "/" + m_client), bytes());
158  else if (m_institution == "XREG")
159  return make_pair(resolve(m_client), bytes());
160  else
161  BOOST_THROW_EXCEPTION(InterfaceNotSupported("ICAP::lookup(), bad institution"));
162  }*/
163  BOOST_THROW_EXCEPTION(InterfaceNotSupported("ICAP::lookup(), bad asset"));
164 }
165 
166 }
167 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string m_asset
Definition: ICAP.h:101
#define function(a, b, c, d, k, s)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
Definition: Common.h:121
std::string m_institution
Definition: ICAP.h:100
#define c(i)
std::hash for asio::adress
Definition: Common.h:323
std::string m_client
Definition: ICAP.h:99
bytes abiIn(std::string _id, T const &..._t)
Definition: ABI.h:64
#define a(i)
#define x(i)
Type m_type
Definition: ICAP.h:97
ExecStats::duration tot
Definition: ExecStats.cpp:34
std::vector< byte > bytes
Definition: Common.h:75
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:87
Encapsulation of an ICAP address.
Definition: ICAP.h:45
Address m_direct
Definition: ICAP.h:98
#define d(i)
Definition: sha.cpp:732
uint8_t const * data
Definition: sha3.h:19