Merge rust-bitcoin/rust-bitcoin#778: Fixups for taproot improvements

62a27a51e2 Document that serde impl of LeafVersion uses u8 in consensus encoding (Dr Maxim Orlovsky)
73e6ce4e53 Re-export Witness at crate level. Closes #770 (Dr Maxim Orlovsky)
6364ebd927 Code style fixups to taproot key functions (Dr Maxim Orlovsky)
7514f2ca18 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 62a27a51e2
  sanket1729:
    utACK 62a27a51e2

Tree-SHA512: 2f10393abab41d1c82f4733d83bf85bd82e268a2891c156eb89744c0fc444fdfec4d60deec2dda6fde2e5881989625c18b9c5ca21d360018edba69b6d2a85eae
This commit is contained in:
sanket1729 2022-01-14 04:12:14 +05:30
commit b2de2bc33d
No known key found for this signature in database
GPG Key ID: 648FFB183E0870A2
4 changed files with 21 additions and 8 deletions

View File

@ -344,9 +344,6 @@ impl Script {
} }
/// Generates P2TR for key spending path for a known [`TweakedPublicKey`]. /// 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 { pub fn new_v1_p2tr_tweaked(output_key: TweakedPublicKey) -> Script {
Script::new_witness_program(WitnessVersion::V1, &output_key.serialize()) Script::new_witness_program(WitnessVersion::V1, &output_key.serialize())
} }
@ -414,7 +411,7 @@ impl Script {
#[inline] #[inline]
fn witness_version(&self) -> Option<WitnessVersion> { fn witness_version(&self) -> Option<WitnessVersion> {
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 /// Checks whether a script pubkey is a p2sh output

View File

@ -124,6 +124,7 @@ pub use blockdata::transaction::TxIn;
pub use blockdata::transaction::TxOut; pub use blockdata::transaction::TxOut;
pub use blockdata::transaction::OutPoint; pub use blockdata::transaction::OutPoint;
pub use blockdata::transaction::EcdsaSigHashType; pub use blockdata::transaction::EcdsaSigHashType;
pub use blockdata::witness::Witness;
pub use consensus::encode::VarInt; pub use consensus::encode::VarInt;
pub use network::constants::Network; pub use network::constants::Network;
pub use util::Error; pub use util::Error;

View File

@ -128,12 +128,11 @@ impl TapTweak for UntweakedKeyPair {
/// ///
/// # Returns /// # Returns
/// The tweaked key and its parity. /// The tweaked key and its parity.
fn tap_tweak<C: Verification>(self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair { fn tap_tweak<C: Verification>(mut self, secp: &Secp256k1<C>, merkle_root: Option<TapBranchHash>) -> TweakedKeyPair {
let pubkey = XOnlyPublicKey::from_keypair(&self); let pubkey = XOnlyPublicKey::from_keypair(&self);
let tweak_value = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).into_inner(); let tweak_value = TapTweakHash::from_key_and_tweak(pubkey, merkle_root).into_inner();
let mut output_key = self.clone(); self.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed");
output_key.tweak_add_assign(&secp, &tweak_value).expect("Tap tweak failed"); TweakedKeyPair(self)
TweakedKeyPair(output_key)
} }
fn dangerous_assume_tweaked(self) -> TweakedKeyPair { fn dangerous_assume_tweaked(self) -> TweakedKeyPair {
@ -189,6 +188,20 @@ impl TweakedKeyPair {
} }
} }
impl From<TweakedPublicKey> for XOnlyPublicKey {
#[inline]
fn from(pair: TweakedPublicKey) -> Self {
pair.0
}
}
impl From<TweakedKeyPair> for KeyPair {
#[inline]
fn from(pair: TweakedKeyPair) -> Self {
pair.0
}
}
/// A BIP340-341 serialized schnorr signature with the corresponding hash type. /// A BIP340-341 serialized schnorr signature with the corresponding hash type.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

View File

@ -869,6 +869,7 @@ impl fmt::UpperHex for LeafVersion {
} }
} }
/// Serializes LeafVersion as u8 using consensus encoding
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl ::serde::Serialize for LeafVersion { 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(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> ::serde::Deserialize<'de> for LeafVersion { impl<'de> ::serde::Deserialize<'de> for LeafVersion {