From 0c0c4133b8edb1f9c8c972b9bacf89f58e04f7e7 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Fri, 18 Oct 2024 17:51:41 -0500 Subject: add create_account --- impls/monero.rs/src/lib.rs | 47 ++++++++++++++++++++++++++++++ impls/monero.rs/tests/integration_tests.rs | 25 ++++++++++++++++ 2 files changed, 72 insertions(+) (limited to 'impls') diff --git a/impls/monero.rs/src/lib.rs b/impls/monero.rs/src/lib.rs index 6ab309d..2f9bac7 100644 --- a/impls/monero.rs/src/lib.rs +++ b/impls/monero.rs/src/lib.rs @@ -453,6 +453,34 @@ impl Wallet { Ok(GetBalance { balance, unlocked_balance }) } } + + /// Creates a new subaddress account with the given label. + /// + /// # Arguments + /// + /// * `label` - A string representing the label for the new subaddress account. + /// + /// # Returns + /// + /// * `WalletResult<()>` - `Ok(())` if the account was successfully created, or a `WalletError` if an error occurred. + /// + /// # Example + /// + /// ``` + /// use monero_c_rust::{WalletManager, NetworkType}; + /// let manager = WalletManager::new().unwrap(); + /// let wallet = manager.create_wallet("wallet_name", "password", "English", NetworkType::Mainnet).unwrap(); + /// let result = wallet.create_account("New Account"); + /// assert!(result.is_ok()); + /// ``` + pub fn create_account(&self, label: &str) -> WalletResult<()> { + let c_label = CString::new(label).map_err(|_| WalletError::FfiError("Invalid label".to_string()))?; + + unsafe { + bindings::MONERO_Wallet_addSubaddressAccount(self.ptr.as_ptr(), c_label.as_ptr()); + self.throw_if_error() + } + } } #[derive(Debug)] @@ -720,3 +748,22 @@ fn test_get_balance() { teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[test] +fn test_create_account() { + let (manager, temp_dir) = setup().expect("Failed to set up test environment"); + + let wallet_path = temp_dir.path().join("wallet_name"); + let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); + + // Create a wallet. + let wallet = manager + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) + .expect("Failed to create wallet"); + + // Create a new account. + let result = wallet.create_account("Test Account"); + assert!(result.is_ok(), "Failed to create account: {:?}", result.err()); + + teardown(&temp_dir).expect("Failed to clean up after test"); +} diff --git a/impls/monero.rs/tests/integration_tests.rs b/impls/monero.rs/tests/integration_tests.rs index 7c7fd0a..fd96228 100644 --- a/impls/monero.rs/tests/integration_tests.rs +++ b/impls/monero.rs/tests/integration_tests.rs @@ -390,3 +390,28 @@ fn test_get_balance_integration() { teardown(&temp_dir).expect("Failed to clean up after test"); } + +#[test] +fn test_create_account_integration() { + println!("Running test_create_account_integration"); + let (manager, temp_dir) = setup().expect("Failed to set up test environment"); + + // Construct the full path for the wallet within temp_dir. + let wallet_path = temp_dir.path().join("test_wallet"); + let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string"); + + // Create the wallet. + let wallet = manager + .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet) + .expect("Failed to create wallet"); + + // Create a new account with a label. + println!("Creating a new account..."); + let start = Instant::now(); + let result = wallet.create_account("Test Account Integration"); + println!("create_account took {:?}", start.elapsed()); + + assert!(result.is_ok(), "Failed to create account: {:?}", result.err()); + + teardown(&temp_dir).expect("Failed to clean up after test"); +} -- cgit v1.2.3