@@ -23,9 +23,8 @@ namespace fc::primitives::address {
23
23
using base32 = cppcodec::base32_rfc4648;
24
24
using UVarint = libp2p::multi::UVarint;
25
25
26
- std::vector<uint8_t > encode (const Address &address) {
26
+ std::vector<uint8_t > encode (const Address &address) noexcept {
27
27
std::vector<uint8_t > res{};
28
- res.push_back (address.network );
29
28
res.push_back (address.getProtocol ());
30
29
std::vector<uint8_t > payload = visit_in_place (
31
30
address.data ,
@@ -38,16 +37,13 @@ namespace fc::primitives::address {
38
37
}
39
38
40
39
outcome::result<Address> decode (const std::vector<uint8_t > &v) {
41
- if (v.size () < 3 ) return outcome::failure (AddressError::INVALID_PAYLOAD);
42
- auto pos = v.begin ();
40
+ if (v.size () < 2 ) return outcome::failure (AddressError::INVALID_PAYLOAD);
43
41
44
- auto net = static_cast <Network>(*(pos++));
45
- if (!(net == Network::TESTNET || net == Network::MAINNET)) {
46
- return outcome::failure (AddressError::UNKNOWN_NETWORK);
47
- }
42
+ // TODO(ekovalev): [FIL-118] make network configurable; hardcoded for now
43
+ Network net{Network::TESTNET};
48
44
49
- auto p = static_cast <Protocol>(*(pos++) );
50
- std::vector<uint8_t > payload (pos , v.end ());
45
+ auto p = static_cast <Protocol>(v[ 0 ] );
46
+ std::vector<uint8_t > payload (std::next (v. begin ()) , v.end ());
51
47
switch (p) {
52
48
case Protocol::ID: {
53
49
boost::optional<UVarint> value = UVarint::create (payload);
@@ -94,8 +90,7 @@ namespace fc::primitives::address {
94
90
std::string encodeToString (const Address &address) {
95
91
std::string res{};
96
92
97
- char networkPrefix = ' f' ;
98
- if (address.network == Network::TESTNET) networkPrefix = ' t' ;
93
+ char networkPrefix = address.network == Network::TESTNET ? ' t' : ' f' ;
99
94
res.push_back (networkPrefix);
100
95
101
96
Protocol p = address.getProtocol ();
@@ -137,17 +132,11 @@ namespace fc::primitives::address {
137
132
outcome::result<Address> decodeFromString (const std::string &s) {
138
133
if (s.size () < 3 ) return outcome::failure (AddressError::INVALID_PAYLOAD);
139
134
140
- std::vector<uint8_t > buffer{};
141
- Network net{Network::MAINNET};
142
-
143
135
if (s[0 ] != ' f' && s[0 ] != ' t' )
144
136
return outcome::failure (AddressError::UNKNOWN_NETWORK);
145
- if (s[0 ] == ' f' )
146
- buffer.push_back (static_cast <uint8_t >(Network::MAINNET));
147
- else {
148
- buffer.push_back (static_cast <uint8_t >(Network::TESTNET));
149
- net = Network::TESTNET;
150
- }
137
+
138
+ std::vector<uint8_t > buffer{};
139
+ Network net = s[0 ] == ' t' ? Network::TESTNET : Network::MAINNET;
151
140
152
141
int protocol = int (s[1 ]) - int (' 0' );
153
142
if (protocol < Protocol::ID || protocol > Protocol::BLS)
@@ -190,7 +179,9 @@ namespace fc::primitives::address {
190
179
std::make_move_iterator (payload.begin ()),
191
180
std::make_move_iterator (payload.end ()) - 4 );
192
181
193
- return decode (buffer);
182
+ OUTCOME_TRY (address, decode (buffer));
183
+ address.network = net;
184
+ return address;
194
185
}
195
186
196
187
std::vector<uint8_t > checksum (const Address &address) {
@@ -208,13 +199,12 @@ namespace fc::primitives::address {
208
199
209
200
ingest.push_back (p);
210
201
211
- ingest = visit_in_place (
212
- address.data ,
213
- [&ingest](uint64_t v) { return ingest; },
214
- [&ingest](const auto &v) {
215
- ingest.insert (ingest.end (), v.begin (), v.end ());
216
- return ingest;
217
- });
202
+ ingest = visit_in_place (address.data ,
203
+ [&ingest](uint64_t v) { return ingest; },
204
+ [&ingest](const auto &v) {
205
+ ingest.insert (ingest.end (), v.begin (), v.end ());
206
+ return ingest;
207
+ });
218
208
219
209
size_t outlen = 4 ;
220
210
blake2b (res.data (), outlen, nullptr , 0 , ingest.data (), ingest.size ());
@@ -227,4 +217,4 @@ namespace fc::primitives::address {
227
217
return digest == expect;
228
218
}
229
219
230
- }; // namespace fc::primitives
220
+ }; // namespace fc::primitives::address
0 commit comments