You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/service/info.cpp

99 lines
2.5 KiB
C++

#include <service/info.hpp>
#include <crypto/crypto.hpp>
#include <service/address.hpp>
#include <util/buffer.hpp>
6 years ago
#include <cassert>
6 years ago
#include <sodium/crypto_generichash.h>
6 years ago
namespace llarp
{
namespace service
{
bool
ServiceInfo::Verify(Crypto* crypto, const llarp_buffer_t& payload,
const Signature& sig) const
{
return crypto->verify(signkey, payload, sig);
}
6 years ago
bool
ServiceInfo::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
6 years ago
{
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;
}
bool
ServiceInfo::BEncode(llarp_buffer_t* buf) const
{
if(!bencode_start_dict(buf))
return false;
if(!BEncodeWriteDictEntry("e", enckey, buf))
return false;
if(!BEncodeWriteDictEntry("s", signkey, buf))
return false;
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
6 years ago
return false;
if(!vanity.IsZero())
{
if(!BEncodeWriteDictEntry("x", vanity, buf))
return false;
}
return bencode_end(buf);
}
std::string
ServiceInfo::Name() const
{
6 years ago
if(m_CachedAddr.IsZero())
{
Address addr;
CalculateAddress(addr.as_array());
6 years ago
return addr.ToString();
}
return m_CachedAddr.ToString();
6 years ago
}
bool ServiceInfo::CalculateAddress(std::array< byte_t, 32 >& data) const
6 years ago
{
std::array< byte_t, 256 > tmp;
llarp_buffer_t buf(tmp);
if(!BEncode(&buf))
return false;
return crypto_generichash_blake2b(data.data(), data.size(), buf.base,
buf.cur - buf.base, nullptr, 0)
6 years ago
!= -1;
}
6 years ago
bool
ServiceInfo::UpdateAddr()
{
return CalculateAddress(m_CachedAddr.as_array());
6 years ago
}
std::ostream&
ServiceInfo::print(std::ostream& stream, int level, int spaces) const
{
Printer printer(stream, level, spaces);
printer.printAttribute("e", enckey);
printer.printAttribute("s", signkey);
printer.printAttribute("v", version);
printer.printAttribute("x", vanity);
return stream;
}
6 years ago
} // namespace service
} // namespace llarp