From cec504d1a219ee92d1a137c38bce63988b1b37b5 Mon Sep 17 00:00:00 2001 From: Gav Wood Date: Thu, 29 Jan 2015 15:26:08 -0800 Subject: [PATCH] Various fixes. --- alethzero/MainWin.cpp | 4 ++-- libweb3jsonrpc/WebThreeStubServerBase.cpp | 5 +---- libwhisper/Interface.h | 9 +-------- libwhisper/Message.cpp | 17 +++++++++-------- libwhisper/Message.h | 16 ++++++++++++---- libwhisper/WhisperHost.cpp | 2 +- test/whisperTopic.cpp | 18 +++++++++--------- 7 files changed, 35 insertions(+), 36 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index b771d4836..a608957a9 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -2414,10 +2414,10 @@ void Main::refreshWhispers() shh::Envelope const& e = w.second; shh::Message m; for (pair const& i: m_server->ids()) - if (!!(m = e.open(i.second))) + if (!!(m = e.open(shh::FilterKey(shh::Undefined, i.second)))) break; if (!m) - m = e.open(); + m = e.open(shh::FilterKey()); QString msg; if (m.from()) diff --git a/libweb3jsonrpc/WebThreeStubServerBase.cpp b/libweb3jsonrpc/WebThreeStubServerBase.cpp index d6f00f7cb..0fd9476a4 100644 --- a/libweb3jsonrpc/WebThreeStubServerBase.cpp +++ b/libweb3jsonrpc/WebThreeStubServerBase.cpp @@ -576,15 +576,12 @@ Json::Value WebThreeStubServerBase::shh_changed(int const& _id) if (pub) { cwarn << "Silently decrypting message from identity" << pub.abridged() << ": User validation hook goes here."; - m = e.open(m_ids[pub], shh::NotPublic); + m = e.open(shh::FilterKey(shh::Undefined, m_ids[pub])); if (!m) continue; } else - { - unsigned i = 0; m = e.open(face()->filterKey(_id)); - } ret.append(toJson(h, e, m)); } diff --git a/libwhisper/Interface.h b/libwhisper/Interface.h index 2a14b4a16..6e3eb1cd1 100644 --- a/libwhisper/Interface.h +++ b/libwhisper/Interface.h @@ -45,17 +45,11 @@ namespace shh class Watch; -struct FilterKey -{ - unsigned topicIndex = (unsigned)-1; - Secret key; -}; - struct InstalledFilter { InstalledFilter(FullTopic const& _f): full(_f), filter(_f) {} - FilterKey filterKey() const { unsigned i; for (i = 0; i < full.size() && !full[i]; ++i) {} return i < full.size() ? FilterKey{i, full[i]} : FilterKey(); } + FilterKey filterKey() const { unsigned i; for (i = 0; i < full.size() && !full[i]; ++i) {} return i < full.size() ? FilterKey(i, full[i]) : FilterKey(); } FullTopic full; TopicFilter filter; @@ -116,7 +110,6 @@ public: Watch(Interface& _c, FullTopic const& _f): m_c(&_c), m_id(_c.installWatch(_f)) {} ~Watch() { if (m_c) m_c->uninstallWatch(m_id); } - FullTopic fullTopic() const { return m_c ? m_c->getFilter(m_id) : FullTopic(); } h256s check() { return m_c ? m_c->checkWatch(m_id) : h256s(); } h256s peek() { return m_c ? m_c->peekWatch(m_id) : h256s(); } diff --git a/libwhisper/Message.cpp b/libwhisper/Message.cpp index ed7faaea8..b87ba0717 100644 --- a/libwhisper/Message.cpp +++ b/libwhisper/Message.cpp @@ -26,27 +26,28 @@ using namespace dev; using namespace dev::p2p; using namespace dev::shh; -Message::Message(Envelope const& _e, Secret const& _s, unsigned _topicIndex) +Message::Message(Envelope const& _e, FilterKey const& _fk) { try { bytes b; - if (_s) - if (!decrypt(_s, &(_e.data()), b)) + if (_fk.topicIndex == Undefined) + if (!_fk.key || !decrypt(_fk.key, &(_e.data()), b)) return; else{} - else if (_topicIndex != (unsigned)-1) + else { // public - need to get the key through combining with the topic/topicIndex we know. if (_e.data().size() < _e.topics().size() * 32) return; // get key from decrypted topic key: just xor - if (!decryptSym(_s ^ h256(bytesConstRef(&(_e.data())).cropped(32 * _topicIndex, 32)), bytesConstRef(&(_e.data())).cropped(32 * _e.topics().size()), b)) + if (!decryptSym(_fk.key ^ h256(bytesConstRef(&(_e.data())).cropped(32 * _fk.topicIndex, 32)), bytesConstRef(&(_e.data())).cropped(32 * _e.topics().size()), b)) return; } if (populate(b)) - m_to = KeyPair(_s).pub(); + if (_fk.key && _fk.topicIndex == Undefined) + m_to = KeyPair(_fk.key).pub(); } catch (...) // Invalid secret? TODO: replace ... with InvalidSecret { @@ -120,9 +121,9 @@ Envelope::Envelope(RLP const& _m) m_nonce = _m[4].toInt(); } -Message Envelope::open(Secret const& _s, unsigned _topicIndex) const +Message Envelope::open(FilterKey const& _filterKey) const { - return Message(*this, _s, _topicIndex); + return Message(*this, _filterKey); } unsigned Envelope::workProved() const diff --git a/libwhisper/Message.h b/libwhisper/Message.h index 0eef832eb..00e619461 100644 --- a/libwhisper/Message.h +++ b/libwhisper/Message.h @@ -39,14 +39,22 @@ namespace shh class Message; +static const unsigned Undefined = (unsigned)-1; + +struct FilterKey +{ + FilterKey() {} + FilterKey(unsigned _tI, Secret const& _k): topicIndex(_tI), key(_k) {} + unsigned topicIndex = Undefined; + Secret key; +}; + enum IncludeNonce { WithoutNonce = 0, WithNonce = 1 }; -static const unsigned NotPublic = (unsigned)-1; - class Envelope { friend class Message; @@ -66,7 +74,7 @@ public: Topic const& topic() const { return m_topic; } bytes const& data() const { return m_data; } - Message open(Secret const& _s, unsigned _topicIndex = NotPublic) const; + Message open(FilterKey const& _fk) const; unsigned workProved() const; void proveWork(unsigned _ms); @@ -93,7 +101,7 @@ class Message { public: Message() {} - Message(Envelope const& _e, Secret const& _s, unsigned _topicIndex); + Message(Envelope const& _e, FilterKey const& _fk); Message(bytes const& _payload): m_payload(_payload) {} Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {} Message(bytes&& _payload) { std::swap(_payload, m_payload); } diff --git a/libwhisper/WhisperHost.cpp b/libwhisper/WhisperHost.cpp index 609d5e8bf..213134db9 100644 --- a/libwhisper/WhisperHost.cpp +++ b/libwhisper/WhisperHost.cpp @@ -56,7 +56,7 @@ void WhisperHost::streamMessage(h256 _m, RLPStream& _s) const void WhisperHost::inject(Envelope const& _m, WhisperPeer* _p) { - cnote << "inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data()); + cnote << this << ": inject: " << _m.expiry() << _m.ttl() << _m.topic() << toHex(_m.data()); if (_m.expiry() <= time(0)) return; diff --git a/test/whisperTopic.cpp b/test/whisperTopic.cpp index a47b0a574..635defd75 100644 --- a/test/whisperTopic.cpp +++ b/test/whisperTopic.cpp @@ -34,7 +34,7 @@ BOOST_AUTO_TEST_CASE(topic) { cnote << "Testing Whisper..."; auto oldLogVerbosity = g_logVerbosity; - g_logVerbosity = 0; + g_logVerbosity = 4; bool started = false; unsigned result = 0; @@ -46,16 +46,16 @@ BOOST_AUTO_TEST_CASE(topic) auto wh = ph.registerCapability(new WhisperHost()); ph.start(); - started = true; - /// Only interested in odd packets auto w = wh->installWatch(BuildTopicMask("odd")); - for (int i = 0, last = 0; i < 200 && last < 81; ++i) + started = true; + + for (int iterout = 0, last = 0; iterout < 200 && last < 81; ++iterout) { for (auto i: wh->checkWatch(w)) { - Message msg = wh->envelope(i).open(wh->filterKey()); + Message msg = wh->envelope(i).open(wh->filterKey(w)); last = RLP(msg.payload()).toInt(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); result += last; @@ -116,7 +116,7 @@ BOOST_AUTO_TEST_CASE(forwarding) { for (auto i: wh->checkWatch(w)) { - Message msg = wh->envelope(i).open(); + Message msg = wh->envelope(i).open(wh->filterKey(w)); unsigned last = RLP(msg.payload()).toInt(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); result = last; @@ -152,7 +152,7 @@ BOOST_AUTO_TEST_CASE(forwarding) { for (auto i: wh->checkWatch(w)) { - Message msg = wh->envelope(i).open(); + Message msg = wh->envelope(i).open(wh->filterKey(w)); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); } this_thread::sleep_for(chrono::milliseconds(50)); @@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { for (auto i: wh->checkWatch(w)) { - Message msg = wh->envelope(i).open(); + Message msg = wh->envelope(i).open(wh->filterKey(w)); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); } this_thread::sleep_for(chrono::milliseconds(50)); @@ -255,7 +255,7 @@ BOOST_AUTO_TEST_CASE(asyncforwarding) { for (auto i: wh->checkWatch(w)) { - Message msg = wh->envelope(i).open(); + Message msg = wh->envelope(i).open(wh->filterKey(w)); unsigned last = RLP(msg.payload()).toInt(); cnote << "New message from:" << msg.from().abridged() << RLP(msg.payload()).toInt(); result = last;