Fabcoin Core  0.16.2
P2P Digital Currency
Message.h
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 */
22 #pragma once
23 
24 #include <mutex>
25 #include <array>
26 #include <set>
27 #include <memory>
28 #include <utility>
29 
30 #include <libdevcore/RLP.h>
31 #include <libdevcore/Guards.h>
32 #include <libdevcrypto/Common.h>
33 #include <libdevcore/SHA3.h>
34 #include "Common.h"
35 
36 namespace dev
37 {
38 namespace shh
39 {
40 
41 class Message;
42 
43 static const unsigned Undefined = (unsigned)-1;
44 
45 struct FilterKey
46 {
47  FilterKey() {}
48  FilterKey(unsigned _tI, Secret const& _k): topicIndex(_tI), key(_k) {}
49  unsigned topicIndex = Undefined;
51 };
52 
54 {
57 };
58 
59 class Envelope
60 {
61  friend class Message;
62 
63 public:
64  Envelope() {}
65  Envelope(RLP const& _m);
66 
67  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; }
68  h256 sha3(IncludeNonce _withNonce = WithNonce) const { RLPStream s; streamRLP(s, _withNonce); return dev::sha3(s.out()); }
69  Message open(Topics const& _t, Secret const& _s = Secret()) const;
70  unsigned workProved() const;
71  void proveWork(unsigned _ms);
72 
73  unsigned sent() const { return m_expiry - m_ttl; }
74  unsigned expiry() const { return m_expiry; }
75  unsigned ttl() const { return m_ttl; }
76  AbridgedTopics const& topic() const { return m_topic; }
77  bytes const& data() const { return m_data; }
78 
79  bool matchesBloomFilter(TopicBloomFilterHash const& f) const;
80  bool isExpired() const { return m_expiry <= utcTime(); }
81 
82 private:
83  Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const& _topic): m_expiry(_exp), m_ttl(_ttl), m_topic(_topic) {}
84 
85  unsigned m_expiry = 0;
86  unsigned m_ttl = 0;
88 
91 };
92 
93 enum /*Message Flags*/
94 {
96 };
97 
100 class Message
101 {
102 public:
103  Message() {}
104  Message(Envelope const& _e, Topics const& _t, Secret const& _s = Secret());
105  Message(bytes const& _payload): m_payload(_payload) {}
106  Message(bytesConstRef _payload): m_payload(_payload.toBytes()) {}
107  Message(bytes&& _payload) { std::swap(_payload, m_payload); }
108 
109  Public from() const { return m_from; }
110  Public to() const { return m_to; }
111  bytes const& payload() const { return m_payload; }
112 
113  void setFrom(Public _from) { m_from = _from; }
114  void setTo(Public _to) { m_to = _to; }
115  void setPayload(bytes const& _payload) { m_payload = _payload; }
116  void setPayload(bytes&& _payload) { swap(m_payload, _payload); }
117 
118  operator bool() const { return !!m_payload.size() || m_from || m_to; }
119 
121  Envelope seal(Secret const& _from, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const;
122  // Overloads for skipping _from or specifying _to.
123  Envelope seal(Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) const { return seal(Secret(), _topics, _ttl, _workToProve); }
124  Envelope sealTo(Public _to, Topics const& _topics, unsigned _ttl = 50, unsigned _workToProve = 50) { m_to = _to; return seal(Secret(), _topics, _ttl, _workToProve); }
125  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); }
126 
127 private:
128  bool populate(bytes const& _data);
129  bool openBroadcastEnvelope(Envelope const& _e, Topics const& _t, bytes& o_b);
130  Secret generateGamma(Secret const& _key, h256 const& _salt) const { return sha3(_key ^ _salt); }
131 
135 };
136 
137 }
138 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
Message(bytes const &_payload)
Definition: Message.h:105
h256 sha3(IncludeNonce _withNonce=WithNonce) const
Definition: Message.h:68
bytes const & data() const
Definition: Message.h:77
Secret generateGamma(Secret const &_key, h256 const &_salt) const
Definition: Message.h:130
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
unsigned topicIndex
Definition: Message.h:49
void streamRLP(RLPStream &_s, IncludeNonce _withNonce=WithNonce) const
Definition: Message.h:67
uint64_t utcTime()
Get the current time in seconds since the epoch in UTC.
Definition: Common.cpp:64
std::vector< AbridgedTopic > AbridgedTopics
Definition: Common.h:68
bytes const & out() const
Read the byte stream.
Definition: RLP.h:433
h256s Topics
Definition: Common.h:69
SecureFixedHash< 32 > Secret
Definition: Common.h:35
AbridgedTopics const & topic() const
Definition: Message.h:76
bytes const & payload() const
Definition: Message.h:111
unsigned expiry() const
Definition: Message.h:74
Message(bytesConstRef _payload)
Definition: Message.h:106
Envelope(unsigned _exp, unsigned _ttl, AbridgedTopics const &_topic)
Definition: Message.h:83
std::vector< byte > bytes
Definition: Common.h:75
RLPStream & appendList(size_t _items)
Appends a list.
Definition: RLP.cpp:276
void setPayload(bytes const &_payload)
Definition: Message.h:115
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
unsigned sent() const
Definition: Message.h:73
void setTo(Public _to)
Definition: Message.h:114
FilterKey(unsigned _tI, Secret const &_k)
Definition: Message.h:48
bool isExpired() const
Definition: Message.h:80
Envelope seal(Topics const &_topics, unsigned _ttl=50, unsigned _workToProve=50) const
Definition: Message.h:123
#define f(x)
Definition: gost.cpp:57
IncludeNonce
Definition: Message.h:53
void setPayload(bytes &&_payload)
Definition: Message.h:116
An (unencrypted) message, constructed from the combination of an Envelope, and, potentially, a Secret key to decrypt the Message.
Definition: Message.h:100
Envelope sealTo(Public _to, Topics const &_topics, unsigned _ttl=50, unsigned _workToProve=50)
Definition: Message.h:124
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
AbridgedTopics m_topic
Definition: Message.h:89
Public to() const
Definition: Message.h:110
unsigned ttl() const
Definition: Message.h:75
Message(bytes &&_payload)
Definition: Message.h:107
Public from() const
Definition: Message.h:109
Class for writing to an RLP bytestream.
Definition: RLP.h:383
Envelope sealTo(Secret const &_from, Public _to, Topics const &_topics, unsigned _ttl=50, unsigned _workToProve=50)
Definition: Message.h:125
Class for interpreting Recursive Linear-Prefix Data.
Definition: RLP.h:64
void setFrom(Public _from)
Definition: Message.h:113