From e05776f176afacf8088f2fb3b07967544d444ce4 Mon Sep 17 00:00:00 2001 From: Tobin Harding Date: Tue, 22 Feb 2022 19:34:26 +0000 Subject: [PATCH] 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`. --- src/util/psbt/map/input.rs | 15 +++++++++++++-- src/util/psbt/serialize.rs | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/util/psbt/map/input.rs b/src/util/psbt/map/input.rs index 9d275cbe..e7132834 100644 --- a/src/util/psbt/map/input.rs +++ b/src/util/psbt/map/input.rs @@ -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 } } diff --git a/src/util/psbt/serialize.rs b/src/util/psbt/serialize.rs index 0ee01fdf..8492e8af 100644 --- a/src/util/psbt/serialize.rs +++ b/src/util/psbt/serialize.rs @@ -193,7 +193,7 @@ impl Deserialize for Vec { impl Serialize for PsbtSigHashType { fn serialize(&self) -> Vec { - serialize(&self.inner()) + serialize(&self.to_u32()) } }