lokinet/llarp/service/info.hpp

113 lines
2.1 KiB
C++
Raw Normal View History

#pragma once
2018-12-12 02:15:08 +00:00
#include <oxenc/bt.h>
#include <llarp/crypto/types.hpp>
#include "address.hpp"
#include "vanity.hpp"
#include <llarp/util/bencode.hpp>
2018-07-09 17:32:11 +00:00
#include <optional>
namespace
{
static auto info_cat = llarp::log::Cat("lokinet.info");
} // namespace
namespace llarp::service
2018-07-09 17:32:11 +00:00
{
struct ServiceInfo
2018-07-09 17:32:11 +00:00
{
private:
PubKey enckey;
PubKey signkey;
mutable Address m_CachedAddr;
public:
VanityNonce vanity;
uint64_t version = llarp::constants::proto_version;
void
RandomizeVanity()
{
vanity.Randomize();
}
2018-08-10 21:34:11 +00:00
bool
verify(uint8_t* buf, size_t size, const Signature& sig) const;
2018-08-10 21:34:11 +00:00
const PubKey&
EncryptionPublicKey() const
{
if (m_CachedAddr.IsZero())
2018-08-10 21:34:11 +00:00
{
CalculateAddress(m_CachedAddr.as_array());
2018-08-10 21:34:11 +00:00
}
return enckey;
}
2018-07-09 17:32:11 +00:00
bool
Update(const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce = {});
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
bool
operator!=(const ServiceInfo& other) const
{
return !(*this == other);
}
bool
operator<(const ServiceInfo& other) const
{
return Addr() < other.Addr();
}
2018-07-16 03:32:13 +00:00
std::string
ToString() const;
2018-07-18 03:10:21 +00:00
/// .loki address
std::string
Name() const;
2018-07-18 03:10:21 +00:00
bool
UpdateAddr();
2018-07-09 17:32:11 +00:00
const Address&
Addr() const
{
if (m_CachedAddr.IsZero())
2018-07-19 04:58:39 +00:00
{
CalculateAddress(m_CachedAddr.as_array());
2018-07-19 04:58:39 +00:00
}
return m_CachedAddr;
}
2018-07-19 04:58:39 +00:00
/// calculate our address
bool
CalculateAddress(std::array<byte_t, 32>& data) const;
2018-07-09 17:32:11 +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
template <>
constexpr inline bool llarp::IsToStringFormattable<llarp::service::ServiceInfo> = true;