1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/service/info.cpp

87 lines
2.1 KiB
C++
Raw Normal View History

2018-12-12 03:15:08 +01:00
#include <service/Info.hpp>
#include <crypto/crypto.hpp>
2018-12-12 03:15:08 +01:00
#include <service/address.hpp>
#include <util/buffer.hpp>
2018-12-12 03:15:08 +01:00
2018-07-16 05:32:13 +02:00
#include <cassert>
2018-10-23 14:40:34 +02:00
#include <sodium/crypto_generichash.h>
2018-12-12 03:15:08 +01:00
2018-07-16 05:32:13 +02:00
namespace llarp
{
namespace service
{
bool
ServiceInfo::Verify(llarp::Crypto* crypto, const llarp_buffer_t& payload,
const Signature& sig) const
{
return crypto->verify(signkey, payload, sig);
}
2018-07-16 05:32:13 +02:00
bool
ServiceInfo::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
2018-07-16 05:32:13 +02:00
{
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))
2018-07-16 05:32:13 +02:00
return false;
if(!vanity.IsZero())
{
if(!BEncodeWriteDictEntry("x", vanity, buf))
return false;
}
return bencode_end(buf);
}
std::string
ServiceInfo::Name() const
{
2018-07-18 05:10:21 +02:00
if(m_CachedAddr.IsZero())
{
Address addr;
CalculateAddress(addr.as_array());
2018-07-18 05:10:21 +02:00
return addr.ToString();
}
return m_CachedAddr.ToString();
2018-07-16 05:32:13 +02:00
}
bool ServiceInfo::CalculateAddress(std::array< byte_t, 32 >& data) const
2018-07-16 05:32:13 +02:00
{
2019-02-03 00:12:42 +01:00
std::array< byte_t, 256 > tmp;
llarp_buffer_t buf(tmp);
2018-08-10 23:34:11 +02:00
if(!BEncode(&buf))
return false;
return crypto_generichash_blake2b(data.data(), data.size(), buf.base,
buf.cur - buf.base, nullptr, 0)
2018-07-16 05:32:13 +02:00
!= -1;
}
2018-07-18 05:10:21 +02:00
bool
ServiceInfo::UpdateAddr()
{
return CalculateAddress(m_CachedAddr.as_array());
2018-07-18 05:10:21 +02:00
}
2018-07-16 05:32:13 +02:00
} // namespace service
2018-10-23 13:29:37 +02:00
} // namespace llarp