Skip to content

Commit 60da291

Browse files
committed
Use find instead of chaining filter and next
Clippy emits a bunch of warnings of type: warning: called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find(..)` instead As suggested, use `find` instead of chaining `filter` and `next`.
1 parent 3b12fc3 commit 60da291

File tree

2 files changed

+24
-38
lines changed

2 files changed

+24
-38
lines changed

src/psbt/finalizer.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -148,37 +148,29 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
148148
}
149149
} else if script_pubkey.is_p2pkh() {
150150
// 2. `Pkh`: creates a `PkH` descriptor if partial_sigs has the corresponding pk
151-
let partial_sig_contains_pk = inp
152-
.partial_sigs
153-
.iter()
154-
.filter(|&(&pk, _sig)| {
155-
// Indirect way to check the equivalence of pubkey-hashes.
156-
// Create a pubkey hash and check if they are the same.
157-
// THIS IS A BUG AND *WILL* PRODUCE WRONG SATISFACTIONS FOR UNCOMPRESSED KEYS
158-
// Partial sigs loses the compressed flag that is necessary
159-
// TODO: See https://github.com/rust-bitcoin/rust-bitcoin/pull/836
160-
// The type checker will fail again after we update to 0.28 and this can be removed
161-
let addr = bitcoin::Address::p2pkh(&pk, bitcoin::Network::Bitcoin);
162-
*script_pubkey == addr.script_pubkey()
163-
})
164-
.next();
151+
let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| {
152+
// Indirect way to check the equivalence of pubkey-hashes.
153+
// Create a pubkey hash and check if they are the same.
154+
// THIS IS A BUG AND *WILL* PRODUCE WRONG SATISFACTIONS FOR UNCOMPRESSED KEYS
155+
// Partial sigs loses the compressed flag that is necessary
156+
// TODO: See https://github.com/rust-bitcoin/rust-bitcoin/pull/836
157+
// The type checker will fail again after we update to 0.28 and this can be removed
158+
let addr = bitcoin::Address::p2pkh(&pk, bitcoin::Network::Bitcoin);
159+
*script_pubkey == addr.script_pubkey()
160+
});
165161
match partial_sig_contains_pk {
166162
Some((pk, _sig)) => Ok(Descriptor::new_pkh(*pk)),
167163
None => Err(InputError::MissingPubkey),
168164
}
169165
} else if script_pubkey.is_v0_p2wpkh() {
170166
// 3. `Wpkh`: creates a `wpkh` descriptor if the partial sig has corresponding pk.
171-
let partial_sig_contains_pk = inp
172-
.partial_sigs
173-
.iter()
174-
.filter(|&(&pk, _sig)| {
175-
// Indirect way to check the equivalence of pubkey-hashes.
176-
// Create a pubkey hash and check if they are the same.
177-
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
178-
.expect("Address corresponding to valid pubkey");
179-
*script_pubkey == addr.script_pubkey()
180-
})
181-
.next();
167+
let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| {
168+
// Indirect way to check the equivalence of pubkey-hashes.
169+
// Create a pubkey hash and check if they are the same.
170+
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
171+
.expect("Address corresponding to valid pubkey");
172+
*script_pubkey == addr.script_pubkey()
173+
});
182174
match partial_sig_contains_pk {
183175
Some((pk, _sig)) => Ok(Descriptor::new_wpkh(*pk)?),
184176
None => Err(InputError::MissingPubkey),
@@ -228,15 +220,11 @@ fn get_descriptor(psbt: &Psbt, index: usize) -> Result<Descriptor<PublicKey>, In
228220
}
229221
} else if redeem_script.is_v0_p2wpkh() {
230222
// 6. `ShWpkh` case
231-
let partial_sig_contains_pk = inp
232-
.partial_sigs
233-
.iter()
234-
.filter(|&(&pk, _sig)| {
235-
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
236-
.expect("Address corresponding to valid pubkey");
237-
*redeem_script == addr.script_pubkey()
238-
})
239-
.next();
223+
let partial_sig_contains_pk = inp.partial_sigs.iter().find(|&(&pk, _sig)| {
224+
let addr = bitcoin::Address::p2wpkh(&pk, bitcoin::Network::Bitcoin)
225+
.expect("Address corresponding to valid pubkey");
226+
*redeem_script == addr.script_pubkey()
227+
});
240228
match partial_sig_contains_pk {
241229
Some((pk, _sig)) => Ok(Descriptor::new_sh_wpkh(*pk)?),
242230
None => Err(InputError::MissingPubkey),

src/psbt/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,9 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfie
278278
self.psbt.inputs[self.index]
279279
.tap_script_sigs
280280
.iter()
281-
.filter(|&((pubkey, lh), _sig)| {
281+
.find(|&((pubkey, lh), _sig)| {
282282
pubkey.to_pubkeyhash() == Pk::hash_to_hash160(&pkh.0) && *lh == pkh.1
283283
})
284-
.next()
285284
.map(|((x_only_pk, _leaf_hash), sig)| (*x_only_pk, *sig))
286285
}
287286

@@ -299,8 +298,7 @@ impl<'psbt, Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfie
299298
self.psbt.inputs[self.index]
300299
.partial_sigs
301300
.iter()
302-
.filter(|&(pubkey, _sig)| pubkey.to_pubkeyhash() == Pk::hash_to_hash160(pkh))
303-
.next()
301+
.find(|&(pubkey, _sig)| pubkey.to_pubkeyhash() == Pk::hash_to_hash160(pkh))
304302
.map(|(pk, sig)| (*pk, *sig))
305303
}
306304

0 commit comments

Comments
 (0)