status-desktop/src-cpp/backend/accounts.cpp

101 lines
3.5 KiB
C++
Raw Normal View History

2022-01-06 19:29:19 +00:00
#include "backend/accounts.h"
#include "backend/types.h"
#include "backend/utils.h"
#include "libstatus.h"
2022-01-06 19:29:19 +00:00
#include <QDebug>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QVector>
namespace
{
constexpr auto NumberOfAddressesToGenerate = 5;
constexpr auto MnemonicPhraseLength = 12;
} // namespace
2022-01-06 19:29:19 +00:00
Backend::RpcResponse<QJsonArray> Backend::Accounts::generateAddresses(QVector<QString> paths)
{
QJsonObject payload{{"n", NumberOfAddressesToGenerate},
{"mnemonicPhraseLength", MnemonicPhraseLength},
{"bip32Passphrase", ""},
{"paths", Utils::toJsonArray(paths)}
2022-01-06 19:29:19 +00:00
};
const char* result = MultiAccountGenerateAndDeriveAddresses(Utils::jsonToStr(payload).toUtf8().data());
return {result, QJsonDocument::fromJson(result).array()};
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QString> Backend::Accounts::generateIdenticon(QString publicKey)
{
if(!publicKey.isEmpty())
{
auto identicon = QString(Identicon(publicKey.toUtf8().data()));
return {identicon, identicon};
}
throw Backend::RpcException("publicKey can't be empty1");
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QString> Backend::Accounts::generateAlias(QString publicKey)
{
if(!publicKey.isEmpty())
{
auto alias = QString(GenerateAlias(publicKey.toUtf8().data()));
return {alias, alias};
}
throw Backend::RpcException("publicKey can't be empty2");
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QJsonObject>
Backend::Accounts::storeDerivedAccounts(QString id, QString hashedPassword, QVector<QString> paths)
{
QJsonObject payload{{"accountID", id}, {"paths", Utils::toJsonArray(paths)}, {"password", hashedPassword}};
auto result = MultiAccountStoreDerivedAccounts(Utils::jsonToStr(payload).toUtf8().data());
auto obj = QJsonDocument::fromJson(result).object();
Backend::Utils::throwOnError(obj);
return {result, obj};
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QJsonObject> Backend::Accounts::saveAccountAndLogin(
QString hashedPassword, QJsonObject account, QJsonArray subaccounts, QJsonObject settings, QJsonObject nodeConfig)
2022-01-06 19:29:19 +00:00
{
auto result = SaveAccountAndLogin(Utils::jsonToStr(account).toUtf8().data(),
hashedPassword.toUtf8().data(),
Utils::jsonToStr(settings).toUtf8().data(),
Utils::jsonToStr(nodeConfig).toUtf8().data(),
Utils::jsonToStr(subaccounts).toUtf8().data());
auto obj = QJsonDocument::fromJson(result).object();
Backend::Utils::throwOnError(obj);
return {result, obj};
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QJsonArray> Backend::Accounts::openAccounts(QString path)
{
const char* result = OpenAccounts(path.toUtf8().data());
auto resp = Backend::RpcResponse<QJsonArray>(result, QJsonDocument::fromJson(result).array());
return resp;
2022-01-06 19:29:19 +00:00
}
Backend::RpcResponse<QJsonObject> Backend::Accounts::login(
QString name, QString keyUid, QString hashedPassword, QString identicon, QString thumbnail, QString large)
2022-01-06 19:29:19 +00:00
{
QJsonObject payload{{"name", name}, {"key-uid", keyUid}, {"identityImage", QJsonValue()}, {"identicon", identicon}};
2022-01-06 19:29:19 +00:00
if(!thumbnail.isEmpty() && !large.isEmpty())
{
payload["identityImage"] = QJsonObject{{"thumbnail", thumbnail}, {"large", large}};
}
2022-01-06 19:29:19 +00:00
auto result = Login(Utils::jsonToStr(payload).toUtf8().data(), hashedPassword.toUtf8().data());
auto obj = QJsonDocument::fromJson(result).object();
Backend::Utils::throwOnError(obj);
return {result, obj};
}