Skip to content

Commit 537b85b

Browse files
committed
Deprecate and replace from_slice
The PrivateKey and XOnlyPublicKey from_slice functions have been deprecated and calls to them from within the crate have been changed to use the equivalent array function.
1 parent 1661f57 commit 537b85b

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/ecdh.rs

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ impl SharedSecret {
6464
pub fn from_bytes(bytes: [u8; SHARED_SECRET_SIZE]) -> SharedSecret { SharedSecret(bytes) }
6565

6666
/// Creates a shared secret from `bytes` slice.
67+
#[deprecated(since = "TBD", note = "Use `from_bytes` instead.")]
6768
#[inline]
6869
pub fn from_slice(bytes: &[u8]) -> Result<SharedSecret, Error> {
6970
match bytes.len() {

src/key.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl str::FromStr for SecretKey {
115115
fn from_str(s: &str) -> Result<SecretKey, Error> {
116116
let mut res = [0u8; constants::SECRET_KEY_SIZE];
117117
match from_hex(s, &mut res) {
118-
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_slice(&res),
118+
Ok(constants::SECRET_KEY_SIZE) => SecretKey::from_byte_array(&res),
119119
_ => Err(Error::InvalidSecretKey),
120120
}
121121
}
@@ -138,7 +138,7 @@ impl str::FromStr for SecretKey {
138138
/// use secp256k1::{SecretKey, Secp256k1, PublicKey};
139139
///
140140
/// let secp = Secp256k1::new();
141-
/// let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
141+
/// let secret_key = SecretKey::from_byte_array(&[0xcd; 32]).expect("32 bytes, within curve order");
142142
/// let public_key = PublicKey::from_secret_key(&secp, &secret_key);
143143
/// # }
144144
/// ```
@@ -168,9 +168,13 @@ impl str::FromStr for PublicKey {
168168
fn from_str(s: &str) -> Result<PublicKey, Error> {
169169
let mut res = [0u8; constants::UNCOMPRESSED_PUBLIC_KEY_SIZE];
170170
match from_hex(s, &mut res) {
171-
Ok(constants::PUBLIC_KEY_SIZE) =>
172-
PublicKey::from_slice(&res[0..constants::PUBLIC_KEY_SIZE]),
173-
Ok(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE) => PublicKey::from_slice(&res),
171+
Ok(constants::PUBLIC_KEY_SIZE) => {
172+
let bytes: [u8; constants::PUBLIC_KEY_SIZE] =
173+
res[0..constants::PUBLIC_KEY_SIZE].try_into().unwrap();
174+
PublicKey::from_byte_array_compressed(&bytes)
175+
}
176+
Ok(constants::UNCOMPRESSED_PUBLIC_KEY_SIZE) =>
177+
PublicKey::from_byte_array_uncompressed(&res),
174178
_ => Err(Error::InvalidPublicKey),
175179
}
176180
}
@@ -211,6 +215,7 @@ impl SecretKey {
211215
/// use secp256k1::SecretKey;
212216
/// let sk = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
213217
/// ```
218+
#[deprecated(since = "TBD", note = "Use `from_byte_array` instead.")]
214219
#[inline]
215220
pub fn from_slice(data: &[u8]) -> Result<SecretKey, Error> {
216221
match <[u8; constants::SECRET_KEY_SIZE]>::try_from(data) {
@@ -362,7 +367,7 @@ impl SecretKey {
362367
impl<T: ThirtyTwoByteHash> From<T> for SecretKey {
363368
/// Converts a 32-byte hash directly to a secret key without error paths.
364369
fn from(t: T) -> SecretKey {
365-
SecretKey::from_slice(&t.into_32()).expect("failed to create secret key")
370+
SecretKey::from_byte_array(&t.into_32()).expect("failed to create secret key")
366371
}
367372
}
368373

@@ -542,7 +547,7 @@ impl PublicKey {
542547
};
543548
buf[1..].clone_from_slice(&pk.serialize());
544549

545-
PublicKey::from_slice(&buf).expect("we know the buffer is valid")
550+
PublicKey::from_byte_array_compressed(&buf).expect("we know the buffer is valid")
546551
}
547552

548553
#[inline]
@@ -1156,8 +1161,7 @@ impl str::FromStr for XOnlyPublicKey {
11561161
fn from_str(s: &str) -> Result<XOnlyPublicKey, Error> {
11571162
let mut res = [0u8; constants::SCHNORR_PUBLIC_KEY_SIZE];
11581163
match from_hex(s, &mut res) {
1159-
Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) =>
1160-
XOnlyPublicKey::from_slice(&res[0..constants::SCHNORR_PUBLIC_KEY_SIZE]),
1164+
Ok(constants::SCHNORR_PUBLIC_KEY_SIZE) => XOnlyPublicKey::from_byte_array(&res),
11611165
_ => Err(Error::InvalidPublicKey),
11621166
}
11631167
}
@@ -1203,6 +1207,7 @@ impl XOnlyPublicKey {
12031207
///
12041208
/// Returns [`Error::InvalidPublicKey`] if the length of the data slice is not 32 bytes or the
12051209
/// slice does not represent a valid Secp256k1 point x coordinate.
1210+
#[deprecated(since = "TBD", note = "Use `from_byte_array` instead.")]
12061211
#[inline]
12071212
pub fn from_slice(data: &[u8]) -> Result<XOnlyPublicKey, Error> {
12081213
match <[u8; constants::SCHNORR_PUBLIC_KEY_SIZE]>::try_from(data) {

src/schnorr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ impl Signature {
7878
pub fn from_byte_array(sig: [u8; constants::SCHNORR_SIGNATURE_SIZE]) -> Self { Self(sig) }
7979

8080
/// Creates a `Signature` directly from a slice.
81+
#[deprecated(since = "TBD", note = "Use `from_byte_array` instead.")]
8182
#[inline]
8283
pub fn from_slice(data: &[u8]) -> Result<Signature, Error> {
8384
match data.len() {

0 commit comments

Comments
 (0)