From 3d8117d8d56cb478b64dd015331da6a80b116ff2 Mon Sep 17 00:00:00 2001 From: xdustinface Date: Wed, 24 Dec 2025 08:53:01 +0100 Subject: [PATCH] chore: Drop unused `utxo.rs` in `key-wallet` crate --- key-wallet-ffi/src/utxo.rs | 14 +- .../src/wallet/managed_wallet_info/mod.rs | 1 - .../src/wallet/managed_wallet_info/utxo.rs | 216 ------------------ 3 files changed, 7 insertions(+), 224 deletions(-) delete mode 100644 key-wallet/src/wallet/managed_wallet_info/utxo.rs diff --git a/key-wallet-ffi/src/utxo.rs b/key-wallet-ffi/src/utxo.rs index 9e64dde2f..ca9d7b1f9 100644 --- a/key-wallet-ffi/src/utxo.rs +++ b/key-wallet-ffi/src/utxo.rs @@ -1,12 +1,12 @@ //! UTXO management +use crate::error::{FFIError, FFIErrorCode}; +use crate::managed_wallet::FFIManagedWalletInfo; +use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface; use std::ffi::CString; use std::os::raw::c_char; use std::ptr; -use crate::error::{FFIError, FFIErrorCode}; -use crate::managed_wallet::FFIManagedWalletInfo; - /// UTXO structure for FFI #[repr(C)] pub struct FFIUTXO { @@ -99,7 +99,7 @@ pub unsafe extern "C" fn managed_wallet_get_utxos( let managed_info = &*managed_info; // Get UTXOs from the managed wallet info - let utxos = managed_info.inner().get_utxos(); + let utxos = managed_info.inner().utxos(); if utxos.is_empty() { *count_out = 0; @@ -108,10 +108,10 @@ pub unsafe extern "C" fn managed_wallet_get_utxos( // Convert UTXOs to FFI format let mut ffi_utxos = Vec::with_capacity(utxos.len()); - for (outpoint, utxo) in utxos { + for utxo in utxos { // Convert txid to byte array let mut txid_bytes = [0u8; 32]; - txid_bytes.copy_from_slice(&outpoint.txid[..]); + txid_bytes.copy_from_slice(&utxo.outpoint.txid[..]); // Convert address to string let address_str = utxo.address.to_string(); @@ -128,7 +128,7 @@ pub unsafe extern "C" fn managed_wallet_get_utxos( let ffi_utxo = FFIUTXO::new( txid_bytes, - outpoint.vout, + utxo.outpoint.vout, utxo.value(), address_str, script_bytes, diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 9cf3be33b..7dbc41def 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -10,7 +10,6 @@ pub mod managed_account_operations; pub mod managed_accounts; pub mod transaction_builder; pub mod transaction_building; -pub mod utxo; pub mod wallet_info_interface; pub use managed_account_operations::ManagedAccountOperations; diff --git a/key-wallet/src/wallet/managed_wallet_info/utxo.rs b/key-wallet/src/wallet/managed_wallet_info/utxo.rs deleted file mode 100644 index 6716a1256..000000000 --- a/key-wallet/src/wallet/managed_wallet_info/utxo.rs +++ /dev/null @@ -1,216 +0,0 @@ -//! UTXO retrieval functionality for managed wallets -//! -//! This module provides methods to retrieve UTXOs from managed wallet accounts. - -use crate::utxo::Utxo; -use alloc::collections::BTreeMap; -use alloc::vec::Vec; -use dashcore::blockdata::transaction::OutPoint; - -use super::ManagedWalletInfo; - -/// Type alias for UTXOs grouped by account type -type UtxosByAccountType = BTreeMap<&'static str, Vec<(u32, Vec<(OutPoint, Utxo)>)>>; - -impl ManagedWalletInfo { - /// Get all UTXOs for the wallet - /// - /// Returns UTXOs from BIP44, BIP32, and CoinJoin accounts only. - /// Does not include UTXOs from identity or provider accounts. - pub fn get_utxos(&self) -> Vec<(OutPoint, Utxo)> { - let mut all_utxos = Vec::new(); - - // Collect UTXOs from standard BIP44 accounts - for account in self.accounts.standard_bip44_accounts.values() { - for (outpoint, utxo) in &account.utxos { - all_utxos.push((*outpoint, utxo.clone())); - } - } - - // Collect UTXOs from standard BIP32 accounts - for account in self.accounts.standard_bip32_accounts.values() { - for (outpoint, utxo) in &account.utxos { - all_utxos.push((*outpoint, utxo.clone())); - } - } - - // Collect UTXOs from CoinJoin accounts - for account in self.accounts.coinjoin_accounts.values() { - for (outpoint, utxo) in &account.utxos { - all_utxos.push((*outpoint, utxo.clone())); - } - } - - all_utxos - } - - /// Get UTXOs grouped by account type - /// - /// Returns a map where: - /// - Keys are account type strings ("bip44", "bip32", "coinjoin") - /// - Values are vectors of (account_index, Vec<(OutPoint, Utxo)>) tuples - pub fn get_utxos_by_account_type(&self) -> UtxosByAccountType { - let mut utxos_by_type = BTreeMap::new(); - - // Collect BIP44 account UTXOs - let mut bip44_utxos = Vec::new(); - for (index, account) in &self.accounts.standard_bip44_accounts { - let account_utxos: Vec<(OutPoint, Utxo)> = - account.utxos.iter().map(|(outpoint, utxo)| (*outpoint, utxo.clone())).collect(); - if !account_utxos.is_empty() { - bip44_utxos.push((*index, account_utxos)); - } - } - if !bip44_utxos.is_empty() { - utxos_by_type.insert("bip44", bip44_utxos); - } - - // Collect BIP32 account UTXOs - let mut bip32_utxos = Vec::new(); - for (index, account) in &self.accounts.standard_bip32_accounts { - let account_utxos: Vec<(OutPoint, Utxo)> = - account.utxos.iter().map(|(outpoint, utxo)| (*outpoint, utxo.clone())).collect(); - if !account_utxos.is_empty() { - bip32_utxos.push((*index, account_utxos)); - } - } - if !bip32_utxos.is_empty() { - utxos_by_type.insert("bip32", bip32_utxos); - } - - // Collect CoinJoin account UTXOs - let mut coinjoin_utxos = Vec::new(); - for (index, account) in &self.accounts.coinjoin_accounts { - let account_utxos: Vec<(OutPoint, Utxo)> = - account.utxos.iter().map(|(outpoint, utxo)| (*outpoint, utxo.clone())).collect(); - if !account_utxos.is_empty() { - coinjoin_utxos.push((*index, account_utxos)); - } - } - if !coinjoin_utxos.is_empty() { - utxos_by_type.insert("coinjoin", coinjoin_utxos); - } - - utxos_by_type - } - - /// Get spendable UTXOs at a given block height - /// - /// Returns only UTXOs that can be spent at the current height from - /// BIP44, BIP32, and CoinJoin accounts. - pub fn get_spendable_utxos(&self, current_height: u32) -> Vec<(OutPoint, Utxo)> { - self.get_utxos().into_iter().filter(|(_, utxo)| utxo.is_spendable(current_height)).collect() - } - - /// Get total value of all UTXOs - /// - /// Returns the sum of all UTXO values from BIP44, BIP32, and CoinJoin accounts - pub fn get_total_utxo_value(&self) -> u64 { - self.get_utxos().iter().map(|(_, utxo)| utxo.value()).sum() - } - - /// Get UTXO count - /// - /// Returns the total number of UTXOs from BIP44, BIP32, and CoinJoin accounts - pub fn get_utxo_count(&self) -> usize { - let mut count = 0; - - // Count BIP44 account UTXOs - for account in self.accounts.standard_bip44_accounts.values() { - count += account.utxos.len(); - } - - // Count BIP32 account UTXOs - for account in self.accounts.standard_bip32_accounts.values() { - count += account.utxos.len(); - } - - // Count CoinJoin account UTXOs - for account in self.accounts.coinjoin_accounts.values() { - count += account.utxos.len(); - } - - count - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::bip32::DerivationPath; - use crate::managed_account::managed_account_type::ManagedAccountType; - use crate::managed_account::ManagedAccount; - use crate::Network; - use dashcore::{Address, PublicKey, ScriptBuf, TxOut, Txid}; - use dashcore_hashes::Hash; - use std::str::FromStr; - - #[test] - fn test_get_utxos_empty() { - let managed_info = ManagedWalletInfo::new(Network::Testnet, [0u8; 32]); - let utxos = managed_info.get_utxos(); - assert_eq!(utxos.len(), 0); - } - - #[test] - fn test_get_utxos_with_accounts() { - let mut managed_info = ManagedWalletInfo::new(Network::Testnet, [0u8; 32]); - - // Create a BIP44 account with some UTXOs - let base_path = DerivationPath::from_str("m/44'/5'/0'").unwrap(); - let external_path = base_path.child(0.into()); - let internal_path = base_path.child(1.into()); - - let mut bip44_account = ManagedAccount::new( - ManagedAccountType::Standard { - index: 0, - standard_account_type: - crate::account::account_type::StandardAccountType::BIP44Account, - external_addresses: - crate::managed_account::address_pool::AddressPool::new_without_generation( - external_path, - crate::managed_account::address_pool::AddressPoolType::External, - 20, - Network::Testnet, - ), - internal_addresses: - crate::managed_account::address_pool::AddressPool::new_without_generation( - internal_path, - crate::managed_account::address_pool::AddressPoolType::Internal, - 20, - Network::Testnet, - ), - }, - Network::Testnet, - false, - ); - - // Add a test UTXO - let outpoint = OutPoint { - txid: Txid::all_zeros(), - vout: 0, - }; - let txout = TxOut { - value: 100000, - script_pubkey: ScriptBuf::new(), - }; - let address = Address::p2pkh( - &PublicKey::from_slice(&[ - 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x01, - ]) - .unwrap(), - Network::Testnet, - ); - let utxo = Utxo::new(outpoint, txout, address, 0, false); - - bip44_account.utxos.insert(outpoint, utxo); - managed_info.accounts.standard_bip44_accounts.insert(0, bip44_account); - - // Test getting UTXOs - let utxos = managed_info.get_utxos(); - assert_eq!(utxos.len(), 1); - assert_eq!(utxos[0].1.value(), 100000); - } -}