// Copyright 2019-2024 The Hush developers // Released under the GPLv3 #include "connection.h" #include "mainwindow.h" #include "settings.h" #include "ui_connection.h" #include "firsttimewizard.h" #include "ui_createhushconfdialog.h" #include "controller.h" #include "../lib/silentdragonlitelib.h" #include "precompiled.h" #include #include "sdl.h" using json = nlohmann::json; #ifdef Q_OS_WIN auto dirwalletconnection = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath("silentdragonlite/silentdragonlite-wallet.dat"); #endif #ifdef Q_OS_MACOS auto dirwalletconnection = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath("silentdragonlite/silentdragonlite-wallet.dat"); #endif #ifdef Q_OS_LINUX auto dirwalletconnection = QDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)).filePath(".silentdragonlite/silentdragonlite-wallet.dat"); #endif ConnectionLoader::ConnectionLoader(MainWindow* main, Controller* rpc) { this->main = main; this->rpc = rpc; d = new QDialog(main); connD = new Ui_ConnectionDialog(); connD->setupUi(d); auto theme = Settings::getInstance()->get_theme_name(); //DEBUG("theme " << theme << " has loaded"); auto size = QSize(512,512); if (theme == "Dark" || theme == "Midnight") { QMovie *movie2 = new QMovie(":/img/res/silentdragonlite-animated-startup-dark.gif");; movie2->setScaledSize(size); qDebug() << "Animation dark loaded"; connD->topIcon->setMovie(movie2); movie2->start(); } else { QMovie *movie1 = new QMovie(":/img/res/silentdragonlite-animated-startup-dark.gif");; movie1->setScaledSize(size); qDebug() << "Animation light loaded"; connD->topIcon->setMovie(movie1); movie1->start(); } main->logger->write("Set animation"); qDebug() << "Set animation"; isSyncing = new QAtomicInteger(); } ConnectionLoader::~ConnectionLoader() { // DEBUG("destroying ConnectionLoader"); delete isSyncing; delete connD; delete d; } void ConnectionLoader::loadConnection() { DEBUG("calling doAutoConnect"); QTimer::singleShot(1, [=]() { this->doAutoConnect(); }); if (!Settings::getInstance()->isHeadless()) d->exec(); } void ConnectionLoader::loadProgress() { bool failed = false; QTimer::singleShot(1, [=]() mutable { DEBUG("failed=" << failed); // continually retry ShowProgress() until it succeeds // by running without an exception do { try { this->ShowProgress(); failed = false; } catch (const std::exception& e) { DEBUG("caught exception " << e.what() ); failed = true; } } while (failed); }); if (!Settings::getInstance()->isHeadless()) d->exec(); } void ConnectionLoader::ShowProgress() { qDebug() << __func__; auto config = std::shared_ptr(new ConnectionConfig()); config->dangerous = false; config->server = Settings::getInstance()->getSettings().server; DEBUG("Creating connection with server=" << config->server); auto connection = makeConnection(config); if (!connection) { DEBUG("Failed to create connection"); return; } auto me = this; isSyncing = new QAtomicInteger(true); DEBUG("isSyncing set to true"); // Do a sync after import syncTimer = new QTimer(main); DEBUG("Created syncTimer"); connection->doRPC("sync", "", [=](auto) { qDebug()<< "Finished syncing"; isSyncing->store(false); syncTimer->deleteLater(); this->doRPCSetConnectionShield(connection); }, [=](auto) { DEBUG("sync rpc error! server=" << config->server); }); QObject::connect(syncTimer, &QTimer::timeout, [=]() { if (!isSyncing || !isSyncing->load()) { DEBUG("Syncing complete or isSyncing is null, stopping timer"); syncTimer->stop(); return; } DEBUG("Checking sync status"); try { connection->doRPC("syncstatus", "", [=](json reply) { if (isSyncing && reply.find("synced_blocks") != reply.end()) { qint64 synced = reply["synced_blocks"].get(); qint64 total = reply["total_blocks"].get(); DEBUG("Sync status: " << synced << " / " << total); me->showInformation( "Syncing... " + QString::number(synced) + " / " + QString::number(total) ); } }, [=](QString err) { DEBUG("Sync status error: " << err); config->server = Settings::getRandomServer(); DEBUG("Changed server to " << config->server); }); } catch (const std::exception& e) { DEBUG("Exception caught in syncstatus: " << e.what()); throw; } }); int interval = 1 * 1000; syncTimer->setInterval(interval); syncTimer->start(); DEBUG("Sync timer started with interval=" << interval); } void ConnectionLoader::doAutoConnect() { auto config = std::shared_ptr(new ConnectionConfig()); config->dangerous = false; config->server = Settings::getInstance()->getSettings().server; DEBUG("Creating connection with server=" << config->server); // Initialize the library DEBUG("Attempting to initialize library with " << config->server); // Check to see if there's an existing wallet if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) { DEBUG("using existing wallet"); main->logger->write(QObject::tr("Using existing wallet.")); QString response = ""; try { char* resp = litelib_initialize_existing( config->dangerous, config->server.toStdString().c_str() ); response = litelib_process_response(resp); } catch (const std::exception& e) { DEBUG("caught an exception, ignoring: " << e.what()); } if (response.toUpper().trimmed() != "OK") { config->server = Settings::getRandomServer(); try { char* resp = litelib_initialize_existing( config->dangerous, config->server.toStdString().c_str() ); response = litelib_process_response(resp); } catch (const std::exception& e) { DEBUG("caught an exception, ignoring: " << e.what()); } if (response.toUpper().trimmed() != "OK") { QString resp = "Error when connecting to " + config->server + ": " + response; showError(resp); return; } else { DEBUG("Successfully connected to random server: " << config->server << " !!!"); } } else { DEBUG("Successfully connected to " << config->server << " !!!"); } } else { DEBUG("no existing wallet"); main->logger->write(QObject::tr("Create/restore wallet.")); createOrRestore(config->dangerous, config->server); d->show(); } auto connection = makeConnection(config); auto me = this; qDebug() << __func__ << ": server=" << config->server << " connection=" << connection << " me=" << me << endl; // After the lib is initialized, try to do get info connection->doRPC("info", "", [=](auto reply) { DEBUG("Connection is online."); connection->setInfo(reply); DEBUG("getting Connection reply"); isSyncing = new QAtomicInteger(); isSyncing->store(true); DEBUG("isSyncing set to true"); // Do a sync at startup syncTimer = new QTimer(main); DEBUG("Beginning sync at startup"); connection->doRPC("sync", "", [=](auto) { qDebug()<<"finished syncing startup"; isSyncing->store(false); // Cancel the timer syncTimer->deleteLater(); // When sync is done, set the connection this->doRPCSetConnection(connection); }, [=](auto) mutable { DEBUG("sync rpc error! server=" << config->server); // Attempt to retry sync RPC with a delay QTimer::singleShot(5000, [=]() { // 5-second delay connection->doRPC("sync", "", [=](auto) mutable { qDebug()<<"sync success with server=" << config->server; isSyncing->store(false); // Cancel the timer syncTimer->deleteLater(); // When sync is done, set the connection this->doRPCSetConnection(connection); }, [=](auto) { DEBUG("sync failed with server=" << config->server << " . retrying after delay"); }); }); }); // While it is syncing, we'll show the status updates while it is alive. QObject::connect(syncTimer, &QTimer::timeout, [=]() { DEBUG("Check the sync status"); if (isSyncing != nullptr && isSyncing->load()) { DEBUG("Getting the sync status"); try { connection->doRPC("syncstatus", "", [=](json reply) { if (isSyncing != nullptr && reply.find("synced_blocks") != reply.end()) { qint64 synced = reply["synced_blocks"].get(); qint64 total = reply["total_blocks"].get(); me->showInformation( "Syncing... " + QString::number(synced) + " / " + QString::number(total) ); } }, [=](QString err) { DEBUG("syncstatus error" << err); }); } catch (const std::exception& e) { DEBUG("caught exception from syncstatus: " << e.what()); } } }); int interval = 1*1000; syncTimer->setInterval(interval); syncTimer->start(); DEBUG("Start sync timer with interval=" << interval); }, [=](QString err) { showError(err); }); } void ConnectionLoader::createOrRestore(bool dangerous, QString server) { qDebug() << __func__ << ": server=" << server; // Close the startup dialog, since we'll be showing the wizard d->hide(); // Create a wizard FirstTimeWizard wizard(dangerous,server); DEBUG("Start new Wallet with FirstimeWizard"); wizard.exec(); } void ConnectionLoader::doRPCSetConnection(Connection* conn) { DEBUG("Connectionloader finished, setting connection"); main->logger->write("Connectionloader finished, setting connection"); rpc->setConnection(conn); d->accept(); QTimer::singleShot(1, [=]() { delete this; }); QFile plaintextWallet(dirwalletconnection); try { main->logger->write("Path to Wallet.dat : " ); qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); } catch (const std::exception& e) { DEBUG("Caught exception" << e.what() ); DEBUG("No plaintext wallet found! file=" << plaintextWallet); main->logger->write("no Plaintext wallet.dat"); } } void ConnectionLoader::doRPCSetConnectionShield(Connection* conn) { DEBUG("Importing finished, setting connection"); rpc->setConnection(conn); d->accept(); main->getRPC()->shield([=] (auto) {}); QTimer::singleShot(1, [=]() { delete this; }); QFile plaintextWallet(dirwalletconnection); try { main->logger->write("Path to Wallet.dat : " ); qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); } catch (const std::exception& e) { DEBUG("Caught exception" << e.what() ); main->logger->write("no Plaintext wallet.dat"); DEBUG("No plaintext wallet found! file=" << plaintextWallet); } } Connection* ConnectionLoader::makeConnection(std::shared_ptr config) { qDebug() << __func__; return new Connection(main, config); } // Update the UI with the status void ConnectionLoader::showInformation(QString info, QString detail) { connD->status->setText(info); connD->statusDetail->setText(detail); } /** * Show error will close the loading dialog and show an error. */ void ConnectionLoader::showError(QString explanation) { rpc->noConnection(); QMessageBox::critical( main, QObject::tr("Connection Error"), explanation, QMessageBox::Ok ); d->close(); } QString litelib_process_response(char* resp) { //qDebug() << __func__ << ": " << resp; char* resp_copy = new char[strlen(resp) + 1]; //a safer version of strcpy strncpy(resp_copy, resp, strlen(resp)+1); litelib_rust_free_string(resp); QString reply = QString::fromStdString(resp_copy); memset(resp_copy, '-', strlen(resp_copy)); delete[] resp_copy; return reply; } /*********************************************************************************** * Connection, Executor and Callback Class ************************************************************************************/ void Executor::run() { auto config = std::shared_ptr(new ConnectionConfig()); //DEBUG("cmd=" << cmd << " args=" << args << " server=" << config->server); QString response = ""; try { char* resp = litelib_execute(this->cmd.toStdString().c_str(), this->args.toStdString().c_str()); response = litelib_process_response(resp); } catch (const std::exception& e) { DEBUG("ignoring exception: " << e.what() ); } //TODO: we can do stricter error checking if (response.isEmpty()) { config->server = Settings::getRandomServer(); try { char* resp = litelib_initialize_existing( config->dangerous, config->server.toStdString().c_str() ); response = litelib_process_response(resp); resp = litelib_execute(this->cmd.toStdString().c_str(), this->args.toStdString().c_str()); response = litelib_process_response(resp); } catch (const std::exception& e) { DEBUG("server= " << config->server << " gave exception: " << e.what() ); emit handleError(response); } } try { auto parsed = json::parse( response.toStdString().c_str(), nullptr, false ); if (parsed.is_discarded() || parsed.is_null()) { emit handleError(response); } else { emit responseReady(parsed); } } catch (const std::exception& e) { DEBUG("exception when parsing json: " << e.what() ); emit handleError(response); } } void Callback::processRPCCallback(json resp) { this->cb(resp); // Destroy self delete this; } void Callback::processError(QString resp) { this->errCb(resp); // Destroy self delete this; } Connection::Connection(MainWindow* m, std::shared_ptr conf) { this->config = conf; this->main = m; // qDebug() << __func__; // Register the JSON type as a type that can be passed between signals and slots. qRegisterMetaType("json"); } void Connection::doRPC(const QString cmd, const QString args, const std::function& cb, const std::function& errCb) { if (shutdownInProgress) { DEBUG("Ignoring RPC because shutdown in progress"); return; } //DEBUG("cmd=" << cmd << " args=" << args); // Create a runner. auto runner = new Executor(cmd, args); // Callback object. Will delete itself auto c = new Callback(cb, errCb); QObject::connect(runner, &Executor::responseReady, c, &Callback::processRPCCallback); QObject::connect(runner, &Executor::handleError, c, &Callback::processError); QThreadPool::globalInstance()->start(runner); } void Connection::doRPCWithDefaultErrorHandling(const QString cmd, const QString args, const std::function& cb) { //DEBUG("cmd=" << cmd << " args=" << args); doRPC(cmd, args, cb, [=] (QString err) { this->showTxError(err); }); } void Connection::doRPCIgnoreError(const QString cmd, const QString args, const std::function& cb) { // DEBUG("cmd=" << cmd << " args=" << args); doRPC(cmd, args, cb, [=] (auto) { // Ignored error handling }); } void Connection::showTxError(const QString& error) { // qDebug() << __func__ << ": " << error; if (error.isNull()) return; // Prevent multiple dialog boxes from showing, because they're all called async static bool shown = false; if (shown) return; shown = true; QMessageBox::critical( main, QObject::tr("Transaction Error"), QObject::tr("There was an error sending the transaction. The error was:") + "\n\n" + error, QMessageBox::StandardButton::Ok ); shown = false; } /** * Prevent all future calls from going through */ void Connection::shutdown() { DEBUG("shutting down"); shutdownInProgress = true; }