From ceea54988a99bc393f27a68e2d8000d65c2b4a5f Mon Sep 17 00:00:00 2001 From: woodser Date: Thu, 28 Nov 2019 21:06:10 -0500 Subject: [PATCH] implement get_address_book_entries() and delete_address_book_entry() --- src/wallet/monero_wallet.cpp | 32 ++++++++++++++++++++++++++++---- src/wallet/monero_wallet.h | 4 ++-- src/wallet/monero_wallet_model.h | 4 +++- todo.txt | 1 + 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/wallet/monero_wallet.cpp b/src/wallet/monero_wallet.cpp index 8608844..ddd052a 100644 --- a/src/wallet/monero_wallet.cpp +++ b/src/wallet/monero_wallet.cpp @@ -2791,19 +2791,43 @@ namespace monero { } vector monero_wallet::get_address_book_entries(const vector& indices) const { - throw runtime_error("get_address_book_entries() not implemented"); + MTRACE("monero_wallet::get_address_book_entries()"); + + // get wallet2 address book entries + const auto w2_entries = m_w2->get_address_book(); + + // build entries from wallet2 types + vector entries; + if (indices.empty()) { + uint64_t idx = 0; + for (const auto &w2_entry: w2_entries) { + monero_address_book_entry entry(idx++, cryptonote::get_account_address_as_str(m_w2->nettype(), w2_entry.m_is_subaddress, w2_entry.m_address), w2_entry.m_description, epee::string_tools::pod_to_hex(w2_entry.m_payment_id)); + entries.push_back(entry); + } + } else { + for (uint64_t idx : indices) { + if (idx >= w2_entries.size()) throw runtime_error("Index out of range: " + std::to_string(idx)); + const auto &w2_entry = w2_entries[idx]; + monero_address_book_entry entry(idx++, cryptonote::get_account_address_as_str(m_w2->nettype(), w2_entry.m_is_subaddress, w2_entry.m_address), w2_entry.m_description, epee::string_tools::pod_to_hex(w2_entry.m_payment_id)); + entries.push_back(entry); + } + } + + return entries; } uint32_t monero_wallet::add_address_book_entry(const string& address, boost::optional description, boost::optional payment_id) { throw runtime_error("add_address_book_entry() not implemented"); } - void monero_wallet::edit_address_book_entry(uint32_t index, bool set_address, boost::optional address, bool set_description, boost::optional description, bool set_payment_id, boost::optional payment_id) { + void monero_wallet::edit_address_book_entry(uint64_t index, bool set_address, boost::optional address, bool set_description, boost::optional description, bool set_payment_id, boost::optional payment_id) { throw runtime_error("edit_address_book_entry() not implemented"); } - void monero_wallet::delete_address_book_entry(uint32_t index) { - throw runtime_error("delete_address_book_entry() not implemented"); + void monero_wallet::delete_address_book_entry(uint64_t index) { + const auto w2_entries = m_w2->get_address_book(); + if (index >= w2_entries.size()) throw runtime_error("Index out of range: " + std::to_string(index)); + if (!m_w2->delete_address_book_row(index)) throw runtime_error("Failed to delete address book entry"); } string monero_wallet::create_payment_uri(const monero_send_request& request) const { diff --git a/src/wallet/monero_wallet.h b/src/wallet/monero_wallet.h index 1eb95fa..983dcb2 100644 --- a/src/wallet/monero_wallet.h +++ b/src/wallet/monero_wallet.h @@ -1195,14 +1195,14 @@ namespace monero { * @param set_payment_id specifies if the payment id should be updated * @param payment_id is the updated payment id */ - void edit_address_book_entry(uint32_t index, bool set_address, boost::optional address, bool set_description, boost::optional description, bool set_payment_id, boost::optional payment_id); + void edit_address_book_entry(uint64_t index, bool set_address, boost::optional address, bool set_description, boost::optional description, bool set_payment_id, boost::optional payment_id); /** * Delete an address book entry. * * @param index is the index of the entry to delete */ - void delete_address_book_entry(uint32_t index); + void delete_address_book_entry(uint64_t index); // /** // * Tag accounts. diff --git a/src/wallet/monero_wallet_model.h b/src/wallet/monero_wallet_model.h index b14bb79..c388c23 100644 --- a/src/wallet/monero_wallet_model.h +++ b/src/wallet/monero_wallet_model.h @@ -448,11 +448,13 @@ namespace monero { * Monero address book entry model. */ struct monero_address_book_entry : serializable_struct { - boost::optional m_index; + boost::optional m_index; // TODO: not boost::optional boost::optional m_address; boost::optional m_description; boost::optional m_payment_id; + monero_address_book_entry() {} + monero_address_book_entry(uint64_t index, const string& address, const string& description, const string& payment_id) : m_index(index), m_address(address), m_description(description), m_payment_id(payment_id) {} boost::property_tree::ptree to_property_tree() const; }; } diff --git a/todo.txt b/todo.txt index c81d046..57b0e1b 100644 --- a/todo.txt +++ b/todo.txt @@ -1,4 +1,5 @@ TODO +preserve rpc error codes, just throwing runtime_error now gen_utils.h and monero_utils.h should not depend on monero_wallet.h add to ecosystem? monero_wallet.cpp move utilities in monero_namespace to be private