From ac0ee6145b43f5d055983a9b4e85d046c1ee4989 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:24:28 +0900 Subject: [PATCH 01/46] wallet: guard the sender field of a light wallet response --- src/wallet/monero_wallet_light.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index ea094c56..9fa5e73b 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2840,8 +2840,10 @@ namespace monero { cryptonote::subaddress_index get_transaction_sender(const std::shared_ptr& tx) { cryptonote::subaddress_index si = {0,0}; for (const auto &output : tx->m_spent_outputs) { - si.major = output->m_sender->m_maj_i; - si.minor = output->m_sender->m_min_i; + if (output->m_sender != nullptr) { // sender is a monero-lws-specific field which spec-conformant servers omit + si.major = output->m_sender->m_maj_i; + si.minor = output->m_sender->m_min_i; + } break; } return si; @@ -3217,7 +3219,10 @@ namespace monero { bool monero_wallet_light::is_spend_real(const std::shared_ptr& spend) const { if (spend->m_key_image == boost::none) return false; std::string key_image = spend->m_key_image.get(); - return is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), spend->m_sender->m_maj_i, spend->m_sender->m_min_i); + // sender is a monero-lws-specific field which spec-conformant servers omit; default to the primary address + uint32_t account_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_maj_i; + uint32_t subaddress_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_min_i; + return is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), account_idx, subaddress_idx); } void monero_wallet_light::run_sync_loop() { From 3fd855356688b4859ad127b1db91cb3678053a5a Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:24:43 +0900 Subject: [PATCH 02/46] wallet: keep the server's amounts for spends a view-only wallet cannot verify --- src/wallet/monero_wallet_light.cpp | 22 +++++++++++++++++++++- src/wallet/monero_wallet_light.h | 10 ++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 9fa5e73b..77c3bf54 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3225,6 +3225,21 @@ namespace monero { return is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), account_idx, subaddress_idx); } + bool monero_wallet_light::is_spend_verifiable(const std::shared_ptr& spend) const { + // a full wallet can always derive key images to verify candidate spends; a view-only wallet + // cannot (is_key_image_ours returns false on the uncached path) so a candidate spend is only + // verifiable when its key image was previously cached, e.g. by import_key_images + if (!is_view_only()) return true; + uint32_t account_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_maj_i; + uint32_t subaddress_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_min_i; + return m_key_image_cache->get(spend->m_tx_pub_key.get(), spend->m_out_index.get(), account_idx, subaddress_idx) != nullptr; + } + + uint64_t monero_wallet_light::get_unrefined_spend_count() const { + assert_not_closed(); + return m_unrefined_spend_count; + } + void monero_wallet_light::run_sync_loop() { if (m_sync_loop_running) return; // only run one loop at a time m_sync_loop_running = true; @@ -3311,6 +3326,7 @@ namespace monero { monero_get_unspent_outs_response unspent_outs = *m_client->get_unspent_outs(0, 0); m_cache->m_subaddrs = m_client->get_subaddrs(); process_subaddresses(); + m_unrefined_spend_count = 0; process_txs(address_txs); unspent_outs.m_amount = process_outputs(unspent_outs.m_outputs, unspent_outs.m_amount.value_or(0)); const uint64_t new_outs_amount = unspent_outs.m_amount.get(); @@ -3357,7 +3373,11 @@ namespace monero { size_t out_idx = 0; for (auto& spend : tx->m_spent_outputs) { - if (!is_spend_real(spend)) { + if (!is_spend_verifiable(spend)) { + // view-only wallet without a cached key image cannot disprove the candidate spend: + // keep the server's conservative amount so spent funds are not shown as available + m_unrefined_spend_count++; + } else if (!is_spend_real(spend)) { uint64_t spend_amount = spend->m_amount.get(); if (spend_amount > tx_total_sent) throw std::runtime_error("tx total sent is negative: " + tx->m_hash.get()); tx_total_sent -= spend_amount; diff --git a/src/wallet/monero_wallet_light.h b/src/wallet/monero_wallet_light.h index 8fa13b9b..1f1d4e86 100644 --- a/src/wallet/monero_wallet_light.h +++ b/src/wallet/monero_wallet_light.h @@ -160,6 +160,14 @@ namespace monero { void start_syncing(uint64_t sync_period_in_ms) override; void stop_syncing() override; void scan_txs(const std::vector& tx_ids) override; + /** + * Get the number of candidate spends the last sync could not verify, which is greater than + * zero only for view-only wallets without imported key images; when non-zero, total sent + * and balances are the server's unrefined conservative values. + * + * @return the number of unrefined spends from the last sync + */ + uint64_t get_unrefined_spend_count() const; uint64_t get_balance() const override; uint64_t get_balance(uint32_t account_idx) const override; uint64_t get_balance(uint32_t account_idx, uint32_t subaddress_idx) const override; @@ -238,6 +246,7 @@ namespace monero { void run_sync_loop(); // run the sync loop in a thread monero_sync_result lock_and_sync(boost::optional start_height = boost::none); // internal function to synchronize request to sync and rescan monero_sync_result sync_aux(boost::optional start_height = boost::none); // internal function to immediately block, sync, and report progress + std::atomic m_unrefined_spend_count{0}; // candidate spends the last sync could not verify (view-only without cached key images) // wallet data mutable boost::recursive_mutex m_sync_data_mutex; @@ -245,6 +254,7 @@ namespace monero { bool is_output_spent(const std::shared_ptr &output, const std::unordered_set& pool_key_images) const; bool is_spend_real(const std::shared_ptr& spend) const; + bool is_spend_verifiable(const std::shared_ptr& spend) const; // whether the wallet can verify a candidate spend is real (spend key present or key image cached) void process_txs(monero_get_address_txs_response& address_txs); uint64_t process_outputs(std::vector>& outputs, uint64_t total_amount); void process_subaddresses(); From 9f85f0ae27d879f7eedccde7199badbee071f18d Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:24:51 +0900 Subject: [PATCH 03/46] wallet: refuse a daemon connection on a light wallet --- src/wallet/monero_wallet_light.cpp | 8 ++++++++ src/wallet/monero_wallet_light.h | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 77c3bf54..503713e2 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1346,6 +1346,14 @@ namespace monero { return m_rpc; } + void monero_wallet_light::set_daemon_connection(const std::string& uri, const std::string& username, const std::string& password, const std::string& proxy_uri, const boost::optional& is_trusted) { + throw std::runtime_error("Cannot set daemon connection for light wallet: light wallets use a light wallet server (monero-lws), not a daemon"); + } + + void monero_wallet_light::set_daemon_connection(const std::shared_ptr& connection, const boost::optional& is_trusted) { + throw std::runtime_error("Cannot set daemon connection for light wallet: light wallets use a light wallet server (monero-lws), not a daemon"); + } + bool monero_wallet_light::is_connected_to_daemon() const { assert_not_closed(); m_is_connected = m_client->is_connected(); diff --git a/src/wallet/monero_wallet_light.h b/src/wallet/monero_wallet_light.h index 1f1d4e86..cc9bdf6d 100644 --- a/src/wallet/monero_wallet_light.h +++ b/src/wallet/monero_wallet_light.h @@ -245,6 +245,13 @@ namespace monero { boost::mutex m_syncing_mutex; // synchronize auto sync loop void run_sync_loop(); // run the sync loop in a thread monero_sync_result lock_and_sync(boost::optional start_height = boost::none); // internal function to synchronize request to sync and rescan + /** + * Light wallets sync from a light wallet server (monero-lws), never from a daemon, so + * setting a daemon connection is unsupported. + */ + void set_daemon_connection(const std::string& uri, const std::string& username = "", const std::string& password = "", const std::string& proxy_uri = "", const boost::optional& is_trusted = boost::none) override; + void set_daemon_connection(const std::shared_ptr& connection, const boost::optional& is_trusted = boost::none) override; + monero_sync_result sync_aux(boost::optional start_height = boost::none); // internal function to immediately block, sync, and report progress std::atomic m_unrefined_spend_count{0}; // candidate spends the last sync could not verify (view-only without cached key images) From c9b1e6f6810e7985639db9a824977203ff46a5b5 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:25:15 +0900 Subject: [PATCH 04/46] wallet: report the transactions that a failed relay already sent --- src/wallet/monero_wallet_light.cpp | 68 +++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 503713e2..7247074c 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1683,8 +1683,12 @@ namespace monero { assert_not_closed(); sync_op_lock op_lock(*this); // do not refresh while relaying txs - // relay each metadata as a tx + // relay each metadata as a tx; the transactions relay one by one, so a failure after the first leaves them + // on the network that no returned list names: a submit that fails is collected per transaction and the set + // is tried to its end, and the report names the hashes that went out, so a caller that rebuilds knows what + // already pays std::vector tx_hashes; + std::vector failed; for (const auto& tx_metadata : tx_metadatas) { // parse tx metadata hex cryptonote::blobdata blob; @@ -1711,8 +1715,17 @@ namespace monero { // commit tx std::string full_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)); - try { m_client->submit_raw_tx(full_hex); } - catch (const std::exception& e) { throw std::runtime_error("Failed to commit tx"); } + std::string failure; + std::shared_ptr submit_res; + try { submit_res = m_client->submit_raw_tx(full_hex); } + catch (const std::exception& e) { failure = std::string("Failed to commit tx: ") + e.what(); } + if (failure.empty() && (submit_res == nullptr || submit_res->m_status == boost::none || submit_res->m_status.get() != std::string("OK"))) { + failure = "Failed to commit tx" + (submit_res != nullptr && submit_res->m_status != boost::none ? (": " + submit_res->m_status.get()) : std::string()); + } + if (!failure.empty()) { + failed.push_back(failure); + continue; + } std::string change_pubkey; std::shared_ptr tx = monero_wallet_utils::ptx_to_tx(ptx, static_cast(m_network_type), this, &change_pubkey); tx->m_full_hex = full_hex; @@ -1722,6 +1735,15 @@ namespace monero { tx_hashes.push_back(pending_tx_hash); } + if (!failed.empty()) { + if (!tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache + std::string relayed_list; + for (const auto& h : tx_hashes) relayed_list += (relayed_list.empty() ? "" : ", ") + h; + std::string failed_list; + for (const auto& f : failed) failed_list += (failed_list.empty() ? "" : "; ") + f; + throw std::runtime_error("failed to relay " + std::to_string(failed.size()) + " of " + std::to_string(tx_metadatas.size()) + " transactions" + (tx_hashes.empty() ? std::string() : "; already relayed: " + relayed_list) + "; " + failed_list); + } + if (!tx_metadatas.empty()) m_cache->calculate_balance(); // notify listeners of spent funds @@ -1916,25 +1938,41 @@ namespace monero { try { ptx_vector = monero_wallet_utils::parse_signed_tx(blob, m_account.get_keys().m_view_secret_key); } catch (const std::exception &e) { throw std::runtime_error(std::string("Failed to parse signed tx: ") + e.what()); } - try { - std::vector tx_hashes; - for (auto &ptx: ptx_vector) { + // the transactions relay one by one, so a failure after the first leaves them on the network that no + // returned list names: a submit that fails is collected per transaction and the set is tried to its end, + // and the report names the hashes that went out, so a caller that rebuilds knows what already pays + std::vector refused; + std::vector tx_hashes; + for (auto &ptx: ptx_vector) { + std::string pending_tx_hash = epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx)); + std::string failure; + try { const auto res = m_client->submit_raw_tx(epee::string_tools::buff_to_hex_nodelimer(cryptonote::tx_to_blob(ptx.tx))); - if (res->m_status == boost::none || res->m_status.get() != std::string("OK")) throw std::runtime_error("Could not relay tx" + signed_tx_hex); - crypto::hash txid; - txid = cryptonote::get_transaction_hash(ptx.tx); - std::string pending_tx_hash = epee::string_tools::pod_to_hex(txid); - tx_hashes.push_back(pending_tx_hash); + if (res->m_status == boost::none || res->m_status.get() != std::string("OK")) failure = res->m_status ? *res->m_status : std::string("no status"); + } catch (const std::exception &e) { failure = e.what(); } + if (!failure.empty()) { + refused.push_back(pending_tx_hash + ": " + failure); + continue; + } + tx_hashes.push_back(pending_tx_hash); + try { std::string change_pubkey; std::shared_ptr tx = monero_wallet_utils::ptx_to_tx(ptx, static_cast(m_network_type), this, &change_pubkey); m_cache->add_unconfirmed_tx(tx, change_pubkey); + } catch (const std::exception &e) { + throw std::runtime_error(std::string("Failed to submit signed tx: ") + e.what()); } + } - m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of spent funds - return tx_hashes; - } catch (const std::exception &e) { - throw std::runtime_error(std::string("Failed to submit signed tx: ") + e.what()); + m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of spent funds + if (!refused.empty()) { + std::string relayed_list; + for (const auto& h : tx_hashes) relayed_list += (relayed_list.empty() ? "" : ", ") + h; + std::string refused_list; + for (const auto& r : refused) refused_list += (refused_list.empty() ? "" : "; ") + r; + throw std::runtime_error("the server did not relay " + std::to_string(refused.size()) + " of " + std::to_string(ptx_vector.size()) + " transactions" + (tx_hashes.empty() ? std::string() : "; already relayed: " + relayed_list) + "; not relayed: " + refused_list); } + return tx_hashes; } // implementation based on monero-project's wallet2::get_tx_key() From 18ff84260f4454bee0f5d62e588b27baee81ddf3 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:52:24 +0900 Subject: [PATCH 05/46] wallet: guard the sender of a spend in the transfers of a light wallet --- src/wallet/monero_wallet_light.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 7247074c..883a6d37 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3073,8 +3073,9 @@ namespace monero { } for (const auto& spent_output : tx->m_spent_outputs) { - uint32_t account_idx = spent_output->m_sender->m_maj_i; - uint32_t subaddress_idx = spent_output->m_sender->m_min_i; + // sender is a monero-lws-specific field which spec-conformant servers omit; default to the primary address + uint32_t account_idx = spent_output->m_sender == nullptr ? 0 : spent_output->m_sender->m_maj_i; + uint32_t subaddress_idx = spent_output->m_sender == nullptr ? 0 : spent_output->m_sender->m_min_i; uint64_t out_amount = spent_output->m_amount.get(); if (account_idx == sender.major && std::find_if(outgoing_transfer->m_subaddress_indices.begin(), outgoing_transfer->m_subaddress_indices.end(), [subaddress_idx](const uint32_t &idx) { return subaddress_idx == idx; }) == outgoing_transfer->m_subaddress_indices.end()) { From 097f308f1b3bd7781c45b4ecb3d80236b3ecc936 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 17:52:24 +0900 Subject: [PATCH 06/46] wallet: report a transaction that relayed but is not tracked --- src/wallet/monero_wallet_light.cpp | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 883a6d37..a55ab3db 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1726,15 +1726,23 @@ namespace monero { failed.push_back(failure); continue; } - std::string change_pubkey; - std::shared_ptr tx = monero_wallet_utils::ptx_to_tx(ptx, static_cast(m_network_type), this, &change_pubkey); - tx->m_full_hex = full_hex; - m_cache->add_unconfirmed_tx(tx, change_pubkey); - // collect resulting hash + // the tx is on the network now, so a failure of the bookkeeping that follows is reported with its + // hash and the set is tried to its end like a submit that fails std::string pending_tx_hash = epee::string_tools::pod_to_hex(cryptonote::get_transaction_hash(ptx.tx)); + try { + std::string change_pubkey; + std::shared_ptr tx = monero_wallet_utils::ptx_to_tx(ptx, static_cast(m_network_type), this, &change_pubkey); + tx->m_full_hex = full_hex; + m_cache->add_unconfirmed_tx(tx, change_pubkey); + } catch (const std::exception& e) { + failed.push_back(pending_tx_hash + " relayed but not tracked: " + e.what()); + continue; + } + // collect resulting hash tx_hashes.push_back(pending_tx_hash); } + m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of the funds that left, also when a later one failed if (!failed.empty()) { if (!tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache std::string relayed_list; @@ -1746,9 +1754,6 @@ namespace monero { if (!tx_metadatas.empty()) m_cache->calculate_balance(); - // notify listeners of spent funds - m_wallet_listener->on_spend_tx_hashes(tx_hashes); - // return relayed tx hashes return tx_hashes; } @@ -1955,16 +1960,20 @@ namespace monero { continue; } tx_hashes.push_back(pending_tx_hash); + // the tx is on the network now, so a failure of the bookkeeping that follows is reported with its + // hash and the set is tried to its end like a submit that fails try { std::string change_pubkey; std::shared_ptr tx = monero_wallet_utils::ptx_to_tx(ptx, static_cast(m_network_type), this, &change_pubkey); m_cache->add_unconfirmed_tx(tx, change_pubkey); } catch (const std::exception &e) { - throw std::runtime_error(std::string("Failed to submit signed tx: ") + e.what()); + refused.push_back(pending_tx_hash + " relayed but not tracked: " + e.what()); + continue; } } m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of spent funds + m_cache->calculate_balance(); // the pending spends change the balances, which a wallet that does not sync in the background keeps otherwise stale if (!refused.empty()) { std::string relayed_list; for (const auto& h : tx_hashes) relayed_list += (relayed_list.empty() ? "" : ", ") + h; From 4731332744c5c5a19696182e695ffcdded05cb37 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:03:18 +0900 Subject: [PATCH 07/46] wallet: lock the listeners of a light wallet --- src/wallet/monero_wallet_light.cpp | 3 +++ src/wallet/monero_wallet_light.h | 1 + 2 files changed, 4 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index a55ab3db..9f7c221b 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1384,16 +1384,19 @@ namespace monero { void monero_wallet_light::add_listener(monero_wallet_listener& listener) { assert_not_closed(); + boost::lock_guard guard(m_listeners_mutex); m_listeners.insert(&listener); } void monero_wallet_light::remove_listener(monero_wallet_listener& listener) { assert_not_closed(); + boost::lock_guard guard(m_listeners_mutex); m_listeners.erase(&listener); } std::set monero_wallet_light::get_listeners() { assert_not_closed(); + boost::lock_guard guard(m_listeners_mutex); return m_listeners; } diff --git a/src/wallet/monero_wallet_light.h b/src/wallet/monero_wallet_light.h index cc9bdf6d..28969373 100644 --- a/src/wallet/monero_wallet_light.h +++ b/src/wallet/monero_wallet_light.h @@ -215,6 +215,7 @@ namespace monero { friend struct wallet_light_listener; std::unique_ptr m_wallet_listener; // internal wallet implementation listener std::set m_listeners; // external wallet listeners + mutable boost::recursive_mutex m_listeners_mutex; // guards m_listeners, which the sync and pool threads notify while the app thread changes it static monero_wallet_light* create_wallet_from_seed(monero_wallet_config& config, const std::shared_ptr& rpc); static monero_wallet_light* create_wallet_from_keys(monero_wallet_config& config, const std::shared_ptr& rpc); From 2fb4ba2943b279a4dc4040c2b3bdf3afbc89d7c3 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:03:19 +0900 Subject: [PATCH 08/46] wallet: lock the reads of a light wallet against its background sync --- src/wallet/monero_wallet_light.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 9f7c221b..fb07b208 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1512,6 +1512,7 @@ namespace monero { uint64_t monero_wallet_light::get_height() const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the scanned height while the sync thread refreshes the cache return m_cache->get_scanned_block_height() + 1; } @@ -1550,31 +1551,37 @@ namespace monero { uint64_t monero_wallet_light::get_balance() const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_balance(); } uint64_t monero_wallet_light::get_balance(uint32_t account_index) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_balance(account_index); } uint64_t monero_wallet_light::get_balance(uint32_t account_idx, uint32_t subaddress_idx) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_balance(account_idx, subaddress_idx); } uint64_t monero_wallet_light::get_unlocked_balance() const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_unlocked_balance(); } uint64_t monero_wallet_light::get_unlocked_balance(uint32_t account_index) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_unlocked_balance(account_index); } uint64_t monero_wallet_light::get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balance while the sync thread recalculates it return m_cache->get_unlocked_balance(account_idx, subaddress_idx); } @@ -1996,6 +2003,8 @@ namespace monero { crypto::hash txid; if (!epee::string_tools::hex_to_pod(tx_hash, txid)) throw std::runtime_error("TX hash has invalid format"); + sync_op_lock op_lock(*this); // do not read the tx keys while another thread adds one + crypto::secret_key tx_key; std::vector additional_tx_keys; @@ -2544,6 +2553,7 @@ namespace monero { std::vector> monero_wallet_light::get_txs(const monero_tx_query& query) const { MTRACE("monero_wallet_light::get_txs(query)"); assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the txs while the sync thread reassigns them // copy query std::shared_ptr query_sp = std::make_shared(query); // convert to shared pointer @@ -2664,6 +2674,7 @@ namespace monero { std::vector> monero_wallet_light::get_outputs(const monero_output_query& query) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the outputs while the sync thread reassigns them // get outputs directly if query does not require tx context (e.g. other outputs, transfers) if (!monero_output_query::is_contextual(query)) return get_outputs_aux(query); From 63508127b22b59289f1feec3a16dc631369d0252 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:06:38 +0900 Subject: [PATCH 09/46] wallet: end a sync of a light wallet that changes nothing --- src/wallet/monero_wallet_light.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index fb07b208..c21fb3b2 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3387,6 +3387,7 @@ namespace monero { if (new_height == last_height) { m_cache->set_sync_status(*addr_info); + m_wallet_listener->on_sync_end(); // end the sync the listeners started, or their bounds stay for the next one return result; } From b39bc3e6dec93400de555e1c599af28efc185f44 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:06:38 +0900 Subject: [PATCH 10/46] wallet: refuse an amount of a light wallet that its total cannot carry --- src/wallet/monero_wallet_light.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index c21fb3b2..0f2453d1 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3474,6 +3474,7 @@ namespace monero { const auto pool_key_images = m_cache->get_pool_key_images(); for (auto& output : outputs) { if (!is_output_spent(output, pool_key_images)) continue; + if (total_amount < output->m_amount.get()) throw std::runtime_error("output amount exceeds the total amount of the server"); total_amount -= output->m_amount.get(); } From 3a4eb4ce2cf5dfb31e969ddfa5776f0b297a6e1a Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:06:38 +0900 Subject: [PATCH 11/46] model: default the recipient of a light wallet output to the primary address --- src/wallet/monero_wallet_light_model.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 22f264cc..1cad1478 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -113,6 +113,8 @@ namespace monero { } void monero_output_light::from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& output) { + // recipient is a monero-lws-specific field which spec-conformant servers omit; default to the primary address + output->m_recipient = std::make_shared(); for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { std::string key = it->first; if (key == std::string("tx_id")) output->m_tx_id = it->second.get_value(); From cc1ec9db934af1d08c308d6f401f562116f999d9 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:18:25 +0900 Subject: [PATCH 12/46] utils: read a uint64 from the digits a string carries --- src/utils/gen_utils.h | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/utils/gen_utils.h b/src/utils/gen_utils.h index 3278fba3..e25a9122 100644 --- a/src/utils/gen_utils.h +++ b/src/utils/gen_utils.h @@ -62,6 +62,9 @@ #include #include "rapidjson/document.h" #include +#include +#include +#include #include #include #include @@ -91,27 +94,19 @@ namespace gen_utils } static bool is_uint64_t(const std::string& str) { + if (str.empty() || !std::all_of(str.begin(), str.end(), [](char c) { return std::isdigit(static_cast(c)); })) return false; try { size_t sz; - std::stol(str, &sz); + std::stoull(str, &sz); return sz == str.size(); } - catch (const std::invalid_argument&) { - // if no conversion could be performed. - return false; - } - catch (const std::out_of_range&) { - // if the converted value would fall out of the range of the result type. - return false; - } + catch (const std::invalid_argument&) { return false; } + catch (const std::out_of_range&) { return false; } } static uint64_t uint64_t_cast(const std::string& str) { if (!is_uint64_t(str)) throw std::out_of_range("String provided is not a valid uint64_t"); - uint64_t value; - std::istringstream itr(str); - itr >> value; - return value; + return std::stoull(str); } static uint64_t timestamp_to_epoch(const std::string& iso_timestamp) { From 57826ad86a24932fb2e00bc51d499d1a414ec2a0 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:19:11 +0900 Subject: [PATCH 13/46] wallet: test a key image of a spend against every subaddress --- src/wallet/monero_wallet_light.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 0f2453d1..10bfab35 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3289,10 +3289,16 @@ namespace monero { bool monero_wallet_light::is_spend_real(const std::shared_ptr& spend) const { if (spend->m_key_image == boost::none) return false; std::string key_image = spend->m_key_image.get(); - // sender is a monero-lws-specific field which spec-conformant servers omit; default to the primary address - uint32_t account_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_maj_i; - uint32_t subaddress_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_min_i; - return is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), account_idx, subaddress_idx); + if (spend->m_sender != nullptr) { + return is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), spend->m_sender->m_maj_i, spend->m_sender->m_min_i); + } + // sender is a monero-lws-specific field which spec-conformant servers omit: the spend does not say + // which subaddress it came from, so the key image is tested against every one the wallet knows, and + // not just the primary address, which would call the spends of the other subaddresses fake + for (const auto& kv : m_cache->m_subaddresses) { + if (is_key_image_ours(key_image, spend->m_tx_pub_key.get(), spend->m_out_index.get(), kv.second.major, kv.second.minor)) return true; + } + return false; } bool monero_wallet_light::is_spend_verifiable(const std::shared_ptr& spend) const { @@ -3300,9 +3306,15 @@ namespace monero { // cannot (is_key_image_ours returns false on the uncached path) so a candidate spend is only // verifiable when its key image was previously cached, e.g. by import_key_images if (!is_view_only()) return true; - uint32_t account_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_maj_i; - uint32_t subaddress_idx = spend->m_sender == nullptr ? 0 : spend->m_sender->m_min_i; - return m_key_image_cache->get(spend->m_tx_pub_key.get(), spend->m_out_index.get(), account_idx, subaddress_idx) != nullptr; + if (spend->m_sender != nullptr) { + return m_key_image_cache->get(spend->m_tx_pub_key.get(), spend->m_out_index.get(), spend->m_sender->m_maj_i, spend->m_sender->m_min_i) != nullptr; + } + // the spend does not say which subaddress it came from, so the cache of imported key images is + // looked in at every subaddress the wallet knows, or the spends of the other ones stay unrefined + for (const auto& kv : m_cache->m_subaddresses) { + if (m_key_image_cache->get(spend->m_tx_pub_key.get(), spend->m_out_index.get(), kv.second.major, kv.second.minor) != nullptr) return true; + } + return false; } uint64_t monero_wallet_light::get_unrefined_spend_count() const { From b49970956f8c86578c026461d664be92c8b53dc7 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:19:23 +0900 Subject: [PATCH 14/46] wallet: default the fee of a light wallet transaction to none --- src/wallet/monero_wallet_light.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 10bfab35..c89718e7 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1047,7 +1047,7 @@ namespace monero { // build dummy input for notification // TODO: this provides one input with outgoing amount like monero-wallet-rpc client, use real inputs instead std::shared_ptr input = std::make_shared(); - input->m_amount = tx->m_outgoing_transfer->m_amount.get() + tx->m_fee.get(); + input->m_amount = tx->m_outgoing_transfer->m_amount.get() + tx->m_fee.value_or(0); input->m_account_index = tx->m_outgoing_transfer->m_account_index; if (tx->m_outgoing_transfer->m_subaddress_indices.size() == 1) input->m_subaddress_index = tx->m_outgoing_transfer->m_subaddress_indices[0]; // initialize if transfer sourced from single subaddress std::shared_ptr tx_notify = std::make_shared(); @@ -2943,7 +2943,7 @@ namespace monero { for (const auto &tx : m_cache->get_txs()) { uint64_t total_sent = tx->m_total_sent.get(); uint64_t total_received = tx->m_total_received.get(); - uint64_t fee = tx->m_fee.get(); + uint64_t fee = tx->m_fee.value_or(0); // fee is a monero-lws-specific field which spec-conformant servers omit bool is_incoming = total_received > 0; bool is_outgoing = total_sent > 0; bool is_change = is_incoming && is_outgoing; From a74895aca4271d0b3d35e6dcdef0a81f703fdacc Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:20:20 +0900 Subject: [PATCH 15/46] model: treat an output of a light wallet whose tx is missing as unlocked --- src/wallet/monero_wallet_light_model.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 1cad1478..b02cef81 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -1130,7 +1130,13 @@ namespace monero { } uint64_t monero_wallet_light_cache::get_num_blocks_to_unlock(const std::string& tx_hash) const { - const auto& tx = get_tx(tx_hash); + // the address txs and the unspent outs arrive in two requests, so an output can name a tx that the + // txs response does not have: the output is treated as unlocked, which the next sync corrects, and + // not as an error that leaves the balance of a partial sync + boost::lock_guard lock(m_mutex); + auto it = m_txs.find(tx_hash); + if (it == m_txs.end()) return 0; + const auto& tx = it->second; uint64_t current_height = get_scanned_block_height() + 1; uint64_t tx_height = tx->m_mempool.get() ? current_height : tx->m_height.get(); uint64_t unlock_time = tx->m_unlock_time.get(); From cfc7881aed7b1d97f3e8318c1d5e96ac51bb78b0 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:20:45 +0900 Subject: [PATCH 16/46] wallet: give up on the next block of a light wallet after a minute --- src/wallet/monero_wallet_light.cpp | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index c89718e7..877c5364 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2827,10 +2827,13 @@ namespace monero { struct block_notifier : monero_wallet_listener { boost::mutex* temp; boost::condition_variable* cv; - uint64_t last_height; + uint64_t last_height = 0; + bool notified = false; block_notifier(boost::mutex* temp, boost::condition_variable* cv) { this->temp = temp; this->cv = cv; } void on_new_block(uint64_t height) { + boost::mutex::scoped_lock lock(*temp); last_height = height; + notified = true; cv->notify_one(); } } block_listener(&temp, &cv); @@ -2838,15 +2841,20 @@ namespace monero { // register the listener add_listener(block_listener); - // wait until condition variable is notified - boost::mutex::scoped_lock lock(temp); - cv.wait(lock); + // wait until the listener reports a block, which a wallet that does not sync in the background + // never gets: the wait gives up after a minute instead of staying for one + uint64_t height; + { + boost::mutex::scoped_lock lock(temp); + cv.wait_for(lock, boost::chrono::minutes(1), [&block_listener]() { return block_listener.notified; }); + height = block_listener.last_height; + } // unregister the listener remove_listener(block_listener); // return last height - return block_listener.last_height; + return height; } bool monero_wallet_light::is_multisig_import_needed() const { From ea7a6796660319a53f518149e9be194ad2717009 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:21:47 +0900 Subject: [PATCH 17/46] wallet: read the existence of a light wallet from a forbidden answer --- src/wallet/monero_wallet_light.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 877c5364..ed086688 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1087,7 +1087,13 @@ namespace monero { return true; } catch (const monero_rpc_error& ex) { - return ex.code == 401; + // a light wallet server answers that the account of the keys does not exist with a forbidden, and + // never with the unauthorized of an http server + if (ex.code == 403) return false; + throw; + } + catch (const monero_error&) { + return false; // a server that does not answer has no wallet on it to find } } From 91a94c41e40abfc5789a235a5834cc9a248f58ed Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:21:47 +0900 Subject: [PATCH 18/46] wallet: say what a light wallet cannot check or scan --- src/wallet/monero_wallet_light.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index ed086688..cd793231 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1473,6 +1473,9 @@ namespace monero { void monero_wallet_light::scan_txs(const std::vector& tx_ids) { assert_not_closed(); + // a light wallet server has no call for the transactions of ids, so the wallet cannot scan them: it + // syncs, which picks up every transaction the server has for the account + if (!tx_ids.empty()) throw std::runtime_error("command not supported by light wallet: scanning transactions of ids"); sync(); } @@ -2082,7 +2085,7 @@ namespace monero { // this wallet only has raw tx bytes cached for txs it itself built and relayed std::shared_ptr cached_tx = m_cache->get_self_constructed_tx(tx_hash); if (cached_tx == nullptr || cached_tx->m_full_hex == boost::none || cached_tx->m_full_hex->empty()) { - throw std::runtime_error("No tx secret key is stored for this tx"); + throw std::runtime_error("No tx is stored for this tx hash: a light wallet checks a tx key against the raw tx of one it built itself"); } cryptonote::blobdata tx_blob; From b62ea2d3a878ba52f9738c67f243a321469abd66 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:21:47 +0900 Subject: [PATCH 19/46] wallet: free a light wallet when its factory throws --- src/wallet/monero_wallet_light.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index cd793231..1251911c 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1181,7 +1181,7 @@ namespace monero { if (offset_set) spend_key_sk = cryptonote::decrypt_key(spend_key_sk, config.m_seed_offset.get()); // initialize wallet account - monero_wallet_light* wallet = new monero_wallet_light(rpc); + std::unique_ptr wallet(new monero_wallet_light(rpc)); // freed when the setup throws wallet->m_account = cryptonote::account_base{}; crypto::secret_key spend_key_val = wallet->m_account.generate(spend_key_sk, true, false); @@ -1206,7 +1206,7 @@ namespace monero { } else if (config.m_restore_height != boost::none) throw std::runtime_error("Cannote restore wallet from height: wallet is not connected to lws"); - return wallet; + return wallet.release(); } monero_wallet_light* monero_wallet_light::create_wallet_from_keys(monero_wallet_config& config, const std::shared_ptr& rpc) { @@ -1267,7 +1267,7 @@ namespace monero { } // initialize wallet account - monero_wallet_light* wallet = new monero_wallet_light(rpc); + std::unique_ptr wallet(new monero_wallet_light(rpc)); // freed when the setup throws if (has_spend_key && has_view_key) wallet->m_account.create_from_keys(address_info.address, spend_key_sk, view_key_sk); else if (has_spend_key) wallet->m_account.generate(spend_key_sk, true, false); else wallet->m_account.create_from_viewkey(address_info.address, view_key_sk); @@ -1292,7 +1292,7 @@ namespace monero { } else if (config.m_restore_height != boost::none) throw std::runtime_error("Cannot restore wallet from height: wallet is not connected to lws"); - return wallet; + return wallet.release(); } monero_wallet_light* monero_wallet_light::create_wallet_random(monero_wallet_config& config, const std::shared_ptr& rpc) { @@ -1305,7 +1305,7 @@ namespace monero { if (!monero_utils::is_valid_language(config_normalized.m_language.get())) throw std::runtime_error("Unknown language: " + config_normalized.m_language.get()); // initialize random wallet account - monero_wallet_light* wallet = new monero_wallet_light(rpc); + std::unique_ptr wallet(new monero_wallet_light(rpc)); // freed when the setup throws crypto::secret_key spend_key_sk = wallet->m_account.generate(); // initialize remaining wallet @@ -1328,7 +1328,7 @@ namespace monero { } } - return wallet; + return wallet.release(); } // ----------------------------- WALLET METHODS ----------------------------- From 52b85b460b231e2a0da50f000059a53b4cc1a40f Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 21:22:11 +0900 Subject: [PATCH 20/46] model: refuse an output index out of the range of a transaction --- src/wallet/monero_wallet_light_model.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index b02cef81..ac33829c 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -981,6 +981,8 @@ namespace monero { const auto &out = unspent_outs[n]; uint64_t out_amount = out->m_amount.get(); auto internal_output_index = out->m_index.get(); + // the index comes from the server: a garbage one would resize the prefix by billions of outputs + if (internal_output_index > 100000) throw std::runtime_error("output index is out of the range of a transaction: " + std::to_string(internal_output_index)); std::string tx_hash = out->m_tx_hash.get(); uint64_t unlock_time = get_tx(tx_hash)->m_unlock_time.get(); From 9312032dcd744f059fd97d5fd6ccb36d555f6648 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:39:08 +0900 Subject: [PATCH 21/46] wallet: say a check of a tx key is not good when the address received nothing --- src/wallet/monero_wallet_full.cpp | 4 +++- src/wallet/monero_wallet_light.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_full.cpp b/src/wallet/monero_wallet_full.cpp index acbe9d69..419a3153 100644 --- a/src/wallet/monero_wallet_full.cpp +++ b/src/wallet/monero_wallet_full.cpp @@ -3034,7 +3034,9 @@ namespace monero { uint64_t num_confirmations; m_w2->check_tx_key(_tx_hash, _tx_key, additional_tx_keys, info.address, received_amount, in_tx_pool, num_confirmations); std::shared_ptr check_tx = std::make_shared(); - check_tx->m_is_good = true; // check is good if we get this far + // a key that is not the one of the tx, or a tx that did not pay the address, receives nothing: the + // check says it is not good and not that it erred + check_tx->m_is_good = received_amount > 0; check_tx->m_received_amount = received_amount; check_tx->m_in_tx_pool = in_tx_pool; check_tx->m_num_confirmations = num_confirmations; diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 1251911c..d8b6be7c 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2109,7 +2109,9 @@ namespace monero { } std::shared_ptr check_tx = std::make_shared(); - check_tx->m_is_good = true; // check is good if we get this far + // a key that is not the one of the tx, or a tx that did not pay the address, receives nothing: the + // check says it is not good and not that it erred + check_tx->m_is_good = received_amount > 0; check_tx->m_received_amount = received_amount; check_tx->m_in_tx_pool = in_tx_pool; check_tx->m_num_confirmations = num_confirmations; From 4924d4189b5b6cb135fcd3bcc4b1b76f2baacc6f Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:50:46 +0900 Subject: [PATCH 22/46] wallet: lock the transfers and subaddresses of a light wallet --- src/wallet/monero_wallet_light.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index d8b6be7c..f62f296b 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1649,6 +1649,7 @@ namespace monero { std::vector monero_wallet_light::get_subaddresses(const uint32_t account_idx, const std::vector& subaddress_indices) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balances while the sync thread recalculates them std::vector subaddresses = get_subaddresses_aux(account_idx, subaddress_indices); for(monero_subaddress& subaddress : subaddresses) m_cache->init_subaddress(subaddress); return subaddresses; @@ -1689,6 +1690,7 @@ namespace monero { monero_subaddress monero_wallet_light::get_subaddress(const uint32_t account_idx, const uint32_t subaddress_idx) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the balances while the sync thread recalculates them std::vector indices; indices.push_back(subaddress_idx); std::vector subaddresses = monero_wallet_keys::get_subaddresses(account_idx, indices); @@ -2670,6 +2672,7 @@ namespace monero { std::vector> monero_wallet_light::get_transfers(const monero_transfer_query& query) const { assert_not_closed(); + sync_op_lock op_lock(*this); // do not read the transfers while the sync thread reassigns the txs // get transfers directly if query does not require tx context (e.g. other transfers, outputs) if (!monero_transfer_query::is_contextual(query)) return get_transfers_aux(query); From 6a5f141fa4d75dd94314fb4a14e49d36876c955d Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:51:08 +0900 Subject: [PATCH 23/46] wallet: report the transactions that a failed sweep already sent --- src/wallet/monero_wallet_light.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index f62f296b..7a643c62 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2325,10 +2325,21 @@ namespace monero { tx_builder tx_builder(static_cast(m_network_type), m_account.get_keys(), is_view_only(), *m_client, m_cache); std::vector unsigned_construction_data; + // a sweep of many outputs relays one tx per iteration, so a build that fails after the first leaves + // them on the network that no returned list names: a build that fails is collected and the loop is + // tried to its end, and the report names the hashes that went out, so a caller that rebuilds knows + // what already pays + std::vector failed; while (!unspent_outs.empty()) { cryptonote::blobdata tx_blob; - tools::wallet2::pending_tx ptx = tx_builder.build(subaddr_account_idx, {dest_address}, config.m_payment_id, {}, true, simple_priority, unspent_outs, fee_per_b, fee_mask, tx_blob); + tools::wallet2::pending_tx ptx; + try { + ptx = tx_builder.build(subaddr_account_idx, {dest_address}, config.m_payment_id, {}, true, simple_priority, unspent_outs, fee_per_b, fee_mask, tx_blob); + } catch (const std::exception& e) { + failed.push_back(e.what()); + break; + } std::string full_hex = epee::string_tools::buff_to_hex_nodelimer(tx_blob); if (ptx.tx_key != crypto::null_skey) { @@ -2390,6 +2401,14 @@ namespace monero { if (!is_view_only() && relayed) m_cache->add_unconfirmed_tx(tx, change_pubkey); } + if (!failed.empty()) { + std::string relayed_list; + for (const auto& tx : result) relayed_list += (relayed_list.empty() ? "" : ", ") + tx->m_hash.get(); + std::string failed_list; + for (const auto& f : failed) failed_list += (failed_list.empty() ? "" : "; ") + f; + throw std::runtime_error("failed to sweep " + std::to_string(failed.size()) + " of the remaining transactions" + (result.empty() ? std::string() : "; already relayed: " + relayed_list) + "; " + failed_list); + } + std::string unsigned_tx_hex; if (is_view_only()) { unsigned_tx_hex = monero_wallet_utils::dump_unsigned_tx(unsigned_construction_data, config.m_payment_id, m_cache->export_outputs(true, 0), m_account.get_keys().m_view_secret_key); From 3ea5712f688525e8b3ced8ce28822180c612ef72 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:51:39 +0900 Subject: [PATCH 24/46] wallet: read the state of a checked tx from the chain of a light wallet --- src/wallet/monero_wallet_light.cpp | 24 +++++++++++++++++++----- src/wallet/monero_wallet_light_model.h | 10 ++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 7a643c62..77451ad2 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2102,12 +2102,26 @@ namespace monero { received_amount = tx_builder.compute_amount_received(tx, _tx_key, additional_tx_keys, info.address); } - bool in_tx_pool = cached_tx->m_in_tx_pool != boost::none && cached_tx->m_in_tx_pool.get(); + // a tx that the wallet relayed is in the pool until the next sync sees it in the chain: the state of + // the chain names it, and not the copy that the relay left, which stays in the pool forever + bool in_tx_pool; uint64_t num_confirmations = 0; - if (!in_tx_pool) { - uint64_t tx_height = cached_tx->get_height().value_or(0); - uint64_t chain_height = get_daemon_height(); - if (chain_height > tx_height) num_confirmations = chain_height - tx_height; + try { + const auto tx = m_cache->get_tx(tx_hash); + in_tx_pool = tx->m_mempool.get(); + if (!in_tx_pool) { + uint64_t tx_height = tx->m_height.get(); + uint64_t chain_height = get_daemon_height(); + if (chain_height > tx_height) num_confirmations = chain_height - tx_height; + } + } catch (const std::runtime_error&) { + // the tx is not in the store of the account yet, so only the copy of the relay tells its state + in_tx_pool = cached_tx->m_in_tx_pool != boost::none && cached_tx->m_in_tx_pool.get(); + if (!in_tx_pool) { + uint64_t tx_height = cached_tx->get_height().value_or(0); + uint64_t chain_height = get_daemon_height(); + if (chain_height > tx_height) num_confirmations = chain_height - tx_height; + } } std::shared_ptr check_tx = std::make_shared(); diff --git a/src/wallet/monero_wallet_light_model.h b/src/wallet/monero_wallet_light_model.h index cc701ef7..6568d6e4 100644 --- a/src/wallet/monero_wallet_light_model.h +++ b/src/wallet/monero_wallet_light_model.h @@ -408,6 +408,14 @@ namespace monero { uint64_t get_num_blocks_to_unlock(uint32_t account_idx, uint32_t subaddress_idx) const; void init_subaddress(monero_subaddress& subaddress) const; + /** + * Get the tx of a hash that the account received or sent, which throws when the store does not have it. + * + * @param hash is the hash of the tx + * @return the tx of the hash + */ + std::shared_ptr get_tx(const std::string& hash) const; + private: mutable boost::recursive_mutex m_mutex; std::shared_ptr m_key_image_cache; @@ -417,8 +425,6 @@ namespace monero { std::vector> m_tx_list; serializable_unordered_map> m_self_constructed_txs; serializable_unordered_map m_spent_key_images; - - std::shared_ptr get_tx(const std::string& hash) const; void set_txs(monero_get_address_txs_response& response, const monero_get_address_info_response& addr_info_response); // blockchain From bf760f79e765f79ec0d0d36ba8e4adac243c8863 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:51:52 +0900 Subject: [PATCH 25/46] utils: tell the change of a pending tx by its address --- src/utils/monero_wallet_utils.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/utils/monero_wallet_utils.cpp b/src/utils/monero_wallet_utils.cpp index 80882d7e..ad22e5e7 100644 --- a/src/utils/monero_wallet_utils.cpp +++ b/src/utils/monero_wallet_utils.cpp @@ -156,7 +156,9 @@ std::shared_ptr monero_wallet_utils::ptx_to_tx(const tools::wa output->m_subaddress_index = subaddress_idx; output->m_is_spent = false; output->m_is_frozen = false; - bool is_change = cd.change_dts.amount > 0 && dest.amount == cd.change_dts.amount && change_output == nullptr; + // the change is the output that pays the address of the change, and not the first one of its + // amount, which a destination can share: wallet2 tells them apart the same way + bool is_change = cd.change_dts.amount > 0 && dest.addr == cd.change_dts.addr && change_output == nullptr; if (is_change) { change_output = output; } From 86853442a908b27e93e33f051fac7b2be5b59ab4 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:52:06 +0900 Subject: [PATCH 26/46] model: credit the change of a pending tx to the account that sent it --- src/wallet/monero_wallet_light_model.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index ac33829c..c81c7c29 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -877,9 +877,11 @@ namespace monero { uint64_t change_amount = 0; if (tx->m_change_amount != boost::none) change_amount = tx->m_change_amount.get(); + // the change of a pending tx goes to the account that sent it, and not always to the primary one + uint32_t change_account_idx = tx->m_outgoing_transfer != nullptr && tx->m_outgoing_transfer->m_account_index != boost::none ? tx->m_outgoing_transfer->m_account_index.get() : 0; m_balance += change_amount; - m_account_balance[0] += change_amount; - m_subaddress_balance[0][0] += change_amount; + m_account_balance[change_account_idx] += change_amount; + m_subaddress_balance[change_account_idx][0] += change_amount; for (const std::shared_ptr &out : tx->m_outputs) { std::shared_ptr output = std::dynamic_pointer_cast(out); From bcd6f429ef3cf2e5d127745b3241fb249bd059b0 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:52:36 +0900 Subject: [PATCH 27/46] model: keep the outputs of a light wallet frozen across a sync --- src/wallet/monero_wallet_light_model.cpp | 7 +++++-- src/wallet/monero_wallet_light_model.h | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index c81c7c29..e9f334de 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -708,7 +708,7 @@ namespace monero { } for (const auto& output : kv.second) { - if (output->m_frozen.value_or(false) || get_num_blocks_to_unlock(output->m_tx_hash.get()) > 0) continue; + if (output->m_frozen.value_or(false) || (output->m_key_image != boost::none && m_frozen_key_images.count(output->m_key_image.get()) > 0) || get_num_blocks_to_unlock(output->m_tx_hash.get()) > 0) continue; if (output->is_key_image_known() && pool_key_images.count(output->m_key_image.get())) continue; spendable.push_back(output); } @@ -936,13 +936,16 @@ namespace monero { auto it = m_key_image_index.find(key_image); if (it == m_key_image_index.end()) throw std::runtime_error("Key image not found"); m_outputs[it->second]->m_frozen = frozen; + // the sync replaces the outputs, so the setting lives in the set of key images and not only on the object + if (frozen) m_frozen_key_images.insert(key_image); + else m_frozen_key_images.erase(key_image); } bool monero_wallet_light_cache::is_key_image_frozen(const std::string& key_image) const { validate_key_image(key_image); auto it = m_key_image_index.find(key_image); if (it == m_key_image_index.end()) throw std::runtime_error("Key image not found"); - return m_outputs[it->second]->m_frozen.value_or(false); + return m_outputs[it->second]->m_frozen.value_or(false) || m_frozen_key_images.count(key_image) > 0; } std::shared_ptr monero_wallet_light_cache::get_output(const std::string& key_image) const { diff --git a/src/wallet/monero_wallet_light_model.h b/src/wallet/monero_wallet_light_model.h index 6568d6e4..5936d902 100644 --- a/src/wallet/monero_wallet_light_model.h +++ b/src/wallet/monero_wallet_light_model.h @@ -420,6 +420,9 @@ namespace monero { mutable boost::recursive_mutex m_mutex; std::shared_ptr m_key_image_cache; + // the frozen key images, which a sync replaces the outputs they mark, so the setting outlives them + std::set m_frozen_key_images; + // transactions serializable_unordered_map> m_txs; std::vector> m_tx_list; From f6942b99947c970640551a7663eeaedbeae2dc1a Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:52:45 +0900 Subject: [PATCH 28/46] model: export an output of a light wallet whose tx is missing --- src/wallet/monero_wallet_light_model.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index e9f334de..0e9f6204 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -989,7 +989,14 @@ namespace monero { // the index comes from the server: a garbage one would resize the prefix by billions of outputs if (internal_output_index > 100000) throw std::runtime_error("output index is out of the range of a transaction: " + std::to_string(internal_output_index)); std::string tx_hash = out->m_tx_hash.get(); - uint64_t unlock_time = get_tx(tx_hash)->m_unlock_time.get(); + // the tx can be missing in the window of the two requests of a sync, like the unlock path: the + // prefix of the export carries no lock then, which the next export after the next sync corrects + uint64_t unlock_time; + { + boost::lock_guard lock(m_mutex); + auto tx_it = m_txs.find(tx_hash); + unlock_time = tx_it == m_txs.end() ? 0 : tx_it->second->m_unlock_time.get(); + } crypto::public_key public_key; crypto::public_key tx_pub_key; From 823469b45233ccaacc7a6163de8710c32811f424 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:53:10 +0900 Subject: [PATCH 29/46] wallet: refuse a subaddress range a light wallet cannot carry --- src/wallet/monero_wallet_light.cpp | 3 +++ src/wallet/monero_wallet_light_model.cpp | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 77451ad2..9fe2c822 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3566,6 +3566,9 @@ namespace monero { for (const auto& index_range : kv.second) { const uint32_t range_start = index_range->at(0); const uint32_t range_end = index_range->at(1); + // a server that answers a range of the whole space of an index would loop a derivation per index + // of it, and the last one wraps: no account holds more subaddresses than the server itself keeps + if (range_start > range_end || range_end - range_start > 16384) throw std::runtime_error("subaddress range is out of the range of an account: [" + std::to_string(range_start) + ", " + std::to_string(range_end) + "]"); for (uint32_t i = range_start; i <= range_end; i++) { if (account_idx == 0 && i == 0) continue; if (has_watermark && i <= watermark) continue; diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 0e9f6204..882d4676 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -215,6 +215,8 @@ namespace monero { if (size() != 2) return indices; uint32_t min_i = at(0); uint32_t maj_i = at(1); + // a range of the whole space of an index would fill the memory with indices, and the last one wraps + if (min_i > maj_i || maj_i - min_i > 16384) throw std::runtime_error("subaddress range is out of the range of an account: [" + std::to_string(min_i) + ", " + std::to_string(maj_i) + "]"); for(uint32_t i = min_i; i <= maj_i; i++) indices.push_back(i); return indices; } From 1d16295cf090d1f1f7f0c4181085addfdc5a593f Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:55:12 +0900 Subject: [PATCH 30/46] wallet: guard the derivations and the sum of the inputs of a light wallet --- src/wallet/monero_wallet_light.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 9fe2c822..3d8be6e2 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -596,8 +596,8 @@ namespace monero { std::vector sources; LOG_PRINT_L2("preparing outputs"); for (size_t out_index = 0; out_index < outputs.size(); out_index++) { + if (outputs[out_index]->m_amount.get() > UINT64_MAX - found_money) throw std::runtime_error("input amount overflow"); found_money += outputs[out_index]->m_amount.get(); - if (found_money > UINT64_MAX) throw std::runtime_error("input amount overflow"); auto src = cryptonote::tx_source_entry{}; src.amount = outputs[out_index]->m_amount.get(); From e00397ab2b00662810e54c8ae58dc304c9a14bb7 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:58:40 +0900 Subject: [PATCH 31/46] wallet: refuse a relay from a view-only light wallet --- src/wallet/monero_wallet_light.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 3d8be6e2..4b4379c7 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2161,6 +2161,9 @@ namespace monero { MINFO("monero_wallet_light::create_txs()"); assert_not_closed(); if (is_multisig()) throw std::runtime_error("Multisig wallet not supported"); + // a view-only wallet cannot sign what it builds, so a tx that it submits is garbage that the daemon + // refuses: it builds the unsigned set instead, which a full wallet signs and submits + if (config.m_relay != boost::none && config.m_relay.get() && is_view_only()) throw std::runtime_error("Cannot relay from a view-only wallet"); if (!m_is_connected) throw std::runtime_error("Wallet is not connected to daemon"); // validate config From a7d4cbf47f070a33135ef9eda2c2abd4170f8e17 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:58:47 +0900 Subject: [PATCH 32/46] wallet: read the not relayed answer of a submit of a light wallet --- src/wallet/monero_wallet_light.cpp | 9 +++++---- src/wallet/monero_wallet_light_model.cpp | 1 + src/wallet/monero_wallet_light_model.h | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 4b4379c7..4e5fdace 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1740,7 +1740,7 @@ namespace monero { std::shared_ptr submit_res; try { submit_res = m_client->submit_raw_tx(full_hex); } catch (const std::exception& e) { failure = std::string("Failed to commit tx: ") + e.what(); } - if (failure.empty() && (submit_res == nullptr || submit_res->m_status == boost::none || submit_res->m_status.get() != std::string("OK"))) { + if (failure.empty() && (submit_res == nullptr || submit_res->m_status == boost::none || submit_res->m_status.get() != std::string("OK") || submit_res->m_not_relayed.value_or(false))) { failure = "Failed to commit tx" + (submit_res != nullptr && submit_res->m_status != boost::none ? (": " + submit_res->m_status.get()) : std::string()); } if (!failure.empty()) { @@ -1975,6 +1975,7 @@ namespace monero { try { const auto res = m_client->submit_raw_tx(epee::string_tools::buff_to_hex_nodelimer(cryptonote::tx_to_blob(ptx.tx))); if (res->m_status == boost::none || res->m_status.get() != std::string("OK")) failure = res->m_status ? *res->m_status : std::string("no status"); + else if (res->m_not_relayed.value_or(false)) failure = "not relayed"; } catch (const std::exception &e) { failure = e.what(); } if (!failure.empty()) { refused.push_back(pending_tx_hash + ": " + failure); @@ -2255,7 +2256,7 @@ namespace monero { if (relay) { try { auto submit_res = m_client->submit_raw_tx(full_hex); - if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK")) { + if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK") && !submit_res->m_not_relayed.value_or(false)) { MINFO("monero_wallet_light::create_txs(): relayed tx"); relayed = true; } @@ -2385,7 +2386,7 @@ namespace monero { if (relay) { try { auto submit_res = m_client->submit_raw_tx(full_hex); - if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK")) { + if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK") && !submit_res->m_not_relayed.value_or(false)) { MINFO("monero_wallet_light::sweep_account(): relayed tx"); relayed = true; } @@ -2546,7 +2547,7 @@ namespace monero { if (relay) { try { auto submit_res = m_client->submit_raw_tx(full_hex); - if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK")) { + if (submit_res->m_status != boost::none && submit_res->m_status == std::string("OK") && !submit_res->m_not_relayed.value_or(false)) { MINFO("monero_wallet_light::sweep_output(): relayed tx"); relayed = true; } diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 882d4676..7a04748e 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -600,6 +600,7 @@ namespace monero { for (boost::property_tree::ptree::const_iterator it = node.begin(); it != node.end(); ++it) { std::string key = it->first; if (key == std::string("status")) response->m_status = it->second.data(); + else if (key == std::string("not_relayed")) response->m_not_relayed = it->second.get_value(); } } diff --git a/src/wallet/monero_wallet_light_model.h b/src/wallet/monero_wallet_light_model.h index 5936d902..2ef5316e 100644 --- a/src/wallet/monero_wallet_light_model.h +++ b/src/wallet/monero_wallet_light_model.h @@ -317,6 +317,7 @@ namespace monero { struct monero_submit_raw_tx_response { boost::optional m_status; + boost::optional m_not_relayed; static void from_property_tree(const boost::property_tree::ptree& node, const std::shared_ptr& response); }; From d918f734d54f26a7b190fb226e7d97c7e1bb158b Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:58:53 +0900 Subject: [PATCH 33/46] wallet: name no address of a rescan that a light wallet server does not give --- src/wallet/monero_wallet_light.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 4e5fdace..5610343b 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1530,7 +1530,7 @@ namespace monero { const uint64_t from_height = restore_height > 0 ? restore_height - 1 : 0; auto response = m_client->import_request(from_height); if (response->m_import_fee != boost::none && response->m_import_fee.get() > 0) { - throw std::runtime_error("Payment is required to rescan blockchain: address " + response->m_payment_address.get() + ", amount " + std::to_string(response->m_import_fee.get())); + throw std::runtime_error("Payment is required to rescan blockchain: address " + response->m_payment_address.value_or("") + ", amount " + std::to_string(response->m_import_fee.get())); } // only update the cache once the server confirms the import took effect; otherwise wait for refresh() if (response->m_request_fullfilled != boost::none && response->m_request_fullfilled.get()) { From 6ceac720085aad7e7078291e89971c32ad49d674 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Mon, 16 Nov 2026 22:58:53 +0900 Subject: [PATCH 34/46] wallet: sort the outputs of a light wallet by their amounts --- src/wallet/monero_wallet_light.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 5610343b..b219422d 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -186,16 +186,12 @@ namespace monero { // ----------------------- INTERNAL PRIVATE HELPERS ----------------------- bool output_before(const std::shared_ptr& ow1, const std::shared_ptr& ow2) { - // compare by account index, subaddress index, output index, then global index - if (ow1->m_recipient->m_maj_i < ow2->m_recipient->m_maj_i) return true; - if (ow1->m_recipient->m_maj_i == ow2->m_recipient->m_maj_i) { - if (ow1->m_recipient->m_min_i < ow2->m_recipient->m_min_i) return true; - if (ow1->m_recipient->m_min_i == ow2->m_recipient->m_min_i) { - if (ow1->m_global_index.get() < ow2->m_global_index.get()) return true; - if (ow1->m_global_index.get() == ow2->m_global_index.get()) throw std::runtime_error("Should never sort outputs with duplicate indices"); - } - } - return false; + // compare by account index, subaddress index, amount, then global index: the global index of a + // pre-rct output counts within its amount, so two outputs of different amounts can share it + if (ow1->m_recipient->m_maj_i != ow2->m_recipient->m_maj_i) return ow1->m_recipient->m_maj_i < ow2->m_recipient->m_maj_i; + if (ow1->m_recipient->m_min_i != ow2->m_recipient->m_min_i) return ow1->m_recipient->m_min_i < ow2->m_recipient->m_min_i; + if (ow1->m_amount.get() != ow2->m_amount.get()) return ow1->m_amount.get() < ow2->m_amount.get(); + return ow1->m_global_index.get() < ow2->m_global_index.get(); } void normalize_subaddress_indices(const std::shared_ptr& tx, const tools::wallet2::pending_tx& ptx, const std::vector>& outs) { From 3f8d79722ae62141515821e6d207ff933c0949c4 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:13 +0900 Subject: [PATCH 35/46] wallet: count a subaddress range of a light wallet where it cannot wrap --- src/wallet/monero_wallet_light.cpp | 14 ++++++++------ src/wallet/monero_wallet_light_model.cpp | 5 +++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index b219422d..1a910b3e 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3567,13 +3567,15 @@ namespace monero { const uint32_t range_start = index_range->at(0); const uint32_t range_end = index_range->at(1); // a server that answers a range of the whole space of an index would loop a derivation per index - // of it, and the last one wraps: no account holds more subaddresses than the server itself keeps + // of it, and the last index of the space wraps when it counts up: no account holds more + // subaddresses than the server itself keeps, and the count runs where it cannot wrap if (range_start > range_end || range_end - range_start > 16384) throw std::runtime_error("subaddress range is out of the range of an account: [" + std::to_string(range_start) + ", " + std::to_string(range_end) + "]"); - for (uint32_t i = range_start; i <= range_end; i++) { - if (account_idx == 0 && i == 0) continue; - if (has_watermark && i <= watermark) continue; - const auto& subaddress_spend_pub_key = hwdev.get_subaddress_spend_public_key(account_keys, {account_idx, i}); - m_cache->m_subaddresses[subaddress_spend_pub_key] = {account_idx, i}; + for (uint64_t i = range_start; i <= (uint64_t) range_end; i++) { + const uint32_t subaddress_idx = (uint32_t) i; + if (account_idx == 0 && subaddress_idx == 0) continue; + if (has_watermark && subaddress_idx <= watermark) continue; + const auto& subaddress_spend_pub_key = hwdev.get_subaddress_spend_public_key(account_keys, {account_idx, subaddress_idx}); + m_cache->m_subaddresses[subaddress_spend_pub_key] = {account_idx, subaddress_idx}; } if (range_end > highest) highest = range_end; } diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 7a04748e..4c9af858 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -215,9 +215,10 @@ namespace monero { if (size() != 2) return indices; uint32_t min_i = at(0); uint32_t maj_i = at(1); - // a range of the whole space of an index would fill the memory with indices, and the last one wraps + // a range of the whole space of an index would fill the memory with indices, and the last index of + // the space wraps when it counts up: the count runs where it cannot wrap if (min_i > maj_i || maj_i - min_i > 16384) throw std::runtime_error("subaddress range is out of the range of an account: [" + std::to_string(min_i) + ", " + std::to_string(maj_i) + "]"); - for(uint32_t i = min_i; i <= maj_i; i++) indices.push_back(i); + for(uint64_t i = min_i; i <= (uint64_t) maj_i; i++) indices.push_back((uint32_t) i); return indices; } From 8030cd119e2321faff2c6bc7a78b15336b725180 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:18 +0900 Subject: [PATCH 36/46] wallet: report only the relayed txs of a failed sweep of a light wallet --- src/wallet/monero_wallet_light.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 1a910b3e..be514f30 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2340,9 +2340,8 @@ namespace monero { tx_builder tx_builder(static_cast(m_network_type), m_account.get_keys(), is_view_only(), *m_client, m_cache); std::vector unsigned_construction_data; // a sweep of many outputs relays one tx per iteration, so a build that fails after the first leaves - // them on the network that no returned list names: a build that fails is collected and the loop is - // tried to its end, and the report names the hashes that went out, so a caller that rebuilds knows - // what already pays + // them on the network that no returned list names: the build that fails stops the loop, and the + // report names the hashes that went out, so a caller that rebuilds knows what already pays std::vector failed; while (!unspent_outs.empty()) { @@ -2417,7 +2416,7 @@ namespace monero { if (!failed.empty()) { std::string relayed_list; - for (const auto& tx : result) relayed_list += (relayed_list.empty() ? "" : ", ") + tx->m_hash.get(); + for (const auto& tx : result) if (tx->m_is_relayed == true) relayed_list += (relayed_list.empty() ? "" : ", ") + tx->m_hash.get(); std::string failed_list; for (const auto& f : failed) failed_list += (failed_list.empty() ? "" : "; ") + f; throw std::runtime_error("failed to sweep " + std::to_string(failed.size()) + " of the remaining transactions" + (result.empty() ? std::string() : "; already relayed: " + relayed_list) + "; " + failed_list); From c2cf2ecce606c26aac1807d6a4784eaa4f71579f Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:18 +0900 Subject: [PATCH 37/46] wallet: read a derivation of an output of a light wallet inside its range --- src/wallet/monero_wallet_light.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index be514f30..af28197c 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -848,7 +848,7 @@ namespace monero { } } - if (!found && !additional_derivations.empty()) { + if (!found && output_index < additional_derivations.size()) { const crypto::key_derivation& additional_derivation = additional_derivations[output_index]; if (cryptonote::out_can_be_to_acc(view_tag_opt, additional_derivation, output_index)) { if (!crypto::derive_public_key(additional_derivation, output_index, address.m_spend_public_key, derived_out_key)) throw std::runtime_error("Failed to derive public key"); From 097aa19fa9c6f2554640e642eef4e8f9f2455a12 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:22 +0900 Subject: [PATCH 38/46] wallet: refuse a sweep that relays from a view-only light wallet --- src/wallet/monero_wallet_light.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index af28197c..739b32ac 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2309,6 +2309,9 @@ namespace monero { std::vector> monero_wallet_light::sweep_account(const monero_tx_config& config) { MINFO("monero_wallet_light::sweep_account()"); + // a view-only wallet cannot sign what it sweeps, so a tx that it submits is garbage that the daemon + // refuses: it builds the unsigned set instead, which a full wallet signs and submits + if (config.m_relay != boost::none && config.m_relay.get() && is_view_only()) throw std::runtime_error("Cannot relay from a view-only wallet"); assert_not_closed(); if (is_multisig()) throw std::runtime_error("Multisig wallet not supported"); if (!m_is_connected) throw std::runtime_error("Wallet is not connected to daemon"); @@ -2493,6 +2496,9 @@ namespace monero { std::shared_ptr monero_wallet_light::sweep_output(const monero_tx_config& config) { MINFO("monero_wallet_light::sweep_output()"); + // a view-only wallet cannot sign what it sweeps, so a tx that it submits is garbage that the daemon + // refuses: it builds the unsigned set instead, which a full wallet signs and submits + if (config.m_relay != boost::none && config.m_relay.get() && is_view_only()) throw std::runtime_error("Cannot relay from a view-only wallet"); assert_not_closed(); if (is_multisig()) throw std::runtime_error("Multisig wallet not supported"); if (!m_is_connected) throw std::runtime_error("Wallet is not connected to daemon"); From 3b3977248878f0493224566ac7a0507fc410155d Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:22 +0900 Subject: [PATCH 39/46] model: report an output of a light wallet that a sync replaced as frozen --- src/wallet/monero_wallet_light_model.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 4c9af858..20dff40d 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -1299,7 +1299,7 @@ namespace monero { if (out->is_key_image_known()) { output->m_key_image = std::make_shared(); output->m_key_image.get()->m_hex = out->m_key_image; - output->m_is_frozen = out->m_frozen.value_or(false); + output->m_is_frozen = out->m_frozen.value_or(false) || (out->m_key_image != boost::none && m_frozen_key_images.count(out->m_key_image.get()) > 0); if (!*output->m_is_spent) output->m_is_spent = is_key_image_spent(out->m_key_image.get(), pool_key_images); } From 2e813c76afb04b5ede79c09132114ac4ba78ef28 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:26 +0900 Subject: [PATCH 40/46] wallet: say a check of a tx key of an rpc wallet is not good when the address received nothing --- src/wallet/monero_wallet_rpc.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wallet/monero_wallet_rpc.cpp b/src/wallet/monero_wallet_rpc.cpp index c83f3053..2881e57d 100644 --- a/src/wallet/monero_wallet_rpc.cpp +++ b/src/wallet/monero_wallet_rpc.cpp @@ -1321,6 +1321,10 @@ namespace monero { auto result = m_rpc->send_json_request("check_tx_key", params); check->m_is_good = true; deserialize_check_tx(result, check); + // the wallet behind the rpc answers the fields of the check and not whether the key checked out: + // the address received nothing when the key is not the one of the tx, which the other wallets + // derive the same way + check->m_is_good = check->m_received_amount != boost::none && check->m_received_amount.get() > 0; } catch (const monero_rpc_error& ex) { normalize_wallet_error(ex); } From 624ff044e58908d23cb6a0ff48b4c77ae3406031 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:16:31 +0900 Subject: [PATCH 41/46] utils: drop an include that the digits of a uint64 do not need --- src/utils/gen_utils.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/gen_utils.h b/src/utils/gen_utils.h index e25a9122..15def2ae 100644 --- a/src/utils/gen_utils.h +++ b/src/utils/gen_utils.h @@ -64,7 +64,6 @@ #include #include #include -#include #include #include #include From 7f99e4a7445c7788f39635029b656dfa099c8df9 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:16:59 +0900 Subject: [PATCH 42/46] wallet: end the pause of a light wallet op before it tells its listeners --- src/wallet/monero_wallet_light.cpp | 17 ++++++++++++++--- src/wallet/monero_wallet_light.h | 2 ++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 739b32ac..3e554387 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1759,9 +1759,10 @@ namespace monero { tx_hashes.push_back(pending_tx_hash); } + if (!failed.empty() && !tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache + op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of the funds that left, also when a later one failed if (!failed.empty()) { - if (!tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache std::string relayed_list; for (const auto& h : tx_hashes) relayed_list += (relayed_list.empty() ? "" : ", ") + h; std::string failed_list; @@ -1990,8 +1991,9 @@ namespace monero { } } - m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of spent funds m_cache->calculate_balance(); // the pending spends change the balances, which a wallet that does not sync in the background keeps otherwise stale + op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one + m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of spent funds if (!refused.empty()) { std::string relayed_list; for (const auto& h : tx_hashes) relayed_list += (relayed_list.empty() ? "" : ", ") + h; @@ -2302,6 +2304,7 @@ namespace monero { m_cache->calculate_balance(); + op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one if (any_relayed) m_wallet_listener->on_spend_txs(result); return result; @@ -2490,6 +2493,7 @@ namespace monero { } // notify listeners of spent funds + op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one if (config.m_relay != boost::none && config.m_relay.get()) m_wallet_listener->on_spend_txs(txs); return txs; } @@ -2591,6 +2595,7 @@ namespace monero { m_cache->calculate_balance(); + op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one if (relayed) m_wallet_listener->on_spend_txs(result); return unconfirmed_tx; @@ -2948,11 +2953,17 @@ namespace monero { wallet.m_sync_data_mutex.lock(); } - monero_wallet_light::sync_op_lock::~sync_op_lock() { + void monero_wallet_light::sync_op_lock::unlock() { + if (!m_locked) return; + m_locked = false; m_wallet.m_num_sync_pauses--; // resume background sync before unlocking so the sync does not yield to a finished operation m_wallet.m_sync_data_mutex.unlock(); } + monero_wallet_light::sync_op_lock::~sync_op_lock() { + unlock(); + } + void monero_wallet_light::init_common() { monero_wallet_keys::init_common(); m_cache = std::make_shared(m_key_image_cache); diff --git a/src/wallet/monero_wallet_light.h b/src/wallet/monero_wallet_light.h index 28969373..3f990e1c 100644 --- a/src/wallet/monero_wallet_light.h +++ b/src/wallet/monero_wallet_light.h @@ -230,7 +230,9 @@ namespace monero { sync_op_lock(const monero_wallet_light& wallet); sync_op_lock(const sync_op_lock&) = delete; ~sync_op_lock(); + void unlock(); // end the pause before the scope ends, for a listener that reads the wallet through its own lock const monero_wallet_light& m_wallet; + bool m_locked = true; }; // blockchain sync management From 53031d42239aad984d4bd9034ac409c9608d49b8 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Tue, 17 Nov 2026 11:59:15 +0900 Subject: [PATCH 43/46] wallet: recalculate the balances of a light wallet before its relay tells its listeners --- src/wallet/monero_wallet_light.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 3e554387..1b5e9ea2 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -1759,7 +1759,7 @@ namespace monero { tx_hashes.push_back(pending_tx_hash); } - if (!failed.empty() && !tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache + if (!tx_hashes.empty()) m_cache->calculate_balance(); // the transactions that relayed are in the cache op_lock.unlock(); // the listener reads the balances of the wallet through the lock itself, and the pool that runs it must not wait on this one m_wallet_listener->on_spend_tx_hashes(tx_hashes); // notify listeners of the funds that left, also when a later one failed if (!failed.empty()) { @@ -1770,8 +1770,6 @@ namespace monero { throw std::runtime_error("failed to relay " + std::to_string(failed.size()) + " of " + std::to_string(tx_metadatas.size()) + " transactions" + (tx_hashes.empty() ? std::string() : "; already relayed: " + relayed_list) + "; " + failed_list); } - if (!tx_metadatas.empty()) m_cache->calculate_balance(); - // return relayed tx hashes return tx_hashes; } From f063bb3dfe179a65bf6b5a5378dc889b2f13c4c7 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:26:38 +0900 Subject: [PATCH 44/46] wallet: keep the count of fetched blocks of a light wallet from wrapping --- src/wallet/monero_wallet_light.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 1b5e9ea2..0cee7eb6 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -3493,7 +3493,9 @@ namespace monero { if (restore_height < current_height) { if (last_height < restore_height) last_height = restore_height; - uint64_t blocks_fetched = current_height - last_height; + // the server can name a start height above the height that the wallet scanned, which the subtraction + // below would wrap around on: the wallet fetched nothing then + uint64_t blocks_fetched = current_height > last_height ? current_height - last_height : 0; result.m_num_blocks_fetched = blocks_fetched; if (current_height > last_height) { From a26c03d0276da99a01e806e55fe5543b944463cc Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:25:44 +0900 Subject: [PATCH 45/46] wallet: keep the fee mask of a light wallet from dividing by nothing --- src/wallet/monero_wallet_light_model.cpp | 6 +++++- src/wallet/monero_wallet_light_model.h | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/wallet/monero_wallet_light_model.cpp b/src/wallet/monero_wallet_light_model.cpp index 20dff40d..eeaf3b3f 100644 --- a/src/wallet/monero_wallet_light_model.cpp +++ b/src/wallet/monero_wallet_light_model.cpp @@ -741,9 +741,13 @@ namespace monero { void monero_wallet_light_cache::set_outputs(monero_get_unspent_outs_response& response) { boost::lock_guard lock(m_mutex); + // a server that omits the mask or answers zero would make the fee computation divide by it: the + // value is clamped to a sane bound and read as one at the least, which the fees of a real chain + // never notice but a hostile or broken answer cannot turn into a crash + static const uint64_t MAX_SANE_FEE_MASK = 1000000; // 100x today's real value m_per_byte_fee = response.m_per_byte_fee.value_or(0); m_fees = std::move(response.m_fees); - m_fee_mask = response.m_fee_mask.value_or(0); + m_fee_mask = std::min(response.m_fee_mask.value_or(0), MAX_SANE_FEE_MASK); m_amount = response.m_amount.value_or(0); m_outputs = std::move(response.m_outputs); std::sort(m_outputs.begin(), m_outputs.end(), [](const std::shared_ptr& a, const std::shared_ptr& b) { diff --git a/src/wallet/monero_wallet_light_model.h b/src/wallet/monero_wallet_light_model.h index 2ef5316e..84f2c2b3 100644 --- a/src/wallet/monero_wallet_light_model.h +++ b/src/wallet/monero_wallet_light_model.h @@ -400,7 +400,8 @@ namespace monero { void reindex_outputs(uint64_t amount); uint64_t get_per_byte_fee() const { return m_per_byte_fee; } uint64_t get_base_fee(uint32_t priority) const; - uint64_t get_fee_mask() const { return m_fee_mask; } + // a zero mask would divide by nothing in the fee computation, so it reads as one at the least + uint64_t get_fee_mask() const { return m_fee_mask == 0 ? 1 : m_fee_mask; } uint64_t get_amount() const { return m_amount; } // subaddresses From 0c13f6f82449e6d73299aa3a7ec69bb403c42707 Mon Sep 17 00:00:00 2001 From: mycomputercrashsalot <256064325+mycomputercrashsalot@users.noreply.github.com> Date: Thu, 19 Nov 2026 11:25:44 +0900 Subject: [PATCH 46/46] wallet: sign the key images of a light wallet over their one-time keys --- src/utils/monero_utils.cpp | 11 ++++++++--- src/utils/monero_utils.h | 2 +- src/wallet/monero_wallet_light.cpp | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/utils/monero_utils.cpp b/src/utils/monero_utils.cpp index e35bf2b1..5b0709dd 100644 --- a/src/utils/monero_utils.cpp +++ b/src/utils/monero_utils.cpp @@ -792,7 +792,7 @@ bool monero_utils::parse_payment_id_short(const std::string& payment_id_str, cry return true; } -std::shared_ptr monero_utils::generate_key_image(const crypto::public_key &ephem_pubkey, const size_t tx_output_index, const cryptonote::subaddress_index &received_subaddr, const cryptonote::account_base& account) { +std::shared_ptr monero_utils::generate_key_image(const crypto::public_key &ephem_pubkey, const size_t tx_output_index, const cryptonote::subaddress_index &received_subaddr, const cryptonote::account_base& account, const boost::optional& expected_output_pubkey) { // - R: ephem_pubkey // - a: ack.m_view_secret_key [private viewkey] // - b: ack.m_spend_secret_key [private spendkey] @@ -824,14 +824,19 @@ std::shared_ptr monero_utils::generate_key_image(const crypto: crypto::public_key onetime_pubkey; CHECK_AND_ASSERT_THROW_MES(hwdev.secret_key_to_public_key(onetime_privkey, onetime_pubkey), "Failed to make public key"); + // the wallet derived the one-time key from the transaction key and its own, so a server that names + // another one-time key for the output says the wallet owns money that it does not + if (expected_output_pubkey != boost::none && onetime_pubkey != *expected_output_pubkey) throw std::runtime_error("The one-time key of the output does not match the one the wallet derived"); + // 6. Key image I = x Hp(K) crypto::key_image ki; hwdev.generate_key_image(onetime_pubkey, onetime_privkey, ki); - // sign the key image with the output secret key + // sign the key image with the output secret key over the one-time key it spends: the verifier checks + // the signature against that key, and one over the transaction key would never verify crypto::signature signature; std::vector key_ptrs; - key_ptrs.push_back(&ephem_pubkey); + key_ptrs.push_back(&onetime_pubkey); crypto::generate_ring_signature((const crypto::hash&)ki, ki, key_ptrs, onetime_privkey, 0, &signature); diff --git a/src/utils/monero_utils.h b/src/utils/monero_utils.h index 8c813fff..9992aee0 100644 --- a/src/utils/monero_utils.h +++ b/src/utils/monero_utils.h @@ -240,7 +240,7 @@ namespace monero_utils * @param account recipient's account * @return the generated key image */ - std::shared_ptr generate_key_image(const crypto::public_key &ephem_pubkey, const size_t tx_output_index, const cryptonote::subaddress_index &received_subaddr, const cryptonote::account_base& account); + std::shared_ptr generate_key_image(const crypto::public_key &ephem_pubkey, const size_t tx_output_index, const cryptonote::subaddress_index &received_subaddr, const cryptonote::account_base& account, const boost::optional& expected_output_pubkey = boost::none); // ----------------------------- GATHER BLOCKS ------------------------------ diff --git a/src/wallet/monero_wallet_light.cpp b/src/wallet/monero_wallet_light.cpp index 0cee7eb6..ef787a39 100644 --- a/src/wallet/monero_wallet_light.cpp +++ b/src/wallet/monero_wallet_light.cpp @@ -2788,7 +2788,13 @@ namespace monero { auto cached_key_image = m_key_image_cache->get(output->m_tx_pub_key.get(), output->m_index.get(), account_idx, subaddress_idx); if (cached_key_image != nullptr) key_image = cached_key_image; - else if (!is_view_only()) key_image = generate_key_image(output->m_tx_pub_key.get(), output->m_index.get(), account_idx, subaddress_idx); + else if (!is_view_only()) { + crypto::public_key expected_pub_key; + if (!epee::string_tools::hex_to_pod(output->m_public_key.get(), expected_pub_key)) throw std::runtime_error("Failed to parse output public key at index " + std::to_string(n)); + crypto::public_key tx_pub_key; + if (!epee::string_tools::hex_to_pod(output->m_tx_pub_key.get(), tx_pub_key)) throw std::runtime_error("Failed to parse output tx public key at index " + std::to_string(n)); + key_image = monero_utils::generate_key_image(tx_pub_key, output->m_index.get(), cryptonote::subaddress_index{account_idx, subaddress_idx}, m_account, boost::optional(expected_pub_key)); + } result->m_key_images.push_back(key_image); } @@ -3336,7 +3342,13 @@ namespace monero { if (!spent && !output->is_key_image_known()) { try { - output->m_key_image = generate_key_image(tx_pub_key, output_idx, account_idx, subaddress_idx)->m_hex; + // the expected one-time key is the one the server names for the output, which the derivation + // checks itself against before it signs + crypto::public_key expected_pub_key; + if (!epee::string_tools::hex_to_pod(output->m_public_key.get(), expected_pub_key)) throw std::runtime_error("Failed to parse output public key"); + crypto::public_key tx_pub_key_pod; + if (!epee::string_tools::hex_to_pod(tx_pub_key, tx_pub_key_pod)) throw std::runtime_error("Failed to parse output tx public key"); + output->m_key_image = monero_utils::generate_key_image(tx_pub_key_pod, output_idx, cryptonote::subaddress_index{account_idx, subaddress_idx}, m_account, boost::optional(expected_pub_key))->m_hex; // check key image is spent in unconfirmed transactions spent = m_cache->is_key_image_spent(output->m_key_image.get(), pool_key_images); checked_unconfirmed = true;