Skip to content

Commit

Permalink
use same segwit API (apart from Error)
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Jul 15, 2021
1 parent a810722 commit e76e5e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
7 changes: 2 additions & 5 deletions src/util/bip143.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use hash_types::SigHash;
use blockdata::script::Script;
use blockdata::transaction::{Transaction, TxIn, SigHashType};
use consensus::{encode, Encodable};

use ::{io, TxOut};
use io;
use core::ops::{Deref, DerefMut};
use util::sighash;

Expand Down Expand Up @@ -131,10 +130,8 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
value: u64,
sighash_type: SigHashType,
) -> Result<(), encode::Error> {
let tx_out = TxOut { value, script_pubkey: script_code.clone() };
let prevout = sighash::Prevouts::One(input_index, &tx_out);
self.cache
.segwit_encode_signing_data_to(writer, input_index, &prevout, sighash_type.into())
.segwit_encode_signing_data_to(writer, input_index, script_code, value, sighash_type.into())
.expect("input_index greater than tx input len");
Ok(())
}
Expand Down
35 changes: 16 additions & 19 deletions src/util/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,10 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
&mut self,
mut writer: Write,
input_index: usize,
prevouts: &Prevouts,
script_code: &Script,
value: u64,
sighash_type: SigHashType,
) -> Result<(), Error> {
prevouts.check_all(&self.tx)?;
self.check_index(input_index)?;

let zero_hash = sha256d::Hash::default();
Expand All @@ -445,14 +445,8 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
let txin = &self.tx.input[input_index];

txin.previous_output.consensus_encode(&mut writer)?;
prevouts
.get(input_index)?
.script_pubkey
.consensus_encode(&mut writer)?;
prevouts
.get(input_index)?
.value
.consensus_encode(&mut writer)?;
script_code.consensus_encode(&mut writer)?;
value.consensus_encode(&mut writer)?;
txin.sequence.consensus_encode(&mut writer)?;
}

Expand All @@ -477,11 +471,18 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
pub fn segwit_signature_hash(
&mut self,
input_index: usize,
prevouts: &Prevouts,
script_code: &Script,
value: u64,
sighash_type: SigHashType,
) -> Result<SigHash, Error> {
let mut enc = SigHash::engine();
self.segwit_encode_signing_data_to(&mut enc, input_index, prevouts, sighash_type)?;
self.segwit_encode_signing_data_to(
&mut enc,
input_index,
script_code,
value,
sighash_type,
)?;
Ok(SigHash::from_engine(enc))
}

Expand Down Expand Up @@ -638,6 +639,7 @@ mod tests {
use hashes::{Hash, HashEngine};
use network::constants::Network;
use std::mem::size_of;
use std::str::FromStr;
use util::address::Address;
use util::bip143;
use util::ecdsa::PublicKey;
Expand All @@ -647,7 +649,6 @@ mod tests {
};
use util::taproot::TapSighashHash;
use {Script, Transaction, TxIn, TxOut};
use std::str::FromStr;

#[test]
fn test_tap_sighash_hash() {
Expand Down Expand Up @@ -883,13 +884,9 @@ mod tests {
let expected_result = SigHash::from_slice(&raw_expected[..]).unwrap();
let mut cache = SigHashCache::new(&tx);
let sighash_type = LegacySigHashType::from_u32_consensus(hash_type);
let tx_out = TxOut {
value,
script_pubkey,
};
let prevout = Prevouts::One(input_index, &tx_out);

let actual_result = cache
.segwit_signature_hash(input_index, &prevout, sighash_type.into())
.segwit_signature_hash(input_index, &script_pubkey, value, sighash_type.into())
.unwrap();
assert_eq!(actual_result, expected_result);
}
Expand Down

0 comments on commit e76e5e1

Please sign in to comment.