Skip to content

Commit ef52ce3

Browse files
refactor: key wallet clippy warnings
1 parent 3b035d8 commit ef52ce3

36 files changed

+189
-198
lines changed

key-wallet-ffi/include/key_wallet_ffi.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3789,16 +3789,30 @@ void wallet_manager_free_addresses(char **addresses,
37893789

37903790
/*
37913791
Encrypt a private key with BIP38
3792+
3793+
# Safety
3794+
3795+
This function is unsafe because it dereferences raw pointers:
3796+
- `private_key` must be a valid, null-terminated C string
3797+
- `passphrase` must be a valid, null-terminated C string
3798+
- `error` must be a valid pointer to an FFIError or null
37923799
*/
37933800

37943801
char *bip38_encrypt_private_key(const char *private_key,
37953802
const char *passphrase,
3796-
FFINetwork network,
3803+
FFINetwork _network,
37973804
FFIError *error)
37983805
;
37993806

38003807
/*
38013808
Decrypt a BIP38 encrypted private key
3809+
3810+
# Safety
3811+
3812+
This function is unsafe because it dereferences raw pointers:
3813+
- `encrypted_key` must be a valid, null-terminated C string
3814+
- `passphrase` must be a valid, null-terminated C string
3815+
- `error` must be a valid pointer to an FFIError or null
38023816
*/
38033817

38043818
char *bip38_decrypt_private_key(const char *encrypted_key,

key-wallet-ffi/src/account_collection.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ mod tests {
11511151
options.option_type = crate::types::FFIAccountCreationOptionType::AllAccounts;
11521152

11531153
// Add provider operator keys account type
1154-
let special_types = vec![crate::types::FFIAccountType::ProviderOperatorKeys];
1154+
let special_types = [crate::types::FFIAccountType::ProviderOperatorKeys];
11551155
options.special_account_types = special_types.as_ptr();
11561156
options.special_account_types_count = special_types.len();
11571157

@@ -1199,7 +1199,7 @@ mod tests {
11991199
options.option_type = crate::types::FFIAccountCreationOptionType::AllAccounts;
12001200

12011201
// Add provider platform keys account type
1202-
let special_types = vec![crate::types::FFIAccountType::ProviderPlatformKeys];
1202+
let special_types = [crate::types::FFIAccountType::ProviderPlatformKeys];
12031203
options.special_account_types = special_types.as_ptr();
12041204
options.special_account_types_count = special_types.len();
12051205

@@ -1248,7 +1248,7 @@ mod tests {
12481248
options.option_type = crate::types::FFIAccountCreationOptionType::AllAccounts;
12491249

12501250
// Add various special accounts
1251-
let special_types = vec![
1251+
let special_types = [
12521252
crate::types::FFIAccountType::ProviderVotingKeys,
12531253
crate::types::FFIAccountType::ProviderOwnerKeys,
12541254
crate::types::FFIAccountType::IdentityRegistration,
@@ -1258,10 +1258,10 @@ mod tests {
12581258
options.special_account_types_count = special_types.len();
12591259

12601260
// Configure standard accounts - store vectors in variables to keep them alive
1261-
let bip44_indices = vec![0, 4, 5, 8];
1262-
let bip32_indices = vec![0];
1263-
let coinjoin_indices = vec![0, 1];
1264-
let topup_indices = vec![0, 1, 2];
1261+
let bip44_indices = [0, 4, 5, 8];
1262+
let bip32_indices = [0];
1263+
let coinjoin_indices = [0, 1];
1264+
let topup_indices = [0, 1, 2];
12651265

12661266
options.bip44_indices = bip44_indices.as_ptr();
12671267
options.bip44_count = bip44_indices.len();
@@ -1392,7 +1392,7 @@ mod tests {
13921392
options.option_type = crate::types::FFIAccountCreationOptionType::AllAccounts;
13931393

13941394
// Add various special accounts
1395-
let special_types = vec![
1395+
let special_types = [
13961396
crate::types::FFIAccountType::ProviderVotingKeys,
13971397
crate::types::FFIAccountType::ProviderOwnerKeys,
13981398
crate::types::FFIAccountType::IdentityRegistration,
@@ -1402,10 +1402,10 @@ mod tests {
14021402
options.special_account_types_count = special_types.len();
14031403

14041404
// Configure standard accounts
1405-
let bip44_indices = vec![0, 4, 5, 8];
1406-
let bip32_indices = vec![0];
1407-
let coinjoin_indices = vec![0, 1];
1408-
let topup_indices = vec![0, 1, 2];
1405+
let bip44_indices = [0, 4, 5, 8];
1406+
let bip32_indices = [0];
1407+
let coinjoin_indices = [0, 1];
1408+
let topup_indices = [0, 1, 2];
14091409

14101410
options.bip44_indices = bip44_indices.as_ptr();
14111411
options.bip44_count = bip44_indices.len();

key-wallet-ffi/src/account_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#[cfg(test)]
2+
#[allow(clippy::module_inception)]
23
mod tests {
34
use super::super::*;
45
use crate::error::{FFIError, FFIErrorCode};

key-wallet-ffi/src/bip38.rs

Lines changed: 54 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
//! BIP38 encryption support
22
3-
use std::ffi::{CStr, CString};
3+
use std::ffi::CStr;
44
use std::os::raw::c_char;
55
use std::ptr;
66

77
use crate::error::{FFIError, FFIErrorCode};
88
use crate::types::FFINetwork;
99

1010
/// Encrypt a private key with BIP38
11+
///
12+
/// # Safety
13+
///
14+
/// This function is unsafe because it dereferences raw pointers:
15+
/// - `private_key` must be a valid, null-terminated C string
16+
/// - `passphrase` must be a valid, null-terminated C string
17+
/// - `error` must be a valid pointer to an FFIError or null
1118
#[no_mangle]
12-
pub extern "C" fn bip38_encrypt_private_key(
19+
pub unsafe extern "C" fn bip38_encrypt_private_key(
1320
private_key: *const c_char,
1421
passphrase: *const c_char,
15-
network: FFINetwork,
22+
_network: FFINetwork,
1623
error: *mut FFIError,
1724
) -> *mut c_char {
1825
#[cfg(feature = "bip38")]
@@ -26,31 +33,27 @@ pub extern "C" fn bip38_encrypt_private_key(
2633
return ptr::null_mut();
2734
}
2835

29-
let privkey_str = unsafe {
30-
match CStr::from_ptr(private_key).to_str() {
31-
Ok(s) => s,
32-
Err(_) => {
33-
FFIError::set_error(
34-
error,
35-
FFIErrorCode::InvalidInput,
36-
"Invalid UTF-8 in private key".to_string(),
37-
);
38-
return ptr::null_mut();
39-
}
36+
let _privkey_str = match CStr::from_ptr(private_key).to_str() {
37+
Ok(s) => s,
38+
Err(_) => {
39+
FFIError::set_error(
40+
error,
41+
FFIErrorCode::InvalidInput,
42+
"Invalid UTF-8 in private key".to_string(),
43+
);
44+
return ptr::null_mut();
4045
}
4146
};
4247

43-
let passphrase_str = unsafe {
44-
match CStr::from_ptr(passphrase).to_str() {
45-
Ok(s) => s,
46-
Err(_) => {
47-
FFIError::set_error(
48-
error,
49-
FFIErrorCode::InvalidInput,
50-
"Invalid UTF-8 in passphrase".to_string(),
51-
);
52-
return ptr::null_mut();
53-
}
48+
let _passphrase_str = match CStr::from_ptr(passphrase).to_str() {
49+
Ok(s) => s,
50+
Err(_) => {
51+
FFIError::set_error(
52+
error,
53+
FFIErrorCode::InvalidInput,
54+
"Invalid UTF-8 in passphrase".to_string(),
55+
);
56+
return ptr::null_mut();
5457
}
5558
};
5659

@@ -75,8 +78,15 @@ pub extern "C" fn bip38_encrypt_private_key(
7578
}
7679

7780
/// Decrypt a BIP38 encrypted private key
81+
///
82+
/// # Safety
83+
///
84+
/// This function is unsafe because it dereferences raw pointers:
85+
/// - `encrypted_key` must be a valid, null-terminated C string
86+
/// - `passphrase` must be a valid, null-terminated C string
87+
/// - `error` must be a valid pointer to an FFIError or null
7888
#[no_mangle]
79-
pub extern "C" fn bip38_decrypt_private_key(
89+
pub unsafe extern "C" fn bip38_decrypt_private_key(
8090
encrypted_key: *const c_char,
8191
passphrase: *const c_char,
8292
error: *mut FFIError,
@@ -92,31 +102,27 @@ pub extern "C" fn bip38_decrypt_private_key(
92102
return ptr::null_mut();
93103
}
94104

95-
let encrypted_str = unsafe {
96-
match CStr::from_ptr(encrypted_key).to_str() {
97-
Ok(s) => s,
98-
Err(_) => {
99-
FFIError::set_error(
100-
error,
101-
FFIErrorCode::InvalidInput,
102-
"Invalid UTF-8 in encrypted key".to_string(),
103-
);
104-
return ptr::null_mut();
105-
}
105+
let _encrypted_str = match CStr::from_ptr(encrypted_key).to_str() {
106+
Ok(s) => s,
107+
Err(_) => {
108+
FFIError::set_error(
109+
error,
110+
FFIErrorCode::InvalidInput,
111+
"Invalid UTF-8 in encrypted key".to_string(),
112+
);
113+
return ptr::null_mut();
106114
}
107115
};
108116

109-
let passphrase_str = unsafe {
110-
match CStr::from_ptr(passphrase).to_str() {
111-
Ok(s) => s,
112-
Err(_) => {
113-
FFIError::set_error(
114-
error,
115-
FFIErrorCode::InvalidInput,
116-
"Invalid UTF-8 in passphrase".to_string(),
117-
);
118-
return ptr::null_mut();
119-
}
117+
let _passphrase_str = match CStr::from_ptr(passphrase).to_str() {
118+
Ok(s) => s,
119+
Err(_) => {
120+
FFIError::set_error(
121+
error,
122+
FFIErrorCode::InvalidInput,
123+
"Invalid UTF-8 in passphrase".to_string(),
124+
);
125+
return ptr::null_mut();
120126
}
121127
};
122128

key-wallet-ffi/src/derivation_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for derivation path FFI functions
22
33
#[cfg(test)]
4+
#[allow(clippy::module_inception)]
45
mod tests {
56
use crate::derivation::*;
67
use crate::error::{FFIError, FFIErrorCode};

key-wallet-ffi/src/keys_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for key derivation FFI functions
22
33
#[cfg(test)]
4+
#[allow(clippy::module_inception)]
45
mod tests {
56
use crate::error::{FFIError, FFIErrorCode};
67
use crate::keys::*;

key-wallet-ffi/src/managed_account.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ mod tests {
856856

857857
let mut options = FFIWalletAccountCreationOptions::default_options();
858858
options.option_type = FFIAccountCreationOptionType::BIP44AccountsOnly;
859-
let bip44_indices = vec![0];
859+
let bip44_indices = [0];
860860
options.bip44_indices = bip44_indices.as_ptr();
861861
options.bip44_count = bip44_indices.len();
862862

@@ -929,9 +929,9 @@ mod tests {
929929
let mut options = FFIWalletAccountCreationOptions::default_options();
930930
options.option_type = FFIAccountCreationOptionType::AllAccounts;
931931

932-
let bip44_indices = vec![0, 1, 2];
933-
let bip32_indices = vec![0];
934-
let coinjoin_indices = vec![0];
932+
let bip44_indices = [0, 1, 2];
933+
let bip32_indices = [0];
934+
let coinjoin_indices = [0];
935935

936936
options.bip44_indices = bip44_indices.as_ptr();
937937
options.bip44_count = bip44_indices.len();
@@ -1046,7 +1046,7 @@ mod tests {
10461046

10471047
// Test get_is_watch_only
10481048
let is_watch_only = managed_account_get_is_watch_only(account);
1049-
assert_eq!(is_watch_only, false);
1049+
assert!(!is_watch_only);
10501050

10511051
// Test get_balance
10521052
let mut balance_out = crate::types::FFIBalance {
@@ -1094,7 +1094,7 @@ mod tests {
10941094
assert_eq!(account_type, FFIAccountType::StandardBIP44); // Default type
10951095

10961096
let is_watch_only = managed_account_get_is_watch_only(ptr::null());
1097-
assert_eq!(is_watch_only, false);
1097+
assert!(!is_watch_only);
10981098

10991099
let tx_count = managed_account_get_transaction_count(ptr::null());
11001100
assert_eq!(tx_count, 0);
@@ -1261,7 +1261,7 @@ mod tests {
12611261
// Create wallet with CoinJoin account
12621262
let mut options = FFIWalletAccountCreationOptions::default_options();
12631263
options.option_type = FFIAccountCreationOptionType::SpecificAccounts;
1264-
let coinjoin_indices = vec![0];
1264+
let coinjoin_indices = [0];
12651265
options.coinjoin_indices = coinjoin_indices.as_ptr();
12661266
options.coinjoin_count = coinjoin_indices.len();
12671267

key-wallet-ffi/src/managed_wallet.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ mod tests {
906906

907907
// Create managed wallet info from the wallet
908908
let wallet_rust = unsafe { &(*wallet).wallet };
909-
let managed_info = ManagedWalletInfo::from_wallet(&wallet_rust);
909+
let managed_info = ManagedWalletInfo::from_wallet(wallet_rust);
910910
let mut ffi_managed = FFIManagedWalletInfo::new(managed_info);
911911

912912
// Test get_next_receive_address with valid pointers
@@ -995,7 +995,7 @@ mod tests {
995995

996996
// We need to work with the existing wallet structure
997997
// Create managed wallet info from the existing wallet
998-
let mut managed_info = ManagedWalletInfo::from_wallet(&wallet_arc);
998+
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc);
999999

10001000
let network = key_wallet::Network::Testnet;
10011001

@@ -1177,7 +1177,7 @@ mod tests {
11771177

11781178
// Create managed wallet info
11791179
let wallet_arc = unsafe { &(*wallet_ptr).wallet };
1180-
let mut managed_info = ManagedWalletInfo::from_wallet(&wallet_arc);
1180+
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc);
11811181

11821182
// Set some test balance values
11831183
managed_info.balance = WalletBalance {

key-wallet-ffi/src/managed_wallet_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ mod tests {
360360
};
361361

362362
// Create invalid UTF-8 string
363-
let invalid_utf8 = vec![0xFF, 0xFE, 0xFD, 0x00]; // Invalid UTF-8 bytes with null terminator
363+
let invalid_utf8 = [0xFF, 0xFE, 0xFD, 0x00]; // Invalid UTF-8 bytes with null terminator
364364
let success = unsafe {
365365
managed_wallet_mark_address_used(
366366
managed_wallet,

key-wallet-ffi/src/mnemonic_tests.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Unit tests for mnemonic FFI module
22
33
#[cfg(test)]
4+
#[allow(clippy::module_inception)]
45
mod tests {
56
use crate::error::{FFIError, FFIErrorCode};
67
use crate::mnemonic;
@@ -380,7 +381,7 @@ mod tests {
380381
let mut error = FFIError::success();
381382

382383
// Create invalid UTF-8 string
383-
let invalid_utf8 = vec![0xFF, 0xFE, 0xFD, 0x00];
384+
let invalid_utf8 = [0xFF, 0xFE, 0xFD, 0x00];
384385
let count = unsafe {
385386
mnemonic::mnemonic_word_count(
386387
invalid_utf8.as_ptr() as *const std::os::raw::c_char,
@@ -467,7 +468,7 @@ mod tests {
467468
let mut seed_len = 0usize;
468469

469470
// Test invalid UTF-8 in mnemonic
470-
let invalid_utf8 = vec![0xFF, 0xFE, 0xFD, 0x00];
471+
let invalid_utf8 = [0xFF, 0xFE, 0xFD, 0x00];
471472
let success = unsafe {
472473
mnemonic::mnemonic_to_seed(
473474
invalid_utf8.as_ptr() as *const std::os::raw::c_char,
@@ -500,7 +501,7 @@ mod tests {
500501
let mut error = FFIError::success();
501502

502503
// Create invalid UTF-8 string
503-
let invalid_utf8 = vec![0xFF, 0xFE, 0xFD, 0x00];
504+
let invalid_utf8 = [0xFF, 0xFE, 0xFD, 0x00];
504505
let is_valid = unsafe {
505506
mnemonic::mnemonic_validate(
506507
invalid_utf8.as_ptr() as *const std::os::raw::c_char,

0 commit comments

Comments
 (0)