From 7514f2ca18966878ea3726e6530c21890181d533 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 13 Jan 2022 17:40:27 +0100 Subject: [PATCH 1/4] Tweaked -> untweaked keys conversions --- src/util/schnorr.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index 8a1d8f9a..ee030275 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -189,6 +189,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))] From 6364ebd927bca9443193bc3ee607c4c8b2a34932 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 13 Jan 2022 17:40:46 +0100 Subject: [PATCH 2/4] Code style fixups to taproot key functions --- src/blockdata/script.rs | 5 +---- src/util/schnorr.rs | 7 +++---- 2 files changed, 4 insertions(+), 8 deletions(-) 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/util/schnorr.rs b/src/util/schnorr.rs index ee030275..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 { From 73e6ce4e5335b560563bb50ebbe29c84cc174095 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 13 Jan 2022 17:51:01 +0100 Subject: [PATCH 3/4] Re-export Witness at crate level. Closes #770 --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) 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; From 62a27a51e2c10e5bfae02ff819a5865795596a5d Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 13 Jan 2022 17:53:50 +0100 Subject: [PATCH 4/4] Document that serde impl of LeafVersion uses u8 in consensus encoding Closes #764 --- src/util/taproot.rs | 2 ++ 1 file changed, 2 insertions(+) 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 {