Message.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 Message.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <mutex>
  20. #include <array>
  21. #include <set>
  22. #include <memory>
  23. #include <utility>
  24. #include <libdevcore/RLP.h>
  25. #include <libdevcore/Guards.h>
  26. #include <libdevcrypto/Common.h>
  27. #include <libdevcore/SHA3.h>
  28. #include "Common.h"
  29. namespace dev
  30. {
  31. namespace shh
  32. {
  33. class Message;
  34. static const unsigned Undefined = (unsigned)-1;
  35. struct FilterKey
  36. {
  37. FilterKey() {}
  38. FilterKey(unsigned _tI, Secret const& _k): topicIndex(_tI), key(_k) {}
  39. unsigned topicIndex = Undefined;
  40. Secret key;
  41. };
  42. enum IncludeNonce
  43. {
  44. WithoutNonce = 0,
  45. WithNonce = 1
  46. };
  47. class Envelope
  48. {
  49. friend class Message;
  50. public:
  51. Envelope() {}
  52. Envelope(RLP const& _m);
  53. void streamRLP(RLPStream& _s, IncludeNonce _withNonce = WithNonce) const { _s.appendList(_withNonce ? 5 : 4) << m_expiry << m_ttl << m_topic << m_data; if (_withNonce) _s << m_nonce; }
  54. h256 sha3(IncludeNonce _withNonce = WithNonce) const { RLPStream s; streamRLP(s, _withNonce); return dev::sha3(s.out()); }
  55. Message open(Topics const& _t, Secret const& _s = Secret()) const;
  56. unsigned workProved() const;
  57. void proveWork(unsigned _ms);
  58. unsigned sent() const { return m_expiry - m_ttl; }
  59. unsigned expiry() const { return m_expiry; }
  60. unsigned ttl() const { return m_ttl; }
  61. AbridgedTopics const& topic() const { return m_topic; }
  62. bytes const& data() const { return m_data; }
  63. bool matchesBloomFilter(TopicBloomFilterHash const& f) const;
  64. bool isExpired() const { return m_expiry <= utcTime(); }
  65. private:
  66. Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {}
  67. unsigned m_expiry = 0;
  68. unsigned m_ttl = 0;
  69. u256 m_nonce;
  70. AbridgedTopics m_topic;
  71. bytes m_data;
  72. };
  73. enum /*Message Flags*/
  74. {
  75. ContainsSignature = 1
  76. };
  77. /// An (unencrypted) message, constructed from the combination of an Envelope, and, potentially,
  78. /// a Secret key to decrypt the Message.
  79. class Message
  80. {
  81. public:
  82. Message() {}
  83. Message(Envelope const& _e, Topics const& _t, Secret const& _s = Secret());
  84. Message(bytes const& _payload): m_payload(_payload) {}
  85. Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {}
  86. Message(bytes&& _payload) { std::swap(_payload, m_payload); }
  87. Public from() const { return m_from; }
  88. Public to() const { return m_to; }
  89. bytes const& payload() const { return m_payload; }
  90. void setFrom(Public _from) { m_from = _from; }
  91. void setTo(Public _to) { m_to = _to; }
  92. void setPayload(bytes const& _payload) { m_payload = _payload; }
  93. void setPayload(bytes&& _payload) { swap(m_payload, _payload); }
  94. operator bool() const { return !!m_payload.size() || m_from || m_to; }
  95. /// Turn this message into a ditributable Envelope.
  96. Envelope seal(Secret const& _from, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const;
  97. // Overloads for skipping _from or specifying _to.
  98. Envelope seal(Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const { return seal(Secret(), _topics, _ttl, _workToProve); }
  99. Envelope sealTo(Public _to, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(Secret(), _topics, _ttl, _workToProve); }
  100. Envelope sealTo(Secret const& _from, Public _to, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(_from, _topics, _ttl, _workToProve); }
  101. private:
  102. bool populate(bytes const& _data);
  103. bool openBroadcastEnvelope(Envelope const& _e, Topics const& _t, bytes& o_b);
  104. Secret generateGamma(Secret const& _key, h256 const& _salt) const { return sha3(_key ^ _salt); }
  105. Public m_from;
  106. Public m_to;
  107. bytes m_payload;
  108. };
  109. }
  110. }