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
This commit is contained in:
Shing Him Ng 2025-04-20 13:19:15 -05:00
parent a9ddac178d
commit 3bb6c73f2d
6 changed files with 28 additions and 11 deletions

View File

@ -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 };

View File

@ -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
};

View File

@ -101,13 +101,13 @@ impl WitnessProgram {
merkle_root: Option<TapNodeHash>,
) -> 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)
}

View File

@ -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]

View File

@ -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);

View File

@ -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);