lokinet/llarp/service/info.hpp

109 lines
2.2 KiB
C++
Raw Normal View History

#pragma once
2018-12-12 02:15:08 +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
#include <optional>
2018-07-09 17:32:11 +00:00
namespace llarp
{
namespace service
{
2019-05-24 02:01:36 +00:00
struct ServiceInfo
2018-07-09 17:32:11 +00:00
{
2018-08-10 21:34:11 +00:00
private:
PubKey enckey;
PubKey signkey;
2019-12-14 18:50:52 +00:00
mutable Address m_CachedAddr;
2018-08-10 21:34:11 +00:00
public:
2018-11-27 21:48:12 +00:00
VanityNonce vanity;
2022-05-26 15:59:44 +00:00
uint64_t version = llarp::constants::proto_version;
2018-08-10 21:34:11 +00:00
void
RandomizeVanity()
{
vanity.Randomize();
}
bool
Verify(const llarp_buffer_t& payload, const Signature& sig) const;
2018-08-10 21:34:11 +00:00
const PubKey&
EncryptionPublicKey() const
2018-08-10 21:34:11 +00:00
{
if (m_CachedAddr.IsZero())
2019-12-14 18:50:52 +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
2018-08-10 21:34:11 +00:00
bool
Update(const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce = {});
2018-07-09 17:32:11 +00:00
bool
operator==(const ServiceInfo& other) const
{
return enckey == other.enckey && signkey == other.signkey && version == other.version
&& vanity == other.vanity;
}
2018-08-10 21:34:11 +00:00
bool
operator!=(const ServiceInfo& other) const
{
return !(*this == other);
}
2018-07-18 03:10:21 +00:00
bool
operator<(const ServiceInfo& other) const
{
return Addr() < other.Addr();
}
std::string
ToString() const;
2018-08-10 21:34:11 +00:00
/// .loki address
2018-07-16 03:32:13 +00:00
std::string
Name() const;
2018-07-18 03:10:21 +00:00
bool
UpdateAddr();
const Address&
Addr() const
{
if (m_CachedAddr.IsZero())
2019-12-14 18:50:52 +00:00
{
CalculateAddress(m_CachedAddr.as_array());
}
2018-07-18 03:10:21 +00:00
return m_CachedAddr;
}
2018-07-09 17:32:11 +00:00
/// calculate our address
bool
CalculateAddress(std::array<byte_t, 32>& data) const;
2018-07-09 17:32:11 +00:00
2018-07-19 04:58:39 +00:00
bool
2019-05-24 02:01:36 +00:00
BDecode(llarp_buffer_t* buf)
2018-07-19 04:58:39 +00:00
{
if (not bencode_decode_dict(*this, buf))
2019-12-14 18:50:52 +00:00
return false;
return UpdateAddr();
2018-07-19 04:58:39 +00:00
}
2018-07-09 17:32:11 +00:00
bool
2019-05-24 02:01:36 +00:00
BEncode(llarp_buffer_t* buf) const;
2018-07-09 17:32:11 +00:00
bool
2019-05-24 02:01:36 +00:00
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
2018-07-09 17:32:11 +00:00
};
} // namespace service
} // namespace llarp
template <>
constexpr inline bool llarp::IsToStringFormattable<llarp::service::ServiceInfo> = true;