From 7c28b474516c60df18d57f47aea83bb8431def2c Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 9 Jan 2022 20:46:51 +0100 Subject: [PATCH 1/4] LowerHex and UpperHex implementations for FutureLeafVersion --- src/util/taproot.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 9a1043e7..691b25b6 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -98,7 +98,6 @@ sha256t_hash_newtype!(TapSighashHash, TapSighashTag, MIDSTATE_TAPSIGHASH, 64, ); impl TapTweakHash { - /// Create a new BIP341 [`TapTweakHash`] from key and tweak /// Produces H_taptweak(P||R) where P is internal key and R is the merkle root pub fn from_key_and_tweak( @@ -791,6 +790,20 @@ impl fmt::Display for FutureLeafVersion { } } +impl fmt::LowerHex for FutureLeafVersion { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::LowerHex::fmt(&self.0, f) + } +} + +impl fmt::UpperHex for FutureLeafVersion { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::UpperHex::fmt(&self.0, f) + } +} + /// The leaf version for tapleafs #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum LeafVersion { From bec669423392cd3e82d8a35a8e311f1507f0ca03 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 9 Jan 2022 20:48:00 +0100 Subject: [PATCH 2/4] Fix docs on error conditions in LeafVersion::from_consensus --- src/util/taproot.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 691b25b6..1420253a 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -820,7 +820,6 @@ impl LeafVersion { /// # Errors /// - If the last bit of the `version` is odd. /// - If the `version` is 0x50 ([`TAPROOT_ANNEX_PREFIX`]). - /// - If the `version` is not a valid [`LeafVersion`] byte. // Text from BIP341: // In order to support some forms of static analysis that rely on // being able to identify script spends without access to the output being From 6a3f3aabafa4331bf40ff288ce3190e18dcce96f Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 9 Jan 2022 20:50:22 +0100 Subject: [PATCH 3/4] Inverse alternative formatting for LeafVersion type --- src/util/taproot.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 1420253a..185006f4 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -849,10 +849,10 @@ impl LeafVersion { impl fmt::Display for LeafVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match (self, f.alternate()) { - (LeafVersion::TapScript, false) => f.write_str("tapscript"), - (LeafVersion::TapScript, true) => fmt::Display::fmt(&TAPROOT_LEAF_TAPSCRIPT, f), - (LeafVersion::Future(version), false) => write!(f, "future_script_{:#02x}", version.0), - (LeafVersion::Future(version), true) => fmt::Display::fmt(version, f), + (LeafVersion::TapScript, true) => f.write_str("tapscript"), + (LeafVersion::TapScript, false) => fmt::Display::fmt(&TAPROOT_LEAF_TAPSCRIPT, f), + (LeafVersion::Future(version), true) => write!(f, "future_script_{:#02x}", version.0), + (LeafVersion::Future(version), false) => fmt::Display::fmt(version, f), } } } From 7f06e91a93c5d35fa2408901cc79ec6410aafd73 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Sun, 9 Jan 2022 20:52:38 +0100 Subject: [PATCH 4/4] LowerHex and UpperHex implementations for LeafVersion --- src/util/taproot.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 185006f4..0e182855 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -857,6 +857,18 @@ impl fmt::Display for LeafVersion { } } +impl fmt::LowerHex for LeafVersion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::LowerHex::fmt(&self.into_consensus(), f) + } +} + +impl fmt::UpperHex for LeafVersion { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::UpperHex::fmt(&self.into_consensus(), f) + } +} + #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl ::serde::Serialize for LeafVersion {