Skip to content

Commit f197f5f

Browse files
author
ChallengeDev210
committed
Merge rust-bitcoin/rust-bitcoin#778: Fixups for taproot improvements
62a27a5 Document that serde impl of LeafVersion uses u8 in consensus encoding (Dr Maxim Orlovsky) 73e6ce4 Re-export Witness at crate level. Closes #770 (Dr Maxim Orlovsky) 6364ebd Code style fixups to taproot key functions (Dr Maxim Orlovsky) 7514f2c Tweaked -> untweaked keys conversions (Dr Maxim Orlovsky) Pull request description: This addresses @Kixunil review comments in #696 post-merge Update: also closes nits from issues #764 and #770 ACKs for top commit: Kixunil: ACK 62a27a5 sanket1729: utACK 62a27a5 Tree-SHA512: 2f10393abab41d1c82f4733d83bf85bd82e268a2891c156eb89744c0fc444fdfec4d60deec2dda6fde2e5881989625c18b9c5ca21d360018edba69b6d2a85eae
2 parents 67a445c + 7555a91 commit f197f5f

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/blockdata/script.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,6 @@ impl Script {
344344
}
345345

346346
/// Generates P2TR for key spending path for a known [`TweakedPublicKey`].
347-
///
348-
/// NB: Make sure that the used key is indeed tweaked (for instance, it comes from `rawtr`
349-
/// descriptor content); otherwise please use [`Script::new_v1_p2tr`] method.
350347
pub fn new_v1_p2tr_tweaked(output_key: TweakedPublicKey) -> Script {
351348
Script::new_witness_program(WitnessVersion::V1, &output_key.serialize())
352349
}
@@ -414,7 +411,7 @@ impl Script {
414411

415412
#[inline]
416413
fn witness_version(&self) -> Option<WitnessVersion> {
417-
WitnessVersion::from_opcode(self.0[0].into()).ok()
414+
self.0.get(0).and_then(|opcode| WitnessVersion::from_opcode(opcodes::All::from(*opcode)).ok())
418415
}
419416

420417
/// Checks whether a script pubkey is a p2sh output

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub use blockdata::transaction::TxIn;
124124
pub use blockdata::transaction::TxOut;
125125
pub use blockdata::transaction::OutPoint;
126126
pub use blockdata::transaction::EcdsaSigHashType;
127+
pub use blockdata::witness::Witness;
127128
pub use consensus::encode::VarInt;
128129
pub use network::constants::Network;
129130
pub use util::Error;

src/util/schnorr.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,11 @@ impl TapTweak for UntweakedKeyPair {
128128
///
129129
/// # Returns
130130
/// The tweaked key and its parity.
131-
fn tap_tweak<C: Verification>(self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
131+
fn tap_tweak<C: Verification>(mut self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
132132
let pubkey = XOnlyPublicKey::from_keypair(&self);
133133
let tweak_value = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).into_inner();
134-
let mut output_key = self.clone();
135-
output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
136-
TweakedKeyPair(output_key)
134+
self.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
135+
TweakedKeyPair(self)
137136
}
138137

139138
fn dangerous_assume_tweaked(self) -> TweakedKeyPair {
@@ -189,6 +188,20 @@ impl TweakedKeyPair {
189188
}
190189
}
191190

191+
impl From<TweakedPublicKey> for XOnlyPublicKey {
192+
#[inline]
193+
fn from(pair: TweakedPublicKey) -> Self {
194+
pair.0
195+
}
196+
}
197+
198+
impl From<TweakedKeyPair> for KeyPair {
199+
#[inline]
200+
fn from(pair: TweakedKeyPair) -> Self {
201+
pair.0
202+
}
203+
}
204+
192205
/// A BIP340-341 serialized schnorr signature with the corresponding hash type.
193206
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
194207
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/util/taproot.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ impl fmt::UpperHex for LeafVersion {
869869
}
870870
}
871871

872+
/// Serializes LeafVersion as u8 using consensus encoding
872873
#[cfg(feature = "serde")]
873874
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
874875
impl ::serde::Serialize for LeafVersion {
@@ -880,6 +881,7 @@ impl ::serde::Serialize for LeafVersion {
880881
}
881882
}
882883

884+
/// Deserializes LeafVersion as u8 using consensus encoding
883885
#[cfg(feature = "serde")]
884886
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
885887
impl<'de> ::serde::Deserialize<'de> for LeafVersion {

0 commit comments

Comments
 (0)