Improve PsbtSigHashType conversion methods

Improve the `PsbtSigHashType` conversion methods by doing:

- Re-name `inner` -> `to_u32` as per Rust convention
- Add `from_u32` method

Note, we explicitly do _not_ use suffix 'consensus' because these
conversion methods make no guarantees about the validity of the
underlying `u32`.
This commit is contained in:
Tobin Harding 2022-02-22 19:34:26 +00:00
parent ac462897b1
commit e05776f176
2 changed files with 14 additions and 3 deletions

View File

@ -184,8 +184,19 @@ impl PsbtSigHashType {
}
}
/// Obtains the inner sighash byte from this [`PsbtSigHashType`].
pub fn inner(self) -> u32 {
/// Creates a [`PsbtSigHashType`] from a raw `u32`.
///
/// Allows construction of a non-standard or non-valid sighash flag
/// ([`EcdsaSigHashType`], [`SchnorrSigHashType`] respectively).
pub fn from_u32(n: u32) -> PsbtSigHashType {
PsbtSigHashType { inner: n }
}
/// Converts [`PsbtSigHashType`] to a raw `u32` sighash flag.
///
/// No guarantees are made as to the standardness or validity of the returned value.
pub fn to_u32(self) -> u32 {
self.inner
}
}

View File

@ -193,7 +193,7 @@ impl Deserialize for Vec<u8> {
impl Serialize for PsbtSigHashType {
fn serialize(&self) -> Vec<u8> {
serialize(&self.inner())
serialize(&self.to_u32())
}
}