2021-03-09 22:24:35 +00:00
|
|
|
#include "info.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include "address.hpp"
|
2018-12-12 02:15:08 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace llarp::service
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
2023-09-27 14:09:48 +00:00
|
|
|
ServiceInfo::verify(uint8_t* buf, size_t size, const Signature& sig) const
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2023-10-19 23:09:22 +00:00
|
|
|
return crypto::verify(signkey, buf, size, sig);
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2019-01-13 16:30:07 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
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
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
vanity = *nonce;
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return UpdateAddr();
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
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
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
if (not vanity.IsZero())
|
|
|
|
btdp.append("x", vanity.ToView());
|
|
|
|
}
|
2018-07-16 03:32:13 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
ServiceInfo::Name() const
|
|
|
|
{
|
|
|
|
if (m_CachedAddr.IsZero())
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
Address addr;
|
|
|
|
CalculateAddress(addr.as_array());
|
|
|
|
return addr.ToString();
|
2018-07-16 03:32:13 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return m_CachedAddr.ToString();
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
2023-08-31 16:28:02 +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
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
ServiceInfo::UpdateAddr()
|
|
|
|
{
|
|
|
|
if (m_CachedAddr.IsZero())
|
2022-07-16 00:41:14 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return CalculateAddress(m_CachedAddr.as_array());
|
2022-07-16 00:41:14 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServiceInfo::ToString() const
|
|
|
|
{
|
|
|
|
return fmt::format("[ServiceInfo e={} s={} v={} x={}]", enckey, signkey, version, vanity);
|
|
|
|
}
|
2022-07-16 00:41:14 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
} // namespace llarp::service
|