diff --git a/src/wallet/api/wallet.cpp b/src/wallet/api/wallet.cpp index 37ff1b979..18d035849 100644 --- a/src/wallet/api/wallet.cpp +++ b/src/wallet/api/wallet.cpp @@ -3621,6 +3621,74 @@ void WalletImpl::setAllowMismatchedDaemonVersion(bool allow_mismatch) m_wallet->allow_mismatched_daemon_version(allow_mismatch); } //------------------------------------------------------------------------------------------------------------------- +bool WalletImpl::setDaemon(const std::string &daemon_address, + const std::string &daemon_username /* = "" */, + const std::string &daemon_password /* = "" */, + bool trusted_daemon /* = true */, + const std::string &ssl_support /* = "autodetect" */, + const std::string &ssl_private_key_path /* = "" */, + const std::string &ssl_certificate_path /* = "" */, + const std::string &ssl_ca_file_path /* = "" */, + const std::vector &ssl_allowed_fingerprints_str /* = {} */, + bool ssl_allow_any_cert /* = false */, + const std::string &proxy /* = "" */) +{ + clearStatus(); + + // SSL allowed fingerprints + std::vector> ssl_allowed_fingerprints; + ssl_allowed_fingerprints.reserve(ssl_allowed_fingerprints_str.size()); + for (const std::string &fp: ssl_allowed_fingerprints_str) + { + ssl_allowed_fingerprints.push_back({}); + std::vector &v = ssl_allowed_fingerprints.back(); + for (auto c: fp) + v.push_back(c); + } + + // SSL options + epee::net_utils::ssl_options_t ssl_options = epee::net_utils::ssl_support_t::e_ssl_support_enabled; + if (ssl_allow_any_cert) + ssl_options.verification = epee::net_utils::ssl_verification_t::none; + else if (!ssl_allowed_fingerprints.empty() || !ssl_ca_file_path.empty()) + ssl_options = epee::net_utils::ssl_options_t{std::move(ssl_allowed_fingerprints), std::move(ssl_ca_file_path)}; + + if (!epee::net_utils::ssl_support_from_string(ssl_options.support, ssl_support)) + { + setStatusError(string(tr("Invalid ssl support option (allowed options:`disabled` | `enabled` | `autodetect`), actual value: ")) + ssl_support); + return false; + } + + ssl_options.auth = epee::net_utils::ssl_authentication_t{ + std::move(ssl_private_key_path), std::move(ssl_certificate_path) + }; + + const bool verification_required = + ssl_options.verification != epee::net_utils::ssl_verification_t::none && + ssl_options.support == epee::net_utils::ssl_support_t::e_ssl_support_enabled; + + if (verification_required && !ssl_options.has_strong_verification(boost::string_ref{})) + { + setStatusError(string(tr("SSL is enabled but no user certificate or fingerprints were provided"))); + return false; + } + + // daemon login + if(daemon_username != "") + m_daemon_login.emplace(daemon_username, daemon_password); + + // set daemon + try + { + return m_wallet->set_daemon(daemon_address, m_daemon_login, trusted_daemon, ssl_options, proxy); + } + catch (const std::exception &e) + { + setStatusError(string(tr("Failed to set daemon: ")) + e.what()); + } + return false; +} +//------------------------------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------- // PRIVATE diff --git a/src/wallet/api/wallet.h b/src/wallet/api/wallet.h index 6ce575f89..85e9579d9 100644 --- a/src/wallet/api/wallet.h +++ b/src/wallet/api/wallet.h @@ -280,6 +280,7 @@ public: bool importKeyImages(std::vector key_images, std::size_t offset = 0, std::unordered_set selected_enotes_indices = {}) override; bool getAllowMismatchedDaemonVersion() const override; void setAllowMismatchedDaemonVersion(bool allow_mismatch) override; + bool setDaemon(const std::string &daemon_address, const std::string &daemon_username = "", const std::string &daemon_password = "", bool trusted_daemon = true, const std::string &ssl_support = "autodetect", const std::string &ssl_private_key_path = "", const std::string &ssl_certificate_path = "", const std::string &ssl_ca_file_path = "", const std::vector &ssl_allowed_fingerprints_str = {}, bool ssl_allow_any_cert = false, const std::string &proxy = "") override; private: void clearStatus() const; diff --git a/src/wallet/api/wallet2_api.h b/src/wallet/api/wallet2_api.h index 928e76d23..1747aaed1 100644 --- a/src/wallet/api/wallet2_api.h +++ b/src/wallet/api/wallet2_api.h @@ -1541,6 +1541,33 @@ struct Wallet * param: allow_mismatch - */ virtual void setAllowMismatchedDaemonVersion(bool allow_mismatch) = 0; + /** + * brief: setDaemon - + * param: daemon_address - + * param: daemon_username - for daemon login (default: empty string) + * param: daemon_password - for daemon login (default: empty string) + * param: trusted_daemon - (default: true) + * param: ssl_support - "disabled" | "enabled" | "autodetect" (default: "autodetect") + * param: ssl_private_key_path - (default: empty string) + * param: ssl_certificate_path - (default: empty string) + * param: ssl_ca_file_path - (default: empty string) + * param: ssl_allowed_fingerprints_str - (default: empty vector) + * param: ssl_allow_any_cert - (default: false) + * param: proxy - (default: empty string) + * return: true if succeeded + * note: sets status error on fail + */ + virtual bool setDaemon(const std::string &daemon_address, + const std::string &daemon_username = "", + const std::string &daemon_password = "", + bool trusted_daemon = true, + const std::string &ssl_support = "autodetect", + const std::string &ssl_private_key_path = "", + const std::string &ssl_certificate_path = "", + const std::string &ssl_ca_file_path = "", + const std::vector &ssl_allowed_fingerprints_str = {}, + bool ssl_allow_any_cert = false, + const std::string &proxy = "") = 0; }; /** diff --git a/src/wallet/api/wallet_manager.cpp b/src/wallet/api/wallet_manager.cpp index 94886b727..97895990b 100644 --- a/src/wallet/api/wallet_manager.cpp +++ b/src/wallet/api/wallet_manager.cpp @@ -231,7 +231,6 @@ std::string WalletManagerImpl::errorString() const return m_errorString; } -// TODO : figure out if we rather modify this to take daemon_login, trusted_daemon and ssl_options as arguments, so this function acts as a replacement for `wallet2::set_daemon()` or should we add a new `setDaemon()` to `WalletImpl` void WalletManagerImpl::setDaemonAddress(const std::string &address) { m_http_client.set_server(address, boost::none);