diff options
| author | sneurlax <sneurlax@gmail.com> | 2024-10-10 20:52:31 -0500 |
|---|---|---|
| committer | sneurlax <sneurlax@gmail.com> | 2024-10-10 20:52:31 -0500 |
| commit | 4694bb913f7709c2b2fa568f065cc56f92f09555 (patch) | |
| tree | 1c46d48bc33552f2b2a2c8656d66f0d6fe13b9f6 /impls/monero_rust/src/main.rs | |
| parent | a42cbbe83120bd4724a050e03fb95802a4a5ff42 (diff) | |
convert to lib
Diffstat (limited to 'impls/monero_rust/src/main.rs')
| -rw-r--r-- | impls/monero_rust/src/main.rs | 102 |
1 files changed, 23 insertions, 79 deletions
diff --git a/impls/monero_rust/src/main.rs b/impls/monero_rust/src/main.rs index a51e542..eb9e148 100644 --- a/impls/monero_rust/src/main.rs +++ b/impls/monero_rust/src/main.rs @@ -1,83 +1,27 @@ -#![allow(non_upper_case_globals)] -#![allow(non_camel_case_types)] -#![allow(non_snake_case)] - -use std::ffi::{CStr, CString}; -use std::os::raw::{c_char, c_int, c_void}; - -const NetworkType_MAINNET: c_int = 0; -// const NetworkType_TESTNET: c_int = 1; -// const NetworkType_STAGENET: c_int = 2; - -extern "C" { - // Get the wallet manager instance. - pub fn MONERO_WalletManagerFactory_getWalletManager() -> *mut c_void; - - // Create a new wallet. - pub fn MONERO_WalletManager_createWallet( - wm_ptr: *mut c_void, - path: *const c_char, - password: *const c_char, - language: *const c_char, - networkType: c_int, - ) -> *mut c_void; - - // Retrieve the seed from the wallet. - pub fn MONERO_Wallet_seed( - wallet_ptr: *mut c_void, - seed_offset: *const c_char, - ) -> *const c_char; - - // Close the wallet. - pub fn MONERO_WalletManager_closeWallet( - wm_ptr: *mut c_void, - wallet_ptr: *mut c_void, - store: bool, - ) -> bool; -} - -fn main() { - unsafe { - // Initialize the Wallet Manager. - let wm_ptr = MONERO_WalletManagerFactory_getWalletManager(); - if wm_ptr.is_null() { - eprintln!("Failed to get WalletManager"); - return; +use monero_rust::{ + WalletError, WalletManager, NETWORK_TYPE_MAINNET, +}; + +fn main() -> Result<(), WalletError> { + let wallet_manager = WalletManager::new()?; + + let wallet = wallet_manager.create_wallet( + "wallet", + "password", + "English", + NETWORK_TYPE_MAINNET, + )?; + + match wallet.get_seed("") { + Ok(seed) => println!("Seed: {}", seed), + Err(e) => { + eprintln!("Failed to get seed: {:?}", e); + return Err(e); } + } - // Set up parameters for the new wallet. - let path = CString::new("my_wallet").expect("CString::new failed"); - let password = CString::new("password").expect("CString::new failed"); - let language = CString::new("English").expect("CString::new failed"); - let network_type = NetworkType_MAINNET; - - // Create a new wallet. - let wallet_ptr = MONERO_WalletManager_createWallet( - wm_ptr, - path.as_ptr(), - password.as_ptr(), - language.as_ptr(), - network_type, - ); - if wallet_ptr.is_null() { - eprintln!("Failed to create wallet"); - return; - } + let address = wallet.get_address(0, 0)?; + println!("Wallet Address: {}", address); - // Get the seed. - let seed_offset = CString::new("").expect("CString::new failed"); - let seed_cstr = MONERO_Wallet_seed(wallet_ptr, seed_offset.as_ptr()); - if seed_cstr.is_null() { - eprintln!("Failed to get seed"); - } else { - let seed = CStr::from_ptr(seed_cstr).to_string_lossy().into_owned(); - println!("Seed: {}", seed); - } - - // Close the wallet. - let result = MONERO_WalletManager_closeWallet(wm_ptr, wallet_ptr, false); - if !result { - eprintln!("Failed to close wallet"); - } - } + Ok(()) } |
