|
| 1 | +// Rust Bitcoin Library |
| 2 | +// Written in 2018 by |
| 3 | +// Andrew Poelstra <[email protected]> |
| 4 | +// To the extent possible under law, the author(s) have dedicated all |
| 5 | +// copyright and related and neighboring rights to this software to |
| 6 | +// the public domain worldwide. This software is distributed without |
| 7 | +// any warranty. |
| 8 | +// |
| 9 | +// You should have received a copy of the CC0 Public Domain Dedication |
| 10 | +// along with this software. |
| 11 | +// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 12 | +// |
| 13 | + |
| 14 | +//! BIP143 Implementation |
| 15 | +//! |
| 16 | +//! Implementation of BIP143 Segwit-style signatures. Should be sufficient |
| 17 | +//! to create signatures for Segwit transactions (which should be pushed into |
| 18 | +//! the appropriate place in the `Transaction::witness` array) or bcash |
| 19 | +//! signatures, which are placed in the scriptSig. |
| 20 | +//! |
| 21 | +
|
| 22 | +use crate::{ |
| 23 | + encode::{self, Encodable}, |
| 24 | + hash_types::SigHash, |
| 25 | + script::Script, |
| 26 | + transaction::{SigHashType, Transaction}, |
| 27 | +}; |
| 28 | +use bitcoin_hashes::{sha256d, Hash}; |
| 29 | +use std::{io, ops::Deref}; |
| 30 | + |
| 31 | +/// A replacement for SigHashComponents which supports all sighash modes |
| 32 | +pub struct SigHashCache<R: Deref<Target = Transaction>> { |
| 33 | + /// Access to transaction required for various introspection |
| 34 | + tx: R, |
| 35 | + /// Hash of all the previous outputs, computed as required |
| 36 | + hash_prevouts: Option<sha256d::Hash>, |
| 37 | + /// Hash of all the input sequence nos, computed as required |
| 38 | + hash_sequence: Option<sha256d::Hash>, |
| 39 | + /// Hash of all the outputs in this transaction, computed as required |
| 40 | + hash_outputs: Option<sha256d::Hash>, |
| 41 | + /// Hash of all the issunaces in this transaction, computed as required |
| 42 | + hash_issuances: Option<sha256d::Hash>, |
| 43 | +} |
| 44 | + |
| 45 | +impl<R: Deref<Target = Transaction>> SigHashCache<R> { |
| 46 | + /// Compute the sighash components from an unsigned transaction and auxiliary |
| 47 | + /// in a lazy manner when required. |
| 48 | + /// For the generated sighashes to be valid, no fields in the transaction may change except for |
| 49 | + /// script_sig and witnesses. |
| 50 | + pub fn new(tx: R) -> Self { |
| 51 | + SigHashCache { |
| 52 | + tx: tx, |
| 53 | + hash_prevouts: None, |
| 54 | + hash_sequence: None, |
| 55 | + hash_outputs: None, |
| 56 | + hash_issuances: None, |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// Calculate hash for prevouts |
| 61 | + pub fn hash_prevouts(&mut self) -> sha256d::Hash { |
| 62 | + let hash_prevout = &mut self.hash_prevouts; |
| 63 | + let input = &self.tx.input; |
| 64 | + *hash_prevout.get_or_insert_with(|| { |
| 65 | + let mut enc = sha256d::Hash::engine(); |
| 66 | + for txin in input { |
| 67 | + txin.previous_output.consensus_encode(&mut enc).unwrap(); |
| 68 | + } |
| 69 | + sha256d::Hash::from_engine(enc) |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + /// Calculate hash for input sequence values |
| 74 | + pub fn hash_sequence(&mut self) -> sha256d::Hash { |
| 75 | + let hash_sequence = &mut self.hash_sequence; |
| 76 | + let input = &self.tx.input; |
| 77 | + *hash_sequence.get_or_insert_with(|| { |
| 78 | + let mut enc = sha256d::Hash::engine(); |
| 79 | + for txin in input { |
| 80 | + txin.sequence.consensus_encode(&mut enc).unwrap(); |
| 81 | + } |
| 82 | + sha256d::Hash::from_engine(enc) |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + /// Calculate hash for issuances |
| 87 | + pub fn hash_issuances(&mut self) -> sha256d::Hash { |
| 88 | + let hash_issuance = &mut self.hash_issuances; |
| 89 | + let input = &self.tx.input; |
| 90 | + *hash_issuance.get_or_insert_with(|| { |
| 91 | + let mut enc = sha256d::Hash::engine(); |
| 92 | + for txin in input { |
| 93 | + if txin.has_issuance() { |
| 94 | + txin.asset_issuance.consensus_encode(&mut enc).unwrap(); |
| 95 | + } else { |
| 96 | + 0u8.consensus_encode(&mut enc).unwrap(); |
| 97 | + } |
| 98 | + } |
| 99 | + sha256d::Hash::from_engine(enc) |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + /// Calculate hash for outputs |
| 104 | + pub fn hash_outputs(&mut self) -> sha256d::Hash { |
| 105 | + let hash_output = &mut self.hash_outputs; |
| 106 | + let output = &self.tx.output; |
| 107 | + *hash_output.get_or_insert_with(|| { |
| 108 | + let mut enc = sha256d::Hash::engine(); |
| 109 | + for txout in output { |
| 110 | + txout.consensus_encode(&mut enc).unwrap(); |
| 111 | + } |
| 112 | + sha256d::Hash::from_engine(enc) |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + /// Encode the BIP143 signing data for any flag type into a given object implementing a |
| 117 | + /// std::io::Write trait. |
| 118 | + pub fn encode_signing_data_to<Write: io::Write>( |
| 119 | + &mut self, |
| 120 | + mut writer: Write, |
| 121 | + input_index: usize, |
| 122 | + script_code: &Script, |
| 123 | + value: u64, |
| 124 | + sighash_type: SigHashType, |
| 125 | + ) -> Result<(), encode::Error> { |
| 126 | + let zero_hash = sha256d::Hash::default(); |
| 127 | + |
| 128 | + let (sighash, anyone_can_pay) = sighash_type.split_anyonecanpay_flag(); |
| 129 | + |
| 130 | + self.tx.version.consensus_encode(&mut writer)?; |
| 131 | + |
| 132 | + if !anyone_can_pay { |
| 133 | + self.hash_prevouts().consensus_encode(&mut writer)?; |
| 134 | + } else { |
| 135 | + zero_hash.consensus_encode(&mut writer)?; |
| 136 | + } |
| 137 | + |
| 138 | + if !anyone_can_pay && sighash != SigHashType::Single && sighash != SigHashType::None { |
| 139 | + self.hash_sequence().consensus_encode(&mut writer)?; |
| 140 | + } else { |
| 141 | + zero_hash.consensus_encode(&mut writer)?; |
| 142 | + } |
| 143 | + |
| 144 | + // Elements: Push the hash issuance zero hash as required |
| 145 | + // If required implement for issuance, but not necessary as of now |
| 146 | + if !anyone_can_pay { |
| 147 | + self.hash_issuances().consensus_encode(&mut writer)?; |
| 148 | + } else { |
| 149 | + zero_hash.consensus_encode(&mut writer)?; |
| 150 | + } |
| 151 | + |
| 152 | + // input specific values |
| 153 | + { |
| 154 | + let txin = &self.tx.input[input_index]; |
| 155 | + |
| 156 | + txin.previous_output.consensus_encode(&mut writer)?; |
| 157 | + script_code.consensus_encode(&mut writer)?; |
| 158 | + value.consensus_encode(&mut writer)?; |
| 159 | + txin.sequence.consensus_encode(&mut writer)?; |
| 160 | + if txin.has_issuance() { |
| 161 | + txin.asset_issuance.consensus_encode(&mut writer)?; |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + // hashoutputs |
| 166 | + if sighash != SigHashType::Single && sighash != SigHashType::None { |
| 167 | + self.hash_outputs().consensus_encode(&mut writer)?; |
| 168 | + } else if sighash == SigHashType::Single && input_index < self.tx.output.len() { |
| 169 | + let mut single_enc = SigHash::engine(); |
| 170 | + self.tx.output[input_index].consensus_encode(&mut single_enc)?; |
| 171 | + SigHash::from_engine(single_enc).consensus_encode(&mut writer)?; |
| 172 | + } else { |
| 173 | + zero_hash.consensus_encode(&mut writer)?; |
| 174 | + } |
| 175 | + |
| 176 | + self.tx.lock_time.consensus_encode(&mut writer)?; |
| 177 | + sighash_type.as_u32().consensus_encode(&mut writer)?; |
| 178 | + Ok(()) |
| 179 | + } |
| 180 | + |
| 181 | + /// Compute the BIP143 sighash for any flag type. |
| 182 | + pub fn signature_hash( |
| 183 | + &mut self, |
| 184 | + input_index: usize, |
| 185 | + script_code: &Script, |
| 186 | + value: u64, |
| 187 | + sighash_type: SigHashType, |
| 188 | + ) -> SigHash { |
| 189 | + let mut enc = SigHash::engine(); |
| 190 | + self.encode_signing_data_to(&mut enc, input_index, script_code, value, sighash_type) |
| 191 | + .expect("engines don't error"); |
| 192 | + SigHash::from_engine(enc) |
| 193 | + } |
| 194 | +} |
0 commit comments