Skip to content

Commit 8b351d2

Browse files
OttoAllmendingerllm-git
andcommitted
feat(wasm-utxo): use rust idiomatic style for boolean expressions
Replace boolean equality comparisons with direct assertions and other idiomatic patterns throughout the codebase. Improve function parameter handling by removing unnecessary references and dereferences. Issue: BTC-2652 Co-authored-by: llm-git <[email protected]>
1 parent 8a7a61f commit 8b351d2

File tree

8 files changed

+52
-54
lines changed

8 files changed

+52
-54
lines changed

packages/wasm-utxo/src/address/cashaddr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ mod tests {
474474
// Test roundtrip
475475
let (decoded_hash, is_p2sh) = decode_cashaddr(&address, "bitcoincash").unwrap();
476476
assert_eq!(decoded_hash, hash);
477-
assert_eq!(is_p2sh, false);
477+
assert!(!is_p2sh);
478478
}
479479

480480
#[test]
@@ -490,7 +490,7 @@ mod tests {
490490
// Test roundtrip
491491
let (decoded_hash, is_p2sh) = decode_cashaddr(&address, "bchtest").unwrap();
492492
assert_eq!(decoded_hash, hash);
493-
assert_eq!(is_p2sh, true);
493+
assert!(is_p2sh);
494494
}
495495

496496
#[test]
@@ -503,7 +503,7 @@ mod tests {
503503
// Test roundtrip
504504
let (decoded_hash, is_p2sh) = decode_cashaddr(&address, "pref").unwrap();
505505
assert_eq!(decoded_hash, hash);
506-
assert_eq!(is_p2sh, true);
506+
assert!(is_p2sh);
507507
}
508508

509509
#[test]
@@ -585,7 +585,7 @@ mod tests {
585585
let (hash, is_p2sh) = decode_cashaddr(uppercase, "bitcoincash").unwrap();
586586

587587
assert_eq!(hex::encode(hash).to_uppercase(), TEST_HASH_20);
588-
assert_eq!(is_p2sh, false);
588+
assert!(!is_p2sh);
589589
}
590590

591591
#[test]
@@ -628,7 +628,7 @@ mod tests {
628628
// Test roundtrip
629629
let (decoded_hash, is_p2sh) = decode_cashaddr(&address, "ecash").unwrap();
630630
assert_eq!(decoded_hash, hash);
631-
assert_eq!(is_p2sh, false);
631+
assert!(!is_p2sh);
632632
}
633633

634634
#[test]

packages/wasm-utxo/src/descriptor.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::error::WasmMiniscriptError;
22
use crate::try_into_js_value::TryIntoJsValue;
3-
use miniscript::bitcoin::secp256k1::{Context, Secp256k1, Signing};
3+
use miniscript::bitcoin::secp256k1::{Secp256k1, Signing};
44
use miniscript::bitcoin::ScriptBuf;
55
use miniscript::descriptor::KeyMap;
66
use miniscript::{DefiniteDescriptorKey, Descriptor, DescriptorPublicKey};
@@ -115,7 +115,7 @@ impl WrapDescriptor {
115115
secp: &Secp256k1<C>,
116116
descriptor: &str,
117117
) -> Result<WrapDescriptor, WasmMiniscriptError> {
118-
let (desc, keys) = Descriptor::parse_descriptor(&secp, descriptor)?;
118+
let (desc, keys) = Descriptor::parse_descriptor(secp, descriptor)?;
119119
Ok(WrapDescriptor(WrapDescriptorEnum::Derivable(desc, keys)))
120120
}
121121

@@ -217,15 +217,14 @@ mod tests {
217217
)
218218
.unwrap();
219219

220-
assert_eq!(desc.has_wildcard(), false);
221-
assert_eq!(
220+
assert!(!desc.has_wildcard());
221+
assert!(
222222
match desc {
223223
WrapDescriptor {
224224
0: crate::descriptor::WrapDescriptorEnum::Definite(_),
225225
} => true,
226226
_ => false,
227-
},
228-
true
227+
}
229228
);
230229
}
231230
}

packages/wasm-utxo/src/fixed_script_wallet/wallet_scripts/checkmultisig.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub fn build_multisig_script_2_of_3(keys: &PubTriple) -> ScriptBuf {
99
let total_count = 3;
1010
let mut builder = Builder::default().push_int(quorum as i64);
1111
for key in keys {
12-
builder = builder.push_slice(&key.to_bytes())
12+
builder = builder.push_slice(key.to_bytes())
1313
}
1414
builder
1515
.push_int(total_count as i64)
@@ -150,7 +150,7 @@ mod tests {
150150
// Test script with wrong number of instructions
151151
let script = Builder::new()
152152
.push_opcode(OP_PUSHNUM_2)
153-
.push_slice(&[0x02; 33]) // Only one key instead of three
153+
.push_slice([0x02; 33]) // Only one key instead of three
154154
.push_opcode(OP_PUSHNUM_3)
155155
.push_opcode(OP_CHECKMULTISIG)
156156
.into_script();
@@ -173,9 +173,9 @@ mod tests {
173173
// Build script with wrong quorum (OP_1 instead of OP_2)
174174
let script = Builder::new()
175175
.push_opcode(OP_PUSHNUM_1)
176-
.push_slice(&pub_triple[0].to_bytes())
177-
.push_slice(&pub_triple[1].to_bytes())
178-
.push_slice(&pub_triple[2].to_bytes())
176+
.push_slice(pub_triple[0].to_bytes())
177+
.push_slice(pub_triple[1].to_bytes())
178+
.push_slice(pub_triple[2].to_bytes())
179179
.push_opcode(OP_PUSHNUM_3)
180180
.push_opcode(OP_CHECKMULTISIG)
181181
.into_script();
@@ -198,9 +198,9 @@ mod tests {
198198
// Build script with wrong total (OP_4 instead of OP_3)
199199
let script = Builder::new()
200200
.push_opcode(OP_PUSHNUM_2)
201-
.push_slice(&pub_triple[0].to_bytes())
202-
.push_slice(&pub_triple[1].to_bytes())
203-
.push_slice(&pub_triple[2].to_bytes())
201+
.push_slice(pub_triple[0].to_bytes())
202+
.push_slice(pub_triple[1].to_bytes())
203+
.push_slice(pub_triple[2].to_bytes())
204204
.push_opcode(OP_PUSHNUM_4)
205205
.push_opcode(OP_CHECKMULTISIG)
206206
.into_script();
@@ -223,9 +223,9 @@ mod tests {
223223
// Build script without OP_CHECKMULTISIG
224224
let script = Builder::new()
225225
.push_opcode(OP_PUSHNUM_2)
226-
.push_slice(&pub_triple[0].to_bytes())
227-
.push_slice(&pub_triple[1].to_bytes())
228-
.push_slice(&pub_triple[2].to_bytes())
226+
.push_slice(pub_triple[0].to_bytes())
227+
.push_slice(pub_triple[1].to_bytes())
228+
.push_slice(pub_triple[2].to_bytes())
229229
.push_opcode(OP_PUSHNUM_3)
230230
.push_opcode(OP_PUSHNUM_1) // Wrong opcode instead of OP_CHECKMULTISIG
231231
.into_script();
@@ -242,9 +242,9 @@ mod tests {
242242
// Build script with invalid public key data
243243
let script = Builder::new()
244244
.push_opcode(OP_PUSHNUM_2)
245-
.push_slice(&[0x00; 10]) // Invalid public key (too short)
246-
.push_slice(&[0x02; 33]) // Valid compressed pubkey format
247-
.push_slice(&[0x03; 33]) // Valid compressed pubkey format
245+
.push_slice([0x00; 10]) // Invalid public key (too short)
246+
.push_slice([0x02; 33]) // Valid compressed pubkey format
247+
.push_slice([0x03; 33]) // Valid compressed pubkey format
248248
.push_opcode(OP_PUSHNUM_3)
249249
.push_opcode(OP_CHECKMULTISIG)
250250
.into_script();
@@ -262,8 +262,8 @@ mod tests {
262262
let script = Builder::new()
263263
.push_opcode(OP_PUSHNUM_2)
264264
.push_opcode(OP_PUSHNUM_1) // Wrong: should be pubkey bytes
265-
.push_slice(&[0x02; 33])
266-
.push_slice(&[0x03; 33])
265+
.push_slice([0x02; 33])
266+
.push_slice([0x03; 33])
267267
.push_opcode(OP_PUSHNUM_3)
268268
.push_opcode(OP_CHECKMULTISIG)
269269
.into_script();

packages/wasm-utxo/src/fixed_script_wallet/wallet_scripts/checksigverify.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub fn build_p2tr_ns_script(keys: &[CompressedPublicKey]) -> ScriptBuf {
2020
for (i, key) in keys.iter().enumerate() {
2121
// convert to xonly key
2222
let key_bytes = to_xonly_pubkey(*key);
23-
builder = builder.push_slice(&key_bytes);
23+
builder = builder.push_slice(key_bytes);
2424
if i == keys.len() - 1 {
2525
builder = builder.push_opcode(OP_CHECKSIG);
2626
} else {
@@ -89,11 +89,11 @@ impl ScriptP2tr {
8989

9090
pub fn output_script(&self) -> ScriptBuf {
9191
let output_key = self.spend_info.output_key().to_inner();
92-
let output_script = Builder::new()
92+
93+
Builder::new()
9394
.push_int(1)
9495
.push_slice(output_key.serialize())
95-
.into_script();
96-
output_script
96+
.into_script()
9797
}
9898
}
9999

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ impl std::fmt::Display for WalletScripts {
3939
f,
4040
"{}",
4141
match self {
42-
WalletScripts::P2sh(_) => format!("P2sh"),
43-
WalletScripts::P2shP2wsh(_) => format!("P2shP2wsh"),
44-
WalletScripts::P2wsh(_) => format!("P2wsh"),
45-
WalletScripts::P2trLegacy(_) => format!("P2trLegacy"),
46-
WalletScripts::P2trMusig2(_) => format!("P2trMusig2"),
42+
WalletScripts::P2sh(_) => "P2sh".to_string(),
43+
WalletScripts::P2shP2wsh(_) => "P2shP2wsh".to_string(),
44+
WalletScripts::P2wsh(_) => "P2wsh".to_string(),
45+
WalletScripts::P2trLegacy(_) => "P2trLegacy".to_string(),
46+
WalletScripts::P2trMusig2(_) => "P2trMusig2".to_string(),
4747
}
4848
)
4949
}
@@ -165,7 +165,7 @@ pub fn derive_xpubs_with_path(
165165
) -> XpubTriple {
166166
let derived = xpubs
167167
.iter()
168-
.map(|k| k.derive_pub(&ctx, &p).unwrap())
168+
.map(|k| k.derive_pub(ctx, &p).unwrap())
169169
.collect::<Vec<_>>();
170170
derived.try_into().expect("could not convert vec to array")
171171
}
@@ -182,7 +182,7 @@ pub fn derive_xpubs(
182182
index: chain as u32,
183183
})
184184
.child(ChildNumber::Normal { index });
185-
derive_xpubs_with_path(&xpubs, ctx, p)
185+
derive_xpubs_with_path(xpubs, ctx, p)
186186
}
187187

188188
#[cfg(test)]
@@ -383,14 +383,14 @@ mod tests {
383383
.expect("Failed to find input with script type");
384384

385385
let (chain, index) =
386-
parse_fixture_paths(&input_fixture).expect("Failed to parse fixture paths");
386+
parse_fixture_paths(input_fixture).expect("Failed to parse fixture paths");
387387
let scripts = WalletScripts::from_xpubs(&xpubs, chain, index);
388388

389389
// Use the new helper methods for validation
390390
match (scripts, input_fixture) {
391391
(WalletScripts::P2sh(scripts), fixtures::PsbtInputFixture::P2sh(fixture_input)) => {
392392
let vout = fixture.inputs[input_index].index as usize;
393-
let output_script = get_output_script_from_non_witness_utxo(&fixture_input, vout);
393+
let output_script = get_output_script_from_non_witness_utxo(fixture_input, vout);
394394
fixture_input
395395
.assert_matches_wallet_scripts(&scripts, &output_script)
396396
.expect("P2sh validation failed");

packages/wasm-utxo/src/fixed_script_wallet/wallet_scripts/singlesig.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::bitcoin::{CompressedPublicKey, ScriptBuf};
77
/// Build bare p2pk script (used for p2sh-p2pk replay protection)
88
pub fn build_p2pk_script(key: CompressedPublicKey) -> ScriptBuf {
99
Builder::default()
10-
.push_slice(&key.to_bytes())
10+
.push_slice(key.to_bytes())
1111
.push_opcode(OP_CHECKSIG)
1212
.into_script()
1313
}
@@ -57,8 +57,7 @@ mod tests {
5757
// Get the expected values from the fixture
5858
let expected_redeem_script = &p2shp2pk_input.redeem_script;
5959
let expected_pubkey = p2shp2pk_input
60-
.partial_sig
61-
.get(0)
60+
.partial_sig.first()
6261
.map(|sig| &sig.pubkey)
6362
.expect("No partial signature found");
6463

packages/wasm-utxo/src/psbt.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::error::WasmMiniscriptError;
33
use crate::try_into_js_value::TryIntoJsValue;
44
use crate::WrapDescriptor;
55
use miniscript::bitcoin::bip32::Fingerprint;
6-
use miniscript::bitcoin::secp256k1::{Context, Secp256k1, Signing};
7-
use miniscript::bitcoin::{bip32, psbt, secp256k1, PublicKey, XOnlyPublicKey};
6+
use miniscript::bitcoin::secp256k1::{Secp256k1, Signing};
7+
use miniscript::bitcoin::{bip32, psbt, PublicKey, XOnlyPublicKey};
88
use miniscript::bitcoin::{PrivateKey, Psbt};
99
use miniscript::descriptor::{SinglePub, SinglePubKey};
1010
use miniscript::psbt::PsbtExt;
@@ -46,23 +46,23 @@ impl psbt::GetKey for SingleKeySigner {
4646
fn get_key<C: Signing>(
4747
&self,
4848
key_request: psbt::KeyRequest,
49-
secp: &Secp256k1<C>,
49+
_secp: &Secp256k1<C>,
5050
) -> Result<Option<PrivateKey>, Self::Error> {
5151
match key_request {
5252
// NOTE: this KeyRequest does not occur for taproot signatures
5353
// even if the descriptor keys are definite, we will receive a bip32 request
5454
// instead based on `DescriptorPublicKey::Single(SinglePub { origin: None, key, })`
5555
psbt::KeyRequest::Pubkey(req_pubkey) => {
5656
if req_pubkey == self.pubkey {
57-
Ok(Some(self.privkey.clone()))
57+
Ok(Some(self.privkey))
5858
} else {
5959
Ok(None)
6060
}
6161
}
6262

63-
psbt::KeyRequest::Bip32((fingerprint, path)) => {
63+
psbt::KeyRequest::Bip32((fingerprint, _path)) => {
6464
if fingerprint.eq(&self.fingerprint) || fingerprint.eq(&self.fingerprint_xonly) {
65-
Ok(Some(self.privkey.clone()))
65+
Ok(Some(self.privkey))
6666
} else {
6767
Ok(None)
6868
}
@@ -99,7 +99,7 @@ impl WrapPsbt {
9999
match &descriptor.0 {
100100
WrapDescriptorEnum::Definite(d) => self
101101
.0
102-
.update_input_with_descriptor(input_index, &d)
102+
.update_input_with_descriptor(input_index, d)
103103
.map_err(JsError::from),
104104
WrapDescriptorEnum::Derivable(_, _) => Err(JsError::new(
105105
"Cannot update input with a derivable descriptor",
@@ -119,7 +119,7 @@ impl WrapPsbt {
119119
match &descriptor.0 {
120120
WrapDescriptorEnum::Definite(d) => self
121121
.0
122-
.update_output_with_descriptor(output_index, &d)
122+
.update_output_with_descriptor(output_index, d)
123123
.map_err(JsError::from),
124124
WrapDescriptorEnum::Derivable(_, _) => Err(JsError::new(
125125
"Cannot update output with a derivable descriptor",
@@ -149,7 +149,7 @@ impl WrapPsbt {
149149
let secp = Secp256k1::new();
150150
self.0
151151
.sign(&SingleKeySigner::from_privkey(privkey, &secp), &secp)
152-
.map_err(|(r, errors)| {
152+
.map_err(|(_r, errors)| {
153153
WasmMiniscriptError::new(&format!("{} errors: {:?}", errors.len(), errors))
154154
})
155155
.and_then(|r| r.try_to_js_value())
@@ -200,7 +200,7 @@ mod tests {
200200
.values()
201201
.for_each(|key_source| {
202202
let key_source_ref: KeySource = (
203-
Fingerprint::from_hex(&"aeee1e6a").unwrap(),
203+
Fingerprint::from_hex("aeee1e6a").unwrap(),
204204
DerivationPath::from(vec![]),
205205
);
206206
assert_eq!(key_source.1, key_source_ref);

packages/wasm-utxo/src/try_into_js_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl TryIntoJsValue for SigningKeysMap {
286286
js_sys::Reflect::set(
287287
&obj,
288288
&key.to_string().into(),
289-
&value.try_to_js_value()?.into(),
289+
&value.try_to_js_value()?,
290290
)
291291
.map_err(|_| WasmMiniscriptError::new("Failed to set object property"))?;
292292
}

0 commit comments

Comments
 (0)