diff --git a/bitcoin/src/crypto/key.rs b/bitcoin/src/crypto/key.rs index 2fcacba37..ed16e7a72 100644 --- a/bitcoin/src/crypto/key.rs +++ b/bitcoin/src/crypto/key.rs @@ -459,17 +459,21 @@ impl PrivateKey { /// Serializes the private key to bytes. pub fn to_vec(self) -> Vec { self.inner[..].to_vec() } + /// Deserializes a private key from a byte array. + pub fn from_byte_array( + data: [u8; 32], + network: impl Into + ) -> Result { + Ok(PrivateKey::new(secp256k1::SecretKey::from_byte_array(&data)?, network)) + } + /// Deserializes a private key from a slice. pub fn from_slice( data: &[u8], network: impl Into, ) -> Result { - Ok(PrivateKey::new( - secp256k1::SecretKey::from_byte_array( - data.try_into().map_err(|_| secp256k1::Error::InvalidSecretKey)?, - )?, - network, - )) + let array = data.try_into().map_err(|_| secp256k1::Error::InvalidSecretKey)?; + Self::from_byte_array(array, network) } /// Formats the private key to WIF format.