2021-03-09 22:24:35 +00:00
|
|
|
#include "router_id.hpp"
|
2023-10-24 13:18:03 +00:00
|
|
|
|
2022-02-17 18:44:31 +00:00
|
|
|
#include <oxenc/base32z.h>
|
2021-02-02 14:35:40 +00:00
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2020-06-29 21:46:25 +00:00
|
|
|
constexpr std::string_view SNODE_TLD = ".snode";
|
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
std::string
|
|
|
|
RouterID::ToString() const
|
|
|
|
{
|
2022-02-17 18:44:31 +00:00
|
|
|
std::string b32 = oxenc::to_base32z(begin(), end());
|
2020-06-29 21:46:25 +00:00
|
|
|
b32 += SNODE_TLD;
|
|
|
|
return b32;
|
2018-12-10 17:22:59 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 00:26:23 +00:00
|
|
|
std::string
|
|
|
|
RouterID::ShortString() const
|
|
|
|
{
|
2020-06-29 21:46:25 +00:00
|
|
|
// 5 bytes produces exactly 8 base32z characters:
|
2022-02-17 18:44:31 +00:00
|
|
|
return oxenc::to_base32z(begin(), begin() + 5);
|
2020-03-01 00:26:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
util::StatusObject
|
|
|
|
RouterID::ExtractStatus() const
|
|
|
|
{
|
|
|
|
util::StatusObject obj{{"snode", ToString()}, {"hex", ToHex()}};
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2018-12-10 17:22:59 +00:00
|
|
|
bool
|
2020-06-29 21:46:25 +00:00
|
|
|
RouterID::FromString(std::string_view str)
|
2018-12-10 17:22:59 +00:00
|
|
|
{
|
2020-06-29 21:46:25 +00:00
|
|
|
auto pos = str.find(SNODE_TLD);
|
|
|
|
if (pos != str.size() - SNODE_TLD.size())
|
|
|
|
return false;
|
|
|
|
str.remove_suffix(SNODE_TLD.size());
|
|
|
|
// Ensure we have something valid:
|
|
|
|
// - must end in a 1-bit value: 'o' or 'y' (i.e. 10000 or 00000)
|
|
|
|
// - must have 51 preceeding base32z chars
|
|
|
|
// - thus we get 51*5+1 = 256 bits = 32 bytes of output
|
2022-02-17 18:44:31 +00:00
|
|
|
if (str.size() != 52 || !oxenc::is_base32z(str) || !(str.back() == 'o' || str.back() == 'y'))
|
2018-12-10 17:22:59 +00:00
|
|
|
return false;
|
2022-02-17 18:44:31 +00:00
|
|
|
oxenc::from_base32z(str.begin(), str.end(), begin());
|
2020-06-29 21:46:25 +00:00
|
|
|
return true;
|
2018-12-10 17:22:59 +00:00
|
|
|
}
|
|
|
|
} // namespace llarp
|