From 6364ebd927bca9443193bc3ee607c4c8b2a34932 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Thu, 13 Jan 2022 17:40:46 +0100 Subject: [PATCH] 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 {