Skip to content

Commit

Permalink
Rename access_witness to witness_mut and return Option
Browse files Browse the repository at this point in the history
fix the example in sighash to refer to sighash::SigHashCache instead of bip143::SigHashCache
  • Loading branch information
RCasatta committed Aug 31, 2021
1 parent c9bc0b9 commit f223be6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/util/bip143.rs
Expand Up @@ -161,6 +161,9 @@ impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
/// 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;
Expand All @@ -178,7 +181,7 @@ impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
/// }
/// ```
pub fn access_witness(&mut self, input_index: usize) -> &mut Vec<Vec<u8>> {
self.cache.access_witness(input_index)
self.cache.witness_mut(input_index).unwrap()
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/util/sighash.rs
Expand Up @@ -36,7 +36,7 @@ use prelude::*;
pub struct SigHashCache<T: Deref<Target = Transaction>> {
/// Access to transaction required for various introspection, moreover type
/// `T: Deref<Target=Transaction>` 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
Expand Down Expand Up @@ -626,7 +626,7 @@ impl<R: DerefMut<Target = Transaction>> SigHashCache<R> {
/// 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() };
Expand All @@ -635,13 +635,13 @@ impl<R: DerefMut<Target = Transaction>> SigHashCache<R> {
/// 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<Vec<u8>> {
&mut self.tx.input[input_index].witness
pub fn witness_mut(&mut self, input_index: usize) -> Option<&mut Vec<Vec<u8>>> {
self.tx.input.get_mut(input_index).map(|i| &mut i.witness)
}
}

Expand Down

0 comments on commit f223be6

Please sign in to comment.