Whisper.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 Whisper.cpp
  15. * @authors:
  16. * Gav Wood <i@gavwood.com>
  17. * Marek Kotewicz <marek@ethdev.com>
  18. * @date 2015
  19. */
  20. #include <jsonrpccpp/common/errors.h>
  21. #include <jsonrpccpp/common/exception.h>
  22. #include <libdevcore/CommonJS.h>
  23. #include <libethcore/CommonJS.h>
  24. #include <libwhisper/Interface.h>
  25. #include <libwebthree/WebThree.h>
  26. #include "Whisper.h"
  27. #include "JsonHelper.h"
  28. using namespace std;
  29. using namespace jsonrpc;
  30. using namespace dev;
  31. using namespace dev::rpc;
  32. Whisper::Whisper(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts): m_web3(_web3)
  33. {
  34. setIdentities(_accounts);
  35. }
  36. void Whisper::setIdentities(std::vector<dev::KeyPair> const& _ids)
  37. {
  38. m_ids.clear();
  39. for (auto i: _ids)
  40. m_ids[i.pub()] = i.secret();
  41. }
  42. shh::Interface* Whisper::shh() const
  43. {
  44. return m_web3.whisper().get();
  45. }
  46. bool Whisper::shh_post(Json::Value const& _json)
  47. {
  48. try
  49. {
  50. shh::Message m = shh::toMessage(_json);
  51. Secret from;
  52. if (m.from() && m_ids.count(m.from()))
  53. {
  54. cwarn << "Silently signing message from identity" << m.from() << ": User validation hook goes here.";
  55. // TODO: insert validification hook here.
  56. from = m_ids[m.from()];
  57. }
  58. shh()->inject(toSealed(_json, m, from));
  59. return true;
  60. }
  61. catch (...)
  62. {
  63. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  64. }
  65. }
  66. std::string Whisper::shh_newIdentity()
  67. {
  68. KeyPair kp = KeyPair::create();
  69. m_ids[kp.pub()] = kp.secret();
  70. return toJS(kp.pub());
  71. }
  72. bool Whisper::shh_hasIdentity(std::string const& _identity)
  73. {
  74. try
  75. {
  76. return m_ids.count(jsToPublic(_identity)) > 0;
  77. }
  78. catch (...)
  79. {
  80. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  81. }
  82. }
  83. std::string Whisper::shh_newGroup(std::string const& _id, std::string const& _who)
  84. {
  85. (void)_id;
  86. (void)_who;
  87. return "";
  88. }
  89. std::string Whisper::shh_addToGroup(std::string const& _group, std::string const& _who)
  90. {
  91. (void)_group;
  92. (void)_who;
  93. return "";
  94. }
  95. std::string Whisper::shh_newFilter(Json::Value const& _json)
  96. {
  97. try
  98. {
  99. pair<shh::Topics, Public> w = shh::toWatch(_json);
  100. auto ret = shh()->installWatch(w.first);
  101. m_watches.insert(make_pair(ret, w.second));
  102. return toJS(ret);
  103. }
  104. catch (...)
  105. {
  106. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  107. }
  108. }
  109. bool Whisper::shh_uninstallFilter(std::string const& _filterId)
  110. {
  111. try
  112. {
  113. shh()->uninstallWatch(jsToInt(_filterId));
  114. return true;
  115. }
  116. catch (...)
  117. {
  118. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  119. }
  120. }
  121. Json::Value Whisper::shh_getFilterChanges(std::string const& _filterId)
  122. {
  123. try
  124. {
  125. Json::Value ret(Json::arrayValue);
  126. int id = jsToInt(_filterId);
  127. auto pub = m_watches[id];
  128. if (!pub || m_ids.count(pub))
  129. for (h256 const& h: shh()->checkWatch(id))
  130. {
  131. auto e = shh()->envelope(h);
  132. shh::Message m;
  133. if (pub)
  134. {
  135. cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
  136. m = e.open(shh()->fullTopics(id), m_ids[pub]);
  137. }
  138. else
  139. m = e.open(shh()->fullTopics(id));
  140. if (!m)
  141. continue;
  142. ret.append(toJson(h, e, m));
  143. }
  144. return ret;
  145. }
  146. catch (...)
  147. {
  148. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  149. }
  150. }
  151. Json::Value Whisper::shh_getMessages(std::string const& _filterId)
  152. {
  153. try
  154. {
  155. Json::Value ret(Json::arrayValue);
  156. int id = jsToInt(_filterId);
  157. auto pub = m_watches[id];
  158. if (!pub || m_ids.count(pub))
  159. for (h256 const& h: shh()->watchMessages(id))
  160. {
  161. auto e = shh()->envelope(h);
  162. shh::Message m;
  163. if (pub)
  164. {
  165. cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
  166. m = e.open(shh()->fullTopics(id), m_ids[pub]);
  167. }
  168. else
  169. m = e.open(shh()->fullTopics(id));
  170. if (!m)
  171. continue;
  172. ret.append(toJson(h, e, m));
  173. }
  174. return ret;
  175. }
  176. catch (...)
  177. {
  178. BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
  179. }
  180. }