Skip to content

Commit

Permalink
Introduce FutureLeafVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jan 8, 2022
1 parent b028385 commit ef8a3a8
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/util/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,14 +760,45 @@ impl ControlBlock {
}
}

/// Inner type representing future (non-tapscript) leaf versions. See [`LeafVersion::Future`].
///
/// NB: NO PUBLIC CONSTRUCTOR!
/// The only way to construct this is by converting u8 to LeafVersion and then extracting it.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct FutureLeafVersion(u8);

impl FutureLeafVersion {
pub(self) fn from_consensus(version: u8) -> Result<FutureLeafVersion, TaprootError> {
match version {
TAPROOT_LEAF_TAPSCRIPT => unreachable!("FutureLeafVersion::from_consensus should be never called for 0xC0 value"),
TAPROOT_ANNEX_PREFIX => Err(TaprootError::InvalidTaprootLeafVersion(TAPROOT_ANNEX_PREFIX)),
odd if odd & 0xFE != odd => Err(TaprootError::InvalidTaprootLeafVersion(odd)),
even => Ok(FutureLeafVersion(even))
}
}

/// Get consensus representation of the future leaf version.
#[inline]
pub fn into_consensus(self) -> u8 {
self.0
}
}

impl fmt::Display for FutureLeafVersion {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

/// The leaf version for tapleafs
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LeafVersion {
/// BIP-342 tapscript
TapScript,

/// Future leaf version
Future(u8)
Future(FutureLeafVersion)
}

impl LeafVersion {
Expand All @@ -790,16 +821,15 @@ impl LeafVersion {
match version {
TAPROOT_LEAF_TAPSCRIPT => Ok(LeafVersion::TapScript),
TAPROOT_ANNEX_PREFIX => Err(TaprootError::InvalidTaprootLeafVersion(TAPROOT_ANNEX_PREFIX)),
odd if odd & TAPROOT_LEAF_MASK != odd => Err(TaprootError::InvalidTaprootLeafVersion(odd)),
future => Ok(LeafVersion::Future(future)),
future => FutureLeafVersion::from_consensus(future).map(LeafVersion::Future),
}
}

/// Get consensus representation of the [`LeafVersion`].
pub fn into_consensus(self) -> u8 {
match self {
LeafVersion::TapScript => TAPROOT_LEAF_TAPSCRIPT,
LeafVersion::Future(version) => version,
LeafVersion::Future(version) => version.into_consensus(),
}
}
}
Expand All @@ -809,7 +839,7 @@ impl fmt::Display for LeafVersion {
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),
(LeafVersion::Future(version), false) => write!(f, "future_script_{:#02x}", version.0),
(LeafVersion::Future(version), true) => fmt::Display::fmt(version, f),
}
}
Expand Down

0 comments on commit ef8a3a8

Please sign in to comment.