Deprecate bip143::SigHashCache in favor of sighash::SigHashCache
This commit is contained in:
parent
15e3caf62d
commit
53d0e176d3
|
@ -19,7 +19,7 @@
|
||||||
//! signatures, which are placed in the scriptSig.
|
//! signatures, which are placed in the scriptSig.
|
||||||
//!
|
//!
|
||||||
|
|
||||||
use hashes::{Hash, sha256d};
|
use hashes::Hash;
|
||||||
use hash_types::SigHash;
|
use hash_types::SigHash;
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::{Transaction, TxIn, SigHashType};
|
use blockdata::transaction::{Transaction, TxIn, SigHashType};
|
||||||
|
@ -29,11 +29,12 @@ use prelude::*;
|
||||||
|
|
||||||
use io;
|
use io;
|
||||||
use core::ops::{Deref, DerefMut};
|
use core::ops::{Deref, DerefMut};
|
||||||
|
use util::sighash;
|
||||||
|
|
||||||
/// Parts of a sighash which are common across inputs or signatures, and which are
|
/// Parts of a sighash which are common across inputs or signatures, and which are
|
||||||
/// sufficient (in conjunction with a private key) to sign the transaction
|
/// sufficient (in conjunction with a private key) to sign the transaction
|
||||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||||
#[deprecated(since="0.24.0", note="please use `SigHashCache` instead")]
|
#[deprecated(since="0.24.0", note="please use [sighash::SigHashCache] instead")]
|
||||||
pub struct SighashComponents {
|
pub struct SighashComponents {
|
||||||
tx_version: i32,
|
tx_version: i32,
|
||||||
tx_locktime: u32,
|
tx_locktime: u32,
|
||||||
|
@ -107,121 +108,34 @@ impl SighashComponents {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A replacement for SigHashComponents which supports all sighash modes
|
/// A replacement for SigHashComponents which supports all sighash modes
|
||||||
|
#[deprecated(since="0.27.0", note="please use [sighash::SigHashCache] instead")]
|
||||||
pub struct SigHashCache<R: Deref<Target=Transaction>> {
|
pub struct SigHashCache<R: Deref<Target=Transaction>> {
|
||||||
/// Access to transaction required for various introspection
|
cache: sighash::SigHashCache<R>,
|
||||||
tx: R,
|
|
||||||
/// Hash of all the previous outputs, computed as required
|
|
||||||
hash_prevouts: Option<sha256d::Hash>,
|
|
||||||
/// Hash of all the input sequence nos, computed as required
|
|
||||||
hash_sequence: Option<sha256d::Hash>,
|
|
||||||
/// Hash of all the outputs in this transaction, computed as required
|
|
||||||
hash_outputs: Option<sha256d::Hash>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
impl<R: Deref<Target=Transaction>> SigHashCache<R> {
|
impl<R: Deref<Target=Transaction>> SigHashCache<R> {
|
||||||
/// Compute the sighash components from an unsigned transaction and auxiliary
|
/// Compute the sighash components from an unsigned transaction and auxiliary
|
||||||
/// in a lazy manner when required.
|
/// in a lazy manner when required.
|
||||||
/// For the generated sighashes to be valid, no fields in the transaction may change except for
|
/// For the generated sighashes to be valid, no fields in the transaction may change except for
|
||||||
/// script_sig and witnesses.
|
/// script_sig and witnesses.
|
||||||
pub fn new(tx: R) -> Self {
|
pub fn new(tx: R) -> Self {
|
||||||
SigHashCache {
|
Self { cache: sighash::SigHashCache::new(tx) }
|
||||||
tx: tx,
|
|
||||||
hash_prevouts: None,
|
|
||||||
hash_sequence: None,
|
|
||||||
hash_outputs: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate hash for prevouts
|
|
||||||
pub fn hash_prevouts(&mut self) -> sha256d::Hash {
|
|
||||||
let hash_prevout = &mut self.hash_prevouts;
|
|
||||||
let input = &self.tx.input;
|
|
||||||
*hash_prevout.get_or_insert_with(|| {
|
|
||||||
let mut enc = sha256d::Hash::engine();
|
|
||||||
for txin in input {
|
|
||||||
txin.previous_output.consensus_encode(&mut enc).unwrap();
|
|
||||||
}
|
|
||||||
sha256d::Hash::from_engine(enc)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate hash for input sequence values
|
|
||||||
pub fn hash_sequence(&mut self) -> sha256d::Hash {
|
|
||||||
let hash_sequence = &mut self.hash_sequence;
|
|
||||||
let input = &self.tx.input;
|
|
||||||
*hash_sequence.get_or_insert_with(|| {
|
|
||||||
let mut enc = sha256d::Hash::engine();
|
|
||||||
for txin in input {
|
|
||||||
txin.sequence.consensus_encode(&mut enc).unwrap();
|
|
||||||
}
|
|
||||||
sha256d::Hash::from_engine(enc)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Calculate hash for outputs
|
|
||||||
pub fn hash_outputs(&mut self) -> sha256d::Hash {
|
|
||||||
let hash_output = &mut self.hash_outputs;
|
|
||||||
let output = &self.tx.output;
|
|
||||||
*hash_output.get_or_insert_with(|| {
|
|
||||||
let mut enc = sha256d::Hash::engine();
|
|
||||||
for txout in output {
|
|
||||||
txout.consensus_encode(&mut enc).unwrap();
|
|
||||||
}
|
|
||||||
sha256d::Hash::from_engine(enc)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode the BIP143 signing data for any flag type into a given object implementing a
|
/// Encode the BIP143 signing data for any flag type into a given object implementing a
|
||||||
/// std::io::Write trait.
|
/// std::io::Write trait.
|
||||||
pub fn encode_signing_data_to<Write: io::Write>(
|
pub fn encode_signing_data_to<Write: io::Write>(
|
||||||
&mut self,
|
&mut self,
|
||||||
mut writer: Write,
|
writer: Write,
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
script_code: &Script,
|
script_code: &Script,
|
||||||
value: u64,
|
value: u64,
|
||||||
sighash_type: SigHashType,
|
sighash_type: SigHashType,
|
||||||
) -> Result<(), encode::Error> {
|
) -> Result<(), encode::Error> {
|
||||||
let zero_hash = sha256d::Hash::default();
|
self.cache
|
||||||
|
.segwit_encode_signing_data_to(writer, input_index, script_code, value, sighash_type.into())
|
||||||
let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag();
|
.expect("input_index greater than tx input len");
|
||||||
|
|
||||||
self.tx.version.consensus_encode(&mut writer)?;
|
|
||||||
|
|
||||||
if !anyone_can_pay {
|
|
||||||
self.hash_prevouts().consensus_encode(&mut writer)?;
|
|
||||||
} else {
|
|
||||||
zero_hash.consensus_encode(&mut writer)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if !anyone_can_pay && sighash != SigHashType::Single && sighash != SigHashType::None {
|
|
||||||
self.hash_sequence().consensus_encode(&mut writer)?;
|
|
||||||
} else {
|
|
||||||
zero_hash.consensus_encode(&mut writer)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
let txin = &self.tx.input[input_index];
|
|
||||||
|
|
||||||
txin
|
|
||||||
.previous_output
|
|
||||||
.consensus_encode(&mut writer)?;
|
|
||||||
script_code.consensus_encode(&mut writer)?;
|
|
||||||
value.consensus_encode(&mut writer)?;
|
|
||||||
txin.sequence.consensus_encode(&mut writer)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if sighash != SigHashType::Single && sighash != SigHashType::None {
|
|
||||||
self.hash_outputs().consensus_encode(&mut writer)?;
|
|
||||||
} else if sighash == SigHashType::Single && input_index < self.tx.output.len() {
|
|
||||||
let mut single_enc = SigHash::engine();
|
|
||||||
self.tx.output[input_index].consensus_encode(&mut single_enc)?;
|
|
||||||
SigHash::from_engine(single_enc).consensus_encode(&mut writer)?;
|
|
||||||
} else {
|
|
||||||
zero_hash.consensus_encode(&mut writer)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.tx.lock_time.consensus_encode(&mut writer)?;
|
|
||||||
sighash_type.as_u32().consensus_encode(&mut writer)?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,6 +155,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(deprecated)]
|
||||||
impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
|
impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
|
||||||
/// When the SigHashCache is initialized with a mutable reference to a transaction instead of a
|
/// When the SigHashCache is initialized with a mutable reference to a transaction instead of a
|
||||||
/// regular reference, this method is available to allow modification to the witnesses.
|
/// regular reference, this method is available to allow modification to the witnesses.
|
||||||
|
@ -263,13 +178,14 @@ impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn access_witness(&mut self, input_index: usize) -> &mut Vec<Vec<u8>> {
|
pub fn access_witness(&mut self, input_index: usize) -> &mut Vec<Vec<u8>> {
|
||||||
&mut self.tx.input[input_index].witness
|
self.cache.access_witness(input_index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::str::FromStr;
|
||||||
use hash_types::SigHash;
|
use hash_types::SigHash;
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::Transaction;
|
use blockdata::transaction::Transaction;
|
||||||
|
@ -282,8 +198,7 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn p2pkh_hex(pk: &str) -> Script {
|
fn p2pkh_hex(pk: &str) -> Script {
|
||||||
let pk = Vec::from_hex(pk).unwrap();
|
let pk: PublicKey = PublicKey::from_str(pk).unwrap();
|
||||||
let pk = PublicKey::from_slice(pk.as_slice()).unwrap();
|
|
||||||
let witness_script = Address::p2pkh(&pk, Network::Bitcoin).script_pubkey();
|
let witness_script = Address::p2pkh(&pk, Network::Bitcoin).script_pubkey();
|
||||||
witness_script
|
witness_script
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue