#include "info.hpp" #include #include "address.hpp" #include #include #include #include namespace llarp::service { bool ServiceInfo::verify(uint8_t* buf, size_t size, const Signature& sig) const { return CryptoManager::instance()->verify(signkey, buf, size, sig); } bool ServiceInfo::Update( const byte_t* sign, const byte_t* enc, const std::optional& nonce) { signkey = sign; enckey = enc; if (nonce) { vanity = *nonce; } return UpdateAddr(); } 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; } void ServiceInfo::bt_decode(oxenc::bt_dict_consumer& btdc) { try { enckey.FromString(btdc.require("e")); signkey.FromString(btdc.require("s")); vanity.from_string(btdc.require("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()); if (not vanity.IsZero()) btdp.append("x", vanity.ToView()); } std::string ServiceInfo::Name() const { if (m_CachedAddr.IsZero()) { Address addr; CalculateAddress(addr.as_array()); return addr.ToString(); } return m_CachedAddr.ToString(); } bool ServiceInfo::CalculateAddress(std::array& data) const { data = signkey.as_array(); return true; } 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