Rename existing SigHashType to EcdsaSigHashType
This commit is contained in:
parent
648b3975a5
commit
94cfe79170
|
@ -333,10 +333,10 @@ impl Transaction {
|
||||||
let sighash_type : u32 = sighash_type.into();
|
let sighash_type : u32 = sighash_type.into();
|
||||||
assert!(input_index < self.input.len()); // Panic on OOB
|
assert!(input_index < self.input.len()); // Panic on OOB
|
||||||
|
|
||||||
let (sighash, anyone_can_pay) = SigHashType::from_u32_consensus(sighash_type).split_anyonecanpay_flag();
|
let (sighash, anyone_can_pay) = EcdsaSigHashType::from_u32_consensus(sighash_type).split_anyonecanpay_flag();
|
||||||
|
|
||||||
// Special-case sighash_single bug because this is easy enough.
|
// Special-case sighash_single bug because this is easy enough.
|
||||||
if sighash == SigHashType::Single && input_index >= self.output.len() {
|
if sighash == EcdsaSigHashType::Single && input_index >= self.output.len() {
|
||||||
writer.write_all(&[1, 0, 0, 0, 0, 0, 0, 0,
|
writer.write_all(&[1, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
@ -365,22 +365,22 @@ impl Transaction {
|
||||||
tx.input.push(TxIn {
|
tx.input.push(TxIn {
|
||||||
previous_output: input.previous_output,
|
previous_output: input.previous_output,
|
||||||
script_sig: if n == input_index { script_pubkey.clone() } else { Script::new() },
|
script_sig: if n == input_index { script_pubkey.clone() } else { Script::new() },
|
||||||
sequence: if n != input_index && (sighash == SigHashType::Single || sighash == SigHashType::None) { 0 } else { input.sequence },
|
sequence: if n != input_index && (sighash == EcdsaSigHashType::Single || sighash == EcdsaSigHashType::None) { 0 } else { input.sequence },
|
||||||
witness: vec![],
|
witness: vec![],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ..then all outputs
|
// ..then all outputs
|
||||||
tx.output = match sighash {
|
tx.output = match sighash {
|
||||||
SigHashType::All => self.output.clone(),
|
EcdsaSigHashType::All => self.output.clone(),
|
||||||
SigHashType::Single => {
|
EcdsaSigHashType::Single => {
|
||||||
let output_iter = self.output.iter()
|
let output_iter = self.output.iter()
|
||||||
.take(input_index + 1) // sign all outputs up to and including this one, but erase
|
.take(input_index + 1) // sign all outputs up to and including this one, but erase
|
||||||
.enumerate() // all of them except for this one
|
.enumerate() // all of them except for this one
|
||||||
.map(|(n, out)| if n == input_index { out.clone() } else { TxOut::default() });
|
.map(|(n, out)| if n == input_index { out.clone() } else { TxOut::default() });
|
||||||
output_iter.collect()
|
output_iter.collect()
|
||||||
}
|
}
|
||||||
SigHashType::None => vec![],
|
EcdsaSigHashType::None => vec![],
|
||||||
_ => unreachable!()
|
_ => unreachable!()
|
||||||
};
|
};
|
||||||
// hash the result
|
// hash the result
|
||||||
|
@ -673,10 +673,15 @@ impl fmt::Display for NonStandardSigHashType {
|
||||||
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
||||||
impl error::Error for NonStandardSigHashType {}
|
impl error::Error for NonStandardSigHashType {}
|
||||||
|
|
||||||
|
/// Legacy Hashtype of an input's signature
|
||||||
|
#[deprecated(since="0.28.0", note="Please use [`EcdsaSigHashType`] instead")]
|
||||||
|
pub type SigHashType = EcdsaSigHashType;
|
||||||
|
|
||||||
/// Hashtype of an input's signature, encoded in the last byte of the signature
|
/// Hashtype of an input's signature, encoded in the last byte of the signature
|
||||||
/// Fixed values so they can be casted as integer types for encoding
|
/// Fixed values so they can be casted as integer types for encoding
|
||||||
|
/// See also [`crate::SchnorrSigHashType`]
|
||||||
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
|
||||||
pub enum SigHashType {
|
pub enum EcdsaSigHashType {
|
||||||
/// 0x1: Sign all outputs
|
/// 0x1: Sign all outputs
|
||||||
All = 0x01,
|
All = 0x01,
|
||||||
/// 0x2: Sign no outputs --- anyone can choose the destination
|
/// 0x2: Sign no outputs --- anyone can choose the destination
|
||||||
|
@ -693,54 +698,54 @@ pub enum SigHashType {
|
||||||
/// 0x83: Sign one output and only this input (see `Single` for what "one output" means)
|
/// 0x83: Sign one output and only this input (see `Single` for what "one output" means)
|
||||||
SinglePlusAnyoneCanPay = 0x83
|
SinglePlusAnyoneCanPay = 0x83
|
||||||
}
|
}
|
||||||
serde_string_impl!(SigHashType, "a SigHashType data");
|
serde_string_impl!(EcdsaSigHashType, "a EcdsaSigHashType data");
|
||||||
|
|
||||||
impl fmt::Display for SigHashType {
|
impl fmt::Display for EcdsaSigHashType {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let s = match self {
|
let s = match self {
|
||||||
SigHashType::All => "SIGHASH_ALL",
|
EcdsaSigHashType::All => "SIGHASH_ALL",
|
||||||
SigHashType::None => "SIGHASH_NONE",
|
EcdsaSigHashType::None => "SIGHASH_NONE",
|
||||||
SigHashType::Single => "SIGHASH_SINGLE",
|
EcdsaSigHashType::Single => "SIGHASH_SINGLE",
|
||||||
SigHashType::AllPlusAnyoneCanPay => "SIGHASH_ALL|SIGHASH_ANYONECANPAY",
|
EcdsaSigHashType::AllPlusAnyoneCanPay => "SIGHASH_ALL|SIGHASH_ANYONECANPAY",
|
||||||
SigHashType::NonePlusAnyoneCanPay => "SIGHASH_NONE|SIGHASH_ANYONECANPAY",
|
EcdsaSigHashType::NonePlusAnyoneCanPay => "SIGHASH_NONE|SIGHASH_ANYONECANPAY",
|
||||||
SigHashType::SinglePlusAnyoneCanPay => "SIGHASH_SINGLE|SIGHASH_ANYONECANPAY",
|
EcdsaSigHashType::SinglePlusAnyoneCanPay => "SIGHASH_SINGLE|SIGHASH_ANYONECANPAY",
|
||||||
};
|
};
|
||||||
f.write_str(s)
|
f.write_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl str::FromStr for SigHashType {
|
impl str::FromStr for EcdsaSigHashType {
|
||||||
type Err = String;
|
type Err = String;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
match s.as_ref() {
|
match s.as_ref() {
|
||||||
"SIGHASH_ALL" => Ok(SigHashType::All),
|
"SIGHASH_ALL" => Ok(EcdsaSigHashType::All),
|
||||||
"SIGHASH_NONE" => Ok(SigHashType::None),
|
"SIGHASH_NONE" => Ok(EcdsaSigHashType::None),
|
||||||
"SIGHASH_SINGLE" => Ok(SigHashType::Single),
|
"SIGHASH_SINGLE" => Ok(EcdsaSigHashType::Single),
|
||||||
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(SigHashType::AllPlusAnyoneCanPay),
|
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::AllPlusAnyoneCanPay),
|
||||||
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(SigHashType::NonePlusAnyoneCanPay),
|
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::NonePlusAnyoneCanPay),
|
||||||
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(SigHashType::SinglePlusAnyoneCanPay),
|
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::SinglePlusAnyoneCanPay),
|
||||||
_ => Err("can't recognize SIGHASH string".to_string())
|
_ => Err("can't recognize SIGHASH string".to_string())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SigHashType {
|
impl EcdsaSigHashType {
|
||||||
/// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean
|
/// Break the sighash flag into the "real" sighash flag and the ANYONECANPAY boolean
|
||||||
pub(crate) fn split_anyonecanpay_flag(self) -> (SigHashType, bool) {
|
pub(crate) fn split_anyonecanpay_flag(self) -> (EcdsaSigHashType, bool) {
|
||||||
match self {
|
match self {
|
||||||
SigHashType::All => (SigHashType::All, false),
|
EcdsaSigHashType::All => (EcdsaSigHashType::All, false),
|
||||||
SigHashType::None => (SigHashType::None, false),
|
EcdsaSigHashType::None => (EcdsaSigHashType::None, false),
|
||||||
SigHashType::Single => (SigHashType::Single, false),
|
EcdsaSigHashType::Single => (EcdsaSigHashType::Single, false),
|
||||||
SigHashType::AllPlusAnyoneCanPay => (SigHashType::All, true),
|
EcdsaSigHashType::AllPlusAnyoneCanPay => (EcdsaSigHashType::All, true),
|
||||||
SigHashType::NonePlusAnyoneCanPay => (SigHashType::None, true),
|
EcdsaSigHashType::NonePlusAnyoneCanPay => (EcdsaSigHashType::None, true),
|
||||||
SigHashType::SinglePlusAnyoneCanPay => (SigHashType::Single, true)
|
EcdsaSigHashType::SinglePlusAnyoneCanPay => (EcdsaSigHashType::Single, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads a 4-byte uint32 as a sighash type.
|
/// Reads a 4-byte uint32 as a sighash type.
|
||||||
#[deprecated(since="0.26.1", note="please use `from_u32_consensus` or `from_u32_standard` instead")]
|
#[deprecated(since="0.26.1", note="please use `from_u32_consensus` or `from_u32_standard` instead")]
|
||||||
pub fn from_u32(n: u32) -> SigHashType {
|
pub fn from_u32(n: u32) -> EcdsaSigHashType {
|
||||||
Self::from_u32_consensus(n)
|
Self::from_u32_consensus(n)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -748,7 +753,7 @@ impl SigHashType {
|
||||||
///
|
///
|
||||||
/// **Note**: this replicates consensus behaviour, for current standardness rules correctness
|
/// **Note**: this replicates consensus behaviour, for current standardness rules correctness
|
||||||
/// you probably want [Self::from_u32_standard].
|
/// you probably want [Self::from_u32_standard].
|
||||||
pub fn from_u32_consensus(n: u32) -> SigHashType {
|
pub fn from_u32_consensus(n: u32) -> EcdsaSigHashType {
|
||||||
// In Bitcoin Core, the SignatureHash function will mask the (int32) value with
|
// In Bitcoin Core, the SignatureHash function will mask the (int32) value with
|
||||||
// 0x1f to (apparently) deactivate ACP when checking for SINGLE and NONE bits.
|
// 0x1f to (apparently) deactivate ACP when checking for SINGLE and NONE bits.
|
||||||
// We however want to be matching also against on ACP-masked ALL, SINGLE, and NONE.
|
// We however want to be matching also against on ACP-masked ALL, SINGLE, and NONE.
|
||||||
|
@ -756,29 +761,29 @@ impl SigHashType {
|
||||||
let mask = 0x1f | 0x80;
|
let mask = 0x1f | 0x80;
|
||||||
match n & mask {
|
match n & mask {
|
||||||
// "real" sighashes
|
// "real" sighashes
|
||||||
0x01 => SigHashType::All,
|
0x01 => EcdsaSigHashType::All,
|
||||||
0x02 => SigHashType::None,
|
0x02 => EcdsaSigHashType::None,
|
||||||
0x03 => SigHashType::Single,
|
0x03 => EcdsaSigHashType::Single,
|
||||||
0x81 => SigHashType::AllPlusAnyoneCanPay,
|
0x81 => EcdsaSigHashType::AllPlusAnyoneCanPay,
|
||||||
0x82 => SigHashType::NonePlusAnyoneCanPay,
|
0x82 => EcdsaSigHashType::NonePlusAnyoneCanPay,
|
||||||
0x83 => SigHashType::SinglePlusAnyoneCanPay,
|
0x83 => EcdsaSigHashType::SinglePlusAnyoneCanPay,
|
||||||
// catchalls
|
// catchalls
|
||||||
x if x & 0x80 == 0x80 => SigHashType::AllPlusAnyoneCanPay,
|
x if x & 0x80 == 0x80 => EcdsaSigHashType::AllPlusAnyoneCanPay,
|
||||||
_ => SigHashType::All
|
_ => EcdsaSigHashType::All
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read a 4-byte uint32 as a standard sighash type, returning an error if the type
|
/// Read a 4-byte uint32 as a standard sighash type, returning an error if the type
|
||||||
/// is non standard.
|
/// is non standard.
|
||||||
pub fn from_u32_standard(n: u32) -> Result<SigHashType, NonStandardSigHashType> {
|
pub fn from_u32_standard(n: u32) -> Result<EcdsaSigHashType, NonStandardSigHashType> {
|
||||||
match n {
|
match n {
|
||||||
// Standard sighashes, see https://github.com/bitcoin/bitcoin/blob/b805dbb0b9c90dadef0424e5b3bf86ac308e103e/src/script/interpreter.cpp#L189-L198
|
// Standard sighashes, see https://github.com/bitcoin/bitcoin/blob/b805dbb0b9c90dadef0424e5b3bf86ac308e103e/src/script/interpreter.cpp#L189-L198
|
||||||
0x01 => Ok(SigHashType::All),
|
0x01 => Ok(EcdsaSigHashType::All),
|
||||||
0x02 => Ok(SigHashType::None),
|
0x02 => Ok(EcdsaSigHashType::None),
|
||||||
0x03 => Ok(SigHashType::Single),
|
0x03 => Ok(EcdsaSigHashType::Single),
|
||||||
0x81 => Ok(SigHashType::AllPlusAnyoneCanPay),
|
0x81 => Ok(EcdsaSigHashType::AllPlusAnyoneCanPay),
|
||||||
0x82 => Ok(SigHashType::NonePlusAnyoneCanPay),
|
0x82 => Ok(EcdsaSigHashType::NonePlusAnyoneCanPay),
|
||||||
0x83 => Ok(SigHashType::SinglePlusAnyoneCanPay),
|
0x83 => Ok(EcdsaSigHashType::SinglePlusAnyoneCanPay),
|
||||||
_ => Err(NonStandardSigHashType)
|
_ => Err(NonStandardSigHashType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -787,8 +792,8 @@ impl SigHashType {
|
||||||
pub fn as_u32(self) -> u32 { self as u32 }
|
pub fn as_u32(self) -> u32 { self as u32 }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<SigHashType> for u32 {
|
impl From<EcdsaSigHashType> for u32 {
|
||||||
fn from(t: SigHashType) -> u32 {
|
fn from(t: EcdsaSigHashType) -> u32 {
|
||||||
t.as_u32()
|
t.as_u32()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -807,7 +812,7 @@ mod tests {
|
||||||
use hashes::hex::FromHex;
|
use hashes::hex::FromHex;
|
||||||
|
|
||||||
use hash_types::*;
|
use hash_types::*;
|
||||||
use SigHashType;
|
use super::EcdsaSigHashType;
|
||||||
use util::sighash::SigHashCache;
|
use util::sighash::SigHashCache;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -1088,15 +1093,15 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_sighashtype_fromstr_display() {
|
fn test_sighashtype_fromstr_display() {
|
||||||
let sighashtypes = vec![("SIGHASH_ALL", SigHashType::All),
|
let sighashtypes = vec![("SIGHASH_ALL", EcdsaSigHashType::All),
|
||||||
("SIGHASH_NONE", SigHashType::None),
|
("SIGHASH_NONE", EcdsaSigHashType::None),
|
||||||
("SIGHASH_SINGLE", SigHashType::Single),
|
("SIGHASH_SINGLE", EcdsaSigHashType::Single),
|
||||||
("SIGHASH_ALL|SIGHASH_ANYONECANPAY", SigHashType::AllPlusAnyoneCanPay),
|
("SIGHASH_ALL|SIGHASH_ANYONECANPAY", EcdsaSigHashType::AllPlusAnyoneCanPay),
|
||||||
("SIGHASH_NONE|SIGHASH_ANYONECANPAY", SigHashType::NonePlusAnyoneCanPay),
|
("SIGHASH_NONE|SIGHASH_ANYONECANPAY", EcdsaSigHashType::NonePlusAnyoneCanPay),
|
||||||
("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", SigHashType::SinglePlusAnyoneCanPay)];
|
("SIGHASH_SINGLE|SIGHASH_ANYONECANPAY", EcdsaSigHashType::SinglePlusAnyoneCanPay)];
|
||||||
for (s, sht) in sighashtypes {
|
for (s, sht) in sighashtypes {
|
||||||
assert_eq!(sht.to_string(), s);
|
assert_eq!(sht.to_string(), s);
|
||||||
assert_eq!(SigHashType::from_str(s).unwrap(), sht);
|
assert_eq!(EcdsaSigHashType::from_str(s).unwrap(), sht);
|
||||||
}
|
}
|
||||||
let sht_mistakes = vec![
|
let sht_mistakes = vec![
|
||||||
"SIGHASH_ALL | SIGHASH_ANYONECANPAY",
|
"SIGHASH_ALL | SIGHASH_ANYONECANPAY",
|
||||||
|
@ -1111,7 +1116,7 @@ mod tests {
|
||||||
"SigHash_NONE",
|
"SigHash_NONE",
|
||||||
];
|
];
|
||||||
for s in sht_mistakes {
|
for s in sht_mistakes {
|
||||||
assert_eq!(SigHashType::from_str(s).unwrap_err(), "can't recognize SIGHASH string");
|
assert_eq!(EcdsaSigHashType::from_str(s).unwrap_err(), "can't recognize SIGHASH string");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1120,10 +1125,10 @@ mod tests {
|
||||||
fn test_sighashtype_standard() {
|
fn test_sighashtype_standard() {
|
||||||
let nonstandard_hashtype = 0x04;
|
let nonstandard_hashtype = 0x04;
|
||||||
// This type is not well defined, by consensus it becomes ALL
|
// This type is not well defined, by consensus it becomes ALL
|
||||||
assert_eq!(SigHashType::from_u32(nonstandard_hashtype), SigHashType::All);
|
assert_eq!(EcdsaSigHashType::from_u32(nonstandard_hashtype), EcdsaSigHashType::All);
|
||||||
assert_eq!(SigHashType::from_u32_consensus(nonstandard_hashtype), SigHashType::All);
|
assert_eq!(EcdsaSigHashType::from_u32_consensus(nonstandard_hashtype), EcdsaSigHashType::All);
|
||||||
// But it's policy-invalid to use it!
|
// But it's policy-invalid to use it!
|
||||||
assert_eq!(SigHashType::from_u32_standard(nonstandard_hashtype), Err(NonStandardSigHashType));
|
assert_eq!(EcdsaSigHashType::from_u32_standard(nonstandard_hashtype), Err(NonStandardSigHashType));
|
||||||
}
|
}
|
||||||
|
|
||||||
// These test vectors were stolen from libbtc, which is Copyright 2014 Jonas Schnelli MIT
|
// These test vectors were stolen from libbtc, which is Copyright 2014 Jonas Schnelli MIT
|
||||||
|
|
|
@ -123,7 +123,7 @@ pub use blockdata::transaction::Transaction;
|
||||||
pub use blockdata::transaction::TxIn;
|
pub use blockdata::transaction::TxIn;
|
||||||
pub use blockdata::transaction::TxOut;
|
pub use blockdata::transaction::TxOut;
|
||||||
pub use blockdata::transaction::OutPoint;
|
pub use blockdata::transaction::OutPoint;
|
||||||
pub use blockdata::transaction::SigHashType;
|
pub use blockdata::transaction::EcdsaSigHashType;
|
||||||
pub use consensus::encode::VarInt;
|
pub use consensus::encode::VarInt;
|
||||||
pub use network::constants::Network;
|
pub use network::constants::Network;
|
||||||
pub use util::Error;
|
pub use util::Error;
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
use hashes::Hash;
|
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, EcdsaSigHashType};
|
||||||
use consensus::{encode, Encodable};
|
use consensus::{encode, Encodable};
|
||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
|
@ -131,7 +131,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
script_code: &Script,
|
script_code: &Script,
|
||||||
value: u64,
|
value: u64,
|
||||||
sighash_type: SigHashType,
|
sighash_type: EcdsaSigHashType,
|
||||||
) -> Result<(), encode::Error> {
|
) -> Result<(), encode::Error> {
|
||||||
self.cache
|
self.cache
|
||||||
.segwit_encode_signing_data_to(writer, input_index, script_code, value, sighash_type.into())
|
.segwit_encode_signing_data_to(writer, input_index, script_code, value, sighash_type.into())
|
||||||
|
@ -146,7 +146,7 @@ impl<R: Deref<Target=Transaction>> SigHashCache<R> {
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
script_code: &Script,
|
script_code: &Script,
|
||||||
value: u64,
|
value: u64,
|
||||||
sighash_type: SigHashType
|
sighash_type: EcdsaSigHashType
|
||||||
) -> SigHash {
|
) -> SigHash {
|
||||||
let mut enc = SigHash::engine();
|
let mut enc = SigHash::engine();
|
||||||
self.encode_signing_data_to(&mut enc, input_index, script_code, value, sighash_type)
|
self.encode_signing_data_to(&mut enc, input_index, script_code, value, sighash_type)
|
||||||
|
@ -165,7 +165,7 @@ impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
|
||||||
/// panics if `input_index` is out of bounds with respect of the number of inputs
|
/// panics if `input_index` is out of bounds with respect of the number of inputs
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use bitcoin::blockdata::transaction::{Transaction, SigHashType};
|
/// use bitcoin::blockdata::transaction::{Transaction, EcdsaSigHashType};
|
||||||
/// use bitcoin::util::bip143::SigHashCache;
|
/// use bitcoin::util::bip143::SigHashCache;
|
||||||
/// use bitcoin::Script;
|
/// use bitcoin::Script;
|
||||||
///
|
///
|
||||||
|
@ -175,7 +175,7 @@ impl<R: DerefMut<Target=Transaction>> SigHashCache<R> {
|
||||||
/// let mut sig_hasher = SigHashCache::new(&mut tx_to_sign);
|
/// let mut sig_hasher = SigHashCache::new(&mut tx_to_sign);
|
||||||
/// for inp in 0..input_count {
|
/// for inp in 0..input_count {
|
||||||
/// let prevout_script = Script::new();
|
/// let prevout_script = Script::new();
|
||||||
/// let _sighash = sig_hasher.signature_hash(inp, &prevout_script, 42, SigHashType::All);
|
/// let _sighash = sig_hasher.signature_hash(inp, &prevout_script, 42, EcdsaSigHashType::All);
|
||||||
/// // ... sign the sighash
|
/// // ... sign the sighash
|
||||||
/// sig_hasher.access_witness(inp).push(Vec::new());
|
/// sig_hasher.access_witness(inp).push(Vec::new());
|
||||||
/// }
|
/// }
|
||||||
|
@ -212,7 +212,7 @@ mod tests {
|
||||||
let raw_expected = SigHash::from_hex(expected_result).unwrap();
|
let raw_expected = SigHash::from_hex(expected_result).unwrap();
|
||||||
let expected_result = SigHash::from_slice(&raw_expected[..]).unwrap();
|
let expected_result = SigHash::from_slice(&raw_expected[..]).unwrap();
|
||||||
let mut cache = SigHashCache::new(&tx);
|
let mut cache = SigHashCache::new(&tx);
|
||||||
let sighash_type = SigHashType::from_u32_consensus(hash_type);
|
let sighash_type = EcdsaSigHashType::from_u32_consensus(hash_type);
|
||||||
let actual_result = cache.signature_hash(input_index, &script, value, sighash_type);
|
let actual_result = cache.signature_hash(input_index, &script, value, sighash_type);
|
||||||
assert_eq!(actual_result, expected_result);
|
assert_eq!(actual_result, expected_result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ use hashes::{Hash, hash160};
|
||||||
use hash_types::{PubkeyHash, WPubkeyHash};
|
use hash_types::{PubkeyHash, WPubkeyHash};
|
||||||
use util::base58;
|
use util::base58;
|
||||||
use util::key::Error;
|
use util::key::Error;
|
||||||
use blockdata::transaction::SigHashType;
|
use blockdata::transaction::EcdsaSigHashType;
|
||||||
|
|
||||||
|
|
||||||
/// A Bitcoin ECDSA public key
|
/// A Bitcoin ECDSA public key
|
||||||
|
@ -421,7 +421,7 @@ pub struct EcdsaSig {
|
||||||
/// The underlying ECDSA Signature
|
/// The underlying ECDSA Signature
|
||||||
pub sig: secp256k1::Signature,
|
pub sig: secp256k1::Signature,
|
||||||
/// The corresponding hash type
|
/// The corresponding hash type
|
||||||
pub hash_ty: SigHashType,
|
pub hash_ty: EcdsaSigHashType,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EcdsaSig {
|
impl EcdsaSig {
|
||||||
|
@ -430,7 +430,7 @@ impl EcdsaSig {
|
||||||
pub fn from_slice(sl: &[u8]) -> Result<Self, EcdsaSigError> {
|
pub fn from_slice(sl: &[u8]) -> Result<Self, EcdsaSigError> {
|
||||||
let (hash_ty, sig) = sl.split_last()
|
let (hash_ty, sig) = sl.split_last()
|
||||||
.ok_or(EcdsaSigError::EmptySignature)?;
|
.ok_or(EcdsaSigError::EmptySignature)?;
|
||||||
let hash_ty = SigHashType::from_u32_standard(*hash_ty as u32)
|
let hash_ty = EcdsaSigHashType::from_u32_standard(*hash_ty as u32)
|
||||||
.map_err(|_| EcdsaSigError::NonStandardSigHashType(*hash_ty))?;
|
.map_err(|_| EcdsaSigError::NonStandardSigHashType(*hash_ty))?;
|
||||||
let sig = secp256k1::Signature::from_der(sig)
|
let sig = secp256k1::Signature::from_der(sig)
|
||||||
.map_err(EcdsaSigError::Secp256k1)?;
|
.map_err(EcdsaSigError::Secp256k1)?;
|
||||||
|
|
|
@ -17,7 +17,7 @@ use prelude::*;
|
||||||
use io;
|
use io;
|
||||||
|
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
use blockdata::transaction::{EcdsaSigHashType, Transaction, TxOut};
|
||||||
use consensus::encode;
|
use consensus::encode;
|
||||||
use util::bip32::KeySource;
|
use util::bip32::KeySource;
|
||||||
use hashes::{self, hash160, ripemd160, sha256, sha256d};
|
use hashes::{self, hash160, ripemd160, sha256, sha256d};
|
||||||
|
@ -76,7 +76,7 @@ pub struct Input {
|
||||||
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
|
pub partial_sigs: BTreeMap<PublicKey, Vec<u8>>,
|
||||||
/// The sighash type to be used for this input. Signatures for this input
|
/// The sighash type to be used for this input. Signatures for this input
|
||||||
/// must use the sighash type.
|
/// must use the sighash type.
|
||||||
pub sighash_type: Option<SigHashType>,
|
pub sighash_type: Option<EcdsaSigHashType>,
|
||||||
/// The redeem script for this input.
|
/// The redeem script for this input.
|
||||||
pub redeem_script: Option<Script>,
|
pub redeem_script: Option<Script>,
|
||||||
/// The witness script for this input.
|
/// The witness script for this input.
|
||||||
|
@ -137,7 +137,7 @@ impl Map for Input {
|
||||||
}
|
}
|
||||||
PSBT_IN_SIGHASH_TYPE => {
|
PSBT_IN_SIGHASH_TYPE => {
|
||||||
impl_psbt_insert_pair! {
|
impl_psbt_insert_pair! {
|
||||||
self.sighash_type <= <raw_key: _>|<raw_value: SigHashType>
|
self.sighash_type <= <raw_key: _>|<raw_value: EcdsaSigHashType>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PSBT_IN_REDEEM_SCRIPT => {
|
PSBT_IN_REDEEM_SCRIPT => {
|
||||||
|
|
|
@ -507,7 +507,7 @@ mod tests {
|
||||||
use hash_types::Txid;
|
use hash_types::Txid;
|
||||||
|
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::{SigHashType, Transaction, TxIn, TxOut, OutPoint};
|
use blockdata::transaction::{EcdsaSigHashType, Transaction, TxIn, TxOut, OutPoint};
|
||||||
use consensus::encode::serialize_hex;
|
use consensus::encode::serialize_hex;
|
||||||
use util::psbt::map::{Map, Input, Output};
|
use util::psbt::map::{Map, Input, Output};
|
||||||
use util::psbt::raw;
|
use util::psbt::raw;
|
||||||
|
@ -733,7 +733,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
(&psbt.inputs[0].sighash_type).as_ref().unwrap(),
|
(&psbt.inputs[0].sighash_type).as_ref().unwrap(),
|
||||||
&SigHashType::All
|
&EcdsaSigHashType::All
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ use prelude::*;
|
||||||
use io;
|
use io;
|
||||||
|
|
||||||
use blockdata::script::Script;
|
use blockdata::script::Script;
|
||||||
use blockdata::transaction::{SigHashType, Transaction, TxOut};
|
use blockdata::transaction::{EcdsaSigHashType, Transaction, TxOut};
|
||||||
use consensus::encode::{self, serialize, Decodable};
|
use consensus::encode::{self, serialize, Decodable};
|
||||||
use util::bip32::{ChildNumber, Fingerprint, KeySource};
|
use util::bip32::{ChildNumber, Fingerprint, KeySource};
|
||||||
use hashes::{hash160, ripemd160, sha256, sha256d, Hash};
|
use hashes::{hash160, ripemd160, sha256, sha256d, Hash};
|
||||||
|
@ -126,16 +126,16 @@ impl Deserialize for Vec<u8> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for SigHashType {
|
impl Serialize for EcdsaSigHashType {
|
||||||
fn serialize(&self) -> Vec<u8> {
|
fn serialize(&self) -> Vec<u8> {
|
||||||
serialize(&self.as_u32())
|
serialize(&self.as_u32())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for SigHashType {
|
impl Deserialize for EcdsaSigHashType {
|
||||||
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
fn deserialize(bytes: &[u8]) -> Result<Self, encode::Error> {
|
||||||
let raw: u32 = encode::deserialize(bytes)?;
|
let raw: u32 = encode::deserialize(bytes)?;
|
||||||
let rv: SigHashType = SigHashType::from_u32_consensus(raw);
|
let rv: EcdsaSigHashType = EcdsaSigHashType::from_u32_consensus(raw);
|
||||||
|
|
||||||
if rv.as_u32() == raw {
|
if rv.as_u32() == raw {
|
||||||
Ok(rv)
|
Ok(rv)
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
//! and legacy (before Bip143).
|
//! and legacy (before Bip143).
|
||||||
//!
|
//!
|
||||||
|
|
||||||
pub use blockdata::transaction::SigHashType as LegacySigHashType;
|
pub use blockdata::transaction::EcdsaSigHashType;
|
||||||
use consensus::{encode, Encodable};
|
use consensus::{encode, Encodable};
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use core::ops::{Deref, DerefMut};
|
use core::ops::{Deref, DerefMut};
|
||||||
|
@ -79,7 +79,7 @@ struct TaprootCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Contains outputs of previous transactions.
|
/// Contains outputs of previous transactions.
|
||||||
/// In the case [`SigHashType`] variant is `ANYONECANPAY`, [`Prevouts::One`] may be provided
|
/// In the case [`SchnorrSigHashType`] variant is `ANYONECANPAY`, [`Prevouts::One`] may be provided
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||||
pub enum Prevouts<'u> {
|
pub enum Prevouts<'u> {
|
||||||
/// `One` variant allows to provide the single Prevout needed. It's useful for example
|
/// `One` variant allows to provide the single Prevout needed. It's useful for example
|
||||||
|
@ -105,7 +105,7 @@ pub struct ScriptPath<'s> {
|
||||||
/// Fixed values so they can be casted as integer types for encoding
|
/// Fixed values so they can be casted as integer types for encoding
|
||||||
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||||
pub enum SchnorrSigHashType {
|
pub enum SchnorrSigHashType {
|
||||||
/// 0x0: Used when not explicitly specified, defaulting to [`SigHashType::All`]
|
/// 0x0: Used when not explicitly specified, defaulting to [`SchnorrSigHashType::All`]
|
||||||
Default = 0x00,
|
Default = 0x00,
|
||||||
/// 0x1: Sign all outputs
|
/// 0x1: Sign all outputs
|
||||||
All = 0x01,
|
All = 0x01,
|
||||||
|
@ -233,15 +233,15 @@ impl<'s> ScriptPath<'s> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<LegacySigHashType> for SchnorrSigHashType {
|
impl From<EcdsaSigHashType> for SchnorrSigHashType {
|
||||||
fn from(s: LegacySigHashType) -> Self {
|
fn from(s: EcdsaSigHashType) -> Self {
|
||||||
match s {
|
match s {
|
||||||
LegacySigHashType::All => SchnorrSigHashType::All,
|
EcdsaSigHashType::All => SchnorrSigHashType::All,
|
||||||
LegacySigHashType::None => SchnorrSigHashType::None,
|
EcdsaSigHashType::None => SchnorrSigHashType::None,
|
||||||
LegacySigHashType::Single => SchnorrSigHashType::Single,
|
EcdsaSigHashType::Single => SchnorrSigHashType::Single,
|
||||||
LegacySigHashType::AllPlusAnyoneCanPay => SchnorrSigHashType::AllPlusAnyoneCanPay,
|
EcdsaSigHashType::AllPlusAnyoneCanPay => SchnorrSigHashType::AllPlusAnyoneCanPay,
|
||||||
LegacySigHashType::NonePlusAnyoneCanPay => SchnorrSigHashType::NonePlusAnyoneCanPay,
|
EcdsaSigHashType::NonePlusAnyoneCanPay => SchnorrSigHashType::NonePlusAnyoneCanPay,
|
||||||
LegacySigHashType::SinglePlusAnyoneCanPay => SchnorrSigHashType::SinglePlusAnyoneCanPay,
|
EcdsaSigHashType::SinglePlusAnyoneCanPay => SchnorrSigHashType::SinglePlusAnyoneCanPay,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -460,7 +460,7 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
script_code: &Script,
|
script_code: &Script,
|
||||||
value: u64,
|
value: u64,
|
||||||
sighash_type: LegacySigHashType,
|
sighash_type: EcdsaSigHashType,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let zero_hash = sha256d::Hash::default();
|
let zero_hash = sha256d::Hash::default();
|
||||||
|
|
||||||
|
@ -475,8 +475,8 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !anyone_can_pay
|
if !anyone_can_pay
|
||||||
&& sighash != LegacySigHashType::Single
|
&& sighash != EcdsaSigHashType::Single
|
||||||
&& sighash != LegacySigHashType::None
|
&& sighash != EcdsaSigHashType::None
|
||||||
{
|
{
|
||||||
self.segwit_cache()
|
self.segwit_cache()
|
||||||
.sequences
|
.sequences
|
||||||
|
@ -502,9 +502,9 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
|
||||||
txin.sequence.consensus_encode(&mut writer)?;
|
txin.sequence.consensus_encode(&mut writer)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
if sighash != LegacySigHashType::Single && sighash != LegacySigHashType::None {
|
if sighash != EcdsaSigHashType::Single && sighash != EcdsaSigHashType::None {
|
||||||
self.segwit_cache().outputs.consensus_encode(&mut writer)?;
|
self.segwit_cache().outputs.consensus_encode(&mut writer)?;
|
||||||
} else if sighash == LegacySigHashType::Single && input_index < self.tx.output.len() {
|
} else if sighash == EcdsaSigHashType::Single && input_index < self.tx.output.len() {
|
||||||
let mut single_enc = SigHash::engine();
|
let mut single_enc = SigHash::engine();
|
||||||
self.tx.output[input_index].consensus_encode(&mut single_enc)?;
|
self.tx.output[input_index].consensus_encode(&mut single_enc)?;
|
||||||
SigHash::from_engine(single_enc).consensus_encode(&mut writer)?;
|
SigHash::from_engine(single_enc).consensus_encode(&mut writer)?;
|
||||||
|
@ -523,7 +523,7 @@ impl<R: Deref<Target = Transaction>> SigHashCache<R> {
|
||||||
input_index: usize,
|
input_index: usize,
|
||||||
script_code: &Script,
|
script_code: &Script,
|
||||||
value: u64,
|
value: u64,
|
||||||
sighash_type: LegacySigHashType,
|
sighash_type: EcdsaSigHashType,
|
||||||
) -> Result<SigHash, Error> {
|
) -> Result<SigHash, Error> {
|
||||||
let mut enc = SigHash::engine();
|
let mut enc = SigHash::engine();
|
||||||
self.segwit_encode_signing_data_to(
|
self.segwit_encode_signing_data_to(
|
||||||
|
@ -645,7 +645,7 @@ impl<R: DerefMut<Target = Transaction>> SigHashCache<R> {
|
||||||
///
|
///
|
||||||
/// This allows in-line signing such as
|
/// This allows in-line signing such as
|
||||||
/// ```
|
/// ```
|
||||||
/// use bitcoin::blockdata::transaction::{Transaction, SigHashType};
|
/// use bitcoin::blockdata::transaction::{Transaction, EcdsaSigHashType};
|
||||||
/// use bitcoin::util::sighash::SigHashCache;
|
/// use bitcoin::util::sighash::SigHashCache;
|
||||||
/// use bitcoin::Script;
|
/// use bitcoin::Script;
|
||||||
///
|
///
|
||||||
|
@ -655,7 +655,7 @@ impl<R: DerefMut<Target = Transaction>> SigHashCache<R> {
|
||||||
/// let mut sig_hasher = SigHashCache::new(&mut tx_to_sign);
|
/// let mut sig_hasher = SigHashCache::new(&mut tx_to_sign);
|
||||||
/// for inp in 0..input_count {
|
/// for inp in 0..input_count {
|
||||||
/// let prevout_script = Script::new();
|
/// let prevout_script = Script::new();
|
||||||
/// let _sighash = sig_hasher.segwit_signature_hash(inp, &prevout_script, 42, SigHashType::All);
|
/// let _sighash = sig_hasher.segwit_signature_hash(inp, &prevout_script, 42, EcdsaSigHashType::All);
|
||||||
/// // ... sign the sighash
|
/// // ... sign the sighash
|
||||||
/// sig_hasher.witness_mut(inp).unwrap().push(Vec::new());
|
/// sig_hasher.witness_mut(inp).unwrap().push(Vec::new());
|
||||||
/// }
|
/// }
|
||||||
|
|
Loading…
Reference in New Issue