summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsneurlax <sneurlax@gmail.com>2024-10-19 17:58:53 -0500
committersneurlax <sneurlax@gmail.com>2024-10-19 17:58:53 -0500
commit230d29a66ac776cd03c611a5f44fb663d4177992 (patch)
tree912a7a03d6726aef32115e1214554832842160de
parent57dce1e530351a0d58a5ac651ff90f64848982b2 (diff)
add set_seed_language and use it to fix the generate_from_keys tests
and fix tests
-rw-r--r--impls/monero.rs/src/lib.rs94
-rw-r--r--impls/monero.rs/tests/integration_tests.rs59
2 files changed, 140 insertions, 13 deletions
diff --git a/impls/monero.rs/src/lib.rs b/impls/monero.rs/src/lib.rs
index 5786a62..4cd6dd3 100644
--- a/impls/monero.rs/src/lib.rs
+++ b/impls/monero.rs/src/lib.rs
@@ -210,18 +210,19 @@ impl WalletManager {
/// use monero_c_rust::{WalletManager, NetworkType};
/// use std::fs;
/// use std::path::Path;
+ /// use tempfile::TempDir;
+ ///
+ /// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
+ /// let wallet_path = temp_dir.path().join("test_wallet");
+ /// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
- /// let wallet = manager.create_wallet("test_wallet", "password", "English", NetworkType::Mainnet);
+ /// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
/// assert!(wallet.is_ok());
///
- /// // Cleanup: remove the wallet file and its corresponding keys file, if they exist.
- /// if Path::new("test_wallet").exists() {
- /// fs::remove_file("test_wallet").expect("Failed to delete test wallet");
- /// }
- /// if Path::new("test_wallet.keys").exists() {
- /// fs::remove_file("test_wallet.keys").expect("Failed to delete test wallet keys");
- /// }
+ /// // Clean up wallet files.
+ /// std::fs::remove_file(wallet_str).expect("Failed to delete test wallet");
+ /// std::fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn create_wallet(
self: &Arc<Self>,
@@ -279,10 +280,17 @@ impl WalletManager {
///
/// ```rust
/// use monero_c_rust::{WalletManager, NetworkType};
+ /// use std::fs;
+ /// use std::path::Path;
+ /// use tempfile::TempDir;
+ ///
+ /// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
+ /// let wallet_path = temp_dir.path().join("test_wallet");
+ /// let wallet_str = wallet_path.to_str().unwrap();
///
/// let manager = WalletManager::new().unwrap();
/// let result = manager.generate_from_keys(
- /// "new_wallet".to_string(),
+ /// wallet_str.to_string(),
/// "45wsWad9EwZgF3VpxQumrUCRaEtdyyh6NG8sVD3YRVVJbK1jkpJ3zq8WHLijVzodQ22LxwkdWx7fS2a6JzaRGzkNU8K2Dhi".to_string(), // Replace with a valid address
/// "29adefc8f67515b4b4bf48031780ab9d071d24f8a674b879ce7f245c37523807".to_string(),
/// "3bc0b202cde92fe5719c3cc0a16aa94f88a5d19f8c515d4e35fae361f6f2120e".to_string(),
@@ -290,10 +298,13 @@ impl WalletManager {
/// "password".to_string(),
/// "English".to_string(),
/// NetworkType::Mainnet,
- /// true,
/// 1, // Default KDF rounds
/// );
/// assert!(result.is_ok(), "Failed to generate wallet from keys: {:?}", result.err());
+ ///
+ /// // Clean up wallet files.
+ /// std::fs::remove_file(wallet_str).expect("Failed to delete test wallet");
+ /// std::fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
/// ```
pub fn generate_from_keys(
self: &Arc<Self>,
@@ -1135,6 +1146,46 @@ impl Wallet {
})
}
}
+
+ /// Sets the seed language for the wallet.
+ ///
+ /// Changing the seed language can be useful for wallets that support multiple languages.
+ ///
+ /// # Arguments
+ ///
+ /// * `language` - The new language to set for the wallet's seed.
+ ///
+ /// # Example
+ ///
+ /// ```rust
+ /// use monero_c_rust::{WalletManager, NetworkType};
+ /// use tempfile::TempDir;
+ /// use std::fs;
+ ///
+ /// let temp_dir = TempDir::new().expect("Failed to create temporary directory");
+ /// let wallet_path = temp_dir.path().join("test_wallet");
+ /// let wallet_str = wallet_path.to_str().unwrap();
+ ///
+ /// let manager = WalletManager::new().unwrap();
+ /// let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet).unwrap();
+ ///
+ /// // Change the seed language to Spanish
+ /// let result = wallet.set_seed_language("Spanish");
+ /// assert!(result.is_ok(), "Failed to set seed language: {:?}", result.err());
+ ///
+ /// // Clean up wallet files.
+ /// fs::remove_file(wallet_str).expect("Failed to delete test wallet");
+ /// fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
+ /// ```
+ pub fn set_seed_language(&self, language: &str) -> WalletResult<()> {
+ let c_language = CString::new(language)
+ .map_err(|_| WalletError::FfiError("Invalid language string".to_string()))?;
+
+ unsafe {
+ bindings::MONERO_Wallet_setSeedLanguage(self.ptr.as_ptr(), c_language.as_ptr());
+ self.throw_if_error()
+ }
+ }
}
#[derive(Debug)]
@@ -1593,3 +1644,26 @@ fn test_refresh_success() {
teardown(&temp_dir).expect("Failed to clean up after test");
}
+
+#[test]
+fn test_set_seed_language() {
+ let (manager, temp_dir) = setup().expect("Failed to set up test environment");
+
+ let wallet_path = temp_dir.path().join("test_wallet_set_seed_language");
+ let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
+
+ // Create a new wallet.
+ let wallet = manager
+ .create_wallet(wallet_str, "password", "English", NetworkType::Mainnet)
+ .expect("Failed to create wallet");
+
+ // Set the seed language to Spanish.
+ let result = wallet.set_seed_language("Spanish");
+ assert!(result.is_ok(), "Failed to set seed language: {:?}", result.err());
+
+ // Optionally, retrieve the seed language to verify it was set correctly.
+ // This requires implementing a corresponding `get_seed_language` method.
+ // For now, we'll assume that if no error was returned, the operation was successful.
+
+ 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 1871d0f..d791706 100644
--- a/impls/monero.rs/tests/integration_tests.rs
+++ b/impls/monero.rs/tests/integration_tests.rs
@@ -98,7 +98,7 @@ fn test_wallet_manager_creation() {
fn test_wallet_creation() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
- let wallet_path = temp_dir.path().join("wallet_name");
+ let wallet_path = temp_dir.path().join("test_wallet");
let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
let wallet = manager.create_wallet(wallet_str, "password", "English", NetworkType::Mainnet);
@@ -114,8 +114,11 @@ fn test_generate_from_keys_integration() {
println!("Running test_generate_from_keys_integration");
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
+ let wallet_path = temp_dir.path().join("test_wallet");
+ let wallet_str = wallet_path.to_str().expect("Failed to convert wallet path to string");
+
let wallet = manager.generate_from_keys(
- "test_wallet".to_string(),
+ wallet_str.to_string(),
"45wsWad9EwZgF3VpxQumrUCRaEtdyyh6NG8sVD3YRVVJbK1jkpJ3zq8WHLijVzodQ22LxwkdWx7fS2a6JzaRGzkNU8K2Dhi".to_string(),
"29adefc8f67515b4b4bf48031780ab9d071d24f8a674b879ce7f245c37523807".to_string(),
"3bc0b202cde92fe5719c3cc0a16aa94f88a5d19f8c515d4e35fae361f6f2120e".to_string(),
@@ -130,8 +133,20 @@ fn test_generate_from_keys_integration() {
// Verify that the wallet was generated correctly.
let wallet = wallet.expect("Failed to create wallet");
+
+ // This is required even though "English" was passed as the seed language above.
+ let set_language_result = wallet.set_seed_language("English");
+ assert!(
+ set_language_result.is_ok(),
+ "Failed to set seed language: {:?}",
+ set_language_result.err()
+ );
+
+ // The address should be "45wsWad9...".
let address_result = wallet.get_address(0, 0);
assert!(address_result.is_ok(), "Failed to get address: {:?}", address_result.err());
+ let address = address_result.unwrap();
+ assert_eq!(address, "45wsWad9EwZgF3VpxQumrUCRaEtdyyh6NG8sVD3YRVVJbK1jkpJ3zq8WHLijVzodQ22LxwkdWx7fS2a6JzaRGzkNU8K2Dhi");
// Get the seed. It should be "hemlock jubilee...".
let seed_result = wallet.get_seed(None);
@@ -345,7 +360,7 @@ fn test_open_wallet_integration() {
fn test_open_wallet_invalid_password() {
let (manager, temp_dir) = setup().expect("Failed to set up test environment");
- let wallet_path = temp_dir.path().join("wallet_name");
+ 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 a wallet with a valid password.
@@ -641,3 +656,41 @@ fn test_init_integration_success() {
teardown(&temp_dir).expect("Failed to clean up after test");
}
+
+#[test]
+fn test_set_seed_language_integration() {
+ println!("Running test_set_seed_language_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("set_seed_language_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");
+ println!("Wallet created successfully.");
+
+ // Set the seed language to French.
+ println!("Setting seed language to French...");
+ let start = Instant::now();
+ let set_language_result = wallet.set_seed_language("French");
+ let duration = start.elapsed();
+ println!("set_seed_language took {:?}", duration);
+
+ assert!(
+ set_language_result.is_ok(),
+ "Failed to set seed language: {:?}",
+ set_language_result.err()
+ );
+
+ // Optionally, verify the seed language by retrieving it.
+ // This requires implementing a corresponding `get_seed_language` method.
+
+ // Clean up wallet files.
+ fs::remove_file(wallet_str).expect("Failed to delete test wallet");
+ fs::remove_file(format!("{}.keys", wallet_str)).expect("Failed to delete test wallet keys");
+
+ teardown(&temp_dir).expect("Failed to clean up after test");
+}