Skip to content

Commit 8a93d06

Browse files
committed
psbt: add accessor to PsbtInputSatisfier for the input it holds
Currently we have ugly and repeated code self.psbt.input[self.index]. Replace this with an accessor function which is easier to read and easier to use outside of the module.
1 parent a9888de commit 8a93d06

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/psbt/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -252,26 +252,29 @@ impl<'psbt> PsbtInputSatisfier<'psbt> {
252252
/// create a new PsbtInputsatisfier from
253253
/// psbt and index
254254
pub fn new(psbt: &'psbt Psbt, index: usize) -> Self { Self { psbt, index } }
255+
256+
/// Accessor for the input this satisfier is associated with.
257+
pub fn psbt_input(&self) -> &psbt::Input { &self.psbt.inputs[self.index] }
255258
}
256259

257260
impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'_> {
258261
fn lookup_tap_key_spend_sig(&self) -> Option<bitcoin::taproot::Signature> {
259-
self.psbt.inputs[self.index].tap_key_sig
262+
self.psbt_input().tap_key_sig
260263
}
261264

262265
fn lookup_tap_leaf_script_sig(
263266
&self,
264267
pk: &Pk,
265268
lh: &TapLeafHash,
266269
) -> Option<bitcoin::taproot::Signature> {
267-
self.psbt.inputs[self.index]
270+
self.psbt_input()
268271
.tap_script_sigs
269272
.get(&(pk.to_x_only_pubkey(), *lh))
270273
.copied()
271274
}
272275

273276
fn lookup_raw_pkh_pk(&self, pkh: &hash160::Hash) -> Option<bitcoin::PublicKey> {
274-
self.psbt.inputs[self.index]
277+
self.psbt_input()
275278
.bip32_derivation
276279
.iter()
277280
.find(|&(pubkey, _)| pubkey.to_pubkeyhash(SigType::Ecdsa) == *pkh)
@@ -281,14 +284,14 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'_> {
281284
fn lookup_tap_control_block_map(
282285
&self,
283286
) -> Option<&BTreeMap<ControlBlock, (bitcoin::ScriptBuf, LeafVersion)>> {
284-
Some(&self.psbt.inputs[self.index].tap_scripts)
287+
Some(&self.psbt_input().tap_scripts)
285288
}
286289

287290
fn lookup_raw_pkh_tap_leaf_script_sig(
288291
&self,
289292
pkh: &(hash160::Hash, TapLeafHash),
290293
) -> Option<(bitcoin::secp256k1::XOnlyPublicKey, bitcoin::taproot::Signature)> {
291-
self.psbt.inputs[self.index]
294+
self.psbt_input()
292295
.tap_script_sigs
293296
.iter()
294297
.find(|&((pubkey, lh), _sig)| {
@@ -298,7 +301,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'_> {
298301
}
299302

300303
fn lookup_ecdsa_sig(&self, pk: &Pk) -> Option<bitcoin::ecdsa::Signature> {
301-
self.psbt.inputs[self.index]
304+
self.psbt_input()
302305
.partial_sigs
303306
.get(&pk.to_public_key())
304307
.copied()
@@ -308,7 +311,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'_> {
308311
&self,
309312
pkh: &hash160::Hash,
310313
) -> Option<(bitcoin::PublicKey, bitcoin::ecdsa::Signature)> {
311-
self.psbt.inputs[self.index]
314+
self.psbt_input()
312315
.partial_sigs
313316
.iter()
314317
.find(|&(pubkey, _sig)| pubkey.to_pubkeyhash(SigType::Ecdsa) == *pkh)
@@ -337,28 +340,28 @@ impl<Pk: MiniscriptKey + ToPublicKey> Satisfier<Pk> for PsbtInputSatisfier<'_> {
337340
}
338341

339342
fn lookup_hash160(&self, h: &Pk::Hash160) -> Option<Preimage32> {
340-
self.psbt.inputs[self.index]
343+
self.psbt_input()
341344
.hash160_preimages
342345
.get(&Pk::to_hash160(h))
343346
.and_then(|x: &Vec<u8>| <[u8; 32]>::try_from(&x[..]).ok())
344347
}
345348

346349
fn lookup_sha256(&self, h: &Pk::Sha256) -> Option<Preimage32> {
347-
self.psbt.inputs[self.index]
350+
self.psbt_input()
348351
.sha256_preimages
349352
.get(&Pk::to_sha256(h))
350353
.and_then(|x: &Vec<u8>| <[u8; 32]>::try_from(&x[..]).ok())
351354
}
352355

353356
fn lookup_hash256(&self, h: &Pk::Hash256) -> Option<Preimage32> {
354-
self.psbt.inputs[self.index]
357+
self.psbt_input()
355358
.hash256_preimages
356359
.get(&sha256d::Hash::from_byte_array(Pk::to_hash256(h).to_byte_array())) // upstream psbt operates on hash256
357360
.and_then(|x: &Vec<u8>| <[u8; 32]>::try_from(&x[..]).ok())
358361
}
359362

360363
fn lookup_ripemd160(&self, h: &Pk::Ripemd160) -> Option<Preimage32> {
361-
self.psbt.inputs[self.index]
364+
self.psbt_input()
362365
.ripemd160_preimages
363366
.get(&Pk::to_ripemd160(h))
364367
.and_then(|x: &Vec<u8>| <[u8; 32]>::try_from(&x[..]).ok())

0 commit comments

Comments
 (0)