2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:15:08 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
#include <oxenc/bt.h>
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include "address.hpp"
|
|
|
|
#include "vanity.hpp"
|
|
|
|
#include <llarp/util/bencode.hpp>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2020-05-01 19:51:15 +00:00
|
|
|
#include <optional>
|
2019-01-02 01:04:04 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
static auto info_cat = llarp::log::Cat("lokinet.info");
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace llarp::service
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
struct ServiceInfo
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
private:
|
|
|
|
PubKey enckey;
|
|
|
|
PubKey signkey;
|
|
|
|
mutable Address m_CachedAddr;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
public:
|
|
|
|
VanityNonce vanity;
|
|
|
|
uint64_t version = llarp::constants::proto_version;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
|
|
|
RandomizeVanity()
|
|
|
|
{
|
|
|
|
vanity.Randomize();
|
|
|
|
}
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
2023-09-27 14:09:48 +00:00
|
|
|
verify(uint8_t* buf, size_t size, const Signature& sig) const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
const PubKey&
|
|
|
|
EncryptionPublicKey() const
|
|
|
|
{
|
|
|
|
if (m_CachedAddr.IsZero())
|
2018-08-10 21:34:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
CalculateAddress(m_CachedAddr.as_array());
|
2018-08-10 21:34:11 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return enckey;
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
Update(const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce = {});
|
2018-07-11 16:11:19 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
operator==(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return enckey == other.enckey && signkey == other.signkey && version == other.version
|
|
|
|
&& vanity == other.vanity;
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
operator!=(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
operator<(const ServiceInfo& other) const
|
|
|
|
{
|
|
|
|
return Addr() < other.Addr();
|
|
|
|
}
|
2018-07-16 03:32:13 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2018-07-18 03:10:21 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// .loki address
|
|
|
|
std::string
|
|
|
|
Name() const;
|
2018-07-18 03:10:21 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
UpdateAddr();
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
const Address&
|
|
|
|
Addr() const
|
|
|
|
{
|
|
|
|
if (m_CachedAddr.IsZero())
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
CalculateAddress(m_CachedAddr.as_array());
|
2018-07-19 04:58:39 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return m_CachedAddr;
|
|
|
|
}
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// calculate our address
|
|
|
|
bool
|
|
|
|
CalculateAddress(std::array<byte_t, 32>& data) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
if (not bencode_decode_dict(*this, buf))
|
|
|
|
return false;
|
|
|
|
return UpdateAddr();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bt_encode(oxenc::bt_dict_producer& btdp) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
|
|
|
};
|
|
|
|
} // namespace llarp::service
|
2022-07-16 00:41:14 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::ServiceInfo> = true;
|