What changed, and why it matters
This commit is a routine API cleanup: it renames the `PublicKey` type to `LegacyPublicKey` in the rust-bitcoin library and keeps `PublicKey` as a deprecated alias so existing code continues to compile. There is no bug fix, behavior change, or security patch in the diff.
No security action needed. Treat as a normal API-evolution change; downstream users can migrate to `LegacyPublicKey` at their own pace before the deprecated alias is removed in a future release.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change is a pure refactor across 13 files. The struct formerly named PublicKey is renamed to LegacyPublicKey, and pub type PublicKey = LegacyPublicKey is added with #[deprecated] to preserve backward compatibility. All internal call sites, trait implementations, doc comments, and tests are updated to use the new name. No cryptographic logic, parsing rules, serialization formats, or validation checks were modified.
Changed components
bitcoin/src/crypto/key.rsbitcoin/src/lib.rs public re-exportsbitcoin/src/address/mod.rsbitcoin/src/blockdata/script/*bitcoin/src/psbt/*bitcoin/src/sign_message.rsbitcoin/tests/bip_174.rsbitcoin/tests/serde.rsInspect captured patch +176 / −158
diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs
index 9294f797..a7a5d3e3 100644
--- a/bitcoin/src/address/mod.rs
+++ b/bitcoin/src/address/mod.rs
@@ -13,11 +13,11 @@
//! #[cfg(feature = "std")]
//! {
//! use bitcoin::secp256k1::rand;
-//! use bitcoin::{Address, Network, PublicKey};
+//! use bitcoin::{Address, Network, LegacyPublicKey};
//!
//! // Generate random key pair.
//! let (_sk, pk) = secp256k1::generate_keypair(&mut rand::rng());
-//! let public_key = PublicKey::from_secp(pk); // Or `PublicKey::from(pk)`.
+//! let public_key = LegacyPublicKey::from_secp(pk); // Or `LegacyPublicKey::from(pk)`.
//!
//! // Generate a mainnet pay-to-pubkey-hash address.
//! let address = Address::p2pkh(&public_key, Network::Bitcoin);
@@ -56,7 +56,8 @@ use crate::constants::{
SCRIPT_ADDRESS_PREFIX_TEST,
};
use crate::crypto::key::{
- FullPublicKey, PubkeyHash, PublicKey, TweakedPublicKey, UntweakedPublicKey, XOnlyPublicKey,
+ FullPublicKey, LegacyPublicKey, PubkeyHash, TweakedPublicKey, UntweakedPublicKey,
+ XOnlyPublicKey,
};
use crate::network::{Network, NetworkKind, Params};
use crate::prelude::{String, ToOwned};
@@ -760,7 +761,7 @@ impl Address {
/// This is determined by directly comparing the address payload with either the
/// hash of the given public key or the SegWit redeem hash generated from the
/// given key. For Taproot addresses, the supplied key is assumed to be tweaked
- pub fn is_related_to_pubkey(&self, pubkey: PublicKey) -> bool {
+ pub fn is_related_to_pubkey(&self, pubkey: LegacyPublicKey) -> bool {
let pubkey_hash = pubkey.pubkey_hash();
let payload = self.payload_as_bytes();
let xonly_pubkey = XOnlyPublicKey::from(pubkey);
@@ -1077,12 +1078,12 @@ mod tests {
#[test]
fn p2pkh_from_key() {
- let key = "048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183".parse::<PublicKey>().unwrap();
+ let key = "048d5141948c1702e8c95f438815794b87f706a8d4cd2bffad1dc1570971032c9b6042a0431ded2478b5c9cf2d81c124a5e57347a3c63ef0e7716cf54d613ba183".parse::<LegacyPublicKey>().unwrap();
let addr = Address::p2pkh(key, NetworkKind::Main);
assert_eq!(&addr.to_string(), "1QJVDzdqb1VpbDK7uDeyVXy9mR27CJiyhY");
let key = "03df154ebfcf29d29cc10d5c2565018bce2d9edbab267c31d2caf44a63056cf99f"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap();
let addr = Address::p2pkh(key, NetworkKind::Test);
assert_eq!(&addr.to_string(), "mqkhEMH6NCeYjFybv7pvFC22MFeaNT9AQC");
@@ -1377,13 +1378,13 @@ mod tests {
.expect("mainnet");
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let result = address.is_related_to_pubkey(pubkey);
assert!(result);
let unused_pubkey = "02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.expect("pubkey");
assert!(!address.is_related_to_pubkey(unused_pubkey))
}
@@ -1398,13 +1399,13 @@ mod tests {
.expect("mainnet");
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let result = address.is_related_to_pubkey(pubkey);
assert!(result);
let unused_pubkey = "02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.expect("pubkey");
assert!(!address.is_related_to_pubkey(unused_pubkey))
}
@@ -1419,13 +1420,13 @@ mod tests {
.expect("mainnet");
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let result = address.is_related_to_pubkey(pubkey);
assert!(result);
let unused_pubkey = "02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.expect("pubkey");
assert!(!address.is_related_to_pubkey(unused_pubkey))
}
@@ -1440,13 +1441,13 @@ mod tests {
.expect("testnet");
let pubkey_string = "04e96e22004e3db93530de27ccddfdf1463975d2138ac018fc3e7ba1a2e5e0aad8e424d0b55e2436eb1d0dcd5cb2b8bcc6d53412c22f358de57803a6a655fbbd04";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let result = address.is_related_to_pubkey(pubkey);
assert!(result);
let unused_pubkey = "02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.expect("pubkey");
assert!(!address.is_related_to_pubkey(unused_pubkey))
}
@@ -1454,7 +1455,7 @@ mod tests {
#[test]
fn is_related_to_pubkey_p2tr() {
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let xonly_pubkey = XOnlyPublicKey::from(pubkey);
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey);
let address = Address::p2tr_tweaked(tweaked_pubkey, KnownHrp::Mainnet);
@@ -1472,7 +1473,7 @@ mod tests {
assert!(result);
let unused_pubkey = "02ba604e6ad9d3864eda8dc41c62668514ef7d5417d3b6db46e45cc4533bff001c"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.expect("pubkey");
assert!(!address.is_related_to_pubkey(unused_pubkey));
}
@@ -1480,7 +1481,7 @@ mod tests {
#[test]
fn is_related_to_xonly_pubkey() {
let pubkey_string = "0347ff3dacd07a1f43805ec6808e801505a6e18245178609972a68afbc2777ff2b";
- let pubkey = pubkey_string.parse::<PublicKey>().expect("pubkey");
+ let pubkey = pubkey_string.parse::<LegacyPublicKey>().expect("pubkey");
let xonly_pubkey = XOnlyPublicKey::from(pubkey);
let tweaked_pubkey = TweakedPublicKey::dangerous_assume_tweaked(xonly_pubkey);
let address = Address::p2tr_tweaked(tweaked_pubkey, KnownHrp::Mainnet);
diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs
index 91ae73d4..7205ca56 100644
--- a/bitcoin/src/blockdata/script/borrowed.rs
+++ b/bitcoin/src/blockdata/script/borrowed.rs
@@ -12,7 +12,7 @@ use super::{
TapScript, WScriptHash, WitnessScript, WitnessScriptSizeError,
};
use crate::consensus::Encodable;
-use crate::key::{PublicKey, UntweakedPublicKey, WPubkeyHash};
+use crate::key::{LegacyPublicKey, UntweakedPublicKey, WPubkeyHash};
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
use crate::policy::{DUST_RELAY_TX_FEE, MAX_OP_RETURN_RELAY};
@@ -290,8 +290,8 @@ internal_macros::define_extension_trait! {
/// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true.
/// This happens when the public key is invalid (e.g. the point not being on the curve).
/// In this situation the script is unspendable.
- fn p2pk_public_key(&self) -> Option<PublicKey> {
- PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
+ fn p2pk_public_key(&self) -> Option<LegacyPublicKey> {
+ LegacyPublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
}
/// Checks whether a script pubkey is a P2SH output.
diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs
index ae9cce64..074d12d9 100644
--- a/bitcoin/src/blockdata/script/builder.rs
+++ b/bitcoin/src/blockdata/script/builder.rs
@@ -3,7 +3,7 @@
use core::fmt;
use super::{opcode_to_verify, Error, PushBytes, Script, ScriptBuf};
-use crate::key::{PublicKey, XOnlyPublicKey};
+use crate::key::{LegacyPublicKey, XOnlyPublicKey};
use crate::locktime::absolute;
use crate::opcodes::all::*;
use crate::opcodes::Opcode;
@@ -136,7 +136,7 @@ impl<T> Builder<T> {
}
/// Adds instructions to push a public key onto the stack.
- pub fn push_key(self, key: PublicKey) -> Self {
+ pub fn push_key(self, key: LegacyPublicKey) -> Self {
if key.compressed() {
self.push_slice(key.serialize_compressed())
} else {
diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs
index 993db45b..f83d4053 100644
--- a/bitcoin/src/blockdata/script/owned.rs
+++ b/bitcoin/src/blockdata/script/owned.rs
@@ -11,7 +11,7 @@ use super::{
};
use crate::internal_macros;
use crate::key::{
- PubkeyHash, PublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash,
+ LegacyPublicKey, PubkeyHash, TapTweak, TweakedPublicKey, UntweakedPublicKey, WPubkeyHash,
};
use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode};
@@ -171,7 +171,7 @@ crate::internal_macros::define_extension_trait! {
}
/// Generates P2PK-type of scriptPubkey.
- fn new_p2pk(pubkey: PublicKey) -> Self {
+ fn new_p2pk(pubkey: LegacyPublicKey) -> Self {
Builder::new().push_key(pubkey).push_opcode(OP_CHECKSIG).into_script()
}
diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs
index 63c605c2..ff25a9b5 100644
--- a/bitcoin/src/blockdata/script/tests.rs
+++ b/bitcoin/src/blockdata/script/tests.rs
@@ -8,7 +8,7 @@ use hex_unstable::hex;
use super::*;
use crate::consensus::encode::{deserialize, serialize};
-use crate::crypto::key::{PublicKey, XOnlyPublicKey};
+use crate::crypto::key::{LegacyPublicKey, XOnlyPublicKey};
use crate::script::borrowed::{ScriptPubKeyExt as _, ScriptPubKeyExtPriv as _, TapScriptExt as _};
use crate::script::witness_program::WitnessProgram;
use crate::script::witness_version::WitnessVersion;
@@ -70,10 +70,10 @@ fn script() {
// keys
const KEYSTR1: &str = "21032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af";
- let key = KEYSTR1[2..].parse::<PublicKey>().unwrap();
+ let key = KEYSTR1[2..].parse::<LegacyPublicKey>().unwrap();
script = script.push_key(key); comp.extend_from_slice(&hex!(KEYSTR1)); assert_eq!(script.as_bytes(), &comp[..]);
const KEYSTR2: &str = "41042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133";
- let key = KEYSTR2[2..].parse::<PublicKey>().unwrap();
+ let key = KEYSTR2[2..].parse::<LegacyPublicKey>().unwrap();
script = script.push_key(key); comp.extend_from_slice(&hex!(KEYSTR2)); assert_eq!(script.as_bytes(), &comp[..]);
// opcodes
@@ -123,7 +123,7 @@ fn script_buf_push_int() {
#[test]
fn p2pk_pubkey_bytes_valid_key_and_valid_script_returns_expected_key() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let p2pk = ScriptPubKey::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script();
let actual = p2pk.p2pk_pubkey_bytes().unwrap();
assert_eq!(actual.to_vec(), key.to_vec());
@@ -132,7 +132,7 @@ fn p2pk_pubkey_bytes_valid_key_and_valid_script_returns_expected_key() {
#[test]
fn p2pk_pubkey_bytes_no_checksig_returns_none() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let no_checksig = ScriptPubKey::builder().push_key(key).into_script();
assert_eq!(no_checksig.p2pk_pubkey_bytes(), None);
}
@@ -153,7 +153,7 @@ fn p2pk_pubkey_bytes_no_key_returns_none() {
#[test]
fn p2pk_pubkey_bytes_different_op_code_returns_none() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let different_op_code = ScriptPubKey::builder().push_key(key).push_opcode(OP_NOP).into_script();
assert!(different_op_code.p2pk_pubkey_bytes().is_none());
}
@@ -178,7 +178,7 @@ fn p2pk_pubkey_bytes_invalid_key_returns_some() {
#[test]
fn p2pk_pubkey_bytes_compressed_key_returns_expected_key() {
let compressed_key_str = "0311db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c";
- let key = compressed_key_str.parse::<PublicKey>().unwrap();
+ let key = compressed_key_str.parse::<LegacyPublicKey>().unwrap();
let p2pk = ScriptPubKey::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script();
let actual = p2pk.p2pk_pubkey_bytes().unwrap();
assert_eq!(actual.to_vec(), key.to_vec());
@@ -187,7 +187,7 @@ fn p2pk_pubkey_bytes_compressed_key_returns_expected_key() {
#[test]
fn p2pk_public_key_valid_key_and_valid_script_returns_expected_key() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let p2pk = ScriptPubKey::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script();
let actual = p2pk.p2pk_public_key().unwrap();
assert_eq!(actual, key);
@@ -196,7 +196,7 @@ fn p2pk_public_key_valid_key_and_valid_script_returns_expected_key() {
#[test]
fn p2pk_public_key_no_checksig_returns_none() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let no_checksig = ScriptPubKey::builder().push_key(key).into_script();
assert_eq!(no_checksig.p2pk_public_key(), None);
}
@@ -216,7 +216,7 @@ fn p2pk_public_key_no_key_returns_none() {
#[test]
fn p2pk_public_key_different_op_code_returns_none() {
let key_str = "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3";
- let key = key_str.parse::<PublicKey>().unwrap();
+ let key = key_str.parse::<LegacyPublicKey>().unwrap();
let different_op_code = ScriptPubKey::builder().push_key(key).push_opcode(OP_NOP).into_script();
assert!(different_op_code.p2pk_public_key().is_none());
}
@@ -240,7 +240,7 @@ fn p2pk_public_key_invalid_key_returns_none() {
#[test]
fn p2pk_public_key_compressed_key_returns_some() {
let compressed_key_str = "0311db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5c";
- let key = compressed_key_str.parse::<PublicKey>().unwrap();
+ let key = compressed_key_str.parse::<LegacyPublicKey>().unwrap();
let p2pk = ScriptPubKey::builder().push_key(key).push_opcode(OP_CHECKSIG).into_script();
let actual = p2pk.p2pk_public_key().unwrap();
assert_eq!(actual, key);
@@ -283,7 +283,7 @@ fn script_builder_with_capacity() {
#[test]
fn script_generators() {
let pubkey = "0234e6a79c5359c613762d537e0e19d86c77c1666d8c9ab050f23acd198e97f93e"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap();
assert!(ScriptPubKeyBuf::new_p2pk(pubkey).is_p2pk());
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 86621372..1c027645 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -32,7 +32,7 @@ use crate::taproot::{TapNodeHash, TapTweakHash};
#[rustfmt::skip] // Keep public re-exports separate.
pub use secp256k1::{constants, Parity, Verification};
pub use encapsulate::{
- FullPublicKey, Keypair, PrivateKey, PublicKey, SerializedXOnlyPublicKey, TweakedKeypair,
+ FullPublicKey, Keypair, LegacyPublicKey, PrivateKey, SerializedXOnlyPublicKey, TweakedKeypair,
TweakedPublicKey, XOnlyPublicKey,
};
#[cfg(feature = "rand")]
@@ -97,14 +97,14 @@ mod encapsulate {
/// A Bitcoin ECDSA public key.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
- pub struct PublicKey {
+ pub struct LegacyPublicKey {
/// Whether this public key should be serialized as compressed.
compressed: bool,
/// The actual ECDSA key.
inner: secp256k1::PublicKey,
}
- impl PublicKey {
+ impl LegacyPublicKey {
/// Constructs a new compressed ECDSA public key from the provided secp256k1 public key.
pub fn from_secp(key: impl Into<secp256k1::PublicKey>) -> Self {
Self { compressed: true, inner: key.into() }
@@ -305,9 +305,11 @@ impl XOnlyPublicKey {
/// Converts this x-only public key to a full public key.
///
- /// The [`PublicKey`] is constructed using the parity in this x-only public key.
+ /// The [`LegacyPublicKey`] is constructed using the parity in this x-only public key.
#[inline]
- pub fn to_public_key(self) -> PublicKey { self.as_inner().public_key(self.parity()).into() }
+ pub fn to_public_key(self) -> LegacyPublicKey {
+ self.as_inner().public_key(self.parity()).into()
+ }
/// Verifies that a tweak produced by [`XOnlyPublicKey::add_tweak`] was computed correctly.
///
@@ -432,11 +434,11 @@ impl Keypair {
self.as_inner().to_secret_bytes()
}
- /// Returns the [`PublicKey`] for this [`Keypair`].
+ /// Returns the [`LegacyPublicKey`] for this [`Keypair`].
///
- /// This is equivalent to using [`PublicKey::from_keypair`].
+ /// This is equivalent to using [`LegacyPublicKey::from_keypair`].
#[inline]
- pub fn to_public_key(&self) -> PublicKey { PublicKey::from_keypair(self) }
+ pub fn to_public_key(&self) -> LegacyPublicKey { LegacyPublicKey::from_keypair(self) }
/// Returns the [`XOnlyPublicKey`] for this [`Keypair`].
///
@@ -491,7 +493,11 @@ impl From<PrivateKey> for Keypair {
fn from(pk: PrivateKey) -> Self { Self::from_private_key(&pk) }
}
-impl PublicKey {
+#[deprecated(since = "TBD", note = "use `LegacyPublicKey` instead")]
+#[doc(hidden)]
+pub type PublicKey = LegacyPublicKey;
+
+impl LegacyPublicKey {
/// Constructs a new compressed ECDSA public key from the provided generic secp256k1 public key.
#[deprecated(since = "TBD", note = "use `from_secp` instead")]
pub fn new(key: impl Into<secp256k1::PublicKey>) -> Self { Self::from_secp(key) }
@@ -508,17 +514,17 @@ impl PublicKey {
/// This will call the provided function with the key as a byte slice in either
/// compressed or uncompressed form.
///
- /// See [`PublicKey::serialize_compressed`] and [`PublicKey::serialize_uncompressed`]
+ /// See [`LegacyPublicKey::serialize_compressed`] and [`LegacyPublicKey::serialize_uncompressed`]
/// for more information on the byte formats for the key.
///
/// # Examples
///
/// ```
- /// use bitcoin::PublicKey;
+ /// use bitcoin::LegacyPublicKey;
/// use bitcoin::hashes::hash160;
///
/// let key = "02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8"
- /// .parse::<PublicKey>()
+ /// .parse::<LegacyPublicKey>()
/// .unwrap();
/// assert!(key.compressed());
/// let vec_out = key.with_serialized(<[_]>::to_vec);
@@ -543,7 +549,7 @@ impl PublicKey {
/// If you want to serialize while considering the compressedness of this key,
/// use [`with_serialized`] instead.
///
- /// [`with_serialized`]: PublicKey::with_serialized
+ /// [`with_serialized`]: LegacyPublicKey::with_serialized
pub fn serialize_compressed(&self) -> [u8; 33] { self.to_inner().serialize() }
/// Serializes the key as a byte-encoded pair of values, in uncompressed form.
@@ -551,7 +557,7 @@ impl PublicKey {
/// If you want to serialize while considering the compressedness of this key,
/// use [`with_serialized`] instead.
///
- /// [`with_serialized`]: PublicKey::with_serialized
+ /// [`with_serialized`]: LegacyPublicKey::with_serialized
pub fn serialize_uncompressed(&self) -> [u8; 65] { self.to_inner().serialize_uncompressed() }
/// Returns bitcoin 160-bit hash of the public key.
@@ -588,10 +594,10 @@ impl PublicKey {
Ok(key.p2wpkh_script_code())
}
- /// Converts this [`PublicKey`] into a [`FullPublicKey`] infallibly.
+ /// Converts this [`LegacyPublicKey`] into a [`FullPublicKey`] infallibly.
///
/// Unlike the `TryFrom` implementation, this function will discard compressedness
- /// information on the [`PublicKey`].
+ /// information on the [`LegacyPublicKey`].
pub fn force_compressed(self) -> FullPublicKey { FullPublicKey::from_secp(self.to_inner()) }
/// Writes the public key into a writer.
@@ -647,14 +653,14 @@ impl PublicKey {
/// Serializes the public key into a `SortKey`.
///
/// `SortKey` is not too useful by itself, but it can be used to sort a
- /// `[PublicKey]` slice using `sort_unstable_by_key`, `sort_by_cached_key`,
+ /// `[LegacyPublicKey]` slice using `sort_unstable_by_key`, `sort_by_cached_key`,
/// `sort_by_key`, or any of the other `*_by_key` methods on slice.
- /// Pass the method into the sort method directly. (ie. `PublicKey::to_sort_key`)
+ /// Pass the method into the sort method directly. (ie. `LegacyPublicKey::to_sort_key`)
///
/// This method of sorting is in line with Bitcoin Core's implementation of
/// sorting keys for output descriptors such as `sortedmulti()`.
///
- /// If every `PublicKey` in the slice is `compressed == true` then this will sort
+ /// If every `LegacyPublicKey` in the slice is `compressed == true` then this will sort
/// the keys in a
/// [BIP-0067](https://github.com/bitcoin/bips/blob/master/bip-0067.mediawiki)
/// compliant way.
@@ -662,9 +668,9 @@ impl PublicKey {
/// # Example: Using with `sort_unstable_by_key`
///
/// ```rust
- /// use bitcoin::PublicKey;
+ /// use bitcoin::LegacyPublicKey;
///
- /// let pk = |s: &str| s.parse::<PublicKey>().unwrap();
+ /// let pk = |s: &str| s.parse::<LegacyPublicKey>().unwrap();
///
/// let mut unsorted = [
/// pk("04c4b0bbb339aa236bff38dbe6a451e111972a7909a126bc424013cba2ec33bc38e98ac269ffe028345c31ac8d0a365f29c8f7e7cfccac72f84e1acd02bc554f35"),
@@ -689,7 +695,7 @@ impl PublicKey {
/// pk("04c4b0bbb339aa236bff38dbe6a451e111972a7909a126bc424013cba2ec33bc38e98ac269ffe028345c31ac8d0a365f29c8f7e7cfccac72f84e1acd02bc554f35"),
/// ];
///
- /// unsorted.sort_unstable_by_key(|k| PublicKey::to_sort_key(*k));
+ /// unsorted.sort_unstable_by_key(|k| LegacyPublicKey::to_sort_key(*k));
///
/// assert_eq!(unsorted, sorted);
/// ```
@@ -751,28 +757,28 @@ impl PublicKey {
}
}
-impl From<secp256k1::PublicKey> for PublicKey {
+impl From<secp256k1::PublicKey> for LegacyPublicKey {
fn from(pk: secp256k1::PublicKey) -> Self { Self::from_secp(pk) }
}
-impl From<PublicKey> for XOnlyPublicKey {
- fn from(pk: PublicKey) -> Self {
+impl From<LegacyPublicKey> for XOnlyPublicKey {
+ fn from(pk: LegacyPublicKey) -> Self {
let (xonly, parity) = pk.to_inner().x_only_public_key();
Self::from_secp(xonly, parity)
}
}
-/// An opaque return type for [`PublicKey::to_sort_key`].
+/// An opaque return type for [`LegacyPublicKey::to_sort_key`].
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct SortKey(ArrayVec<u8, 65>);
-impl fmt::Display for PublicKey {
+impl fmt::Display for LegacyPublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.with_serialized(|bytes| fmt::Display::fmt(&bytes.as_hex(), f))
}
}
-impl FromStr for PublicKey {
+impl FromStr for LegacyPublicKey {
type Err = ParsePublicKeyError;
fn from_str(s: &str) -> Result<Self, ParsePublicKeyError> {
match s.len() {
@@ -812,12 +818,12 @@ hashes::impl_serde_for_newtype!(PubkeyHash, WPubkeyHash);
impl_asref_push_bytes!(PubkeyHash, WPubkeyHash);
-impl From<PublicKey> for PubkeyHash {
- fn from(key: PublicKey) -> Self { key.pubkey_hash() }
+impl From<LegacyPublicKey> for PubkeyHash {
+ fn from(key: LegacyPublicKey) -> Self { key.pubkey_hash() }
}
-impl From<&PublicKey> for PubkeyHash {
- fn from(key: &PublicKey) -> Self { key.pubkey_hash() }
+impl From<&LegacyPublicKey> for PubkeyHash {
+ fn from(key: &LegacyPublicKey) -> Self { key.pubkey_hash() }
}
#[deprecated(since = "TBD", note = "use `FullPublicKey` instead")]
@@ -923,7 +929,7 @@ impl FullPublicKey {
///
/// # Errors
///
- /// See [`PublicKey::verify`].
+ /// See [`LegacyPublicKey::verify`].
pub fn verify(
&self,
msg: secp256k1::Message,
@@ -953,10 +959,10 @@ impl FromStr for FullPublicKey {
}
}
-impl TryFrom<PublicKey> for FullPublicKey {
+impl TryFrom<LegacyPublicKey> for FullPublicKey {
type Error = UncompressedPublicKeyError;
- fn try_from(value: PublicKey) -> Result<Self, Self::Error> {
+ fn try_from(value: LegacyPublicKey) -> Result<Self, Self::Error> {
if value.compressed() {
Ok(Self::from_secp(value.to_inner()))
} else {
@@ -969,7 +975,7 @@ impl From<secp256k1::PublicKey> for FullPublicKey {
fn from(pk: secp256k1::PublicKey) -> Self { Self::from_secp(pk) }
}
-impl From<FullPublicKey> for PublicKey {
+impl From<FullPublicKey> for LegacyPublicKey {
fn from(value: FullPublicKey) -> Self { Self::from_secp(value.to_inner()) }
}
@@ -1004,18 +1010,19 @@ impl PrivateKey {
}
/// Constructs a new public key from this private key.
- pub fn to_public_key(&self) -> PublicKey {
+ pub fn to_public_key(&self) -> LegacyPublicKey {
match self.compressed() {
- true => PublicKey::from_secp(secp256k1::PublicKey::from_secret_key(self.as_inner())),
- false => PublicKey::from_secp_uncompressed(secp256k1::PublicKey::from_secret_key(
- self.as_inner(),
- )),
+ true =>
+ LegacyPublicKey::from_secp(secp256k1::PublicKey::from_secret_key(self.as_inner())),
+ false => LegacyPublicKey::from_secp_uncompressed(
+ secp256k1::PublicKey::from_secret_key(self.as_inner()),
+ ),
}
}
/// Constructs a new public key from this private key.
#[deprecated(since = "TBD", note = "use `to_public_key` instead")]
- pub fn public_key(&self) -> PublicKey { self.to_public_key() }
+ pub fn public_key(&self) -> LegacyPublicKey { self.to_public_key() }
/// Serializes the private key to bytes.
#[deprecated(since = "TBD", note = "use to_secret_vec instead")]
@@ -1242,7 +1249,7 @@ impl<'de> serde::Deserialize<'de> for WifKey {
#[cfg(feature = "serde")]
#[allow(clippy::collapsible_else_if)] // Aids readability.
-impl serde::Serialize for PublicKey {
+impl serde::Serialize for LegacyPublicKey {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
if s.is_human_readable() {
s.collect_str(self)
@@ -1253,13 +1260,13 @@ impl serde::Serialize for PublicKey {
}
#[cfg(feature = "serde")]
-impl<'de> serde::Deserialize<'de> for PublicKey {
+impl<'de> serde::Deserialize<'de> for LegacyPublicKey {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
if d.is_human_readable() {
struct HexVisitor;
impl serde::de::Visitor<'_> for HexVisitor {
- type Value = PublicKey;
+ type Value = LegacyPublicKey;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("an ASCII hex string")
@@ -1270,7 +1277,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
E: serde::de::Error,
{
if let Ok(hex) = core::str::from_utf8(v) {
- hex.parse::<PublicKey>().map_err(E::custom)
+ hex.parse::<LegacyPublicKey>().map_err(E::custom)
} else {
Err(E::invalid_value(::serde::de::Unexpected::Bytes(v), &self))
}
@@ -1280,7 +1287,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
where
E: serde::de::Error,
{
- v.parse::<PublicKey>().map_err(E::custom)
+ v.parse::<LegacyPublicKey>().map_err(E::custom)
}
}
d.deserialize_str(HexVisitor)
@@ -1288,7 +1295,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
struct BytesVisitor;
impl serde::de::Visitor<'_> for BytesVisitor {
- type Value = PublicKey;
+ type Value = LegacyPublicKey;
fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
formatter.write_str("a bytestring")
@@ -1298,7 +1305,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
where
E: serde::de::Error,
{
- PublicKey::from_slice(v).map_err(E::custom)
+ LegacyPublicKey::from_slice(v).map_err(E::custom)
}
}
@@ -1657,7 +1664,7 @@ pub enum ParsePublicKeyError {
Encoding(FromSliceError),
/// Hex decoding error.
InvalidChar(hex::error::InvalidCharError),
- /// `PublicKey` hex should be 66 or 130 digits long.
+ /// `LegacyPublicKey` hex should be 66 or 130 digits long.
InvalidHexLength(usize),
}
@@ -1879,7 +1886,7 @@ impl fmt::Display for TweakXOnlyPublicKeyError {
impl std::error::Error for TweakXOnlyPublicKeyError {}
#[cfg(feature = "arbitrary")]
-impl<'a> Arbitrary<'a> for PublicKey {
+impl<'a> Arbitrary<'a> for LegacyPublicKey {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self::from_secp(secp256k1::PublicKey::arbitrary(u)?))
}
@@ -1935,10 +1942,10 @@ mod tests {
assert!(!pk.compressed());
assert_eq!(&pk.to_string(), "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133");
assert_eq!(pk, "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133"
- .parse::<PublicKey>().unwrap());
+ .parse::<LegacyPublicKey>().unwrap());
let addr = Address::p2pkh(pk, sk.network_kind);
assert_eq!(&addr.to_string(), "1GhQvF6dL8xa6wBxLnWmHcQsurx9RxiMc8");
- pk = PublicKey::from_secp(pk.to_inner());
+ pk = LegacyPublicKey::from_secp(pk.to_inner());
assert_eq!(
&pk.to_string(),
"032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"
@@ -1946,7 +1953,7 @@ mod tests {
assert_eq!(
pk,
"032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap()
);
}
@@ -1954,10 +1961,10 @@ mod tests {
#[test]
fn pubkey_hash() {
let pk = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap();
let upk = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133"
- .parse::<PublicKey>().unwrap();
+ .parse::<LegacyPublicKey>().unwrap();
assert_eq!(pk.pubkey_hash().to_string(), "9511aa27ef39bbfa4e4f3dd15f4d66ea57f475b4");
assert_eq!(upk.pubkey_hash().to_string(), "ac2e7daf42d2c97418fd9f78af2de552bb9c6a7a");
}
@@ -1965,9 +1972,9 @@ mod tests {
#[test]
fn wpubkey_hash() {
let pk = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap();
- let upk = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133".parse::<PublicKey>().unwrap();
+ let upk = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133".parse::<LegacyPublicKey>().unwrap();
assert_eq!(
pk.wpubkey_hash().unwrap().to_string(),
"9511aa27ef39bbfa4e4f3dd15f4d66ea57f475b4"
@@ -2009,8 +2016,8 @@ mod tests {
];
let wk = KEY_WIF.parse::<WifKey>().unwrap();
- let pk = PublicKey::from_private_key(&wk.private_key);
- let pk_u = PublicKey::from_secp_uncompressed(pk.to_inner());
+ let pk = LegacyPublicKey::from_private_key(&wk.private_key);
+ let pk_u = LegacyPublicKey::from_secp_uncompressed(pk.to_inner());
assert_tokens(&wk, &[Token::BorrowedStr(KEY_WIF)]);
assert_tokens(&pk.compact(), &[Token::BorrowedBytes(&PK_BYTES[..])]);
@@ -2019,7 +2026,7 @@ mod tests {
assert_tokens(&pk_u.readable(), &[Token::BorrowedStr(PK_STR_U)]);
}
- fn random_key(mut seed: u8) -> PublicKey {
+ fn random_key(mut seed: u8) -> LegacyPublicKey {
loop {
let mut data = [0; 65];
for byte in &mut data[..] {
@@ -2029,12 +2036,12 @@ mod tests {
}
if data[0] % 2 == 0 {
data[0] = 4;
- if let Ok(key) = PublicKey::from_slice(&data[..]) {
+ if let Ok(key) = LegacyPublicKey::from_slice(&data[..]) {
return key;
}
} else {
data[0] = 2 + (data[0] >> 7);
- if let Ok(key) = PublicKey::from_slice(&data[..33]) {
+ if let Ok(key) = LegacyPublicKey::from_slice(&data[..33]) {
return key;
}
}
@@ -2054,26 +2061,26 @@ mod tests {
let mut reader = v.as_slice();
let mut dec_keys = vec![];
for _ in 0..N_KEYS {
- dec_keys.push(PublicKey::read_from(&mut reader).expect("reading from vec"));
+ dec_keys.push(LegacyPublicKey::read_from(&mut reader).expect("reading from vec"));
}
assert_eq!(keys, dec_keys);
- assert!(PublicKey::read_from(&mut reader).is_err());
+ assert!(LegacyPublicKey::read_from(&mut reader).is_err());
// sanity checks
let mut empty: &[u8] = &[];
- assert!(PublicKey::read_from(&mut empty).is_err());
- assert!(PublicKey::read_from(&mut &[0; 33][..]).is_err());
- assert!(PublicKey::read_from(&mut &[2; 32][..]).is_err());
- assert!(PublicKey::read_from(&mut &[0; 65][..]).is_err());
- assert!(PublicKey::read_from(&mut &[4; 64][..]).is_err());
+ assert!(LegacyPublicKey::read_from(&mut empty).is_err());
+ assert!(LegacyPublicKey::read_from(&mut &[0; 33][..]).is_err());
+ assert!(LegacyPublicKey::read_from(&mut &[2; 32][..]).is_err());
+ assert!(LegacyPublicKey::read_from(&mut &[0; 65][..]).is_err());
+ assert!(LegacyPublicKey::read_from(&mut &[4; 64][..]).is_err());
}
#[test]
fn pubkey_to_sort_key() {
let key1 = "02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8"
- .parse::<PublicKey>()
+ .parse::<LegacyPublicKey>()
.unwrap();
- let key2 = PublicKey::from_secp_uncompressed(key1.to_inner());
+ let key2 = LegacyPublicKey::from_secp_uncompressed(key1.to_inner());
let arrayvec1 = ArrayVec::from_slice(
&hex::decode_to_array::<33>(
"02ff12471208c14bd580709cb2358d98975247d8765f92bc25eab3b2763ed605f8",
@@ -2092,11 +2099,11 @@ mod tests {
#[test]
fn pubkey_sort() {
struct Vector {
- input: Vec<PublicKey>,
- expect: Vec<PublicKey>,
+ input: Vec<LegacyPublicKey>,
+ expect: Vec<LegacyPublicKey>,
}
let fmt = |v: Vec<_>| {
- v.into_iter().map(|s: &str| s.parse::<PublicKey>().unwrap()).collect::<Vec<_>>()
+ v.into_iter().map(|s: &str| s.parse::<LegacyPublicKey>().unwrap()).collect::<Vec<_>>()
};
let vectors = vec![
// Start BIP-0067 vectors
@@ -2194,7 +2201,7 @@ mod tests {
},
];
for mut vector in vectors {
- vector.input.sort_by_cached_key(|k| PublicKey::to_sort_key(*k));
+ vector.input.sort_by_cached_key(|k| LegacyPublicKey::to_sort_key(*k));
assert_eq!(vector.input, vector.expect);
}
}
@@ -2205,8 +2212,8 @@ mod tests {
fn public_key_constructors() {
let kp = Keypair::generate();
- let _ = PublicKey::from_secp(kp.clone());
- let _ = PublicKey::from_secp_uncompressed(kp);
+ let _ = LegacyPublicKey::from_secp(kp.clone());
+ let _ = LegacyPublicKey::from_secp_uncompressed(kp);
}
#[test]
@@ -2214,25 +2221,25 @@ mod tests {
// Sanity checks, we accept string length 130 digits.
let s = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133";
assert_eq!(s.len(), 130);
- assert!(s.parse::<PublicKey>().is_ok());
+ assert!(s.parse::<LegacyPublicKey>().is_ok());
// And 66 digits.
let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af";
assert_eq!(s.len(), 66);
- assert!(s.parse::<PublicKey>().is_ok());
+ assert!(s.parse::<LegacyPublicKey>().is_ok());
let s = "aoeusthb";
assert_eq!(s.len(), 8);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
assert_eq!(res.unwrap_err(), ParsePublicKeyError::InvalidHexLength(8));
}
#[test]
fn public_key_from_str_invalid_str() {
- // Ensuring test cases fail when PublicKey::from_str is used on invalid keys
+ // Ensuring test cases fail when LegacyPublicKey::from_str is used on invalid keys
let s = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b142";
assert_eq!(s.len(), 130);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
assert_eq!(
res.unwrap_err(),
@@ -2243,7 +2250,7 @@ mod tests {
let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd169";
assert_eq!(s.len(), 66);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
assert_eq!(
res.unwrap_err(),
@@ -2254,7 +2261,7 @@ mod tests {
let s = "062e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b133";
assert_eq!(s.len(), 130);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
assert_eq!(
res.unwrap_err(),
@@ -2263,7 +2270,7 @@ mod tests {
let s = "042e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1af191923a2964c177f5b5923ae500fca49e99492d534aa3759d6b25a8bc971b13g";
assert_eq!(s.len(), 130);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
if let Err(ParsePublicKeyError::InvalidChar(err)) = res {
assert_eq!(err.pos(), 129);
@@ -2273,7 +2280,7 @@ mod tests {
let s = "032e58afe51f9ed8ad3cc7897f634d881fdbe49a81564629ded8156bebd2ffd1ag";
assert_eq!(s.len(), 66);
- let res = s.parse::<PublicKey>();
+ let res = s.parse::<LegacyPublicKey>();
assert!(res.is_err());
if let Err(ParsePublicKeyError::InvalidChar(err)) = res {
assert_eq!(err.pos(), 65);
@@ -2355,7 +2362,7 @@ mod tests {
let secp_key =
secp256k1::PublicKey::from_byte_array_compressed(bitcoin_key.serialize_compressed())
.unwrap();
- assert_eq!(PublicKey::from_secp(secp_key), bitcoin_key);
+ assert_eq!(LegacyPublicKey::from_secp(secp_key), bitcoin_key);
// Also assert that generating a secp from compressed or uncompressed yields the same value
assert_eq!(
secp256k1::PublicKey::from_byte_array_uncompressed(
diff --git a/bitcoin/src/lib.rs b/bitcoin/src/lib.rs
index 8bfe6ebc..8919d396 100644
--- a/bitcoin/src/lib.rs
+++ b/bitcoin/src/lib.rs
@@ -170,12 +170,18 @@ pub type BlockInterval = BlockHeightInterval;
#[doc(hidden)]
pub type CompressedPublicKey = FullPublicKey;
+#[deprecated(since = "TBD", note = "use `LegacyPublicKey` instead")]
+#[doc(hidden)]
+pub type PublicKey = LegacyPublicKey;
+
#[doc(inline)]
pub use crate::{
address::{Address, AddressType, KnownHrp},
bip32::XKeyIdentifier,
crypto::ecdsa,
- crypto::key::{self, FullPublicKey, Keypair, PrivateKey, PublicKey, WifKey, XOnlyPublicKey},
+ crypto::key::{
+ self, FullPublicKey, Keypair, LegacyPublicKey, PrivateKey, WifKey, XOnlyPublicKey,
+ },
crypto::sighash::{self, LegacySighash, SegwitV0Sighash, TapSighash, TapSighashTag},
network::params::{self, Params},
network::{Network, NetworkKind, TestnetVersion},
diff --git a/bitcoin/src/psbt/map/input.rs b/bitcoin/src/psbt/map/input.rs
index f803b729..4e2fbe30 100644
--- a/bitcoin/src/psbt/map/input.rs
+++ b/bitcoin/src/psbt/map/input.rs
@@ -8,7 +8,7 @@ use arbitrary::{Arbitrary, Unstructured};
use hashes::{hash160, ripemd160, sha256, sha256d};
use crate::bip32::KeySource;
-use crate::crypto::key::{PublicKey, XOnlyPublicKey};
+use crate::crypto::key::{LegacyPublicKey, XOnlyPublicKey};
use crate::crypto::{ecdsa, taproot};
use crate::prelude::{btree_map, BTreeMap, Borrow, Box, ToOwned, Vec};
use crate::psbt::map::Map;
@@ -80,7 +80,7 @@ pub struct Input {
pub witness_utxo: Option<TxOut>,
/// A map from public keys to their corresponding signature as would be
/// pushed to the stack from a scriptSig or witness for a non-Taproot inputs.
- pub partial_sigs: BTreeMap<PublicKey, ecdsa::Signature>,
+ pub partial_sigs: BTreeMap<LegacyPublicKey, ecdsa::Signature>,
/// The sighash type to be used for this input. Signatures for this input
/// must use the sighash type.
pub sighash_type: Option<PsbtSighashType>,
@@ -271,7 +271,7 @@ impl Input {
}
PSBT_IN_PARTIAL_SIG => {
impl_psbt_insert_pair! {
- self.partial_sigs <= <raw_key: PublicKey>|<raw_value: ecdsa::Signature>
+ self.partial_sigs <= <raw_key: LegacyPublicKey>|<raw_value: ecdsa::Signature>
}
}
PSBT_IN_SIGHASH_TYPE => {
diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs
index 7565c22f..73708b14 100644
--- a/bitcoin/src/psbt/mod.rs
+++ b/bitcoin/src/psbt/mod.rs
@@ -24,7 +24,7 @@ use internals::write_err;
use secp256k1::Message;
use crate::bip32::{self, KeySource, Xpriv, Xpub};
-use crate::crypto::key::{PrivateKey, PublicKey};
+use crate::crypto::key::{LegacyPublicKey, PrivateKey};
use crate::crypto::{ecdsa, taproot};
use crate::key::{Keypair, TapTweak, XOnlyPublicKey};
use crate::prelude::{btree_map, BTreeMap, BTreeSet, Borrow, Box, Vec};
@@ -344,7 +344,7 @@ impl Psbt {
k: &K,
input_index: usize,
cache: &mut SighashCache<T>,
- ) -> Result<Vec<PublicKey>, SignError>
+ ) -> Result<Vec<LegacyPublicKey>, SignError>
where
T: Borrow<Transaction>,
K: GetKey,
@@ -358,7 +358,9 @@ impl Psbt {
for (pk, key_source) in input.bip32_derivation.iter() {
let sk = if let Ok(Some(sk)) = k.get_key(&KeyRequest::Bip32(key_source.clone())) {
sk
- } else if let Ok(Some(sk)) = k.get_key(&KeyRequest::Pubkey(PublicKey::from_secp(*pk))) {
+ } else if let Ok(Some(sk)) =
+ k.get_key(&KeyRequest::Pubkey(LegacyPublicKey::from_secp(*pk)))
+ {
sk
} else {
continue;
@@ -760,7 +762,7 @@ impl<'de> serde::Deserialize<'de> for Psbt {
#[non_exhaustive]
pub enum KeyRequest {
/// Request a private key using the associated public key.
- Pubkey(PublicKey),
+ Pubkey(LegacyPublicKey),
/// Request a private key using BIP-0032 fingerprint and derivation path.
Bip32(KeySource),
/// Request a private key using the associated x-only public key.
@@ -815,7 +817,7 @@ pub type SigningKeysMap = BTreeMap<usize, SigningKeys>;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SigningKeys {
/// Keys used to sign an ECDSA input.
- Ecdsa(Vec<PublicKey>),
+ Ecdsa(Vec<LegacyPublicKey>),
/// Keys used to sign a Taproot input.
///
/// - Key path spend: This is the internal key.
@@ -853,7 +855,7 @@ impl_get_key_for_set!(HashSet);
macro_rules! impl_get_key_for_pubkey_map {
($map:ident) => {
-impl GetKey for $map<PublicKey, PrivateKey> {
+impl GetKey for $map<LegacyPublicKey, PrivateKey> {
type Error = GetKeyError;
fn get_key(
@@ -2354,12 +2356,12 @@ mod tests {
#[cfg(feature = "rand")]
#[cfg(feature = "std")]
- fn gen_keys() -> (PrivateKey, PublicKey) {
+ fn gen_keys() -> (PrivateKey, LegacyPublicKey) {
use secp256k1::rand;
let sk = SecretKey::new(&mut rand::rng());
let priv_key = PrivateKey::from_secp(sk);
- let pk = PublicKey::from_private_key(&priv_key);
+ let pk = LegacyPublicKey::from_private_key(&priv_key);
(priv_key, pk)
}
@@ -2386,7 +2388,7 @@ mod tests {
let (mut priv_key, mut pk) = gen_keys();
let xonly = XOnlyPublicKey::from(pk);
- let mut pubkey_map: HashMap<PublicKey, PrivateKey> = HashMap::new();
+ let mut pubkey_map: HashMap<LegacyPublicKey, PrivateKey> = HashMap::new();
if xonly.parity() == secp256k1::Parity::Even {
priv_key = priv_key.negate();
@@ -2645,7 +2647,7 @@ mod tests {
script_pubkey: ScriptPubKeyBuf::new_p2tr(internal_key, None),
});
- let mut key_map: HashMap<PublicKey, PrivateKey> = HashMap::new();
+ let mut key_map: HashMap<LegacyPublicKey, PrivateKey> = HashMap::new();
key_map.insert(pk, priv_key);
let key_source = (Fingerprint::default(), DerivationPath::default());
diff --git a/bitcoin/src/psbt/serialize.rs b/bitcoin/src/psbt/serialize.rs
index f8435b21..c895b88c 100644
--- a/bitcoin/src/psbt/serialize.rs
+++ b/bitcoin/src/psbt/serialize.rs
@@ -13,7 +13,7 @@ use internals::slice::SliceExt;
use super::map::{Input, Map, Output, PsbtSighashType};
use crate::bip32::{ChildNumber, Fingerprint, KeySource};
use crate::consensus::encode::{self, deserialize_partial, serialize, Decodable, Encodable};
-use crate::crypto::key::{PublicKey, XOnlyPublicKey};
+use crate::crypto::key::{LegacyPublicKey, XOnlyPublicKey};
use crate::crypto::{ecdsa, taproot};
use crate::io::Write;
use crate::prelude::{DisplayHex, String, Vec};
@@ -167,7 +167,7 @@ impl<T> Deserialize for ScriptBuf<T> {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> { Ok(Self::from(bytes.to_vec())) }
}
-impl Serialize for PublicKey {
+impl Serialize for LegacyPublicKey {
fn serialize(&self) -> Vec<u8> {
let mut buf = Vec::new();
self.write_into(&mut buf).expect("vecs don't error");
@@ -175,7 +175,7 @@ impl Serialize for PublicKey {
}
}
-impl Deserialize for PublicKey {
+impl Deserialize for LegacyPublicKey {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
Self::from_slice(bytes).map_err(Error::InvalidPublicKey)
}
diff --git a/bitcoin/src/sign_message.rs b/bitcoin/src/sign_message.rs
index 2b37177c..7ee75311 100644
--- a/bitcoin/src/sign_message.rs
+++ b/bitcoin/src/sign_message.rs
@@ -29,7 +29,7 @@ mod message_signing {
use secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
use crate::address::{Address, AddressType};
- use crate::crypto::key::PublicKey;
+ use crate::crypto::key::LegacyPublicKey;
/// An error used for dealing with Bitcoin Signed Messages.
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -130,12 +130,12 @@ mod message_signing {
pub fn recover_pubkey(
&self,
msg_hash: sha256d::Hash,
- ) -> Result<PublicKey, MessageSignatureError> {
+ ) -> Result<LegacyPublicKey, MessageSignatureError> {
let msg = secp256k1::Message::from_digest(msg_hash.to_byte_array());
let pubkey = self.signature.recover_ecdsa(msg)?;
Ok(match self.compressed {
- true => PublicKey::from_secp(pubkey),
- false => PublicKey::from_secp_uncompressed(pubkey),
+ true => LegacyPublicKey::from_secp(pubkey),
+ false => LegacyPublicKey::from_secp_uncompressed(pubkey),
})
}
@@ -282,7 +282,7 @@ mod tests {
fn incorrect_message_signature() {
use base64::prelude::{Engine as _, BASE64_STANDARD};
- use crate::crypto::key::PublicKey;
+ use crate::crypto::key::LegacyPublicKey;
use crate::{Address, NetworkKind};
let message = "a different message from what was signed";
@@ -295,9 +295,10 @@ mod tests {
let signature =
super::MessageSignature::from_base64(signature_base64).expect("message signature");
- let pubkey =
- PublicKey::from_slice(&BASE64_STANDARD.decode(pubkey_base64).expect("base64 string"))
- .expect("pubkey slice");
+ let pubkey = LegacyPublicKey::from_slice(
+ &BASE64_STANDARD.decode(pubkey_base64).expect("base64 string"),
+ )
+ .expect("pubkey slice");
let p2pkh = Address::p2pkh(pubkey, NetworkKind::Main);
assert_eq!(signature.is_signed_by_address(&p2pkh, msg_hash), Ok(false));
diff --git a/bitcoin/tests/bip_174.rs b/bitcoin/tests/bip_174.rs
index dfa03eb3..175cf009 100644
--- a/bitcoin/tests/bip_174.rs
+++ b/bitcoin/tests/bip_174.rs
@@ -10,7 +10,7 @@ use bitcoin::opcodes::all::OP_0;
use bitcoin::psbt::{Psbt, PsbtSighashType};
use bitcoin::script::{PushBytes, ScriptBuf};
use bitcoin::{
- absolute, hex, script, transaction, NetworkKind, OutPoint, PrivateKey, PublicKey,
+ absolute, hex, script, transaction, LegacyPublicKey, NetworkKind, OutPoint, PrivateKey,
ScriptPubKeyBuf, ScriptSigBuf, Sequence, Transaction, TxIn, TxOut, WifKey, Witness,
};
@@ -274,7 +274,7 @@ fn bip32_derivation(
let pk = pk_path[i].0;
let path = pk_path[i].1;
- let pk = pk.parse::<PublicKey>().unwrap();
+ let pk = pk.parse::<LegacyPublicKey>().unwrap();
let path = path.into_derivation_path().unwrap();
tree.insert(pk.to_inner(), (fingerprint, path));
@@ -305,7 +305,7 @@ fn update_psbt_with_sighash_all(mut psbt: Psbt) -> Psbt {
fn parse_and_verify_keys(
ext_priv: &Xpriv,
sk_path: &[(&str, &str)],
-) -> BTreeMap<PublicKey, PrivateKey> {
+) -> BTreeMap<LegacyPublicKey, PrivateKey> {
let mut key_map = BTreeMap::new();
for (secret_key, derivation_path) in sk_path.iter() {
let wif_priv = WifKey::from_wif(secret_key).expect("failed to parse key");
@@ -323,7 +323,7 @@ fn parse_and_verify_keys(
/// Does the first signing according to the BIP, returns the signed PSBT. Verifies against BIP 174 test vector.
#[track_caller]
-fn signer_one_sign(psbt: Psbt, key_map: BTreeMap<bitcoin::PublicKey, PrivateKey>) -> Psbt {
+fn signer_one_sign(psbt: Psbt, key_map: BTreeMap<LegacyPublicKey, PrivateKey>) -> Psbt {
let expected_psbt_hex = include_str!("data/sign_1_psbt_hex");
let expected_psbt: Psbt = hex_psbt(expected_psbt_hex);
@@ -361,7 +361,7 @@ fn combine_lexicographically() {
}
/// Signs `psbt` with `keys` if required.
-fn sign(mut psbt: Psbt, keys: BTreeMap<bitcoin::PublicKey, PrivateKey>) -> Psbt {
+fn sign(mut psbt: Psbt, keys: BTreeMap<LegacyPublicKey, PrivateKey>) -> Psbt {
psbt.sign(&keys).unwrap();
psbt
}
diff --git a/bitcoin/tests/serde.rs b/bitcoin/tests/serde.rs
index aac0f40b..59b9be40 100644
--- a/bitcoin/tests/serde.rs
+++ b/bitcoin/tests/serde.rs
@@ -30,8 +30,9 @@ use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
use bitcoin::taproot::{self, ControlBlock, LeafVersion, TapTree, TaprootBuilder};
use bitcoin::witness::Witness;
use bitcoin::{
- ecdsa, hex, transaction, Address, Amount, NetworkKind, OutPoint, PublicKey, ScriptPubKeyBuf,
- ScriptSigBuf, Sequence, TapScriptBuf, Target, Transaction, TxIn, TxOut, Txid, WifKey, Work,
+ ecdsa, hex, transaction, Address, Amount, LegacyPublicKey, NetworkKind, OutPoint,
+ ScriptPubKeyBuf, ScriptSigBuf, Sequence, TapScriptBuf, Target, Transaction, TxIn, TxOut, Txid,
+ WifKey, Work,
};
#[test]
@@ -111,7 +112,7 @@ fn serde_regression_witness() {
#[test]
fn serde_regression_address() {
let s = include_str!("data/serde/public_key_hex");
- let pk = s.trim().parse::<PublicKey>().unwrap();
+ let pk = s.trim().parse::<LegacyPublicKey>().unwrap();
let addr = Address::p2pkh(pk, NetworkKind::Main);
let got = serialize(&addr).unwrap();
@@ -183,7 +184,7 @@ fn serde_regression_private_key() {
#[test]
fn serde_regression_public_key() {
let s = include_str!("data/serde/public_key_hex");
- let pk = s.trim().parse::<PublicKey>().unwrap();
+ let pk = s.trim().parse::<LegacyPublicKey>().unwrap();
let got = serialize(&pk).unwrap();
let want = include_bytes!("data/serde/public_key_bincode") as &[_];
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.