2023-10-04 13:25:25 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2023-10-06 15:34:50 +00:00
|
|
|
/*
|
|
|
|
TODO:
|
2023-10-10 20:49:53 +00:00
|
|
|
- change these parameters to ustringviews and ustrings where needed after bumping oxenc
|
2023-10-25 19:43:32 +00:00
|
|
|
- change std::string sig(64, '\0') --> std::array<unsigned char, 64> sig
|
2023-10-06 15:34:50 +00:00
|
|
|
*/
|
|
|
|
|
2023-10-16 16:55:51 +00:00
|
|
|
namespace ObtainExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
{
|
|
|
|
inline auto EXCEPTION = "EXCEPTION"sv;
|
|
|
|
|
2023-10-06 15:34:50 +00:00
|
|
|
// flag: 0 = Exit, 1 = Snode
|
|
|
|
inline std::string
|
2023-10-16 16:55:51 +00:00
|
|
|
sign_and_serialize(SecretKey sk, uint64_t flag, std::string tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
|
|
|
|
{
|
|
|
|
auto btdp = btlp.append_dict();
|
|
|
|
|
|
|
|
btdp.append("E", flag);
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (not crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: ObtainExitMessage failed to sign and serialize contents!"};
|
|
|
|
}
|
|
|
|
|
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2023-10-16 13:39:57 +00:00
|
|
|
sign_and_serialize_response(SecretKey sk, std::string_view tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
std::string nonce(16, '\0');
|
|
|
|
randombytes(reinterpret_cast<uint8_t*>(nonce.data()), 16);
|
|
|
|
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
|
|
|
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
btdp.append("Y", nonce);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: ObtainExitMessage response failed to sign and serialize contents!"};
|
|
|
|
}
|
|
|
|
|
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
|
|
|
|
2023-10-16 16:55:51 +00:00
|
|
|
} // namespace ObtainExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
|
2023-10-16 16:55:51 +00:00
|
|
|
namespace UpdateExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
{
|
|
|
|
inline auto EXCEPTION = "EXCEPTION"sv;
|
2023-10-06 15:34:50 +00:00
|
|
|
inline auto UPDATE_FAILED = "EXIT UPDATE FAILED"sv;
|
|
|
|
|
|
|
|
inline std::string
|
2023-10-16 13:39:57 +00:00
|
|
|
sign_and_serialize(SecretKey sk, std::string path_id, std::string tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
|
|
|
|
{
|
|
|
|
auto btdp = btlp.append_dict();
|
|
|
|
|
|
|
|
btdp.append("P", path_id);
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (not crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: UpdateExitMessage failed to sign and serialize contents!"};
|
|
|
|
}
|
2023-10-04 13:25:25 +00:00
|
|
|
|
2023-10-06 15:34:50 +00:00
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2023-10-16 13:39:57 +00:00
|
|
|
sign_and_serialize_response(SecretKey sk, std::string_view tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
std::string nonce(16, '\0');
|
|
|
|
randombytes(reinterpret_cast<uint8_t*>(nonce.data()), 16);
|
|
|
|
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
|
|
|
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
btdp.append("Y", nonce);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: UpdateExitMessage response failed to sign and serialize contents!"};
|
|
|
|
}
|
|
|
|
|
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
2023-10-16 16:55:51 +00:00
|
|
|
} // namespace UpdateExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
|
2023-10-16 16:55:51 +00:00
|
|
|
namespace CloseExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
{
|
|
|
|
inline auto EXCEPTION = "EXCEPTION"sv;
|
2023-10-06 15:34:50 +00:00
|
|
|
inline auto UPDATE_FAILED = "CLOSE EXIT FAILED"sv;
|
|
|
|
|
|
|
|
inline std::string
|
2023-10-16 13:39:57 +00:00
|
|
|
sign_and_serialize(SecretKey sk, std::string tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
std::string nonce(16, '\0');
|
|
|
|
randombytes(reinterpret_cast<uint8_t*>(nonce.data()), 16);
|
|
|
|
|
|
|
|
{
|
|
|
|
auto btdp = btlp.append_dict();
|
|
|
|
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
btdp.append("Y", nonce);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (not crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: CloseExitMessage failed to sign and serialize contents!"};
|
|
|
|
}
|
|
|
|
|
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string
|
2023-10-16 13:39:57 +00:00
|
|
|
sign_and_serialize_response(SecretKey sk, std::string_view tx_id)
|
2023-10-06 15:34:50 +00:00
|
|
|
{
|
|
|
|
oxenc::bt_list_producer btlp;
|
|
|
|
std::string sig(64, '\0');
|
|
|
|
std::string nonce(16, '\0');
|
|
|
|
randombytes(reinterpret_cast<uint8_t*>(nonce.data()), 16);
|
|
|
|
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
|
|
|
|
|
|
|
btdp.append("T", tx_id);
|
|
|
|
btdp.append("Y", nonce);
|
|
|
|
|
2023-10-23 22:58:58 +00:00
|
|
|
if (crypto::sign(reinterpret_cast<uint8_t*>(sig.data()), sk, to_usv(btdp.view())))
|
2023-10-06 15:34:50 +00:00
|
|
|
throw std::runtime_error{
|
|
|
|
"Error: CloseExitMessage response failed to sign and serialize contents!"};
|
|
|
|
}
|
2023-10-04 13:25:25 +00:00
|
|
|
|
2023-10-06 15:34:50 +00:00
|
|
|
btlp.append(sig.data());
|
|
|
|
return std::move(btlp).str();
|
|
|
|
}
|
2023-10-16 16:55:51 +00:00
|
|
|
} // namespace CloseExitMessage
|
2023-10-04 13:25:25 +00:00
|
|
|
|
|
|
|
} // namespace llarp
|