2022-02-14 14:38:41 +00:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
#include "backend/types.h"
|
|
|
|
#include "backend/utils.h"
|
2022-02-21 18:03:38 +00:00
|
|
|
#include "backend/wallet_accounts.h"
|
2022-02-14 14:38:41 +00:00
|
|
|
#include "libstatus.h"
|
|
|
|
|
2022-02-16 21:51:04 +00:00
|
|
|
namespace Backend::Wallet::Accounts
|
2022-02-14 14:38:41 +00:00
|
|
|
{
|
|
|
|
RpcResponse<QJsonArray> getAccounts()
|
|
|
|
{
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_getAccounts"}, {"params", QJsonValue()}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
2022-02-21 17:07:16 +00:00
|
|
|
auto obj = QJsonDocument::fromJson(result).object();
|
|
|
|
Backend::Utils::throwOnError(obj);
|
2022-02-22 08:02:34 +00:00
|
|
|
return {result, obj["result"].toArray(), RpcError(-1, QJsonDocument::fromJson(result)["error"].toString())};
|
2022-02-21 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RpcResponse<QString> generateNewAccount(const QString& password, const QString& accountName, const QString& color)
|
|
|
|
{
|
|
|
|
QString hashedPassword(Backend::Utils::hashString(password));
|
|
|
|
QJsonArray payload = {hashedPassword, accountName, color};
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_generateAccount"}, {"params", payload}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
|
|
|
auto response = QJsonDocument::fromJson(result);
|
2022-02-22 08:02:34 +00:00
|
|
|
return RpcResponse<QString>(
|
|
|
|
result,
|
|
|
|
response["result"].toString(),
|
|
|
|
RpcError(response["error"]["code"].toDouble(), response["error"]["message"].toString()));
|
2022-02-21 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:02:34 +00:00
|
|
|
RpcResponse<QString> addAccountsFromPrivateKey(const QString& privateKey,
|
|
|
|
const QString& password,
|
|
|
|
const QString& accountName,
|
|
|
|
const QString& color)
|
2022-02-21 17:07:16 +00:00
|
|
|
{
|
|
|
|
QString hashedPassword(Backend::Utils::hashString(password));
|
|
|
|
QJsonArray payload = {privateKey, hashedPassword, accountName, color};
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_addAccountWithMnemonic"}, {"params", payload}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
|
|
|
auto response = QJsonDocument::fromJson(result);
|
2022-02-22 08:02:34 +00:00
|
|
|
return RpcResponse<QString>(
|
|
|
|
result,
|
|
|
|
response["result"].toString(),
|
|
|
|
RpcError(response["error"]["code"].toDouble(), response["error"]["message"].toString()));
|
2022-02-21 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:02:34 +00:00
|
|
|
RpcResponse<QString> addAccountsFromSeed(const QString& seedPhrase,
|
|
|
|
const QString& password,
|
|
|
|
const QString& accountName,
|
|
|
|
const QString& color)
|
2022-02-21 17:07:16 +00:00
|
|
|
{
|
|
|
|
QString hashedPassword(Backend::Utils::hashString(password));
|
|
|
|
QJsonArray payload = {seedPhrase, hashedPassword, accountName, color};
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_addAccountWithPrivateKey"}, {"params", payload}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
|
|
|
auto response = QJsonDocument::fromJson(result);
|
2022-02-22 08:02:34 +00:00
|
|
|
return RpcResponse<QString>(
|
|
|
|
result,
|
|
|
|
response["result"].toString(),
|
|
|
|
RpcError(response["error"]["code"].toDouble(), response["error"]["message"].toString()));
|
2022-02-21 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 08:02:34 +00:00
|
|
|
RpcResponse<QString> addWatchOnlyAccount(const QString& address, const QString& accountName, const QString& color)
|
2022-02-21 17:07:16 +00:00
|
|
|
{
|
|
|
|
QJsonArray payload = {address, accountName, color};
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_addAccountWatch"}, {"params", payload}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
|
|
|
auto response = QJsonDocument::fromJson(result);
|
2022-02-22 08:02:34 +00:00
|
|
|
return RpcResponse<QString>(
|
|
|
|
result,
|
|
|
|
response["result"].toString(),
|
|
|
|
RpcError(response["error"]["code"].toDouble(), response["error"]["message"].toString()));
|
2022-02-21 17:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RpcResponse<QString> deleteAccount(const QString& address)
|
|
|
|
{
|
|
|
|
QJsonArray payload = {address};
|
|
|
|
QJsonObject inputJSON{{"jsonrpc", "2.0"}, {"method", "accounts_deleteAccount"}, {"params", payload}};
|
|
|
|
auto result = CallPrivateRPC(Utils::jsonToStr(inputJSON).toUtf8().data());
|
|
|
|
auto response = QJsonDocument::fromJson(result);
|
2022-02-22 08:02:34 +00:00
|
|
|
return RpcResponse<QString>(
|
|
|
|
result,
|
|
|
|
response["result"].toString(),
|
|
|
|
RpcError(response["error"]["code"].toDouble(), response["error"]["message"].toString()));
|
2022-02-14 14:38:41 +00:00
|
|
|
}
|
2022-02-16 21:51:04 +00:00
|
|
|
|
|
|
|
} // namespace Backend::Wallet::Accounts
|