Skip to content

Commit 805b2e8

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): enable Rust lints and fix clippy warnings
Add Rust linting configuration to check for unexpected cfg conditions from dependencies. Fix multiple clippy warnings including: - Replace map().unwrap_or(false) with simpler expressions - Use *cached instead of clone() for XpubTriple - Use x_only_public_key() instead of to_inner() - Simplify control flow with div_ceil() and find() - Avoid unnecessary cloning with tap_script_sigs.insert(*key, *sig) - Add #[allow(clippy::too_many_arguments)] where appropriate Issue: BTC-2650 Co-authored-by: llm-git <llm-git@ttll.de>
1 parent 3b710f5 commit 805b2e8

9 files changed

Lines changed: 37 additions & 27 deletions

File tree

packages/wasm-utxo/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ crate-type = ["cdylib", "rlib"]
1313
[lints.clippy]
1414
all = "warn"
1515

16+
[lints.rust]
17+
# These cfg conditions come from the zebra-chain dependency
18+
unexpected_cfgs = { level = "warn", check-cfg = [
19+
'cfg(zcash_unstable, values("zfuture"))',
20+
'cfg(feature, values("zebra-test"))',
21+
] }
22+
1623
[dependencies]
1724
wasm-bindgen = "0.2"
1825
js-sys = "0.3"

packages/wasm-utxo/src/bip322/bitgo_psbt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ pub fn add_bip322_input(
182182
// Key path spending (MuSig2 with user/bitgo)
183183
let internal_key = script.spend_info.internal_key();
184184
inner_psbt.inputs[input_index].tap_internal_key = Some(internal_key);
185-
inner_psbt.inputs[input_index].tap_merkle_root =
186-
script.spend_info.merkle_root().map(Into::into);
185+
inner_psbt.inputs[input_index].tap_merkle_root = script.spend_info.merkle_root();
187186
inner_psbt.inputs[input_index].tap_key_origins = create_tap_bip32_derivation(
188187
wallet_keys,
189188
chain,
@@ -209,6 +208,7 @@ pub fn add_bip322_input(
209208
/// * `wallet_keys` - The wallet's root keys
210209
/// * `network` - The network
211210
/// * `tag` - Optional custom tag for message hashing
211+
#[allow(clippy::too_many_arguments)]
212212
pub fn verify_bip322_tx_input(
213213
tx: &Transaction,
214214
input_index: usize,

packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ impl BitGoPsbt {
19861986

19871987
// Copy tap_script_sigs (Taproot script path signatures)
19881988
for (key, sig) in &cloned_input.tap_script_sigs {
1989-
target_input.tap_script_sigs.insert(key.clone(), *sig);
1989+
target_input.tap_script_sigs.insert(*key, *sig);
19901990
}
19911991

19921992
// Copy tap_key_sig (Taproot key path signature)
@@ -2106,7 +2106,7 @@ impl BitGoPsbt {
21062106
.psbt()
21072107
.inputs
21082108
.get(**input_index)
2109-
.map(|input| p2tr_musig2_input::Musig2Input::is_musig2_input(input))
2109+
.map(p2tr_musig2_input::Musig2Input::is_musig2_input)
21102110
.unwrap_or(false);
21112111

21122112
// Don't report "key not found" errors - those are normal for inputs
@@ -2205,7 +2205,7 @@ impl BitGoPsbt {
22052205

22062206
// Copy tap_script_sigs (Taproot script path signatures)
22072207
for (key, sig) in &cloned_input.tap_script_sigs {
2208-
target_input.tap_script_sigs.insert(key.clone(), *sig);
2208+
target_input.tap_script_sigs.insert(*key, *sig);
22092209
}
22102210

22112211
// Copy tap_key_sig (Taproot key path signature)
@@ -2485,6 +2485,7 @@ impl BitGoPsbt {
24852485
/// # Returns
24862486
/// - `Ok(EcdsaSignature)` containing the signature and sighash type
24872487
/// - `Err(String)` if sighash computation fails
2488+
#[allow(clippy::too_many_arguments)]
24882489
fn sign_p2sh_p2pk_input_zcash<C: secp256k1::Signing>(
24892490
psbt: &Psbt,
24902491
input_index: usize,
@@ -2621,7 +2622,7 @@ impl BitGoPsbt {
26212622
input_index,
26222623
redeem_script,
26232624
value,
2624-
ecdsa_sig.sighash_type as u32,
2625+
ecdsa_sig.sighash_type,
26252626
branch_id,
26262627
version_group_id,
26272628
expiry_height,
@@ -2645,7 +2646,7 @@ impl BitGoPsbt {
26452646
input_index,
26462647
redeem_script,
26472648
value,
2648-
ecdsa_sig.sighash_type as u32,
2649+
ecdsa_sig.sighash_type,
26492650
Some(fork_id),
26502651
)
26512652
.map_err(|e| format!("Failed to compute FORKID sighash: {}", e))?;
@@ -3816,7 +3817,7 @@ mod tests {
38163817
let psbt = bitgo_psbt.psbt();
38173818
let has_matching_input = psbt.inputs.iter().any(|input| {
38183819
// Check if this input matches the script type we're testing
3819-
if let Some(ref witness_script) = input.witness_script {
3820+
if let Some(ref _witness_script) = input.witness_script {
38203821
// p2wsh or p2shP2wsh
38213822
if input.redeem_script.is_some() {
38223823
matches!(script_type, fixtures::ScriptType::P2shP2wsh)

packages/wasm-utxo/src/fixed_script_wallet/bitgo_psbt/p2tr_musig2_input_utxolib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ fn create_musig2_deterministic_nonce(
393393
let secp = secp256k1::Secp256k1::new();
394394
let (tweaked_key, _parity): (crate::bitcoin::key::TweakedPublicKey, Parity) =
395395
internal_pub_key.tap_tweak(&secp, Some(*tap_tree_root));
396-
let tap_output_key = tweaked_key.to_inner().serialize();
396+
let tap_output_key = tweaked_key.to_x_only_public_key().serialize();
397397

398398
// Serialize aggregate other nonce
399399
let agg_other_nonce_bytes = agg_other_nonce.serialize();

packages/wasm-utxo/src/fixed_script_wallet/wallet_keys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl RootWalletKeys {
102102
{
103103
let cache = self.derivation_cache.borrow();
104104
if let Some(cached) = cache.get(&cache_key) {
105-
return Ok(cached.clone());
105+
return Ok(*cached);
106106
}
107107
}
108108

@@ -128,7 +128,7 @@ impl RootWalletKeys {
128128
if cache.len() >= DERIVATION_CACHE_MAX_SIZE {
129129
cache.clear();
130130
}
131-
cache.insert(cache_key, derived.clone());
131+
cache.insert(cache_key, derived);
132132
}
133133

134134
Ok(derived)

packages/wasm-utxo/src/inscriptions/envelope.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ pub fn build_inscription_script(
8282
#[cfg(test)]
8383
mod tests {
8484
use super::*;
85-
use miniscript::bitcoin::hashes::Hash;
8685
use miniscript::bitcoin::secp256k1::{Secp256k1, SecretKey};
8786
use miniscript::bitcoin::XOnlyPublicKey;
8887

packages/wasm-utxo/src/inscriptions/reveal.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub fn create_inscription_reveal_data(
9797
output_script: output_script.to_bytes(),
9898
reveal_transaction_vsize: reveal_vsize,
9999
tap_leaf_script: TapLeafScript {
100-
leaf_version: LeafVersion::TapScript.to_consensus() as u8,
100+
leaf_version: LeafVersion::TapScript.to_consensus(),
101101
script: script.to_bytes(),
102102
control_block: control_block.serialize(),
103103
},
@@ -238,7 +238,7 @@ fn estimate_reveal_vsize(script: &ScriptBuf, control_block: &ControlBlock) -> us
238238
let total_weight = base_weight + segwit_overhead + witness_weight;
239239

240240
// Virtual size = ceil(weight / 4)
241-
(total_weight + 3) / 4
241+
total_weight.div_ceil(4)
242242
}
243243

244244
#[cfg(test)]

packages/wasm-utxo/src/wasm/bip322.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl Bip322Namespace {
4646
/// # Returns
4747
/// The index of the added input
4848
#[wasm_bindgen]
49+
#[allow(clippy::too_many_arguments)]
4950
pub fn add_bip322_input(
5051
psbt: &mut super::fixed_script_wallet::BitGoPsbt,
5152
message: &str,
@@ -97,6 +98,7 @@ impl Bip322Namespace {
9798
/// # Throws
9899
/// Throws an error if verification fails
99100
#[wasm_bindgen]
101+
#[allow(clippy::too_many_arguments)]
100102
pub fn verify_bip322_tx_input(
101103
tx: &super::transaction::WasmTransaction,
102104
input_index: u32,

packages/wasm-utxo/src/zcash/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,11 @@ impl NetworkUpgrade {
123123
/// Returns `None` if the height is before Overwinter activation.
124124
pub fn network_upgrade_at_height(height: u32, is_mainnet: bool) -> Option<NetworkUpgrade> {
125125
// Iterate in reverse chronological order
126-
for &upgrade in NetworkUpgrade::ALL.iter().rev() {
127-
if height >= upgrade.activation_height(is_mainnet) {
128-
return Some(upgrade);
129-
}
130-
}
131-
None
126+
NetworkUpgrade::ALL
127+
.iter()
128+
.rev()
129+
.find(|&&upgrade| height >= upgrade.activation_height(is_mainnet))
130+
.copied()
132131
}
133132

134133
/// Get the consensus branch ID for a given block height
@@ -197,9 +196,9 @@ mod tests {
197196
ZebraNetworkUpgrade::Nu6 => Some(NetworkUpgrade::Nu6),
198197
ZebraNetworkUpgrade::Nu6_1 => Some(NetworkUpgrade::Nu6_1),
199198
#[cfg(any(test, feature = "zebra-test"))]
200-
ZebraNetworkUpgrade::Nu7 => None, // Not yet in production
199+
ZebraNetworkUpgrade::Nu7 => None,
201200
#[cfg(zcash_unstable = "zfuture")]
202-
ZebraNetworkUpgrade::ZFuture => None, // Test-only
201+
ZebraNetworkUpgrade::ZFuture => None,
203202
}
204203
}
205204

@@ -222,10 +221,12 @@ mod tests {
222221
continue;
223222
}
224223

225-
let our_upgrade = from_zebra_upgrade(zebra_upgrade).expect(&format!(
226-
"Missing mapping for zebra upgrade {:?}. Add it to NetworkUpgrade enum!",
227-
zebra_upgrade
228-
));
224+
let our_upgrade = from_zebra_upgrade(zebra_upgrade).unwrap_or_else(|| {
225+
panic!(
226+
"Missing mapping for zebra upgrade {:?}. Add it to NetworkUpgrade enum!",
227+
zebra_upgrade
228+
)
229+
});
229230

230231
// Verify round-trip
231232
assert_eq!(
@@ -253,7 +254,7 @@ mod tests {
253254
let zebra_upgrade = to_zebra_upgrade(upgrade);
254255
let expected = zebra_upgrade
255256
.branch_id()
256-
.map(|b| u32::from(b))
257+
.map(u32::from)
257258
.expect("upgrade should have branch_id");
258259

259260
assert_eq!(

0 commit comments

Comments
 (0)