From 3bb6c73f2d3edd1165b7b7f3a833fa471786e166 Mon Sep 17 00:00:00 2001 From: Shing Him Ng Date: Sun, 20 Apr 2025 13:19:15 -0500 Subject: [PATCH] Add methods to retrieve inner types For TweakedKeypair, `to_inner` is also renamed to `to_keypair` to maintain consistency. Similarly, `to_inner` is renamed to `to_x_only_pubkey` for TweakedPublicKey --- bitcoin/examples/sign-tx-taproot.rs | 2 +- bitcoin/examples/taproot-psbt.rs | 2 +- .../src/blockdata/script/witness_program.rs | 4 ++-- bitcoin/src/crypto/key.rs | 21 +++++++++++++++++-- bitcoin/src/psbt/mod.rs | 2 +- bitcoin/src/taproot/mod.rs | 8 +++---- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/bitcoin/examples/sign-tx-taproot.rs b/bitcoin/examples/sign-tx-taproot.rs index 06a3fab16..2abb4d580 100644 --- a/bitcoin/examples/sign-tx-taproot.rs +++ b/bitcoin/examples/sign-tx-taproot.rs @@ -71,7 +71,7 @@ fn main() { // Sign the sighash using the secp256k1 library (exported by rust-bitcoin). let tweaked: TweakedKeypair = keypair.tap_tweak(&secp, None); let msg = Message::from(sighash); - let signature = secp.sign_schnorr(msg.as_ref(), &tweaked.to_inner()); + let signature = secp.sign_schnorr(msg.as_ref(), tweaked.as_keypair()); // Update the witness stack. let signature = bitcoin::taproot::Signature { signature, sighash_type }; diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index ab8f8af4e..07dfdf16e 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -744,7 +744,7 @@ fn sign_psbt_taproot( ) { let keypair = secp256k1::Keypair::from_seckey_slice(secp, secret_key.as_ref()).unwrap(); let keypair = match leaf_hash { - None => keypair.tap_tweak(secp, psbt_input.tap_merkle_root).to_inner(), + None => keypair.tap_tweak(secp, psbt_input.tap_merkle_root).to_keypair(), Some(_) => keypair, // no tweak for script spend }; diff --git a/bitcoin/src/blockdata/script/witness_program.rs b/bitcoin/src/blockdata/script/witness_program.rs index f2780f85b..79a22dc8a 100644 --- a/bitcoin/src/blockdata/script/witness_program.rs +++ b/bitcoin/src/blockdata/script/witness_program.rs @@ -101,13 +101,13 @@ impl WitnessProgram { merkle_root: Option, ) -> Self { let (output_key, _parity) = internal_key.tap_tweak(secp, merkle_root); - let pubkey = output_key.to_inner().serialize(); + let pubkey = output_key.as_x_only_public_key().serialize(); WitnessProgram::new_p2tr(pubkey) } /// Constructs a new [`WitnessProgram`] from a tweaked key for a P2TR output. pub fn p2tr_tweaked(output_key: TweakedPublicKey) -> Self { - let pubkey = output_key.to_inner().serialize(); + let pubkey = output_key.as_x_only_public_key().serialize(); WitnessProgram::new_p2tr(pubkey) } diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 45a232941..fe62e2583 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -887,9 +887,18 @@ impl TweakedPublicKey { TweakedPublicKey(key) } - /// Returns the underlying public key. + #[doc(hidden)] + #[deprecated(since="0.33.0", note="use to_x_only_public_key() instead")] pub fn to_inner(self) -> XOnlyPublicKey { self.0 } + /// Returns the underlying x-only public key. + #[inline] + pub fn to_x_only_public_key(self) -> XOnlyPublicKey { self.0 } + + /// Returns a reference to the underlying x-only public key. + #[inline] + pub fn as_x_only_public_key(&self) -> &XOnlyPublicKey { &self.0 } + /// Serializes the key as a byte-encoded pair of values. In compressed form /// the y-coordinate is represented by only a single bit, as x determines /// it up to one bit. @@ -906,9 +915,17 @@ impl TweakedKeypair { #[inline] pub fn dangerous_assume_tweaked(pair: Keypair) -> TweakedKeypair { TweakedKeypair(pair) } + #[doc(hidden)] + #[deprecated(since="0.33.0", note="use to_keypair() instead")] + pub fn to_inner(self) -> Keypair { self.0 } + /// Returns the underlying key pair. #[inline] - pub fn to_inner(self) -> Keypair { self.0 } + pub fn to_keypair(self) -> Keypair { self.0 } + + /// Returns a reference to the underlying key pair. + #[inline] + pub fn as_keypair(&self) -> &Keypair { &self.0 } /// Returns the [`TweakedPublicKey`] and its [`Parity`] for this [`TweakedKeypair`]. #[inline] diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 2fef0a8b1..d298b7f2e 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -448,7 +448,7 @@ impl Psbt { let (msg, sighash_type) = self.sighash_taproot(input_index, cache, None)?; let key_pair = Keypair::from_secret_key(secp, &sk.inner) .tap_tweak(secp, input.tap_merkle_root) - .to_inner(); + .to_keypair(); #[cfg(feature = "rand-std")] let signature = secp.sign_schnorr(msg.as_ref(), &key_pair); diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs index d1f40a264..78a4f8a59 100644 --- a/bitcoin/src/taproot/mod.rs +++ b/bitcoin/src/taproot/mod.rs @@ -1784,7 +1784,7 @@ mod test { let script = ScriptBuf::from_hex_no_length_prefix(script_hex).unwrap(); let control_block = ControlBlock::from_hex(control_block_hex).unwrap(); assert_eq!(control_block_hex, control_block.serialize().to_lower_hex_string()); - assert!(control_block.verify_taproot_commitment(secp, out_pk.to_inner(), &script)); + assert!(control_block.verify_taproot_commitment(secp, out_pk.to_x_only_public_key(), &script)); } #[test] @@ -1893,7 +1893,7 @@ mod test { let ctrl_block = tree_info.control_block(&ver_script).unwrap(); assert!(ctrl_block.verify_taproot_commitment( &secp, - output_key.to_inner(), + output_key.to_x_only_public_key(), &ver_script.0 )) } @@ -1967,7 +1967,7 @@ mod test { let ctrl_block = tree_info.control_block(&ver_script).unwrap(); assert!(ctrl_block.verify_taproot_commitment( &secp, - output_key.to_inner(), + output_key.to_x_only_public_key(), &ver_script.0 )) } @@ -2099,7 +2099,7 @@ mod test { let addr = Address::p2tr(secp, internal_key, merkle_root, KnownHrp::Mainnet); let spk = addr.script_pubkey(); - assert_eq!(expected_output_key, output_key.to_inner()); + assert_eq!(expected_output_key, output_key.to_x_only_public_key()); assert_eq!(expected_tweak, tweak); assert_eq!(expected_addr, addr); assert_eq!(expected_spk, spk);