Skip to content

Commit be0c9fe

Browse files
CopilotSteake
andcommitted
Fix code review and clippy issues in bitcell-wallet
Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
1 parent 7dffef5 commit be0c9fe

6 files changed

Lines changed: 31 additions & 29 deletions

File tree

crates/bitcell-wallet/src/address.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Address {
5050
// For simplicity, we use SHA256 twice (similar to Bitcoin but simplified)
5151
let pubkey_bytes = public_key.as_bytes();
5252
let hash1 = Sha256::digest(pubkey_bytes);
53-
let hash2 = Sha256::digest(&hash1);
53+
let hash2 = Sha256::digest(hash1);
5454
// Take first 20 bytes as address
5555
let address_bytes = hash2[..20].to_vec();
5656

@@ -63,7 +63,7 @@ impl Address {
6363
// Bitcoin address = RIPEMD160(SHA256(compressed_pubkey))
6464
// Simplified: using double SHA256 and taking 20 bytes
6565
let hash1 = Sha256::digest(pubkey_bytes);
66-
let hash2 = Sha256::digest(&hash1);
66+
let hash2 = Sha256::digest(hash1);
6767
let address_bytes = hash2[..20].to_vec();
6868

6969
let chain = if testnet { Chain::BitcoinTestnet } else { Chain::Bitcoin };
@@ -115,7 +115,7 @@ impl Address {
115115
let mut data = vec![version];
116116
data.extend_from_slice(&self.bytes);
117117
// Checksum
118-
let checksum = Sha256::digest(&Sha256::digest(&data));
118+
let checksum = Sha256::digest(Sha256::digest(&data));
119119
data.extend_from_slice(&checksum[..4]);
120120
bs58::encode(&data).into_string()
121121
}
@@ -152,7 +152,7 @@ impl Address {
152152
// Verify checksum
153153
let payload = &bytes[..bytes.len() - 4];
154154
let checksum = &bytes[bytes.len() - 4..];
155-
let computed_checksum = Sha256::digest(&Sha256::digest(payload));
155+
let computed_checksum = Sha256::digest(Sha256::digest(payload));
156156
if &computed_checksum[..4] != checksum {
157157
return Err(Error::InvalidAddress("Invalid checksum".into()));
158158
}

crates/bitcell-wallet/src/chain.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
use serde::{Deserialize, Serialize};
66

77
/// Supported blockchain networks
8-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
8+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
99
pub enum Chain {
1010
/// BitCell native blockchain
11+
#[default]
1112
BitCell,
1213
/// Bitcoin mainnet
1314
Bitcoin,
@@ -94,12 +95,6 @@ impl Chain {
9495
}
9596
}
9697

97-
impl Default for Chain {
98-
fn default() -> Self {
99-
Chain::BitCell
100-
}
101-
}
102-
10398
/// Chain-specific configuration
10499
#[derive(Debug, Clone, Serialize, Deserialize)]
105100
pub struct ChainConfig {

crates/bitcell-wallet/src/history.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub struct TransactionRecord {
4848

4949
impl TransactionRecord {
5050
/// Create a new transaction record
51+
#[allow(clippy::too_many_arguments)]
5152
pub fn new(
5253
tx_hash: String,
5354
chain: Chain,

crates/bitcell-wallet/src/mnemonic.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ use bip39::{Language, Mnemonic as Bip39Mnemonic, MnemonicType, Seed};
88
use zeroize::Zeroize;
99

1010
/// Mnemonic word count options
11-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
1212
pub enum WordCount {
1313
/// 12 words (128 bits entropy)
1414
Words12,
1515
/// 18 words (192 bits entropy)
1616
Words18,
1717
/// 24 words (256 bits entropy)
18+
#[default]
1819
Words24,
1920
}
2021

@@ -28,12 +29,6 @@ impl WordCount {
2829
}
2930
}
3031

31-
impl Default for WordCount {
32-
fn default() -> Self {
33-
WordCount::Words24
34-
}
35-
}
36-
3732
/// BIP39 mnemonic phrase for wallet generation
3833
#[derive(Clone)]
3934
pub struct Mnemonic {

crates/bitcell-wallet/src/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl FeeEstimator {
270270

271271
/// Set priority multiplier (1.0 = normal, 2.0 = fast, 0.5 = slow)
272272
pub fn with_priority(mut self, multiplier: f64) -> Self {
273-
self.priority_multiplier = multiplier.max(0.1).min(10.0);
273+
self.priority_multiplier = multiplier.clamp(0.1, 10.0);
274274
self
275275
}
276276

crates/bitcell-wallet/src/wallet.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ impl DerivationPath {
7272
pub fn for_chain(chain: Chain, index: u32) -> Self {
7373
Self::bip44(chain.coin_type(), 0, 0, index)
7474
}
75+
}
7576

76-
/// Get path as string (BIP44 format)
77-
pub fn to_string(&self) -> String {
78-
format!(
77+
impl std::fmt::Display for DerivationPath {
78+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79+
write!(
80+
f,
7981
"m/{}'/{}'/{}'/{}'/{}",
8082
self.purpose, self.coin_type, self.account, self.change, self.index
8183
)
@@ -147,11 +149,14 @@ impl Wallet {
147149

148150
// Pre-generate addresses for enabled chains
149151
if wallet.config.auto_generate_addresses {
150-
for chain_config in &wallet.config.chains.clone() {
151-
if chain_config.enabled {
152-
for i in 0..wallet.config.address_lookahead {
153-
let _ = wallet.generate_address(chain_config.chain, i);
154-
}
152+
let chains: Vec<_> = wallet.config.chains.iter()
153+
.filter(|c| c.enabled)
154+
.map(|c| (c.chain, wallet.config.address_lookahead))
155+
.collect();
156+
157+
for (chain, lookahead) in chains {
158+
for i in 0..lookahead {
159+
let _ = wallet.generate_address(chain, i);
155160
}
156161
}
157162
}
@@ -210,6 +215,12 @@ impl Wallet {
210215
}
211216

212217
/// Derive a key at a specific path
218+
///
219+
/// Note: This uses a simplified key derivation scheme for the initial implementation.
220+
/// For full BIP32 compatibility with external wallets, implement proper HMAC-SHA512
221+
/// based hierarchical deterministic key derivation. The current implementation
222+
/// provides deterministic key generation that is secure but may not be compatible
223+
/// with other BIP32-compliant wallets.
213224
fn derive_key(&mut self, path: &DerivationPath) -> Result<&DerivedKey> {
214225
let path_str = path.to_string();
215226

@@ -219,8 +230,8 @@ impl Wallet {
219230

220231
let seed = self.master_seed.as_ref().ok_or(Error::WalletLocked)?;
221232

222-
// Simplified key derivation (in production, use proper BIP32)
223-
// Derive key by hashing seed with path
233+
// Simplified key derivation using HMAC-like construction
234+
// For full BIP32 compatibility, use a proper BIP32 library
224235
let mut derivation_data = Vec::new();
225236
derivation_data.extend_from_slice(seed.as_bytes());
226237
derivation_data.extend_from_slice(path_str.as_bytes());

0 commit comments

Comments
 (0)