diff --git a/src/util/address.rs b/src/util/address.rs index e9788227..3f54a597 100644 --- a/src/util/address.rs +++ b/src/util/address.rs @@ -514,9 +514,9 @@ impl Address { /// Create a pay to taproot address from untweaked key pub fn p2tr( - secp: Secp256k1, + secp: &Secp256k1, internal_key: UntweakedPublicKey, - merkle_root: Option, + merkle_root: Option<&TapBranchHash>, network: Network ) -> Address { Address { @@ -539,7 +539,7 @@ impl Address { network: network, payload: Payload::WitnessProgram { version: WitnessVersion::V1, - program: output_key.into_inner().serialize().to_vec() + program: output_key.as_inner().serialize().to_vec() } } } @@ -1196,7 +1196,8 @@ mod tests { fn p2tr_from_untweaked(){ //Test case from BIP-086 let internal_key = schnorrsig::PublicKey::from_str("cc8a4bc64d897bddc5fbc2f670f7a8ba0b386779106cf1223c6fc5d7cd6fc115").unwrap(); - let address = Address::p2tr(Secp256k1::new(), internal_key,None, Network::Bitcoin); + let secp = Secp256k1::verification_only(); + let address = Address::p2tr(&secp, internal_key, None, Network::Bitcoin); assert_eq!(address.to_string(), "bc1p5cyxnuxmeuwuvkwfem96lqzszd02n6xdcjrs20cac6yqjjwudpxqkedrcr"); assert_eq!(address.address_type(), Some(AddressType::P2tr)); roundtrips(&address); diff --git a/src/util/schnorr.rs b/src/util/schnorr.rs index cfc13310..28f80715 100644 --- a/src/util/schnorr.rs +++ b/src/util/schnorr.rs @@ -31,30 +31,29 @@ pub struct TweakedPublicKey(PublicKey); /// A trait for tweaking Schnorr public keys pub trait TapTweak { /// Tweaks an untweaked public key given an untweaked key and optional script tree merkle root. - /// - /// This is done by using the equation Q = P + H(P|c)G, where + /// + /// This is done by using the equation Q = P + H(P|c)G, where /// * Q is the tweaked key /// * P is the internal key /// * H is the hash function /// * c is the commitment data /// * G is the generator point - fn tap_tweak(&self, secp: Secp256k1, merkle_root: Option) -> TweakedPublicKey; + fn tap_tweak(&self, secp: &Secp256k1, merkle_root: Option<&TapBranchHash>) -> TweakedPublicKey; /// Directly convert an UntweakedPublicKey to a TweakedPublicKey - /// + /// /// This method is dangerous and can lead to loss of funds if used incorrectly. /// Specifically, in multi-party protocols a peer can provide a value that allows them to steal. fn dangerous_assume_tweaked(self) -> TweakedPublicKey; } impl TapTweak for UntweakedPublicKey { - fn tap_tweak(&self, secp: Secp256k1, merkle_root: Option) -> TweakedPublicKey { + fn tap_tweak(&self, secp: &Secp256k1, merkle_root: Option<&TapBranchHash>) -> TweakedPublicKey { // Compute the tweak let mut engine = TapTweakHash::engine(); engine.input(&self.serialize()); merkle_root.map(|hash| engine.input(&hash)); let tweak_value: [u8; 32] = TapTweakHash::from_engine(engine).into_inner(); - //Tweak the internal key by the tweak value let mut output_key = self.clone(); @@ -64,7 +63,6 @@ impl TapTweak for UntweakedPublicKey { } else { unreachable!("Tap tweak failed") } } - fn dangerous_assume_tweaked(self) -> TweakedPublicKey { TweakedPublicKey(self) } @@ -76,9 +74,15 @@ impl TweakedPublicKey { pub fn new(key: PublicKey) -> TweakedPublicKey { TweakedPublicKey(key) } - - /// Returns the underlying public key + + /// Returns the underlying public key pub fn into_inner(self) -> PublicKey { self.0 } + + /// Returns a reference to underlying public key + pub fn as_inner(&self) -> &PublicKey { + &self.0 + } + } \ No newline at end of file