From f223be618fff7f06d6a1b2e0ca94c506f3ea2fd7 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 31 Aug 2021 08:26:20 +0200 Subject: [PATCH] Rename access_witness to witness_mut and return Option fix the example in sighash to refer to sighash::SigHashCache instead of bip143::SigHashCache --- src/util/bip143.rs | 5 ++++- src/util/sighash.rs | 12 ++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/util/bip143.rs b/src/util/bip143.rs index 7cfe166f..23b18967 100644 --- a/src/util/bip143.rs +++ b/src/util/bip143.rs @@ -161,6 +161,9 @@ impl> SigHashCache { /// regular reference, this method is available to allow modification to the witnesses. /// /// This allows in-line signing such as + /// + /// panics if `input_index` is out of bounds with respect of the number of inputs + /// /// ``` /// use bitcoin::blockdata::transaction::{Transaction, SigHashType}; /// use bitcoin::util::bip143::SigHashCache; @@ -178,7 +181,7 @@ impl> SigHashCache { /// } /// ``` pub fn access_witness(&mut self, input_index: usize) -> &mut Vec> { - self.cache.access_witness(input_index) + self.cache.witness_mut(input_index).unwrap() } } diff --git a/src/util/sighash.rs b/src/util/sighash.rs index 66ea4253..8e5d4a34 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -36,7 +36,7 @@ use prelude::*; pub struct SigHashCache> { /// Access to transaction required for various introspection, moreover type /// `T: Deref` allows to accept borrow and mutable borrow, the - /// latter in particular is necessary for [SigHashCache::access_witness] + /// latter in particular is necessary for [SigHashCache::witness_mut] tx: T, /// Common cache for taproot and segwit inputs. It's an option because it's not needed for legacy inputs @@ -626,7 +626,7 @@ impl> SigHashCache { /// This allows in-line signing such as /// ``` /// use bitcoin::blockdata::transaction::{Transaction, SigHashType}; - /// use bitcoin::util::bip143::SigHashCache; + /// use bitcoin::util::sighash::SigHashCache; /// use bitcoin::Script; /// /// let mut tx_to_sign = Transaction { version: 2, lock_time: 0, input: Vec::new(), output: Vec::new() }; @@ -635,13 +635,13 @@ impl> SigHashCache { /// let mut sig_hasher = SigHashCache::new(&mut tx_to_sign); /// for inp in 0..input_count { /// let prevout_script = Script::new(); - /// let _sighash = sig_hasher.signature_hash(inp, &prevout_script, 42, SigHashType::All); + /// let _sighash = sig_hasher.segwit_signature_hash(inp, &prevout_script, 42, SigHashType::All); /// // ... sign the sighash - /// sig_hasher.access_witness(inp).push(Vec::new()); + /// sig_hasher.witness_mut(inp).unwrap().push(Vec::new()); /// } /// ``` - pub fn access_witness(&mut self, input_index: usize) -> &mut Vec> { - &mut self.tx.input[input_index].witness + pub fn witness_mut(&mut self, input_index: usize) -> Option<&mut Vec>> { + self.tx.input.get_mut(input_index).map(|i| &mut i.witness) } }