Skip to content

Commit

Permalink
Bump ed25519-dalek dependency to v2.0.0-pre.0 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored Mar 9, 2023
1 parent 7e12e4e commit a30fb65
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 45 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ zeroize = "1.1.0"
sha3 = "0.10"
k256 = { version = "0.13", features = ["ecdsa"], optional = true }
serde = { version = "1.0.110", optional = true }
ed25519-dalek = { version = "1.0.0-pre.4", optional = true }
ed25519-dalek = { version = "2.0.0-pre.0", optional = true, features = ["rand_core"] }
secp256k1 = { version = "0.26", optional = true, default-features = false, features = [
"global-context",
] }

[dev-dependencies]
rand_07 = { package = "rand", version = "0.7" }
secp256k1 = { features = ["rand-std"], version = "0.26" }

[features]
Expand Down
39 changes: 12 additions & 27 deletions src/keys/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
use super::{ed25519_dalek as ed25519, EnrKey, EnrPublicKey, SigningError};
use bytes::Bytes;
pub use k256;
use rand::RngCore;
use rlp::DecoderError;
use std::collections::BTreeMap;
use std::{collections::BTreeMap, convert::TryFrom};
use zeroize::Zeroize;

use crate::Key;
Expand All @@ -19,7 +18,7 @@ pub enum CombinedKey {
/// An `secp256k1` keypair.
Secp256k1(k256::ecdsa::SigningKey),
/// An `Ed25519` keypair.
Ed25519(ed25519::Keypair),
Ed25519(ed25519::SigningKey),
}

impl From<k256::ecdsa::SigningKey> for CombinedKey {
Expand All @@ -28,20 +27,12 @@ impl From<k256::ecdsa::SigningKey> for CombinedKey {
}
}

impl From<ed25519::Keypair> for CombinedKey {
fn from(keypair: ed25519_dalek::Keypair) -> Self {
impl From<ed25519::SigningKey> for CombinedKey {
fn from(keypair: ed25519_dalek::SigningKey) -> Self {
Self::Ed25519(keypair)
}
}

/// Promote an Ed25519 secret key into a keypair.
impl From<ed25519::SecretKey> for CombinedKey {
fn from(secret: ed25519::SecretKey) -> Self {
let public = ed25519::PublicKey::from(&secret);
Self::Ed25519(ed25519::Keypair { secret, public })
}
}

impl EnrKey for CombinedKey {
type PublicKey = CombinedPublicKey;

Expand Down Expand Up @@ -70,7 +61,7 @@ impl EnrKey for CombinedKey {
fn enr_to_public(content: &BTreeMap<Key, Bytes>) -> Result<Self::PublicKey, DecoderError> {
k256::ecdsa::SigningKey::enr_to_public(content)
.map(CombinedPublicKey::Secp256k1)
.or_else(|_| ed25519::Keypair::enr_to_public(content).map(CombinedPublicKey::from))
.or_else(|_| ed25519::SigningKey::enr_to_public(content).map(CombinedPublicKey::from))
}
}

Expand All @@ -85,14 +76,7 @@ impl CombinedKey {
/// Generates a new ed25510 key.
#[must_use]
pub fn generate_ed25519() -> Self {
let mut bytes = [0_u8; 32];
rand::thread_rng().fill_bytes(&mut bytes);
let key =
Self::from(ed25519::SecretKey::from_bytes(&bytes).expect(
"this returns `Err` only if the length is wrong; the length is correct; qed",
));
bytes.zeroize();
key
Self::Ed25519(ed25519::SigningKey::generate(&mut rand::thread_rng()))
}

/// Imports a secp256k1 from raw bytes in any format.
Expand All @@ -106,7 +90,8 @@ impl CombinedKey {

/// Imports an ed25519 key from raw 32 bytes.
pub fn ed25519_from_bytes(bytes: &mut [u8]) -> Result<Self, DecoderError> {
let key = ed25519::SecretKey::from_bytes(bytes)
#[allow(clippy::useless_asref)]
let key = ed25519::SigningKey::try_from(bytes.as_ref())
.map_err(|_| DecoderError::Custom("Invalid ed25519 secret key"))
.map(Self::from)?;
bytes.zeroize();
Expand All @@ -118,7 +103,7 @@ impl CombinedKey {
pub fn encode(&self) -> Vec<u8> {
match self {
Self::Secp256k1(key) => key.to_bytes().to_vec(),
Self::Ed25519(key) => key.secret.as_bytes().to_vec(),
Self::Ed25519(key) => key.to_bytes().to_vec(),
}
}
}
Expand All @@ -130,7 +115,7 @@ pub enum CombinedPublicKey {
/// An `Secp256k1` public key.
Secp256k1(k256::ecdsa::VerifyingKey),
/// An `Ed25519` public key.
Ed25519(ed25519::PublicKey),
Ed25519(ed25519::VerifyingKey),
}

impl From<k256::ecdsa::VerifyingKey> for CombinedPublicKey {
Expand All @@ -139,8 +124,8 @@ impl From<k256::ecdsa::VerifyingKey> for CombinedPublicKey {
}
}

impl From<ed25519::PublicKey> for CombinedPublicKey {
fn from(public_key: ed25519::PublicKey) -> Self {
impl From<ed25519::VerifyingKey> for CombinedPublicKey {
fn from(public_key: ed25519::VerifyingKey) -> Self {
Self::Ed25519(public_key)
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/keys/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use std::{collections::BTreeMap, convert::TryFrom};
/// The ENR key that stores the public key in the ENR record.
pub const ENR_KEY: &str = "ed25519";

impl EnrKey for ed25519::Keypair {
type PublicKey = ed25519::PublicKey;
impl EnrKey for ed25519::SigningKey {
type PublicKey = ed25519::VerifyingKey;

/// Performs ENR-specific signing.
///
Expand All @@ -23,7 +23,7 @@ impl EnrKey for ed25519::Keypair {

/// Returns the public key associated with the private key.
fn public(&self) -> Self::PublicKey {
self.public
self.verifying_key()
}

/// Decodes the raw bytes of an ENR's content into a public key if possible.
Expand All @@ -39,14 +39,14 @@ impl EnrKey for ed25519::Keypair {
}
}

impl EnrKeyUnambiguous for ed25519::Keypair {
impl EnrKeyUnambiguous for ed25519::SigningKey {
fn decode_public(bytes: &[u8]) -> Result<Self::PublicKey, DecoderError> {
ed25519::PublicKey::from_bytes(bytes)
ed25519::VerifyingKey::try_from(bytes)
.map_err(|_| DecoderError::Custom("Invalid ed25519 Signature"))
}
}

impl EnrPublicKey for ed25519::PublicKey {
impl EnrPublicKey for ed25519::VerifyingKey {
type Raw = [u8; ed25519::PUBLIC_KEY_LENGTH];
type RawUncompressed = [u8; ed25519::PUBLIC_KEY_LENGTH];

Expand Down
19 changes: 9 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! implement the [`EnrKey`] trait and apply it to an [`Enr`].
//!
//! By default, `k256::SigningKey` implement [`EnrKey`] and can be used to sign and
//! verify ENR records. This library also implements [`EnrKey`] for `ed25519_dalek::Keypair` via the `ed25519`
//! verify ENR records. This library also implements [`EnrKey`] for `ed25519_dalek::SigningKey` via the `ed25519`
//! feature flag.
//!
//! Furthermore, a [`CombinedKey`] is provided if the `ed25519` feature flag is set, which provides an
Expand Down Expand Up @@ -127,23 +127,22 @@
//!
//! ```rust
//! # #[cfg(feature = "ed25519")] {
//! use enr::{EnrBuilder, k256::ecdsa::SigningKey, Enr, ed25519_dalek::Keypair, CombinedKey};
//! use enr::{EnrBuilder, k256::ecdsa, Enr, ed25519_dalek as ed25519, CombinedKey};
//! use std::net::Ipv4Addr;
//! use rand::thread_rng;
//! use rand::Rng;
//!
//! // generate a random secp256k1 key
//! let mut rng = thread_rng();
//! let key = SigningKey::random(&mut rng);
//! let key = ecdsa::SigningKey::random(&mut rng);
//! let ip = Ipv4Addr::new(192,168,0,1);
//! let enr_secp256k1 = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
//!
//! // encode to base64
//! let base64_string_secp256k1 = enr_secp256k1.to_base64();
//!
//! // generate a random ed25519 key
//! # let mut rng = rand_07::thread_rng();
//! let key = Keypair::generate(&mut rng);
//! let key = ed25519::SigningKey::generate(&mut rng);
//! let enr_ed25519 = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
//!
//! // encode to base64
Expand All @@ -153,7 +152,7 @@
//! // decode the secp256k1 with default Enr
//! let decoded_enr_secp256k1: Enr<k256::ecdsa::SigningKey> = base64_string_secp256k1.parse().unwrap();
//! // decode ed25519 ENRs
//! let decoded_enr_ed25519: Enr<ed25519_dalek::Keypair> = base64_string_ed25519.parse().unwrap();
//! let decoded_enr_ed25519: Enr<ed25519_dalek::SigningKey> = base64_string_ed25519.parse().unwrap();
//!
//! // use the combined key to be able to decode either
//! let decoded_enr: Enr<CombinedKey> = base64_string_secp256k1.parse().unwrap();
Expand Down Expand Up @@ -1280,8 +1279,8 @@ mod tests {
#[cfg(all(feature = "ed25519", feature = "k256"))]
#[test]
fn test_encode_decode_ed25519() {
let mut rng = rand_07::thread_rng();
let key = ed25519_dalek::Keypair::generate(&mut rng);
let mut rng = rand::thread_rng();
let key = ed25519_dalek::SigningKey::generate(&mut rng);
let ip = Ipv4Addr::new(10, 0, 0, 1);
let tcp = 30303;

Expand Down Expand Up @@ -1386,7 +1385,7 @@ mod tests {
let base64_string_secp256k1 = enr_secp256k1.to_base64();

// generate a random ed25519 key
let key = ed25519_dalek::Keypair::generate(&mut rand_07::thread_rng());
let key = ed25519_dalek::SigningKey::generate(&mut rand::thread_rng());
let enr_ed25519 = EnrBuilder::new("v4")
.ip(ip.into())
.tcp4(8000)
Expand All @@ -1400,7 +1399,7 @@ mod tests {
// decode the secp256k1 with default Enr
let _decoded_enr_secp256k1: DefaultEnr = base64_string_secp256k1.parse().unwrap();
// decode ed25519 ENRs
let _decoded_enr_ed25519: Enr<ed25519_dalek::Keypair> =
let _decoded_enr_ed25519: Enr<ed25519_dalek::SigningKey> =
base64_string_ed25519.parse().unwrap();

// use the combined key to be able to decode either
Expand Down

0 comments on commit a30fb65

Please sign in to comment.