From 6ba060e116eafa524fdc98eacc9fccf460cf0bcd Mon Sep 17 00:00:00 2001 From: Some Random Crypto Guy Date: Wed, 15 Oct 2025 13:29:04 +0100 Subject: [PATCH] fixed subaddress lookahead generation for Carrot; optimised to reduce scanning times --- src/carrot_core/account.cpp | 19 ++++++++++++------- src/carrot_core/account.h | 2 +- src/wallet/wallet2.cpp | 7 +++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/carrot_core/account.cpp b/src/carrot_core/account.cpp index 9c6cae6..f0b3433 100644 --- a/src/carrot_core/account.cpp +++ b/src/carrot_core/account.cpp @@ -282,13 +282,13 @@ crypto::key_image carrot_and_legacy_account::derive_key_image(const crypto::publ return L; } //---------------------------------------------------------------------------------------------------------------------- -void carrot_and_legacy_account::generate_subaddress_map() +void carrot_and_legacy_account::generate_subaddress_map(const std::pair& lookahead_size) { const std::vector derive_types{AddressDeriveType::Carrot, AddressDeriveType::PreCarrot}; - for (uint32_t major_index = 0; major_index <= MAX_SUBADDRESS_MAJOR_INDEX; ++major_index) + for (uint32_t major_index = 0; major_index <= lookahead_size.first; ++major_index) { - for (uint32_t minor_index = 0; minor_index <= MAX_SUBADDRESS_MINOR_INDEX; ++minor_index) + for (uint32_t minor_index = 0; minor_index <= lookahead_size.first; ++minor_index) { for (const AddressDeriveType derive_type : derive_types) { @@ -361,7 +361,6 @@ void carrot_and_legacy_account::create_from_svb_key(const cryptonote::account_pu ); this->default_derive_type = AddressDeriveType::Carrot; - generate_subaddress_map(); } //---------------------------------------------------------------------------------------------------------------------- void carrot_and_legacy_account::set_carrot_keys(const AddressDeriveType default_derive_type) @@ -393,13 +392,19 @@ void carrot_and_legacy_account::set_carrot_keys(const AddressDeriveType default_ m_keys.m_carrot_main_address.m_is_carrot = true; this->default_derive_type = default_derive_type; - generate_subaddress_map(); } //---------------------------------------------------------------------------------------------------------------------- void carrot_and_legacy_account::insert_subaddresses(const std::unordered_map& subaddress_map_cn) { - for (const auto &p : subaddress_map_cn) - subaddress_map.insert({p.first, {{p.second.index.major, p.second.index.minor}, p.second.derive_type, p.second.is_return_spend_key}}); + for (const auto &p : subaddress_map_cn) { + subaddress_map.insert({p.first, {{p.second.index.major, p.second.index.minor}, p.second.derive_type, p.second.is_return_spend_key}}); + if (p.second.derive_type == AddressDeriveType::PreCarrot) { + // Create a matching Carrot address + const subaddress_index_extended subaddr_index{{p.second.index.major, p.second.index.minor}, AddressDeriveType::Carrot, p.second.is_return_spend_key}; + const CarrotDestinationV1 addr = subaddress(subaddr_index); + subaddress_map.insert({addr.address_spend_pubkey, subaddr_index}); + } + } } //---------------------------------------------------------------------------------------------------------------------- void carrot_and_legacy_account::insert_return_output_info(const std::unordered_map& roi_map) diff --git a/src/carrot_core/account.h b/src/carrot_core/account.h index 0df6460..94695f8 100644 --- a/src/carrot_core/account.h +++ b/src/carrot_core/account.h @@ -144,7 +144,7 @@ namespace carrot const crypto::secret_key &sender_extension_t, const crypto::public_key &onetime_address) const; - void generate_subaddress_map(); + void generate_subaddress_map(const std::pair& lookahead_size); crypto::secret_key generate( const crypto::secret_key& recovery_key = crypto::secret_key(), diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index bd46b82..444fa22 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1651,6 +1651,7 @@ bool wallet2::should_expand(const cryptonote::subaddress_index &index) const void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index) { hw::device &hwdev = m_account.get_device(); + std::unordered_map new_addresses; if (m_subaddress_labels.size() <= index.major) { // add new accounts @@ -1664,6 +1665,7 @@ void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index) { const crypto::public_key &D = pkeys[index2.minor]; m_subaddresses[D] = index2; + new_addresses.insert({D, {{index2.major, index2.minor}, carrot::AddressDeriveType::PreCarrot, false}}); } } m_subaddress_labels.resize(index.major + 1, {"Untitled account"}); @@ -1681,9 +1683,12 @@ void wallet2::expand_subaddresses(const cryptonote::subaddress_index& index) { const crypto::public_key &D = pkeys[index2.minor - begin]; m_subaddresses[D] = index2; + new_addresses.insert({D, {{index2.major, index2.minor}, carrot::AddressDeriveType::PreCarrot, false}}); } m_subaddress_labels[index.major].resize(index.minor + 1); } + // Add to the Carrot account + m_account.insert_subaddresses(new_addresses); } //---------------------------------------------------------------------------------------------------- void wallet2::create_one_off_subaddress(const cryptonote::subaddress_index& index) @@ -6653,6 +6658,8 @@ void wallet2::load(const std::string& wallet_, const epee::wipeable_string& pass if (get_num_subaddress_accounts() == 0) add_subaddress_account(tr("Primary account")); + m_account.generate_subaddress_map(get_subaddress_lookahead()); + // populate account subaddress list if (!m_subaddresses.empty()) {