Skip to content

Commit

Permalink
Merge #761: Taproot trivial post-merge fixups
Browse files Browse the repository at this point in the history
7f06e91 LowerHex and UpperHex implementations for LeafVersion (Dr Maxim Orlovsky)
6a3f3aa Inverse alternative formatting for LeafVersion type (Dr Maxim Orlovsky)
bec6694 Fix docs on error conditions in LeafVersion::from_consensus (Dr Maxim Orlovsky)
7c28b47 LowerHex and UpperHex implementations for FutureLeafVersion (Dr Maxim Orlovsky)

Pull request description:

  Trivial post-merge fixups from review comments in #718

ACKs for top commit:
  Kixunil:
    ACK 7f06e91
  sanket1729:
    ACK 7f06e91

Tree-SHA512: d94c4bd3d0b466287c8965103f74ecaba185d14c13b6c3f37d9fbe194343b3fc902fd2c7716554ad01fe28ff89cda933df199b7e8388a3fa6097028caf62522b
  • Loading branch information
sanket1729 committed Jan 9, 2022
2 parents 476eed7 + 7f06e91 commit d82afc6
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/util/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 {
Expand All @@ -807,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
Expand Down Expand Up @@ -837,14 +849,26 @@ 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),
}
}
}

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 {
Expand Down

0 comments on commit d82afc6

Please sign in to comment.