key: Adjust all from_* functions to take refs
What changed, and why it matters
This commit is a routine ergonomic/API change in the rust-bitcoin library. It changes several key-conversion functions so they accept references instead of taking ownership of private key and keypair values. There is no security bug being fixed and no new vulnerability being introduced; it simply makes the API easier to use after the underlying secret key types stopped being implicitly copyable.
No security action required. Treat as a normal API-breaking semver change if you depend on these functions; update call sites to pass references instead of owned values.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch updates three constructors/conversion functions in bitcoin/src/crypto/key.rs and one test call in bitcoin/src/sign_message.rs to take references: TweakedPublicKey::from_keypair now takes &TweakedKeypair, FullPublicKey::from_private_key now takes &PrivateKey, and PrivateKey::from_secret_bytes now takes &[u8; 32]. Internally the code dereferences where needed (e.g., *data for SecretKey::from_secret_bytes). The From
Changed components
bitcoin/src/crypto/key.rsbitcoin/src/sign_message.rsInspect captured patch +8 / −8
diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs
index 661d52af..74656985 100644
--- a/bitcoin/src/crypto/key.rs
+++ b/bitcoin/src/crypto/key.rs
@@ -186,7 +186,7 @@ mod encapsulate {
impl TweakedPublicKey {
/// Returns the [`TweakedPublicKey`] for `keypair`.
#[inline]
- pub fn from_keypair(keypair: TweakedKeypair) -> Self {
+ pub fn from_keypair(keypair: &TweakedKeypair) -> Self {
Self(keypair.as_keypair().to_x_only_public_key())
}
@@ -221,7 +221,7 @@ mod encapsulate {
/// # let keypair = TweakedKeypair::dangerous_assume_tweaked(Keypair::generate());
/// // There are various conversion methods available to get a tweaked pubkey from a tweaked keypair.
/// let (_pk, _parity) = keypair.public_parts();
- /// let _pk = TweakedPublicKey::from_keypair(keypair.clone());
+ /// let _pk = TweakedPublicKey::from_keypair(&keypair);
/// let _pk = TweakedPublicKey::from(keypair.clone());
/// # }
/// ```
@@ -916,7 +916,7 @@ impl FullPublicKey {
/// # Errors
///
/// Errors if the private key is not compressed.
- pub fn from_private_key(sk: PrivateKey) -> Result<Self, UncompressedPublicKeyError> {
+ pub fn from_private_key(sk: &PrivateKey) -> Result<Self, UncompressedPublicKeyError> {
sk.to_public_key().try_into()
}
@@ -1040,8 +1040,8 @@ impl PrivateKey {
///
/// Errors when the secret key is invalid: when it is all-zeros or would exceed
/// the curve order when interpreted as a big-endian unsigned integer.
- pub fn from_secret_bytes(data: [u8; 32]) -> Result<Self, secp256k1::Error> {
- Ok(Self::from_secp(secp256k1::SecretKey::from_secret_bytes(data)?))
+ pub fn from_secret_bytes(data: &[u8; 32]) -> Result<Self, secp256k1::Error> {
+ Ok(Self::from_secp(secp256k1::SecretKey::from_secret_bytes(*data)?))
}
/// Deserializes a private key from a slice.
@@ -1526,7 +1526,7 @@ impl From<TweakedKeypair> for Keypair {
impl From<TweakedKeypair> for TweakedPublicKey {
#[inline]
- fn from(pair: TweakedKeypair) -> Self { Self::from_keypair(pair) }
+ fn from(pair: TweakedKeypair) -> Self { Self::from_keypair(&pair) }
}
/// Error returned while generating key from slice.
@@ -2354,7 +2354,7 @@ mod tests {
"1ede31b0e7e47c2afc65ffd158b1b1b9d3b752bba8fd117dc8b9e944a390e8d9",
)
.unwrap();
- let sk = PrivateKey::from_secret_bytes(bytes).unwrap();
+ let sk = PrivateKey::from_secret_bytes(&bytes).unwrap();
Keypair::from_private_key(&sk)
};
diff --git a/bitcoin/src/sign_message.rs b/bitcoin/src/sign_message.rs
index 7ee75311..9ef0ca31 100644
--- a/bitcoin/src/sign_message.rs
+++ b/bitcoin/src/sign_message.rs
@@ -269,7 +269,7 @@ mod tests {
let p2pkh = Address::p2pkh(pubkey, Network::Bitcoin);
assert_eq!(signature2.is_signed_by_address(&p2pkh, msg_hash), Ok(true));
- assert_eq!(pubkey, FullPublicKey::from_private_key(privkey).unwrap());
+ assert_eq!(pubkey, FullPublicKey::from_private_key(&privkey).unwrap());
let signature_base64 = signature.to_base64();
let signature_round_trip =
super::MessageSignature::from_base64(&signature_base64).expect("message signature");
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.