123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- /*
- This file is part of cpp-ethereum.
- cpp-ethereum is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- cpp-ethereum is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
- */
- /** @file Whisper.cpp
- * @authors:
- * Gav Wood <i@gavwood.com>
- * Marek Kotewicz <marek@ethdev.com>
- * @date 2015
- */
- #include <jsonrpccpp/common/errors.h>
- #include <jsonrpccpp/common/exception.h>
- #include <libdevcore/CommonJS.h>
- #include <libethcore/CommonJS.h>
- #include <libwhisper/Interface.h>
- #include <libwebthree/WebThree.h>
- #include "Whisper.h"
- #include "JsonHelper.h"
- using namespace std;
- using namespace jsonrpc;
- using namespace dev;
- using namespace dev::rpc;
- Whisper::Whisper(WebThreeDirect& _web3, std::vector<dev::KeyPair> const& _accounts): m_web3(_web3)
- {
- setIdentities(_accounts);
- }
- void Whisper::setIdentities(std::vector<dev::KeyPair> const& _ids)
- {
- m_ids.clear();
- for (auto i: _ids)
- m_ids[i.pub()] = i.secret();
- }
- shh::Interface* Whisper::shh() const
- {
- return m_web3.whisper().get();
- }
- bool Whisper::shh_post(Json::Value const& _json)
- {
- try
- {
- shh::Message m = shh::toMessage(_json);
- Secret from;
- if (m.from() && m_ids.count(m.from()))
- {
- cwarn << "Silently signing message from identity" << m.from() << ": User validation hook goes here.";
- // TODO: insert validification hook here.
- from = m_ids[m.from()];
- }
-
- shh()->inject(toSealed(_json, m, from));
- return true;
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
- std::string Whisper::shh_newIdentity()
- {
- KeyPair kp = KeyPair::create();
- m_ids[kp.pub()] = kp.secret();
- return toJS(kp.pub());
- }
- bool Whisper::shh_hasIdentity(std::string const& _identity)
- {
- try
- {
- return m_ids.count(jsToPublic(_identity)) > 0;
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
- std::string Whisper::shh_newGroup(std::string const& _id, std::string const& _who)
- {
- (void)_id;
- (void)_who;
- return "";
- }
- std::string Whisper::shh_addToGroup(std::string const& _group, std::string const& _who)
- {
- (void)_group;
- (void)_who;
- return "";
- }
- std::string Whisper::shh_newFilter(Json::Value const& _json)
- {
- try
- {
- pair<shh::Topics, Public> w = shh::toWatch(_json);
- auto ret = shh()->installWatch(w.first);
- m_watches.insert(make_pair(ret, w.second));
- return toJS(ret);
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
- bool Whisper::shh_uninstallFilter(std::string const& _filterId)
- {
- try
- {
- shh()->uninstallWatch(jsToInt(_filterId));
- return true;
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
- Json::Value Whisper::shh_getFilterChanges(std::string const& _filterId)
- {
- try
- {
- Json::Value ret(Json::arrayValue);
-
- int id = jsToInt(_filterId);
- auto pub = m_watches[id];
- if (!pub || m_ids.count(pub))
- for (h256 const& h: shh()->checkWatch(id))
- {
- auto e = shh()->envelope(h);
- shh::Message m;
- if (pub)
- {
- cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
- m = e.open(shh()->fullTopics(id), m_ids[pub]);
- }
- else
- m = e.open(shh()->fullTopics(id));
- if (!m)
- continue;
- ret.append(toJson(h, e, m));
- }
-
- return ret;
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
- Json::Value Whisper::shh_getMessages(std::string const& _filterId)
- {
- try
- {
- Json::Value ret(Json::arrayValue);
-
- int id = jsToInt(_filterId);
- auto pub = m_watches[id];
- if (!pub || m_ids.count(pub))
- for (h256 const& h: shh()->watchMessages(id))
- {
- auto e = shh()->envelope(h);
- shh::Message m;
- if (pub)
- {
- cwarn << "Silently decrypting message from identity" << pub << ": User validation hook goes here.";
- m = e.open(shh()->fullTopics(id), m_ids[pub]);
- }
- else
- m = e.open(shh()->fullTopics(id));
- if (!m)
- continue;
- ret.append(toJson(h, e, m));
- }
- return ret;
- }
- catch (...)
- {
- BOOST_THROW_EXCEPTION(JsonRpcException(Errors::ERROR_RPC_INVALID_PARAMS));
- }
- }
|