2022-07-04 21:14:13 +00:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-13 16:45:18 +00:00
|
|
|
#include "Accounts/accounts_types.h"
|
2022-10-19 13:41:53 +00:00
|
|
|
#include "Types.h"
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
#include <QJsonArray>
|
2022-07-04 21:14:13 +00:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
namespace Status::StatusGo::Utils
|
|
|
|
{
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
namespace Param
|
|
|
|
{
|
|
|
|
static constexpr auto Id{"id"};
|
|
|
|
static constexpr auto JsonRpc{"jsonrpc"};
|
|
|
|
static constexpr auto Result{"result"};
|
|
|
|
static constexpr auto Error{"error"};
|
|
|
|
static constexpr auto ErrorMessage{"message"};
|
|
|
|
static constexpr auto ErrorCode{"code"};
|
|
|
|
} // namespace Param
|
|
|
|
|
|
|
|
template <class T>
|
2022-07-04 21:14:13 +00:00
|
|
|
QByteArray jsonToByteArray(const T& json)
|
|
|
|
{
|
2022-10-19 13:41:53 +00:00
|
|
|
static_assert(std::is_same_v<T, QJsonObject> || std::is_same_v<T, QJsonArray>,
|
|
|
|
"Wrong Json type. Supported: Object, Array");
|
2022-07-04 21:14:13 +00:00
|
|
|
return QJsonDocument(json).toJson(QJsonDocument::Compact);
|
|
|
|
}
|
|
|
|
|
2022-07-13 16:45:18 +00:00
|
|
|
QJsonArray toJsonArray(const std::vector<Accounts::DerivationPath>& value);
|
2022-07-04 21:14:13 +00:00
|
|
|
|
|
|
|
/// Check if json contains a standard status-go error and
|
|
|
|
std::optional<RpcError> getRPCErrorInJson(const QJsonObject& json);
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
template <class T>
|
2022-07-04 21:14:13 +00:00
|
|
|
bool checkReceivedResponse(const QString& response, T& json)
|
|
|
|
{
|
|
|
|
QJsonParseError error;
|
|
|
|
auto jsonDocument = QJsonDocument::fromJson(response.toUtf8(), &error);
|
2022-10-19 13:41:53 +00:00
|
|
|
if(error.error != QJsonParseError::NoError) return false;
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
if constexpr(std::is_same_v<T, QJsonObject>)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
json = jsonDocument.object();
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-19 13:41:53 +00:00
|
|
|
else if constexpr(std::is_same_v<T, QJsonArray>)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
json = jsonDocument.array();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Clarify scope. The assumption done here are valid only for status-go::CallPrivateRPC API.
|
2022-10-19 13:41:53 +00:00
|
|
|
template <class T>
|
2022-07-04 21:14:13 +00:00
|
|
|
RpcResponse<T> buildPrivateRPCResponse(const T& json)
|
|
|
|
{
|
|
|
|
auto response = RpcResponse<T>(T());
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
if constexpr(std::is_same_v<T, QJsonObject>)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
2022-10-19 13:41:53 +00:00
|
|
|
if(!json[Param::Id].isNull() && !json[Param::Id].isUndefined()) response.id = json[Param::Id].toInt();
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
if(!json[Param::JsonRpc].isNull() && !json[Param::JsonRpc].isUndefined())
|
|
|
|
response.jsonRpcVersion = json[Param::JsonRpc].toString();
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
response.error = getRPCErrorInJson(json).value_or(RpcError());
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
if(!json[Param::Result].isNull() && !json[Param::Result].isUndefined())
|
|
|
|
response.result = json[Param::Result].toObject();
|
2022-07-04 21:14:13 +00:00
|
|
|
}
|
2022-10-19 13:41:53 +00:00
|
|
|
else if constexpr(std::is_same_v<T, QJsonArray>)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
response.result = json;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2022-07-07 18:16:59 +00:00
|
|
|
const char* statusGoCallPrivateRPC(const char* inputJSON);
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
template <class T>
|
2022-07-04 21:14:13 +00:00
|
|
|
RpcResponse<T> callPrivateRpc(const QByteArray& payload)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-07-07 18:16:59 +00:00
|
|
|
auto result = statusGoCallPrivateRPC(payload.data());
|
2022-07-04 21:14:13 +00:00
|
|
|
T jsonResult;
|
|
|
|
if(!Utils::checkReceivedResponse(result, jsonResult))
|
|
|
|
{
|
|
|
|
auto msg = QObject::tr("parsing response failed");
|
|
|
|
throw std::domain_error(msg.toStdString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Utils::buildPrivateRPCResponse(jsonResult);
|
|
|
|
}
|
2022-10-19 13:41:53 +00:00
|
|
|
catch(std::exception& e)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
auto response = RpcResponse<T>(T());
|
|
|
|
response.error.message = QObject::tr("an error executing rpc call occurred, msg: %1").arg(e.what());
|
|
|
|
return response;
|
|
|
|
}
|
2022-10-19 13:41:53 +00:00
|
|
|
catch(...)
|
2022-07-04 21:14:13 +00:00
|
|
|
{
|
|
|
|
auto response = RpcResponse<T>(T());
|
|
|
|
response.error.message = QObject::tr("an error executing rpc call");
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
HashedPassword hashPassword(const QString& str);
|
2022-07-04 21:14:13 +00:00
|
|
|
|
2022-10-19 13:41:53 +00:00
|
|
|
} // namespace Status::StatusGo::Utils
|