PsbtSigHashType Display and FromStr implementation

This commit is contained in:
Dr Maxim Orlovsky 2022-03-24 07:22:28 +01:00
parent 7cdcdaad6c
commit 5be1cdb8c7
No known key found for this signature in database
GPG Key ID: FFC0250947E5C6F7
2 changed files with 38 additions and 2 deletions

View File

@ -758,7 +758,7 @@ impl str::FromStr for EcdsaSigHashType {
"SIGHASH_ALL|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::AllPlusAnyoneCanPay),
"SIGHASH_NONE|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::NonePlusAnyoneCanPay),
"SIGHASH_SINGLE|SIGHASH_ANYONECANPAY" => Ok(EcdsaSigHashType::SinglePlusAnyoneCanPay),
_ => Err(SigHashTypeParseError{ unrecognized: s.to_owned() }),
_ => Err(SigHashTypeParseError { unrecognized: s.to_owned() }),
}
}
}

View File

@ -14,11 +14,13 @@
use prelude::*;
use io;
use core::fmt;
use core::str::FromStr;
use secp256k1;
use blockdata::script::Script;
use blockdata::witness::Witness;
use blockdata::transaction::{Transaction, TxOut, NonStandardSigHashType};
use blockdata::transaction::{Transaction, TxOut, NonStandardSigHashType, SigHashTypeParseError};
use consensus::encode;
use hashes::{self, hash160, ripemd160, sha256, sha256d};
use secp256k1::XOnlyPublicKey;
@ -155,6 +157,40 @@ pub struct PsbtSigHashType {
pub (in ::util::psbt) inner: u32,
}
impl fmt::Display for PsbtSigHashType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.schnorr_hash_ty() {
Ok(SchnorrSigHashType::Reserved) | Err(_) => write!(f, "{:#x}", self.inner),
Ok(schnorr_hash_ty) => fmt::Display::fmt(&schnorr_hash_ty, f),
}
}
}
impl FromStr for PsbtSigHashType {
type Err = SigHashTypeParseError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
// We accept strings of form: "SIGHASH_ALL" etc.
//
// NB: some of Schnorr sighash types are non-standard for pre-taproot
// inputs. We also do not support SIGHASH_RESERVED in verbatim form
// ("0xFF" string should be used instead).
match SchnorrSigHashType::from_str(s) {
Ok(SchnorrSigHashType::Reserved) => return Err(SigHashTypeParseError{ unrecognized: s.to_owned() }),
Ok(ty) => return Ok(ty.into()),
Err(_) => {}
}
// We accept non-standard sighash values.
// TODO: Swap `trim_left_matches` for `trim_start_matches` once MSRV >= 1.30.
if let Ok(inner) = u32::from_str_radix(s.trim_left_matches("0x"), 16) {
return Ok(PsbtSigHashType { inner });
}
Err(SigHashTypeParseError{ unrecognized: s.to_owned() })
}
}
impl From<EcdsaSigHashType> for PsbtSigHashType {
fn from(ecdsa_hash_ty: EcdsaSigHashType) -> Self {
PsbtSigHashType { inner: ecdsa_hash_ty as u32 }