diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index d0aa9a19..d38bcea2 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -344,9 +344,6 @@ impl Script { } /// Generates P2TR for key spending path for a known [`TweakedPublicKey`]. - /// - /// NB: Make sure that the used key is indeed tweaked (for instance, it comes from `rawtr` - /// descriptor content); otherwise please use [`Script::new_v1_p2tr`] method. pub fn new_v1_p2tr_tweaked(output_key: TweakedPublicKey) -> Script { Script::new_witness_program(WitnessVersion::V1, &output_key.serialize()) } @@ -414,7 +411,7 @@ impl Script { #[inline] fn witness_version(&self) -> Option { - WitnessVersion::from_opcode(self.0[0].into()).ok() + self.0.get(0).and_then(|opcode| WitnessVersion::from_opcode(opcodes::All::from(*opcode)).ok()) } /// Checks whether a script pubkey is a p2sh output diff --git a/src/lib.rs b/src/lib.rs index c347d0bc..ef326038 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,7 @@ pub use blockdata::transaction::TxIn; pub use blockdata::transaction::TxOut; pub use blockdata::transaction::OutPoint; pub use blockdata::transaction::EcdsaSigHashType; +pub use blockdata::witness::Witness; pub use consensus::encode::VarInt; pub use network::constants::Network; pub use util::Error; diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index 8a1d8f9a..6a0c9fa1 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -128,12 +128,11 @@ impl TapTweak for UntweakedKeyPair { /// /// # Returns /// The tweaked key and its parity. - fn tap_tweak(self, secp: &Secp256k1, merkle_root: Option) -> TweakedKeyPair { + fn tap_tweak(mut self, secp: &Secp256k1, merkle_root: Option) -> TweakedKeyPair { let pubkey = XOnlyPublicKey::from_keypair(&self); let tweak_value = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).into_inner(); - let mut output_key = self.clone(); - output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed"); - TweakedKeyPair(output_key) + self.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed"); + TweakedKeyPair(self) } fn dangerous_assume_tweaked(self) -> TweakedKeyPair { @@ -189,6 +188,20 @@ impl TweakedKeyPair { } } +impl From for XOnlyPublicKey { + #[inline] + fn from(pair: TweakedPublicKey) -> Self { + pair.0 + } +} + +impl From for KeyPair { + #[inline] + fn from(pair: TweakedKeyPair) -> Self { + pair.0 + } +} + /// A BIP340-341 serialized schnorr signature with the corresponding hash type. #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] diff --git a/src/util/taproot.rs b/src/util/taproot.rs index 9eb87f6e..a4ef1eac 100644 --- a/src/util/taproot.rs +++ b/src/util/taproot.rs @@ -869,6 +869,7 @@ impl fmt::UpperHex for LeafVersion { } } +/// Serializes LeafVersion as u8 using consensus encoding #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl ::serde::Serialize for LeafVersion { @@ -880,6 +881,7 @@ impl ::serde::Serialize for LeafVersion { } } +/// Deserializes LeafVersion as u8 using consensus encoding #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl<'de> ::serde::Deserialize<'de> for LeafVersion {