diff --git a/src/cpp/common/py_monero_common.cpp b/src/cpp/common/py_monero_common.cpp index 72506f7..2e89814 100644 --- a/src/cpp/common/py_monero_common.cpp +++ b/src/cpp/common/py_monero_common.cpp @@ -1,6 +1,14 @@ #include "py_monero_common.h" #include "utils/monero_utils.h" +boost::property_tree::ptree json_to_property_node(const std::string& json) { + // deserialize json to property node + std::istringstream iss = json.empty() ? std::istringstream() : std::istringstream(json); + boost::property_tree::ptree node; + boost::property_tree::read_json(iss, node); + return node; +} + PyThreadPoller::~PyThreadPoller() { set_is_polling(false); } @@ -237,11 +245,8 @@ boost::optional PyMoneroJsonResponse::get_result() const { } std::shared_ptr PyMoneroJsonResponse::deserialize(const std::string& response_json) { - // deserialize json to property node - std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json); - boost::property_tree::ptree node; - boost::property_tree::read_json(iss, node); - + // parse json to property node + boost::property_tree::ptree node = json_to_property_node(response_json); auto response = std::make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { @@ -283,12 +288,9 @@ boost::optional PyMoneroPathResponse::get_response() const { } std::shared_ptr PyMoneroPathResponse::deserialize(const std::string& response_json) { - // deserialize json to property node - std::istringstream iss = response_json.empty() ? std::istringstream() : std::istringstream(response_json); - boost::property_tree::ptree node; - boost::property_tree::read_json(iss, node); + // parse json to property node auto response = std::make_shared(); - response->m_response = node; + response->m_response = json_to_property_node(response_json); return response; } diff --git a/src/cpp/daemon/py_monero_daemon_model.cpp b/src/cpp/daemon/py_monero_daemon_model.cpp index 056a972..9237a5e 100644 --- a/src/cpp/daemon/py_monero_daemon_model.cpp +++ b/src/cpp/daemon/py_monero_daemon_model.cpp @@ -6,7 +6,7 @@ PyMoneroDownloadUpdateParams::PyMoneroDownloadUpdateParams(const std::string& co } PyMoneroStartMiningParams::PyMoneroStartMiningParams(const std::string& address, int num_threads, bool is_background, bool ignore_battery): - m_address(address), + m_miner_address(address), m_num_threads(num_threads), m_is_background(is_background), m_ignore_battery(ignore_battery) { @@ -389,35 +389,23 @@ void PyMoneroTx::from_property_tree(const boost::property_tree::ptree& node, con void PyMoneroTx::from_property_tree(const boost::property_tree::ptree& node, std::vector>& txs) { for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { std::string key = it->first; + bool pool_txs = key == std::string("transactions"); - if (key == std::string("transactions")) { - auto node2 = it->second; - - for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) { - auto node3 = it2->second; - auto tx = std::make_shared(); - tx->m_is_confirmed = false; - tx->m_is_miner_tx = false; - tx->m_in_tx_pool = true; - tx->m_num_confirmations = 0; - from_property_tree(node3, tx); - txs.push_back(tx); - } - - return; - } - else if (key == std::string("txs")) { + if (pool_txs || key == std::string("txs")) { auto node2 = it->second; for(boost::property_tree::ptree::const_iterator it2 = node2.begin(); it2 != node2.end(); ++it2) { auto node3 = it2->second; auto tx = std::make_shared(); tx->m_is_miner_tx = false; + if (pool_txs) { + tx->m_is_confirmed = false; + tx->m_in_tx_pool = true; + tx->m_num_confirmations = 0; + } from_property_tree(node3, tx); txs.push_back(tx); } - - return; } } } @@ -851,7 +839,7 @@ rapidjson::Value PyMoneroStartMiningParams::to_rapidjson_val(rapidjson::Document rapidjson::Value root(rapidjson::kObjectType); rapidjson::Value value_str(rapidjson::kStringType); rapidjson::Value value_num(rapidjson::kNumberType); - if (m_address != boost::none) monero_utils::add_json_member("miner_address", m_address.get(), allocator, root, value_str); + if (m_miner_address != boost::none) monero_utils::add_json_member("miner_address", m_miner_address.get(), allocator, root, value_str); if (m_num_threads != boost::none) monero_utils::add_json_member("threads_count", m_num_threads.get(), allocator, root, value_num); if (m_is_background != boost::none) monero_utils::add_json_member("do_background_mining", m_is_background.get(), allocator, root); if (m_ignore_battery != boost::none) monero_utils::add_json_member("ignore_battery", m_ignore_battery.get(), allocator, root); diff --git a/src/cpp/daemon/py_monero_daemon_model.h b/src/cpp/daemon/py_monero_daemon_model.h index a41481d..f4da2ac 100644 --- a/src/cpp/daemon/py_monero_daemon_model.h +++ b/src/cpp/daemon/py_monero_daemon_model.h @@ -52,7 +52,7 @@ class PyMoneroDownloadUpdateParams : public PyMoneroRequestParams { class PyMoneroStartMiningParams : public PyMoneroJsonRequestParams { public: - boost::optional m_address; + boost::optional m_miner_address; boost::optional m_num_threads; boost::optional m_is_background; boost::optional m_ignore_battery; diff --git a/src/cpp/py_monero.cpp b/src/cpp/py_monero.cpp index d76df89..dfaea82 100644 --- a/src/cpp/py_monero.cpp +++ b/src/cpp/py_monero.cpp @@ -1137,47 +1137,17 @@ PYBIND11_MODULE(monero, m) { .def_property("transfer_query", [](const monero::monero_tx_query& self) { return self.m_transfer_query; }, [](std::shared_ptr& self, const boost::optional>& val) { - const auto old_query = self->m_transfer_query; - self->m_transfer_query = val; - if (self->m_transfer_query != boost::none) { - self->m_transfer_query.get()->m_tx_query = self; - } - if (old_query != boost::none) { - if (val != boost::none && val.get() == old_query.get()) { - return; - } - old_query.get()->m_tx_query = boost::none; - } + set_query(self, self->m_transfer_query, val); }) .def_property("input_query", [](const monero::monero_tx_query& self) { return self.m_input_query; }, [](std::shared_ptr& self, const boost::optional>& val) { - const auto old_query = self->m_input_query; - self->m_input_query = val; - if (self->m_input_query != boost::none) { - self->m_input_query.get()->m_tx_query = self; - } - if (old_query != boost::none) { - if (val != boost::none && val.get() == old_query.get()) { - return; - } - old_query.get()->m_tx_query = boost::none; - } + set_query(self, self->m_input_query, val); }) .def_property("output_query", [](const monero::monero_tx_query& self) { return self.m_output_query; }, [](std::shared_ptr& self, const boost::optional>& val) { - const auto old_query = self->m_output_query; - self->m_output_query = val; - if (self->m_output_query != boost::none) { - self->m_output_query.get()->m_tx_query = self; - } - if (old_query != boost::none) { - if (val != boost::none && val.get() == old_query.get()) { - return; - } - old_query.get()->m_tx_query = boost::none; - } + set_query(self, self->m_output_query, val); }) .def("copy", [](const std::shared_ptr& self) { auto tgt = std::make_shared(); @@ -1774,34 +1744,10 @@ PYBIND11_MODULE(monero, m) { MONERO_CATCH_AND_RETHROW(self.get_txs()); }) .def("get_txs", [](PyMoneroWallet& self, const monero::monero_tx_query& query) { - try { - auto txs = self.get_txs(query); - PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes); - return txs; - } catch (const PyMoneroRpcError& e) { - throw; - } catch (const PyMoneroError& e) { - throw; - } - catch (const std::exception& e) { - throw PyMoneroError(e.what()); - } + MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, query)); }, py::arg("query")) .def("get_txs", [](PyMoneroWallet& self, const std::vector& tx_hashes) { - try { - monero_tx_query query; - query.m_hashes = tx_hashes; - auto txs = self.get_txs(query); - PyMoneroUtils::sort_txs_wallet(txs, query.m_hashes); - return txs; - } catch (const PyMoneroRpcError& e) { - throw; - } catch (const PyMoneroError& e) { - throw; - } - catch (const std::exception& e) { - throw PyMoneroError(e.what()); - } + MONERO_CATCH_AND_RETHROW(PyMoneroUtils::get_and_sort_txs(self, tx_hashes)); }, py::arg("tx_hashes")) .def("get_transfers", [](PyMoneroWallet& self, const monero::monero_transfer_query& query) { MONERO_CATCH_AND_RETHROW(self.get_transfers(query)); @@ -2130,49 +2076,13 @@ PYBIND11_MODULE(monero, m) { .def(py::init&>(), py::arg("rpc_connection")) .def(py::init(), py::arg("uri") = "", py::arg("username") = "", py::arg("password") = "", py::arg("proxy_uri") = "", py::arg("zmq_uri") = "", py::arg("timeout") = 20000) .def("create_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr& config) { - try { - self.create_wallet(config); - return &self; - } - catch(const PyMoneroRpcError& ex) { - throw; - } - catch(const PyMoneroError& ex) { - throw; - } - catch(const std::exception& ex) { - throw PyMoneroError(ex.what()); - } + MONERO_CATCH_AND_RETHROW(self.create_wallet(config)); }, py::arg("config")) .def("open_wallet", [](PyMoneroWalletRpc& self, const std::shared_ptr& config) { - try { - self.open_wallet(config); - return &self; - } - catch(const PyMoneroRpcError& ex) { - throw; - } - catch(const PyMoneroError& ex) { - throw; - } - catch(const std::exception& ex) { - throw PyMoneroError(ex.what()); - } + MONERO_CATCH_AND_RETHROW(self.open_wallet(config)); }, py::arg("config")) .def("open_wallet", [](PyMoneroWalletRpc& self, const std::string& name, const std::string& password) { - try { - self.open_wallet(name, password); - return &self; - } - catch(const PyMoneroRpcError& ex) { - throw; - } - catch(const PyMoneroError& ex) { - throw; - } - catch(const std::exception& ex) { - throw PyMoneroError(ex.what()); - } + MONERO_CATCH_AND_RETHROW(self.open_wallet(name, password)); }, py::arg("name"), py::arg("password")) .def("is_closed", [](const PyMoneroWalletRpc& self) { MONERO_CATCH_AND_RETHROW(self.is_closed()); diff --git a/src/cpp/utils/py_monero_utils.cpp b/src/cpp/utils/py_monero_utils.cpp index 76f01c2..d72aea8 100644 --- a/src/cpp/utils/py_monero_utils.cpp +++ b/src/cpp/utils/py_monero_utils.cpp @@ -308,3 +308,16 @@ void PyMoneroUtils::sort_txs_wallet(std::vector> PyMoneroUtils::get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query) { + auto txs = wallet.get_txs(tx_query); + sort_txs_wallet(txs, tx_query.m_hashes); + return txs; +} + +std::vector> PyMoneroUtils::get_and_sort_txs(const monero::monero_wallet& wallet, const std::vector& tx_hashes) { + monero_tx_query tx_query; + tx_query.m_hashes = tx_hashes; + return get_and_sort_txs(wallet, tx_query); +} + diff --git a/src/cpp/utils/py_monero_utils.h b/src/cpp/utils/py_monero_utils.h index f0c7648..62a5c8a 100644 --- a/src/cpp/utils/py_monero_utils.h +++ b/src/cpp/utils/py_monero_utils.h @@ -2,6 +2,7 @@ #include "common/py_monero_common.h" #include "utils/monero_utils.h" +#include "wallet/monero_wallet.h" class PyMoneroUtils { @@ -45,6 +46,8 @@ class PyMoneroUtils { static double atomic_units_to_xmr(uint64_t amount_atomic_units); static void sort_txs_wallet(std::vector>& txs, const std::vector& hashes); + static std::vector> get_and_sort_txs(const monero::monero_wallet& wallet, const std::vector& tx_hashes); + static std::vector> get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query); private: diff --git a/src/cpp/wallet/py_monero_wallet.h b/src/cpp/wallet/py_monero_wallet.h index d2ad893..85b90e9 100644 --- a/src/cpp/wallet/py_monero_wallet.h +++ b/src/cpp/wallet/py_monero_wallet.h @@ -5,6 +5,9 @@ #include "py_monero_wallet_model.h" #include "wallet/monero_wallet.h" +// TODO sorting is really needed? +std::vector> get_and_sort_txs(const monero::monero_wallet& wallet, const monero::monero_tx_query& tx_query); + class PyMoneroWalletConnectionManagerListener : public PyMoneroConnectionManagerListener { public: PyMoneroWalletConnectionManagerListener(monero::monero_wallet* wallet); diff --git a/src/cpp/wallet/py_monero_wallet_model.cpp b/src/cpp/wallet/py_monero_wallet_model.cpp index eeee49b..a6ded16 100644 --- a/src/cpp/wallet/py_monero_wallet_model.cpp +++ b/src/cpp/wallet/py_monero_wallet_model.cpp @@ -169,14 +169,6 @@ PyMoneroGetBalanceParams::PyMoneroGetBalanceParams(uint32_t account_idx, boost:: if (address_idx != boost::none) m_address_indices.push_back(address_idx.get()); } -PyMoneroGetBalanceResponse::PyMoneroGetBalanceResponse(uint64_t balance, uint64_t unlocked_balance, bool multisig_import_needed, uint64_t time_to_unlock, uint64_t blocks_to_unlock): - m_balance(balance), - m_unlocked_balance(unlocked_balance), - m_multisig_import_needed(multisig_import_needed), - m_time_to_unlock(time_to_unlock), - m_blocks_to_unlock(blocks_to_unlock) { -} - PyMoneroAddressBookEntryParams::PyMoneroAddressBookEntryParams(uint64_t index, bool set_address, const std::string& address, bool set_description, const std::string& description): m_index(index), m_set_address(set_address), @@ -204,19 +196,13 @@ PyMoneroCheckTxKeyParams::PyMoneroCheckTxKeyParams(const std::string &tx_hash, c m_address(address) { } -PyMoneroSetSubaddressLabelParams::PyMoneroSetSubaddressLabelParams(uint32_t account_idx, uint32_t subaddress_idx, const std::string& label): - m_account_index(account_idx), - m_subaddress_index(subaddress_idx), - m_label(label) { -} - PyMoneroImportExportKeyImagesParams::PyMoneroImportExportKeyImagesParams(const std::vector> &key_images) { for(const auto &key_image : key_images) { m_key_images.push_back(std::make_shared(*key_image)); } } -PyMoneroGetPaymentUriParams::PyMoneroGetPaymentUriParams(const monero_tx_config& config): +PyMoneroGetParsePaymentUri::PyMoneroGetParsePaymentUri(const monero_tx_config& config): m_recipient_name(config.m_recipient_name), m_tx_description(config.m_note), m_payment_id(config.m_payment_id) { @@ -353,19 +339,6 @@ std::shared_ptr PyMoneroTxQuery::decontextualize(const std::sha return query; } -monero::monero_tx_query PyMoneroTxQuery::decontextualize(monero::monero_tx_query &query) { - query.m_is_incoming = boost::none; - query.m_is_outgoing = boost::none; - query.m_transfer_query = boost::none; - query.m_input_query = boost::none; - query.m_output_query = boost::none; - return query; -} - -bool PyMoneroOutputQuery::is_contextual(const std::shared_ptr &query) { - if (query == nullptr) return false; - return is_contextual(*query); -} bool PyMoneroOutputQuery::is_contextual(const monero::monero_output_query &query) { if (query.m_tx_query == boost::none) return false; @@ -375,17 +348,15 @@ bool PyMoneroOutputQuery::is_contextual(const monero::monero_output_query &query return false; } -bool PyMoneroTransferQuery::is_contextual(const std::shared_ptr &query) { - if (query == nullptr) return false; - return is_contextual(*query); -} - bool PyMoneroTransferQuery::is_contextual(const monero::monero_transfer_query &query) { if (query.m_tx_query == boost::none) return false; - if (query.m_tx_query.get()->m_is_incoming != boost::none) return true; // requires context of all transfers + // requires context of all transfers + if (query.m_tx_query.get()->m_is_incoming != boost::none) return true; if (query.m_tx_query.get()->m_is_outgoing != boost::none) return true; - if (query.m_tx_query.get()->m_input_query != boost::none) return true; // requires context of inputs - if (query.m_tx_query.get()->m_output_query != boost::none) return true; // requires context of inputs + // requires context of inputs + if (query.m_tx_query.get()->m_input_query != boost::none) return true; + // requires context of inputs + if (query.m_tx_query.get()->m_output_query != boost::none) return true; return false; } @@ -1270,11 +1241,11 @@ uint64_t PyMoneroWalletGetHeightResponse::from_property_tree(const boost::proper throw std::runtime_error("Invalid get_height response"); } -void PyMoneroParsePaymentUriResponse::from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response) { +void PyMoneroGetParsePaymentUri::from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response) { for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { std::string key = it->first; if (key == std::string("uri")) { - PyMoneroParsePaymentUriResponse::from_property_tree(it->second, response); + PyMoneroGetParsePaymentUri::from_property_tree(it->second, response); return; } if (key == std::string("address") && !it->second.data().empty()) response->m_address = it->second.data(); @@ -1371,7 +1342,7 @@ std::string PyMoneroPrepareMakeMultisigResponse::from_property_tree(const boost: throw std::runtime_error("Invalid prepare multisig response"); } -std::string PyMoneroGetPaymentUriResponse::from_property_tree(const boost::property_tree::ptree& node) { +std::string PyMoneroGetParsePaymentUri::from_property_tree(const boost::property_tree::ptree& node) { for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { std::string key = it->first; if (key == std::string("uri")) return it->second.data(); @@ -1379,7 +1350,7 @@ std::string PyMoneroGetPaymentUriResponse::from_property_tree(const boost::prope throw std::runtime_error("Invalid make uri response"); } -void PyMoneroWalletAttributeParams::from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& attributes) { +void PyMoneroKeyValue::from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& attributes) { attributes->m_key = boost::none; attributes->m_value = boost::none; @@ -1556,7 +1527,7 @@ rapidjson::Value PyMoneroParsePaymentUriParams::to_rapidjson_val(rapidjson::Docu return root; } -rapidjson::Value PyMoneroGetPaymentUriParams::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { +rapidjson::Value PyMoneroGetParsePaymentUri::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { rapidjson::Value root(rapidjson::kObjectType); rapidjson::Value value_str(rapidjson::kStringType); rapidjson::Value value_num(rapidjson::kNumberType); @@ -1599,7 +1570,7 @@ rapidjson::Value PyMoneroChangeWalletPasswordParams::to_rapidjson_val(rapidjson: return root; } -rapidjson::Value PyMoneroWalletAttributeParams::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { +rapidjson::Value PyMoneroKeyValue::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { rapidjson::Value root(rapidjson::kObjectType); rapidjson::Value value_str(rapidjson::kStringType); if (m_key != boost::none) monero_utils::add_json_member("key", m_key.get(), allocator, root, value_str); @@ -1734,16 +1705,7 @@ rapidjson::Value PyMoneroSubmitTransferParams::to_rapidjson_val(rapidjson::Docum return root; } -rapidjson::Value PyMoneroCreateSubaddressParams::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { - rapidjson::Value root(rapidjson::kObjectType); - rapidjson::Value val_str(rapidjson::kStringType); - rapidjson::Value val_num(rapidjson::kNumberType); - if (m_label != boost::none) monero_utils::add_json_member("label", m_label.get(), allocator, root, val_str); - if (m_account_index != boost::none) monero_utils::add_json_member("account_index", m_account_index.get(), allocator, root, val_num); - return root; -} - -rapidjson::Value PyMoneroSetSubaddressLabelParams::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { +rapidjson::Value PyMoneroCreateEditSubaddressParams::to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const { rapidjson::Value root(rapidjson::kObjectType); rapidjson::Value val_str(rapidjson::kStringType); rapidjson::Value val_num(rapidjson::kNumberType); @@ -1754,6 +1716,7 @@ rapidjson::Value PyMoneroSetSubaddressLabelParams::to_rapidjson_val(rapidjson::D monero_utils::add_json_member("minor", m_subaddress_index.get(), allocator, index, val_num); root.AddMember("index", index, allocator); } + else if (m_account_index != boost::none) monero_utils::add_json_member("account_index", m_account_index.get(), allocator, root, val_num); return root; } @@ -1878,7 +1841,7 @@ rapidjson::Value PyMoneroGetTransfersParams::to_rapidjson_val(rapidjson::Documen return root; } -std::shared_ptr PyMoneroParsePaymentUriResponse::to_tx_config() const { +std::shared_ptr PyMoneroGetParsePaymentUri::to_tx_config() const { auto tx_config = std::make_shared(); tx_config->m_payment_id = m_payment_id; tx_config->m_recipient_name = m_recipient_name; diff --git a/src/cpp/wallet/py_monero_wallet_model.h b/src/cpp/wallet/py_monero_wallet_model.h index 6292824..2a3adb7 100644 --- a/src/cpp/wallet/py_monero_wallet_model.h +++ b/src/cpp/wallet/py_monero_wallet_model.h @@ -3,6 +3,26 @@ #include "daemon/py_monero_daemon_model.h" #include "wallet/monero_wallet_model.h" +template +void set_query(std::shared_ptr& self, boost::optional>& field, const boost::optional>& val) { + + const auto old = field; + field = val; + + // set new reference + if (field != boost::none) { + field.get()->m_tx_query = self; + } + + // cleanup old + if (old != boost::none) { + if (val != boost::none && val.get() == old.get()) { + return; + } + old.get()->m_tx_query = boost::none; + } +} + enum PyMoneroAddressType : uint8_t { PRIMARY_ADDRESS = 0, INTEGRATED_ADDRESS, @@ -29,20 +49,17 @@ class PyMoneroTxQuery : public monero::monero_tx_query { public: static std::shared_ptr decontextualize(const std::shared_ptr &query); - static monero::monero_tx_query decontextualize(monero::monero_tx_query &query); }; class PyMoneroOutputQuery : public monero::monero_output_query { public: - static bool is_contextual(const std::shared_ptr &query); static bool is_contextual(const monero::monero_output_query &query); }; class PyMoneroTransferQuery : public monero::monero_transfer_query { public: - static bool is_contextual(const std::shared_ptr &query); static bool is_contextual(const monero::monero_transfer_query &query); }; @@ -303,20 +320,7 @@ class PyMoneroParsePaymentUriParams : public PyMoneroJsonRequestParams { rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; }; -class PyMoneroParsePaymentUriResponse { -public: - boost::optional m_address; - boost::optional m_amount; - boost::optional m_payment_id; - boost::optional m_recipient_name; - boost::optional m_tx_description; - - std::shared_ptr to_tx_config() const; - static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response); -}; - -// TODO refactory get/parse payment uri? -class PyMoneroGetPaymentUriParams : public PyMoneroJsonRequestParams { +class PyMoneroGetParsePaymentUri : public PyMoneroJsonRequestParams { public: boost::optional m_address; boost::optional m_amount; @@ -324,13 +328,13 @@ class PyMoneroGetPaymentUriParams : public PyMoneroJsonRequestParams { boost::optional m_recipient_name; boost::optional m_tx_description; - PyMoneroGetPaymentUriParams(const monero_tx_config& config); + PyMoneroGetParsePaymentUri() { } + PyMoneroGetParsePaymentUri(const monero_tx_config& config); + std::shared_ptr to_tx_config() const; rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; -}; -class PyMoneroGetPaymentUriResponse { -public: + static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response); static std::string from_property_tree(const boost::property_tree::ptree& node); }; @@ -358,7 +362,6 @@ class PyMoneroGetBalanceResponse { std::vector> m_per_subaddress; PyMoneroGetBalanceResponse(): m_balance(0), m_unlocked_balance(0) { } - PyMoneroGetBalanceResponse(uint64_t balance, uint64_t unlocked_balance, bool multisig_import_needed, uint64_t time_to_unlock, uint64_t blocks_to_unlock); static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response); }; @@ -391,16 +394,17 @@ class PyMoneroChangeWalletPasswordParams : public PyMoneroJsonRequestParams { rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; }; -class PyMoneroWalletAttributeParams : public PyMoneroJsonRequestParams { +class PyMoneroKeyValue : public PyMoneroJsonRequestParams { public: boost::optional m_key; boost::optional m_value; - PyMoneroWalletAttributeParams(const std::string& key): m_key(key) { } - PyMoneroWalletAttributeParams(const std::string& key, const std::string& value): m_key(key), m_value(value) { } + PyMoneroKeyValue() { } + PyMoneroKeyValue(const std::string& key): m_key(key) { } + PyMoneroKeyValue(const std::string& key, const std::string& value): m_key(key), m_value(value) { } rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; - static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& attributes); + static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& attributes); }; class PyMoneroSetDaemonParams : public PyMoneroJsonRequestParams { @@ -562,24 +566,14 @@ class PyMoneroSubmitTransferParams : public PyMoneroJsonRequestParams { rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; }; -class PyMoneroCreateSubaddressParams : public PyMoneroJsonRequestParams { -public: - boost::optional m_label; - boost::optional m_account_index; - boost::optional m_subaddress_index; - - PyMoneroCreateSubaddressParams(uint32_t account_idx, const std::string& label): m_account_index(account_idx), m_label(label) { } - - rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; -}; - -class PyMoneroSetSubaddressLabelParams : public PyMoneroJsonRequestParams { +class PyMoneroCreateEditSubaddressParams : public PyMoneroJsonRequestParams { public: boost::optional m_label; boost::optional m_account_index; boost::optional m_subaddress_index; - PyMoneroSetSubaddressLabelParams(uint32_t account_idx, uint32_t subaddress_idx, const std::string& label); + PyMoneroCreateEditSubaddressParams(uint32_t account_idx, const std::string& label): m_account_index(account_idx), m_label(label) { } + PyMoneroCreateEditSubaddressParams(uint32_t account_idx, uint32_t subaddress_idx, const std::string& label): m_account_index(account_idx), m_subaddress_index(subaddress_idx), m_label(label) { } rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator) const override; }; diff --git a/src/cpp/wallet/py_monero_wallet_rpc.cpp b/src/cpp/wallet/py_monero_wallet_rpc.cpp index 2b42fae..a764266 100644 --- a/src/cpp/wallet/py_monero_wallet_rpc.cpp +++ b/src/cpp/wallet/py_monero_wallet_rpc.cpp @@ -758,7 +758,7 @@ monero_subaddress PyMoneroWalletRpc::get_subaddress(const uint32_t account_idx, } monero_subaddress PyMoneroWalletRpc::create_subaddress(uint32_t account_idx, const std::string& label) { - auto params = std::make_shared(account_idx, label); + auto params = std::make_shared(account_idx, label); auto node = m_rpc->send_json_request("create_address", params); monero_subaddress sub; sub.m_account_index = account_idx; @@ -779,7 +779,7 @@ monero_subaddress PyMoneroWalletRpc::create_subaddress(uint32_t account_idx, con } void PyMoneroWalletRpc::set_subaddress_label(uint32_t account_idx, uint32_t subaddress_idx, const std::string& label) { - auto params = std::make_shared(account_idx, subaddress_idx, label); + auto params = std::make_shared(account_idx, subaddress_idx, label); m_rpc->send_json_request("label_address", params); } @@ -1030,7 +1030,6 @@ std::shared_ptr PyMoneroWalletRpc::sweep_output(const monero_t if (config.m_sweep_each_subaddress != boost::none) throw std::runtime_error("Cannot sweep each subaddress when sweeping single output"); if (config.m_below_amount != boost::none) throw std::runtime_error("Cannot specifiy below_amount when sweeping single output"); if (config.m_can_split != boost::none) throw std::runtime_error("Splitting is not applicable when sweeping output"); - // TODO check first destination address is not boost::none/empty if (destinations.size() != 1) throw std::runtime_error("Must provide exactly one destination address to sweep output to"); if (destinations[0]->m_address == boost::none) throw std::runtime_error("Must specify destination address to sweep to"); if (destinations[0]->m_amount != boost::none) throw std::runtime_error("Cannot specify amount to sweep"); @@ -1352,29 +1351,29 @@ void PyMoneroWalletRpc::set_account_tag_label(const std::string& tag, const std: } std::string PyMoneroWalletRpc::get_payment_uri(const monero_tx_config& config) const { - auto params = std::make_shared(config); + auto params = std::make_shared(config); auto res = m_rpc->send_json_request("make_uri", params); - return PyMoneroGetPaymentUriResponse::from_property_tree(res); + return PyMoneroGetParsePaymentUri::from_property_tree(res); } std::shared_ptr PyMoneroWalletRpc::parse_payment_uri(const std::string& uri) const { auto params = std::make_shared(uri); auto res = m_rpc->send_json_request("parse_uri", params); - auto uri_response = std::make_shared(); - PyMoneroParsePaymentUriResponse::from_property_tree(res, uri_response); + auto uri_response = std::make_shared(); + PyMoneroGetParsePaymentUri::from_property_tree(res, uri_response); return uri_response->to_tx_config(); } void PyMoneroWalletRpc::set_attribute(const std::string& key, const std::string& val) { - auto params = std::make_shared(key, val); + auto params = std::make_shared(key, val); m_rpc->send_json_request("set_attribute", params); } bool PyMoneroWalletRpc::get_attribute(const std::string& key, std::string& value) const { try { - auto params = std::make_shared(key); + auto params = std::make_shared(key); auto res = m_rpc->send_json_request("get_attribute", params); - PyMoneroWalletAttributeParams::from_property_tree(res, params); + PyMoneroKeyValue::from_property_tree(res, params); if (params->m_value == boost::none) return false; value = params->m_value.get(); return true; @@ -1589,13 +1588,10 @@ PyMoneroWalletRpc* PyMoneroWalletRpc::create_wallet_from_keys(const std::shared_ std::string PyMoneroWalletRpc::query_key(const std::string& key_type) const { auto params = std::make_shared(key_type); auto node = m_rpc->send_json_request("query_key", params); - - for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { - std::string key = it->first; - if (key == std::string("key")) return it->second.data(); - } - - throw std::runtime_error(std::string("Cloud not query key: ") + key_type); + auto kv = std::make_shared(); + PyMoneroKeyValue::from_property_tree(node, kv); + if (kv->m_key == boost::none) throw std::runtime_error(std::string("Cloud not query key: ") + key_type); + return *kv->m_key; } std::vector> PyMoneroWalletRpc::sweep_account(const monero_tx_config &conf) { diff --git a/src/python/monero_wallet_rpc.pyi b/src/python/monero_wallet_rpc.pyi index f581dc2..950a669 100644 --- a/src/python/monero_wallet_rpc.pyi +++ b/src/python/monero_wallet_rpc.pyi @@ -31,12 +31,11 @@ class MoneroWalletRpc(MoneroWallet): :param int timeout: Connection timeout. """ ... - def create_wallet(self, config: MoneroWalletConfig) -> MoneroWalletRpc: + def create_wallet(self, config: MoneroWalletConfig) -> None: """ Create and open a wallet on the monero-wallet-rpc server. :param config: configures the wallet to create. - :return MoneroWalletConfig: this wallet client. """ ... def get_rpc_connection(self) -> MoneroRpcConnection | None: @@ -47,22 +46,20 @@ class MoneroWalletRpc(MoneroWallet): """ ... @typing.overload - def open_wallet(self, config: MoneroWalletConfig) -> MoneroWalletRpc: + def open_wallet(self, config: MoneroWalletConfig) -> None: """ Open an existing wallet on the monero-wallet-rpc server. :param MoneroWalletConfig config: configures the wallet to open - :return MoneroWalletRpc: this wallet client """ ... @typing.overload - def open_wallet(self, name: str, password: str) -> MoneroWalletRpc: + def open_wallet(self, name: str, password: str) -> None: """ Open an existing wallet on the monero-wallet-rpc server. :param str name: is the name of the wallet file to open :param str password: is the wallet's password - :return MoneroWalletRpc: this wallet client """ ... def stop(self) -> None: diff --git a/tests/utils/wallet_sync_tester.py b/tests/utils/wallet_sync_tester.py index 2a66c5a..893da19 100644 --- a/tests/utils/wallet_sync_tester.py +++ b/tests/utils/wallet_sync_tester.py @@ -71,50 +71,29 @@ def on_balances_changed(self, new_balance: int, new_unclocked_balance: int) -> N self.prev_balance = new_balance self.prev_unlocked_balance = new_unclocked_balance - @override - def on_output_received(self, output: MoneroOutputWallet) -> None: - assert output is not None - self.prev_output_received = output - - # test output - assert output.amount is not None - assert output.account_index is not None - assert output.account_index >= 0 - assert output.subaddress_index is not None - assert output.subaddress_index >= 0 - - # test output's tx - assert output.tx is not None - assert isinstance(output.tx, MoneroTxWallet) - assert output.tx.hash is not None - assert len(output.tx.hash) == 64 - assert output.tx.version is not None - assert output.tx.version >= 0 - assert output.tx.unlock_time is not None - assert output.tx.unlock_time >= 0 - # TODO this part is failing, maybe for little differences in java data model - #assert len(output.tx.inputs) == 0 - #assert len(output.tx.outputs) == 1 - #assert output.tx.outputs[0] == output - assert len(output.tx.extra) > 0 - - # add incoming amount to running total - if output.tx.is_locked is True: - # TODO: only add if not unlocked, test unlocked received - self.incoming_total += output.amount + # test received/spent output + def test_output(self, output: MoneroOutputWallet, received: bool) -> None: + """ + Test received or spent output. - @override - def on_output_spent(self, output: MoneroOutputWallet) -> None: + :param MoneroOutputWallet output: output to test. + :param bool received: `True` for received output, `False` for spent. + """ assert output is not None - self.prev_output_spent = output + if received: + self.prev_output_received = output + else: + self.prev_output_spent = output # test output assert output.amount is not None assert output.account_index is not None assert output.account_index >= 0 - if output.subaddress_index is not None: - # TODO (monero-project): can be undefined because inputs not - # provided so one created from outgoing transfer + + # TODO (monero-project): if spent, can be undefined because inputs not + # provided so one created from outgoing transfer + if received: + assert output.subaddress_index is not None assert output.subaddress_index >= 0 # test output's tx @@ -127,14 +106,32 @@ def on_output_spent(self, output: MoneroOutputWallet) -> None: assert output.tx.unlock_time is not None assert output.tx.unlock_time >= 0 # TODO this part is failing, maybe for little differences in java data model - #assert len(output.tx.inputs) == 1 - #assert output.tx.inputs[0] == output - #assert len(output.tx.outputs) == 0 + #if received: + #assert len(output.tx.inputs) == 0 + #assert len(output.tx.outputs) == 1 + #assert output.tx.outputs[0] == output + #else: + #assert len(output.tx.inputs) == 1 + #assert output.tx.inputs[0] == output + #assert len(output.tx.outputs) == 0 assert len(output.tx.extra) > 0 - # add outgoing amount to running total if output.tx.is_locked: - self.outgoing_total += output.amount + if received: + # add incoming amount to running total + # TODO: only add if not unlocked, test unlocked received + self.incoming_total += output.amount + else: + # add outgoing amount to running total + self.outgoing_total += output.amount + + @override + def on_output_received(self, output: MoneroOutputWallet) -> None: + self.test_output(output, True) + + @override + def on_output_spent(self, output: MoneroOutputWallet) -> None: + self.test_output(output, False) @override def on_done(self, chain_height: int) -> None: