lokinet/llarp/service/info.cpp

104 lines
2.2 KiB
C++
Raw Normal View History

#include "info.hpp"
2023-10-19 21:59:57 +00:00
#include "address.hpp"
2018-12-12 02:15:08 +00:00
#include <llarp/crypto/crypto.hpp>
namespace llarp::service
2018-07-16 03:32:13 +00:00
{
bool
ServiceInfo::verify(uint8_t* buf, size_t size, const Signature& sig) const
2018-07-16 03:32:13 +00:00
{
return crypto::verify(signkey, buf, size, sig);
}
bool
ServiceInfo::Update(
const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce)
{
signkey = sign;
enckey = enc;
if (nonce)
2020-01-27 21:30:41 +00:00
{
vanity = *nonce;
2020-01-27 21:30:41 +00:00
}
return UpdateAddr();
}
2020-01-27 21:30:41 +00:00
bool
ServiceInfo::decode_key(const llarp_buffer_t& key, llarp_buffer_t* val)
{
bool read = false;
if (!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))
return false;
if (!BEncodeMaybeReadDictEntry("s", signkey, read, key, val))
return false;
if (!BEncodeMaybeReadDictInt("v", version, read, key, val))
return false;
if (!BEncodeMaybeReadDictEntry("x", vanity, read, key, val))
return false;
return read;
}
2018-07-16 03:32:13 +00:00
2023-10-03 20:00:23 +00:00
void
ServiceInfo::bt_decode(oxenc::bt_dict_consumer& btdc)
{
try
{
enckey.FromString(btdc.require<std::string>("e"));
signkey.FromString(btdc.require<std::string>("s"));
vanity.from_string(btdc.require<std::string>("x"));
}
catch (...)
{
log::critical(info_cat, "ServiceInfo failed to populate with bt encoded contents");
}
}
void
ServiceInfo::bt_encode(oxenc::bt_dict_producer& btdp) const
{
btdp.append("e", enckey.ToView());
btdp.append("s", signkey.ToView());
2018-07-16 03:32:13 +00:00
if (not vanity.IsZero())
btdp.append("x", vanity.ToView());
}
2018-07-16 03:32:13 +00:00
std::string
ServiceInfo::Name() const
{
if (m_CachedAddr.IsZero())
2018-07-16 03:32:13 +00:00
{
Address addr;
CalculateAddress(addr.as_array());
return addr.ToString();
2018-07-16 03:32:13 +00:00
}
return m_CachedAddr.ToString();
}
2018-07-18 03:10:21 +00:00
bool
ServiceInfo::CalculateAddress(std::array<byte_t, 32>& data) const
{
data = signkey.as_array();
return true;
}
2018-07-18 03:10:21 +00:00
bool
ServiceInfo::UpdateAddr()
{
if (m_CachedAddr.IsZero())
{
return CalculateAddress(m_CachedAddr.as_array());
}
return true;
}
std::string
ServiceInfo::ToString() const
{
return fmt::format("[ServiceInfo e={} s={} v={} x={}]", enckey, signkey, version, vanity);
}
} // namespace llarp::service