Kagome
Polkadot Runtime Engine in C++17
libp2p_types.cpp
Go to the documentation of this file.
1 
6 #include "scale/libp2p_types.hpp"
7 
8 #include <algorithm>
9 #include <exception>
10 
11 namespace scale {
12 
14 
16  // some valid dummy peer id
17  auto res = libp2p::peer::PeerId::fromBase58(
18  "12D3KooWFN2mhgpkJsDBuNuE5427AcDrsib8EoqGMZmkxWwx3Md4");
19  BOOST_ASSERT(res);
20  return res.value();
21  }
22 
23  scale::ScaleEncoderStream &operator<<(
24  scale::ScaleEncoderStream &s, const libp2p::peer::PeerInfo &peer_info) {
25  std::vector<std::string> addresses;
26  for (const auto &addr : peer_info.addresses) {
27  addresses.emplace_back(addr.getStringAddress());
28  }
29  return s << peer_info.id.toBase58() << addresses;
30  }
31 
32  scale::ScaleDecoderStream &operator>>(scale::ScaleDecoderStream &s,
33  libp2p::peer::PeerInfo &peer_info) {
34  std::string peer_id_base58;
35  std::vector<std::string> addresses;
36  s >> peer_id_base58 >> addresses;
37  auto peer_id_res = libp2p::peer::PeerId::fromBase58(peer_id_base58);
38  if (not peer_id_res) {
39  throw std::runtime_error(peer_id_res.error().message());
40  }
41  peer_info.id = std::move(peer_id_res.value());
42  std::vector<libp2p::multi::Multiaddress> multi_addrs;
43  multi_addrs.reserve(addresses.size());
44  std::for_each(
45  addresses.begin(), addresses.end(), [&multi_addrs](const auto &addr) {
46  // filling in only supported kinds of addresses
47  auto res = libp2p::multi::Multiaddress::create(addr);
48  if (res) {
49  multi_addrs.emplace_back(std::move(res.value()));
50  }
51  });
52  peer_info.addresses = std::move(multi_addrs);
53  return s;
54  }
55 } // namespace scale
static libp2p::peer::PeerId dummyPeerId()
scale::ScaleEncoderStream & operator<<(scale::ScaleEncoderStream &s, const libp2p::peer::PeerInfo &peer_info)
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::PeerId PeerId
scale::ScaleDecoderStream & operator>>(scale::ScaleDecoderStream &s, libp2p::peer::PeerInfo &peer_info)