Fabcoin Core  0.16.2
P2P Digital Currency
Whisper.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 */
24 #include <jsonrpccpp/common/errors.h>
25 #include <jsonrpccpp/common/exception.h>
26 #include <libdevcore/CommonJS.h>
27 #include <libethcore/CommonJS.h>
28 #include <libwhisper/Interface.h>
29 #include <libwebthree/WebThree.h>
30 #include "Whisper.h"
31 #include "JsonHelper.h"
32 
33 using namespace std;
34 using namespace jsonrpc;
35 using namespace dev;
36 using namespace dev::rpc;
37 
38 
39 Whisper::Whisper(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts): m_web3(_web3)
40 {
41  setIdentities(_accounts);
42 }
43 
44 void Whisper::setIdentities(std::vector<dev::KeyPair> const& _ids)
45 {
46  m_ids.clear();
47  for (auto i: _ids)
48  m_ids[i.pub()] = i.secret();
49 }
50 
52 {
53  return m_web3.whisper().get();
54 }
55 
56 bool Whisper::shh_post(Json::Value const& _json)
57 {
58  try
59  {
60  shh::Message m = shh::toMessage(_json);
61  Secret from;
62  if (m.from() && m_ids.count(m.from()))
63  {
64  cwarn << "Silently signing message from identity" << m.from() << ": User validation hook goes here.";
65  // TODO: insert validification hook here.
66  from = m_ids[m.from()];
67  }
68 
69  shh()->inject(toSealed(_json, m, from));
70  return true;
71  }
72  catch (...)
73  {
74  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
75  }
76 }
77 
79 {
80  KeyPair kp = KeyPair::create();
81  m_ids[kp.pub()] = kp.secret();
82  return toJS(kp.pub());
83 }
84 
85 bool Whisper::shh_hasIdentity(std::string const& _identity)
86 {
87  try
88  {
89  return m_ids.count(jsToPublic(_identity)) > 0;
90  }
91  catch (...)
92  {
93  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
94  }
95 }
96 
97 std::string Whisper::shh_newGroup(std::string const& _id, std::string const& _who)
98 {
99  (void)_id;
100  (void)_who;
101  return "";
102 }
103 
104 std::string Whisper::shh_addToGroup(std::string const& _group, std::string const& _who)
105 {
106  (void)_group;
107  (void)_who;
108  return "";
109 }
110 
111 std::string Whisper::shh_newFilter(Json::Value const& _json)
112 {
113  try
114  {
115  pair<shh::Topics, Public> w = shh::toWatch(_json);
116  auto ret = shh()->installWatch(w.first);
117  m_watches.insert(make_pair(ret, w.second));
118  return toJS(ret);
119  }
120  catch (...)
121  {
122  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
123  }
124 }
125 
126 bool Whisper::shh_uninstallFilter(std::string const& _filterId)
127 {
128  try
129  {
130  shh()->uninstallWatch(jsToInt(_filterId));
131  return true;
132  }
133  catch (...)
134  {
135  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
136  }
137 }
138 
139 Json::Value Whisper::shh_getFilterChanges(std::string const& _filterId)
140 {
141  try
142  {
143  Json::Value ret(Json::arrayValue);
144 
145  int id = jsToInt(_filterId);
146  auto pub = m_watches[id];
147  if (!pub || m_ids.count(pub))
148  for (h256 const& h: shh()->checkWatch(id))
149  {
150  auto e = shh()->envelope(h);
151  shh::Message m;
152  if (pub)
153  {
154  cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
155  m = e.open(shh()->fullTopics(id), m_ids[pub]);
156  }
157  else
158  m = e.open(shh()->fullTopics(id));
159  if (!m)
160  continue;
161  ret.append(toJson(h, e, m));
162  }
163 
164  return ret;
165  }
166  catch (...)
167  {
168  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
169  }
170 }
171 
172 Json::Value Whisper::shh_getMessages(std::string const& _filterId)
173 {
174  try
175  {
176  Json::Value ret(Json::arrayValue);
177 
178  int id = jsToInt(_filterId);
179  auto pub = m_watches[id];
180  if (!pub || m_ids.count(pub))
181  for (h256 const& h: shh()->watchMessages(id))
182  {
183  auto e = shh()->envelope(h);
184  shh::Message m;
185  if (pub)
186  {
187  cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
188  m = e.open(shh()->fullTopics(id), m_ids[pub]);
189  }
190  else
191  m = e.open(shh()->fullTopics(id));
192  if (!m)
193  continue;
194  ret.append(toJson(h, e, m));
195  }
196  return ret;
197  }
198  catch (...)
199  {
200  BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
201  }
202 }
virtual std::string shh_newFilter(Json::Value const &_json) override
Definition: Whisper.cpp:111
virtual bool shh_uninstallFilter(std::string const &_filterId) override
Definition: Whisper.cpp:126
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
virtual h256s checkWatch(unsigned _watchId)=0
virtual Envelope envelope(h256 _m) const =0
shh::Interface * shh() const
Definition: Whisper.cpp:51
virtual h256s watchMessages(unsigned _watchId)=0
virtual void setIdentities(std::vector< dev::KeyPair > const &_ids)
Definition: Whisper.cpp:44
#define h(i)
Definition: sha.cpp:736
virtual void uninstallWatch(unsigned _watchId)=0
pair< shh::Topics, Public > toWatch(Json::Value const &_json)
Definition: JsonHelper.cpp:522
Simple class that represents a "key pair".
Definition: Common.h:150
static KeyPair create()
Create a new, randomly generated object.
Definition: Common.cpp:307
std::hash for asio::adress
Definition: Common.h:323
WebThreeDirect & m_web3
Definition: Whisper.h:67
Main API hub for interfacing with Web 3 components.
Definition: WebThree.h:119
Secret const & secret() const
Definition: Common.h:167
virtual bool shh_post(Json::Value const &_json) override
Definition: Whisper.cpp:56
virtual std::string shh_newIdentity() override
Definition: Whisper.cpp:78
Public const & pub() const
Retrieve the public key.
Definition: Common.h:170
virtual std::string shh_newGroup(std::string const &_id, std::string const &_who) override
Definition: Whisper.cpp:97
virtual Json::Value shh_getFilterChanges(std::string const &_filterId) override
Definition: Whisper.cpp:139
Config::Value_type Value
std::shared_ptr< shh::WhisperHost > whisper() const
Definition: WebThree.h:141
std::string toJS(FixedHash< S > const &_h)
Definition: CommonJS.h:34
virtual std::string shh_addToGroup(std::string const &_group, std::string const &_who) override
Definition: Whisper.cpp:104
Public jsToPublic(std::string const &_s)
Leniently convert string to Public (h512). Accepts integers, "0x" prefixing, non-exact length...
Definition: CommonJS.h:37
#define cwarn
Definition: Log.h:304
virtual void inject(Envelope const &_m, WhisperPeer *_from=nullptr)=0
virtual bool shh_hasIdentity(std::string const &_identity) override
Definition: Whisper.cpp:85
Json::Value toJson(unordered_map< u256, u256 > const &_storage)
Definition: JsonHelper.cpp:41
virtual unsigned installWatch(Topics const &_filter)=0
An (unencrypted) message, constructed from the combination of an Envelope, and, potentially, a Secret key to decrypt the Message.
Definition: Message.h:100
#define e(i)
Definition: sha.cpp:733
shh::Message toMessage(Json::Value const &_json)
Definition: JsonHelper.cpp:482
virtual Json::Value shh_getMessages(std::string const &_filterId) override
Definition: Whisper.cpp:172
Public from() const
Definition: Message.h:109
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< N *8, N *8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > jsToInt(std::string const &_s)
Convert a string representation of a number to an int String can be a normal decimal number...
Definition: CommonJS.h:97
shh::Envelope toSealed(Json::Value const &_json, shh::Message const &_m, Secret const &_from)
Definition: JsonHelper.cpp:494
std::map< unsigned, dev::Public > m_watches
Definition: Whisper.h:69
std::map< dev::Public, dev::Secret > m_ids
Definition: Whisper.h:68