diff options
| author | sneurlax <sneurlax@gmail.com> | 2024-10-20 16:09:04 -0500 |
|---|---|---|
| committer | sneurlax <sneurlax@gmail.com> | 2024-10-20 16:09:04 -0500 |
| commit | 4d339e8bf5226c545526196cab4386bbcd1ddc5e (patch) | |
| tree | 8700f4a9bc0659d4aa5a204ddf0cac9ed3fc2730 /impls/monero.rs/src | |
| parent | 5e9a87823c8b59ade0262f8789cede26728caf6c (diff) | |
add WIP check_tx_key
and reorganize and remove extraneous wallet2_api_c.h additions
Diffstat (limited to 'impls/monero.rs/src')
| -rw-r--r-- | impls/monero.rs/src/bindings.rs | 11 | ||||
| -rw-r--r-- | impls/monero.rs/src/lib.rs | 103 |
2 files changed, 114 insertions, 0 deletions
diff --git a/impls/monero.rs/src/bindings.rs b/impls/monero.rs/src/bindings.rs index 556f5ad..b7543b1 100644 --- a/impls/monero.rs/src/bindings.rs +++ b/impls/monero.rs/src/bindings.rs @@ -1244,6 +1244,17 @@ extern "C" { ) -> *const ::std::os::raw::c_char; } extern "C" { + pub fn MONERO_Wallet_checkTxKey( + wallet_ptr: *mut ::std::os::raw::c_void, + txid: *const ::std::os::raw::c_char, + tx_key: *const ::std::os::raw::c_char, + address: *const ::std::os::raw::c_char, + received: *mut u64, + in_pool: *mut bool, + confirmations: *mut u64, + ) -> bool; +} +extern "C" { pub fn MONERO_Wallet_signMessage( wallet_ptr: *mut ::std::os::raw::c_void, message: *const ::std::os::raw::c_char, diff --git a/impls/monero.rs/src/lib.rs b/impls/monero.rs/src/lib.rs index cfc8d46..dae1e3c 100644 --- a/impls/monero.rs/src/lib.rs +++ b/impls/monero.rs/src/lib.rs @@ -115,6 +115,21 @@ pub struct Transfer { pub fee: u64, } +/// Represents the result of checking a transaction key against a transaction ID and address. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct CheckTxKey { + /// Indicates whether the transaction key is valid. + pub valid: bool, + /// The amount received in the transaction, if valid. + pub received: Option<u64>, + /// Whether the transaction is in the pool, if valid. + pub in_pool: Option<bool>, + /// The number of confirmations, if valid. + pub confirmations: Option<u64>, + /// An optional error message providing details if the verification fails. + pub error: Option<String>, +} + impl WalletManager { /// Creates a new `WalletManager`. /// @@ -1166,6 +1181,94 @@ impl Wallet { self.throw_if_error() } } + + /// Checks the validity of a transaction key for a given transaction ID and address. + /// + /// This method verifies whether the provided transaction key (`tx_key`) is valid for the + /// specified transaction ID (`txid`) and address (`address`). If valid, it returns the + /// amount received, whether the transaction is in the pool, and the number of confirmations. + /// + /// TODO: Example / docs-tests. + pub fn check_tx_key(&self, txid: String, tx_key: String, address: String) -> CheckTxKey { + // Convert Rust strings to C strings. + let c_txid = match CString::new(txid) { + Ok(cstr) => cstr, + Err(_) => return CheckTxKey { + valid: false, + received: None, + in_pool: None, + confirmations: None, + error: Some("Invalid txid string".to_string()), + }, + }; + let c_tx_key = match CString::new(tx_key) { + Ok(cstr) => cstr, + Err(_) => return CheckTxKey { + valid: false, + received: None, + in_pool: None, + confirmations: None, + error: Some("Invalid tx_key string".to_string()), + }, + }; + let c_address = match CString::new(address) { + Ok(cstr) => cstr, + Err(_) => return CheckTxKey { + valid: false, + received: None, + in_pool: None, + confirmations: None, + error: Some("Invalid address string".to_string()), + }, + }; + + // Prepare output variables. + let mut received: u64 = 0; + let mut in_pool: bool = false; + let mut confirmations: u64 = 0; + + // Call the C function. + let result = unsafe { + bindings::MONERO_Wallet_checkTxKey( + self.ptr.as_ptr(), + c_txid.as_ptr(), + c_tx_key.as_ptr(), + c_address.as_ptr(), + &mut received as *mut u64, + &mut in_pool as *mut bool, + &mut confirmations as *mut u64, + ) + }; + + if result { + CheckTxKey { + valid: true, + received: Some(received), + in_pool: Some(in_pool), + confirmations: Some(confirmations), + error: None, + } + } else { + // Retrieve the last error. + let error = self.get_last_error(); + match error { + WalletError::WalletErrorCode(_, msg) => CheckTxKey { + valid: false, + received: None, + in_pool: None, + confirmations: None, + error: Some(msg), + }, + _ => CheckTxKey { + valid: false, + received: None, + in_pool: None, + confirmations: None, + error: Some("Unknown error".to_string()), + }, + } + } + } } #[derive(Debug)] |
