diff --git a/src/daemon/monero_daemon_model.cpp b/src/daemon/monero_daemon_model.cpp index 9c360bd..28047b6 100644 --- a/src/daemon/monero_daemon_model.cpp +++ b/src/daemon/monero_daemon_model.cpp @@ -106,6 +106,23 @@ namespace monero { return root; } + // --------------------------- MONERO RPC VERSION --------------------------- + + rapidjson::Value monero_rpc_connection::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { + + // create root + rapidjson::Value root(rapidjson::kObjectType); + + // set string values + rapidjson::Value value_str(rapidjson::kStringType); + if (m_uri != boost::none) monero_utils::addJsonMember("uri", m_uri.get(), allocator, root, value_str); + if (m_username != boost::none) monero_utils::addJsonMember("username", m_username.get(), allocator, root, value_str); + if (m_password != boost::none) monero_utils::addJsonMember("password", m_password.get(), allocator, root, value_str); + + // return root + return root; + } + // ------------------------- MONERO BLOCK HEADER ---------------------------- rapidjson::Value monero_block_header::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { diff --git a/src/daemon/monero_daemon_model.h b/src/daemon/monero_daemon_model.h index cb83d20..d5bc992 100644 --- a/src/daemon/monero_daemon_model.h +++ b/src/daemon/monero_daemon_model.h @@ -107,14 +107,14 @@ namespace monero { /** * Models a connection to a daemon. - * - * TODO: switch to boost::optional */ - struct monero_rpc_connection { - string m_uri; + struct monero_rpc_connection : public serializable_struct { + boost::optional m_uri; boost::optional m_username; boost::optional m_password; - monero_rpc_connection(const string& uri = "", const boost::optional& username = boost::none, const boost::optional& password = boost::none) : m_uri(uri), m_username(username), m_password(password) {} + + monero_rpc_connection(const boost::optional& uri = boost::none, const boost::optional& username = boost::none, const boost::optional& password = boost::none) : m_uri(uri), m_username(username), m_password(password) {} + rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const; }; // forward declarations diff --git a/src/wallet/monero_wallet.h b/src/wallet/monero_wallet.h index e7a06ca..510151d 100644 --- a/src/wallet/monero_wallet.h +++ b/src/wallet/monero_wallet.h @@ -145,7 +145,7 @@ namespace monero { * * @param connection is the connection to set */ - virtual void set_daemon_connection(const monero_rpc_connection& connection) { + virtual void set_daemon_connection(const boost::optional& connection) { throw runtime_error("set_daemon_connection() not supported"); } @@ -154,7 +154,7 @@ namespace monero { * * @return the wallet's daemon connection */ - virtual shared_ptr get_daemon_connection() const { + virtual boost::optional get_daemon_connection() const { throw runtime_error("get_daemon_connection() not supported"); } diff --git a/src/wallet/monero_wallet_core.cpp b/src/wallet/monero_wallet_core.cpp index 63b4978..553d198 100644 --- a/src/wallet/monero_wallet_core.cpp +++ b/src/wallet/monero_wallet_core.cpp @@ -945,23 +945,24 @@ namespace monero { // try { is_trusted = tools::is_local_address(uri); } // wallet is trusted iff local // catch (const exception &e) { } - // manually set ssl by https prefix because http_client's if (ssl_options) set by wallet2 evaluates to true despite http // TODO - epee::net_utils::ssl_options_t ssl_options = uri.rfind("https", 0) == 0 ? epee::net_utils::ssl_support_t::e_ssl_support_enabled : epee::net_utils::ssl_support_t::e_ssl_support_disabled; + // detect ssl TODO: wallet2 does not detect ssl from uri + epee::net_utils::ssl_support_t ssl = uri.rfind("https", 0) == 0 ? epee::net_utils::ssl_support_t::e_ssl_support_enabled : epee::net_utils::ssl_support_t::e_ssl_support_disabled; - // init wallet2 with daemon connection - if (!m_w2->init(uri, login, {}, 0, is_trusted, ssl_options)) throw runtime_error("Failed to initialize wallet with daemon connection"); + // init wallet2 and set daemon connection + if (!m_w2->init(uri, login, {}, 0, is_trusted, ssl)) throw runtime_error("Failed to initialize wallet with daemon connection"); is_connected(); // update m_is_connected cache // TODO: better naming? } - void monero_wallet_core::set_daemon_connection(const monero_rpc_connection& connection) { - set_daemon_connection(connection.m_uri, connection.m_username == boost::none ? "" : connection.m_username.get(), connection.m_password == boost::none ? "" : connection.m_password.get()); + void monero_wallet_core::set_daemon_connection(const boost::optional& connection) { + if (connection == boost::none) set_daemon_connection(""); + else set_daemon_connection(connection->m_uri == boost::none ? "" : connection->m_uri.get(), connection->m_username == boost::none ? "" : connection->m_username.get(), connection->m_password == boost::none ? "" : connection->m_password.get()); } - shared_ptr monero_wallet_core::get_daemon_connection() const { + boost::optional monero_wallet_core::get_daemon_connection() const { MTRACE("monero_wallet_core::get_daemon_connection()"); - if (m_w2->get_daemon_address().empty()) return nullptr; - shared_ptr connection = make_shared(); - if (!m_w2->get_daemon_address().empty()) connection->m_uri = m_w2->get_daemon_address(); + if (m_w2->get_daemon_address().empty()) return boost::none; + boost::optional connection = monero_rpc_connection(); + connection->m_uri = m_w2->get_daemon_address(); if (m_w2->get_daemon_login()) { if (!m_w2->get_daemon_login()->username.empty()) connection->m_username = m_w2->get_daemon_login()->username; epee::wipeable_string wipeablePassword = m_w2->get_daemon_login()->password; @@ -3233,7 +3234,7 @@ namespace monero { void monero_wallet_core::init_common() { MTRACE("monero_wallet_core.cpp init_common()"); m_w2_listener = unique_ptr(new wallet2_listener(*this, *m_w2)); - if (get_daemon_connection() == nullptr) m_is_connected = false; + if (get_daemon_connection() == boost::none) m_is_connected = false; m_is_synced = false; m_rescan_on_sync = false; m_syncing_enabled = false; diff --git a/src/wallet/monero_wallet_core.h b/src/wallet/monero_wallet_core.h index 7e11339..dfb7200 100644 --- a/src/wallet/monero_wallet_core.h +++ b/src/wallet/monero_wallet_core.h @@ -174,8 +174,8 @@ namespace monero { * Supported wallet methods. */ void set_daemon_connection(const string& uri, const string& username = "", const string& password = ""); - void set_daemon_connection(const monero_rpc_connection& connection); - shared_ptr get_daemon_connection() const; + void set_daemon_connection(const boost::optional& connection); + boost::optional get_daemon_connection() const; bool is_connected() const; bool is_daemon_synced() const; bool is_daemon_trusted() const;