psbt: implement const for PsbtSighashType::ALL

This commit is contained in:
Jose Storopoli 2024-06-23 08:47:28 +00:00
parent 09740853a9
commit d1f84329e4
No known key found for this signature in database
GPG Key ID: 29E00111DE172C28
1 changed files with 23 additions and 0 deletions

View File

@ -197,6 +197,22 @@ impl From<TapSighashType> for PsbtSighashType {
}
impl PsbtSighashType {
/// Ambiguous `ALL` sighash type, may refer to either [`EcdsaSighashType::All`]
/// or [`TapSighashType::All`].
///
/// This is equivalent to either `EcdsaSighashType::All.into()` or `TapSighashType::All.into()`.
/// For sighash types other than `ALL` use the ECDSA or Taproot sighash type directly.
///
/// # Examples
///
/// ```
/// use bitcoin::{EcdsaSighashType, TapSighashType};
/// use bitcoin::psbt::PsbtSighashType;
/// let ecdsa_sighash_anyone_can_pay: PsbtSighashType = EcdsaSighashType::AllPlusAnyoneCanPay.into();
/// let tap_sighash_anyone_can_pay: PsbtSighashType = TapSighashType::AllPlusAnyoneCanPay.into();
/// ```
pub const ALL: PsbtSighashType = PsbtSighashType { inner: 0x01 };
/// Returns the [`EcdsaSighashType`] if the [`PsbtSighashType`] can be
/// converted to one.
pub fn ecdsa_hash_ty(self) -> Result<EcdsaSighashType, NonStandardSighashTypeError> {
@ -545,4 +561,11 @@ mod test {
assert_eq!(back.ecdsa_hash_ty(), Err(NonStandardSighashTypeError(nonstd)));
assert_eq!(back.taproot_hash_ty(), Err(InvalidSighashTypeError(nonstd)));
}
#[test]
fn psbt_sighash_const_all() {
assert_eq!(PsbtSighashType::ALL.to_u32(), 0x01);
assert_eq!(PsbtSighashType::ALL.ecdsa_hash_ty().unwrap(), EcdsaSighashType::All);
assert_eq!(PsbtSighashType::ALL.taproot_hash_ty().unwrap(), TapSighashType::All);
}
}