What changed, and why it matters
This patch fixes a bookkeeping bug in Monero's wallet. When a user spent funds from one of their own accounts and the same transaction also sent change or payments to other subaddresses within that same account, the wallet was only removing the first matching incoming record instead of all matching ones. This could leave phantom 'incoming' payments showing money that was actually the user's own change, making the wallet balance and history look wrong. The fix now removes all matching records that belong to the spending account while preserving genuine incoming payments to other accounts. A new functional test confirms the corrected behavior.
Review and merge the fix; run the updated cold_signing functional test to confirm multi-subaddress self-payments are cleaned up correctly. Users relying on view-only or cold wallets should update to avoid balance/history discrepancies.
Security signals we found
Incorrect state cleanup in wallet transaction processing
Self-payment / change records incorrectly retained as incoming payments
Functional test added to reproduce multi-subaddress self-payment scenario
Evidence from the diff
In wallet2::import_key_images, the original loop searched m_payments for a record with the spent transaction’s hash and erased only the first match, then broke. If a single transaction paid multiple subaddresses within the same account, the remaining matching payment entries were not removed, causing stale incoming-payment records for what is effectively change/self-payment. The patch changes the loop to iterate over all m_payments, erasing every entry whose tx_hash equals the spent_txid and whose subaddr_index.major equals the spending subaddr_account, while advancing the iterator safely. The accompanying cold_signing.py test creates a transfer to multiple subaddresses plus a separate account, verifies incoming records exist before key-image import, and asserts that after import only the other account’s genuine incoming payment remains.
Changed components
src/wallet/wallet2.cppwallet2::import_key_imagestests/functional_tests/cold_signing.pyInspect captured patch +49 / −12
### src/wallet/wallet2.cpp
@@ -13891,14 +13891,15 @@ uint64_t wallet2::import_key_images(const std::vector<std::pair<crypto::key_imag
// create outgoing payment
process_outgoing(*spent_txid, spent_tx, e.block_height, e.block_timestamp, tx_money_spent_in_ins, received_by_account[subaddr_account], subaddr_account, subaddr_indices);
- // erase corresponding incoming payment
- for (auto j = m_payments.begin(); j != m_payments.end(); ++j)
+ // erase incoming payment(s) that were actually just change back to the spending
+ // account; a tx can pay more than one of our subaddresses, and payments to other
+ // accounts are genuine incoming payments from their point of view, so keep those
+ for (auto j = m_payments.begin(); j != m_payments.end(); )
{
- if (j->second.m_tx_hash == *spent_txid)
- {
- m_payments.erase(j);
- break;
- }
+ if (j->second.m_tx_hash == *spent_txid && j->second.m_subaddr_index.major == subaddr_account)
+ j = m_payments.erase(j);
+ else
+ ++j;
}
++spent_txid;
### tests/functional_tests/cold_signing.py
@@ -300,14 +300,50 @@ def in_hash_list(txid, tx_hash_list):
def transfer_to_multi_subaddresses(self):
# This test triggers the non-standard additional keys case
# const bool need_additional_txkeys = num_subaddresses > 0 && (num_stdaddresses > 0 || num_subaddresses > 1);
- print("Transfer to 2 subaddresses in 1 tx")
- dst1 = {'address': SUBADDRESS, 'amount': 1000000000000}
- dst2 = {'address': SUBADDRESS2, 'amount': 1000000000000}
+ print("Transfer to multiple subaddresses and another account")
+ amount = 1000000000000
+ other_account = self.hot_wallet.create_account()
+ assert self.cold_wallet.create_account().address == other_account.address
+ dst1 = {'address': SUBADDRESS, 'amount': amount}
+ dst2 = {'address': SUBADDRESS2, 'amount': amount}
+ dst3 = {'address': other_account.address, 'amount': amount}
self.export_import(False)
- res = self.hot_wallet.transfer([dst1, dst2])
+ res = self.hot_wallet.transfer([dst1, dst2, dst3])
+ txids = self.sign_and_submit(res.unsigned_txset)
+ assert len(txids) == 1
+ txid = txids[0]
- self.sign_and_submit(res.unsigned_txset)
+ # Scan without key images so self-payments initially appear incoming.
+ observer = Wallet(idx = 1)
+ try: observer.close_wallet()
+ except: pass
+ observer.generate_from_keys(
+ viewkey = self.cold_wallet.query_key("view_key").key,
+ address = STANDARD_ADDRESS)
+ assert observer.create_account().address == other_account.address
+ observer.refresh()
+
+ def incoming(account_index):
+ transfers = observer.get_transfers(account_index = account_index)
+ return sorted((entry.subaddr_index.minor, entry.amount)
+ for entry in transfers.get('in', []) if entry.txid == txid)
+
+ # At least two source-account records must exist to catch erase-one.
+ assert {1, 2} <= {minor for minor, _ in incoming(0)}
+ assert incoming(other_account.account_index) == [(0, amount)]
+
+ outputs = observer.export_outputs(all = True)
+ self.cold_wallet.import_outputs(outputs.outputs_data_hex)
+ key_images = self.cold_wallet.export_key_images(True)
+ observer.import_key_images(key_images.signed_key_images, offset = key_images.offset)
+
+ # Remove every source-account payment, preserving the other account.
+ assert incoming(0) == []
+ assert incoming(other_account.account_index) == [(0, amount)]
+ outgoing = observer.get_transfers(account_index = 0).get('out', [])
+ assert len([entry for entry in outgoing if entry.txid == txid]) == 1
+ observer.close_wallet()
def self_transfer_to_other_account(self):
print("Self-spending to another account in a view-only wallet")Why this scored 58/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.