Skip to content

Commit 017ba2f

Browse files
authored
keypair: Deprecate generate to avoid public rand dep (#166)
#### Problem The `generate()` function on `Keypair` exposes a public dependency on `rand` because it requires certain types from the crate. Using the public type from an external crate in our interface means that we can't upgrade without breaking downstream users. #### Summary of changes Mark the function as deprecated, and provide a new way to create a random keypair using `new_from_array`. Unfortunately, we can't mark the function as `const` because the dalek types don't have const constructors.
1 parent 56e1aab commit 017ba2f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

keypair/src/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl Keypair {
3232
pub const SECRET_KEY_LENGTH: usize = 32;
3333

3434
/// Constructs a new, random `Keypair` using a caller-provided RNG
35+
#[deprecated(
36+
since = "2.2.2",
37+
note = "Use `Keypair::new()` instead or generate 32 random bytes and use `Keypair::new_from_array`"
38+
)]
3539
pub fn generate<R>(csprng: &mut R) -> Self
3640
where
3741
R: CryptoRng + RngCore,
@@ -42,7 +46,15 @@ impl Keypair {
4246
/// Constructs a new, random `Keypair` using `OsRng`
4347
pub fn new() -> Self {
4448
let mut rng = OsRng;
45-
Self::generate(&mut rng)
49+
Self(ed25519_dalek::Keypair::generate(&mut rng))
50+
}
51+
52+
/// Constructs a new `Keypair` using secret key bytes
53+
pub fn new_from_array(secret_key: [u8; 32]) -> Self {
54+
// unwrap is safe because the only error condition is an incorrect length
55+
let secret = ed25519_dalek::SecretKey::from_bytes(&secret_key).unwrap();
56+
let public = ed25519_dalek::PublicKey::from(&secret);
57+
Self(ed25519_dalek::Keypair { secret, public })
4658
}
4759

4860
/// Recovers a `Keypair` from a byte array

0 commit comments

Comments
 (0)