lokinet/include/llarp/service/Info.hpp

140 lines
3.0 KiB
C++
Raw Normal View History

2018-07-09 17:32:11 +00:00
#ifndef LLARP_SERVICE_INFO_HPP
#define LLARP_SERVICE_INFO_HPP
#include <llarp/bencode.hpp>
#include <llarp/crypto.hpp>
#include <llarp/service/types.hpp>
namespace llarp
{
namespace service
{
struct ServiceInfo : public llarp::IBEncodeMessage
{
2018-08-10 21:34:11 +00:00
private:
2018-07-09 17:32:11 +00:00
llarp::PubKey enckey;
llarp::PubKey signkey;
VanityNonce vanity;
2018-08-10 21:34:11 +00:00
public:
ServiceInfo() = default;
ServiceInfo(const ServiceInfo&& other)
{
enckey = std::move(other.enckey);
signkey = std::move(other.signkey);
version = std::move(other.version);
vanity = std::move(other.vanity);
m_CachedAddr = std::move(other.m_CachedAddr);
}
ServiceInfo(const ServiceInfo& other)
{
enckey = other.enckey;
signkey = other.signkey;
version = other.version;
vanity = other.vanity;
m_CachedAddr = other.m_CachedAddr;
}
void
RandomizeVanity()
{
vanity.Randomize();
}
bool
Verify(llarp_crypto* crypto, llarp_buffer_t payload,
const Signature& sig) const
{
return crypto->verify(signkey, payload, sig);
}
byte_t*
EncryptionPublicKey()
{
return enckey;
}
2018-07-09 17:32:11 +00:00
2018-08-10 21:34:11 +00:00
bool
Update(const byte_t* enc, const byte_t* sign)
{
enckey = enc;
signkey = sign;
return UpdateAddr();
}
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-09 17:32:11 +00:00
ServiceInfo&
operator=(const ServiceInfo& other)
{
enckey = other.enckey;
signkey = other.signkey;
version = other.version;
vanity = other.vanity;
2018-07-18 03:10:21 +00:00
UpdateAddr();
2018-07-09 17:32:11 +00:00
return *this;
};
2018-07-18 03:10:21 +00:00
bool
operator<(const ServiceInfo& other) const
{
return Addr() < other.Addr();
}
2018-07-09 17:32:11 +00:00
friend std::ostream&
operator<<(std::ostream& out, const ServiceInfo& i)
{
return out << "[e=" << i.enckey << " s=" << i.signkey
<< " v=" << i.version << " x=" << i.vanity << "]";
}
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
{
return m_CachedAddr;
}
2018-07-09 17:32:11 +00:00
/// calculate our address
bool
CalculateAddress(byte_t* buf) const;
2018-07-09 17:32:11 +00:00
2018-07-19 04:58:39 +00:00
bool
BDecode(llarp_buffer_t* buf)
{
2018-08-10 21:34:11 +00:00
if(IBEncodeMessage::BDecode(buf))
return CalculateAddress(m_CachedAddr.data());
return false;
2018-07-19 04:58:39 +00:00
}
2018-07-09 17:32:11 +00:00
bool
BEncode(llarp_buffer_t* buf) const;
bool
DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf);
2018-07-18 03:10:21 +00:00
private:
Address m_CachedAddr;
2018-07-09 17:32:11 +00:00
};
} // namespace service
} // namespace llarp
#endif