Skip to content

Commit f3cf722

Browse files
quantumquantum
authored andcommitted
refactor(key-wallet): improve code quality with clippy fixes and formatting
- Remove unnecessary clone() in basic_usage.rs example - Change Into<u32> to From<KeyDerivationType> for more idiomatic Rust - Remove unnecessary lifetime annotation on &str IntoDerivationPath impl - Use matches! macro instead of match for boolean checks - Fix extend() call to not need a reference - Apply cargo fmt to improve code formatting consistency These changes follow Rust best practices and improve code maintainability without affecting functionality.
1 parent 44e12a9 commit f3cf722

File tree

5 files changed

+11
-19
lines changed

5 files changed

+11
-19
lines changed

key-wallet/examples/basic_usage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() -> core::result::Result<(), Box<dyn std::error::Error>> {
3636

3737
// 5. Create account derivation
3838
println!("\n5. Deriving addresses...");
39-
let account_derivation = AccountDerivation::new(account.clone());
39+
let account_derivation = AccountDerivation::new(account);
4040

4141
// Derive first 5 receive addresses
4242
println!(" Receive addresses:");

key-wallet/src/bip32.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,9 @@ pub enum KeyDerivationType {
774774
BLS = 1,
775775
}
776776

777-
impl Into<u32> for KeyDerivationType {
778-
fn into(self) -> u32 {
779-
match self {
777+
impl From<KeyDerivationType> for u32 {
778+
fn from(val: KeyDerivationType) -> Self {
779+
match val {
780780
KeyDerivationType::ECDSA => 0,
781781
KeyDerivationType::BLS => 1,
782782
}
@@ -985,7 +985,7 @@ impl IntoDerivationPath for String {
985985
}
986986
}
987987

988-
impl<'a> IntoDerivationPath for &'a str {
988+
impl IntoDerivationPath for &str {
989989
fn into_derivation_path(self) -> Result<DerivationPath, Error> {
990990
self.parse()
991991
}
@@ -1443,10 +1443,7 @@ impl ExtendedPrivKey {
14431443
let parent_fingerprint = data[5..9].try_into().expect("4 bytes for fingerprint");
14441444

14451445
let hardening_byte = data[9];
1446-
let is_hardened = match hardening_byte {
1447-
0x00 => false,
1448-
_ => true,
1449-
};
1446+
let is_hardened = !matches!(hardening_byte, 0x00);
14501447

14511448
let child_number_bytes = data[10..42].try_into().expect("32 bytes for child number");
14521449
let child_number = if is_hardened {
@@ -1778,10 +1775,7 @@ impl ExtendedPubKey {
17781775
let parent_fingerprint = data[5..9].try_into().expect("4 bytes for fingerprint");
17791776

17801777
let hardening_byte = data[9];
1781-
let is_hardened = match hardening_byte {
1782-
0x00 => false,
1783-
_ => true,
1784-
};
1778+
let is_hardened = !matches!(hardening_byte, 0x00);
17851779

17861780
let child_number_bytes = data[10..42].try_into().expect("32 bytes for child number");
17871781
let child_number = if is_hardened {

key-wallet/src/derivation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ impl AccountDerivation {
158158
let path = format!("m/0/{}", index)
159159
.parse::<DerivationPath>()
160160
.map_err(|e| Error::InvalidDerivationPath(e.to_string()))?;
161-
let priv_key = self.account_key.derive_priv(&self.secp, &path)
162-
.map_err(Error::Bip32)?;
161+
let priv_key = self.account_key.derive_priv(&self.secp, &path).map_err(Error::Bip32)?;
163162
Ok(ExtendedPubKey::from_priv(&self.secp, &priv_key))
164163
}
165164

@@ -168,8 +167,7 @@ impl AccountDerivation {
168167
let path = format!("m/1/{}", index)
169168
.parse::<DerivationPath>()
170169
.map_err(|e| Error::InvalidDerivationPath(e.to_string()))?;
171-
let priv_key = self.account_key.derive_priv(&self.secp, &path)
172-
.map_err(Error::Bip32)?;
170+
let priv_key = self.account_key.derive_priv(&self.secp, &path).map_err(Error::Bip32)?;
173171
Ok(ExtendedPubKey::from_priv(&self.secp, &priv_key))
174172
}
175173
}

key-wallet/src/dip9.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<const N: usize> IndexConstPath<N> {
7575

7676
pub fn append(&self, child_number: ChildNumber) -> DerivationPath {
7777
let root_derivation_path = DerivationPath::from(self.indexes.as_ref());
78-
root_derivation_path.extend(&[child_number]);
78+
root_derivation_path.extend([child_number]);
7979
root_derivation_path
8080
}
8181

key-wallet/tests/address_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! Address tests
22
3-
use std::str::FromStr;
43
use bitcoin_hashes::{hash160, Hash};
54
use key_wallet::address::{Address, AddressGenerator, AddressType};
65
use key_wallet::derivation::HDWallet;
76
use key_wallet::Network;
87
use secp256k1::{PublicKey, Secp256k1};
8+
use std::str::FromStr;
99

1010
#[test]
1111
fn test_p2pkh_address_creation() {

0 commit comments

Comments
 (0)