aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Cargo.toml7
-rw-r--r--core/src/crypto/key_pair.rs (renamed from core/src/key_pair.rs)109
-rw-r--r--core/src/crypto/mod.rs3
-rw-r--r--core/src/error.rs1
-rw-r--r--core/src/lib.rs5
-rw-r--r--p2p/Cargo.toml2
-rw-r--r--p2p/examples/chat.rs2
-rw-r--r--p2p/examples/monitor.rs2
-rw-r--r--p2p/examples/peer.rs2
-rw-r--r--p2p/src/backend.rs2
-rw-r--r--p2p/src/connector.rs2
-rw-r--r--p2p/src/discovery/lookup.rs2
-rw-r--r--p2p/src/discovery/mod.rs2
-rw-r--r--p2p/src/lib.rs2
-rw-r--r--p2p/src/listener.rs2
-rw-r--r--p2p/src/monitor.rs2
-rw-r--r--p2p/src/peer/peer_id.rs2
-rw-r--r--p2p/src/protocol.rs2
-rw-r--r--p2p/src/tls_config.rs2
19 files changed, 58 insertions, 95 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml
index 5a99e2d..3c8cb9c 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -16,7 +16,12 @@ rand = "0.8.5"
thiserror = "1.0.47"
dirs = "5.0.1"
async-task = "4.5.0"
-ed25519-dalek = { version = "2.1.0", features = ["rand_core"]}
+ed25519-dalek = { version = "2.1.0", features = ["rand_core"], optional = true}
+
+
+[features]
+default = []
+crypto = ["dep:ed25519-dalek"]
diff --git a/core/src/key_pair.rs b/core/src/crypto/key_pair.rs
index 4016351..899cb6a 100644
--- a/core/src/key_pair.rs
+++ b/core/src/crypto/key_pair.rs
@@ -8,52 +8,41 @@ pub enum KeyPairType {
Ed25519,
}
-/// A Public key
-pub struct PublicKey(PublicKeyInner);
-
/// A Secret key
pub struct SecretKey(Vec<u8>);
-impl PublicKey {
- pub fn as_bytes(&self) -> &[u8] {
- self.0.as_bytes()
- }
-
- /// Verify a signature on a message with this public key.
- pub fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
- self.0.verify(msg, signature)
- }
-}
-
-impl PublicKey {
- pub fn from_bytes(kp_type: &KeyPairType, pk: &[u8]) -> Result<Self> {
- Ok(Self(PublicKeyInner::from_bytes(kp_type, pk)?))
- }
-}
-
-/// A KeyPair.
#[derive(Clone)]
-pub struct KeyPair(KeyPairInner);
+pub enum KeyPair {
+ Ed25519(Ed25519KeyPair),
+}
impl KeyPair {
/// Generate a new random keypair.
pub fn generate(kp_type: &KeyPairType) -> Self {
- Self(KeyPairInner::generate(kp_type))
+ match kp_type {
+ KeyPairType::Ed25519 => Self::Ed25519(Ed25519KeyPair::generate()),
+ }
}
/// Sign a message using the private key.
pub fn sign(&self, msg: &[u8]) -> Vec<u8> {
- self.0.sign(msg)
+ match self {
+ KeyPair::Ed25519(kp) => kp.sign(msg),
+ }
}
/// Get the public key of this keypair.
pub fn public(&self) -> PublicKey {
- self.0.public()
+ match self {
+ KeyPair::Ed25519(kp) => kp.public(),
+ }
}
/// Get the secret key of this keypair.
pub fn secret(&self) -> SecretKey {
- self.0.secret()
+ match self {
+ KeyPair::Ed25519(kp) => kp.secret(),
+ }
}
}
@@ -70,40 +59,7 @@ trait KeyPairExt {
}
#[derive(Clone)]
-enum KeyPairInner {
- Ed25519(Ed25519KeyPair),
-}
-
-impl KeyPairInner {
- fn generate(kp_type: &KeyPairType) -> Self {
- match kp_type {
- KeyPairType::Ed25519 => Self::Ed25519(Ed25519KeyPair::generate()),
- }
- }
-}
-
-impl KeyPairExt for KeyPairInner {
- fn sign(&self, msg: &[u8]) -> Vec<u8> {
- match self {
- KeyPairInner::Ed25519(kp) => kp.sign(msg),
- }
- }
-
- fn public(&self) -> PublicKey {
- match self {
- KeyPairInner::Ed25519(kp) => kp.public(),
- }
- }
-
- fn secret(&self) -> SecretKey {
- match self {
- KeyPairInner::Ed25519(kp) => kp.secret(),
- }
- }
-}
-
-#[derive(Clone)]
-struct Ed25519KeyPair(ed25519_dalek::SigningKey);
+pub struct Ed25519KeyPair(ed25519_dalek::SigningKey);
impl Ed25519KeyPair {
fn generate() -> Self {
@@ -117,9 +73,7 @@ impl KeyPairExt for Ed25519KeyPair {
}
fn public(&self) -> PublicKey {
- PublicKey(PublicKeyInner::Ed25519(Ed25519PublicKey(
- self.0.verifying_key(),
- )))
+ PublicKey::Ed25519(Ed25519PublicKey(self.0.verifying_key()))
}
fn secret(&self) -> SecretKey {
@@ -127,41 +81,40 @@ impl KeyPairExt for Ed25519KeyPair {
}
}
-/// An extension trait, adding essential methods to all [`PublicKey`] types.
-trait PublicKeyExt {
- fn as_bytes(&self) -> &[u8];
-
- /// Verify a signature on a message with this public key.
- fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()>;
-}
-
-enum PublicKeyInner {
+pub enum PublicKey {
Ed25519(Ed25519PublicKey),
}
-impl PublicKeyInner {
+impl PublicKey {
pub fn from_bytes(kp_type: &KeyPairType, pk: &[u8]) -> Result<Self> {
match kp_type {
KeyPairType::Ed25519 => Ok(Self::Ed25519(Ed25519PublicKey::from_bytes(pk)?)),
}
}
-}
-impl PublicKeyExt for PublicKeyInner {
- fn as_bytes(&self) -> &[u8] {
+ pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Ed25519(pk) => pk.as_bytes(),
}
}
- fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
+ /// Verify a signature on a message with this public key.
+ pub fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()> {
match self {
Self::Ed25519(pk) => pk.verify(msg, signature),
}
}
}
-struct Ed25519PublicKey(ed25519_dalek::VerifyingKey);
+/// An extension trait, adding essential methods to all [`PublicKey`] types.
+trait PublicKeyExt {
+ fn as_bytes(&self) -> &[u8];
+
+ /// Verify a signature on a message with this public key.
+ fn verify(&self, msg: &[u8], signature: &[u8]) -> Result<()>;
+}
+
+pub struct Ed25519PublicKey(ed25519_dalek::VerifyingKey);
impl Ed25519PublicKey {
pub fn from_bytes(pk: &[u8]) -> Result<Self> {
diff --git a/core/src/crypto/mod.rs b/core/src/crypto/mod.rs
new file mode 100644
index 0000000..7d56e69
--- /dev/null
+++ b/core/src/crypto/mod.rs
@@ -0,0 +1,3 @@
+mod key_pair;
+
+pub use key_pair::{KeyPair, KeyPairType, PublicKey, SecretKey};
diff --git a/core/src/error.rs b/core/src/error.rs
index 7c547c4..cc60696 100644
--- a/core/src/error.rs
+++ b/core/src/error.rs
@@ -16,6 +16,7 @@ pub enum Error {
#[error("Path Not Found Error: {0}")]
PathNotFound(&'static str),
+ #[cfg(feature = "crypto")]
#[error(transparent)]
Ed25519(#[from] ed25519_dalek::ed25519::Error),
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 276ed89..a4ea432 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -14,8 +14,9 @@ pub mod event;
/// A simple publish-subscribe system [`Read More`](./pubsub/struct.Publisher.html)
pub mod pubsub;
-/// A cryptographic key pair
-pub mod key_pair;
+#[cfg(feature = "crypto")]
+/// Collects common cryptographic tools
+pub mod crypto;
use smol::Executor as SmolEx;
use std::sync::Arc;
diff --git a/p2p/Cargo.toml b/p2p/Cargo.toml
index 315983b..31bac2d 100644
--- a/p2p/Cargo.toml
+++ b/p2p/Cargo.toml
@@ -6,7 +6,7 @@ edition.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-karyons_core.workspace = true
+karyons_core = { workspace = true, features=["crypto"] }
karyons_net.workspace = true
smol = "1.3.0"
diff --git a/p2p/examples/chat.rs b/p2p/examples/chat.rs
index 925c832..99cde7b 100644
--- a/p2p/examples/chat.rs
+++ b/p2p/examples/chat.rs
@@ -7,7 +7,7 @@ use async_trait::async_trait;
use clap::Parser;
use smol::{channel, Executor};
-use karyons_core::key_pair::{KeyPair, KeyPairType};
+use karyons_core::crypto::{KeyPair, KeyPairType};
use karyons_net::{Endpoint, Port};
use karyons_p2p::{
diff --git a/p2p/examples/monitor.rs b/p2p/examples/monitor.rs
index 530d2d5..0b6571c 100644
--- a/p2p/examples/monitor.rs
+++ b/p2p/examples/monitor.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use clap::Parser;
use smol::{channel, Executor};
-use karyons_core::key_pair::{KeyPair, KeyPairType};
+use karyons_core::crypto::{KeyPair, KeyPairType};
use karyons_net::{Endpoint, Port};
use karyons_p2p::{Backend, Config};
diff --git a/p2p/examples/peer.rs b/p2p/examples/peer.rs
index b595b4a..c0b05c6 100644
--- a/p2p/examples/peer.rs
+++ b/p2p/examples/peer.rs
@@ -5,7 +5,7 @@ use std::sync::Arc;
use clap::Parser;
use smol::{channel, Executor};
-use karyons_core::key_pair::{KeyPair, KeyPairType};
+use karyons_core::crypto::{KeyPair, KeyPairType};
use karyons_net::{Endpoint, Port};
use karyons_p2p::{Backend, Config};
diff --git a/p2p/src/backend.rs b/p2p/src/backend.rs
index 56d79f7..f0740b1 100644
--- a/p2p/src/backend.rs
+++ b/p2p/src/backend.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
use log::info;
-use karyons_core::{key_pair::KeyPair, pubsub::Subscription, GlobalExecutor};
+use karyons_core::{crypto::KeyPair, pubsub::Subscription, GlobalExecutor};
use crate::{
config::Config,
diff --git a/p2p/src/connector.rs b/p2p/src/connector.rs
index 6fc5734..835b1c9 100644
--- a/p2p/src/connector.rs
+++ b/p2p/src/connector.rs
@@ -4,7 +4,7 @@ use log::{error, trace, warn};
use karyons_core::{
async_util::{Backoff, TaskGroup, TaskResult},
- key_pair::KeyPair,
+ crypto::KeyPair,
GlobalExecutor,
};
use karyons_net::{dial, tls, Conn, Endpoint, NetError};
diff --git a/p2p/src/discovery/lookup.rs b/p2p/src/discovery/lookup.rs
index 60d8635..aefc3a0 100644
--- a/p2p/src/discovery/lookup.rs
+++ b/p2p/src/discovery/lookup.rs
@@ -5,7 +5,7 @@ use log::{error, trace};
use rand::{rngs::OsRng, seq::SliceRandom, RngCore};
use smol::lock::{Mutex, RwLock};
-use karyons_core::{async_util::timeout, key_pair::KeyPair, util::decode, GlobalExecutor};
+use karyons_core::{async_util::timeout, crypto::KeyPair, util::decode, GlobalExecutor};
use karyons_net::{Conn, Endpoint};
diff --git a/p2p/src/discovery/mod.rs b/p2p/src/discovery/mod.rs
index 2c1bcd8..8091991 100644
--- a/p2p/src/discovery/mod.rs
+++ b/p2p/src/discovery/mod.rs
@@ -9,7 +9,7 @@ use smol::lock::Mutex;
use karyons_core::{
async_util::{Backoff, TaskGroup, TaskResult},
- key_pair::KeyPair,
+ crypto::KeyPair,
GlobalExecutor,
};
diff --git a/p2p/src/lib.rs b/p2p/src/lib.rs
index 6585287..39f4bc1 100644
--- a/p2p/src/lib.rs
+++ b/p2p/src/lib.rs
@@ -7,7 +7,7 @@
//! use easy_parallel::Parallel;
//! use smol::{channel as smol_channel, future, Executor};
//!
-//! use karyons_core::key_pair::{KeyPair, KeyPairType};
+//! use karyons_core::crypto::{KeyPair, KeyPairType};
//! use karyons_p2p::{Backend, Config, PeerID};
//!
//! let key_pair = KeyPair::generate(&KeyPairType::Ed25519);
diff --git a/p2p/src/listener.rs b/p2p/src/listener.rs
index 58a0931..879f046 100644
--- a/p2p/src/listener.rs
+++ b/p2p/src/listener.rs
@@ -4,7 +4,7 @@ use log::{debug, error, info};
use karyons_core::{
async_util::{TaskGroup, TaskResult},
- key_pair::KeyPair,
+ crypto::KeyPair,
GlobalExecutor,
};
diff --git a/p2p/src/monitor.rs b/p2p/src/monitor.rs
index 1f74503..1ea6a0b 100644
--- a/p2p/src/monitor.rs
+++ b/p2p/src/monitor.rs
@@ -17,7 +17,7 @@ use karyons_net::Endpoint;
///
/// use smol::Executor;
///
-/// use karyons_core::key_pair::{KeyPair, KeyPairType};
+/// use karyons_core::crypto::{KeyPair, KeyPairType};
/// use karyons_p2p::{Config, Backend, PeerID};
///
/// async {
diff --git a/p2p/src/peer/peer_id.rs b/p2p/src/peer/peer_id.rs
index 903d827..0208e05 100644
--- a/p2p/src/peer/peer_id.rs
+++ b/p2p/src/peer/peer_id.rs
@@ -2,7 +2,7 @@ use bincode::{Decode, Encode};
use rand::{rngs::OsRng, RngCore};
use sha2::{Digest, Sha256};
-use karyons_core::key_pair::PublicKey;
+use karyons_core::crypto::PublicKey;
use crate::Error;
diff --git a/p2p/src/protocol.rs b/p2p/src/protocol.rs
index 582502e..7261f19 100644
--- a/p2p/src/protocol.rs
+++ b/p2p/src/protocol.rs
@@ -37,7 +37,7 @@ impl EventValue for ProtocolEvent {
/// use async_trait::async_trait;
/// use smol::Executor;
///
-/// use karyons_core::key_pair::{KeyPair, KeyPairType};
+/// use karyons_core::crypto::{KeyPair, KeyPairType};
/// use karyons_p2p::{
/// protocol::{ArcProtocol, Protocol, ProtocolID, ProtocolEvent},
/// Backend, PeerID, Config, Version, P2pError, ArcPeer};
diff --git a/p2p/src/tls_config.rs b/p2p/src/tls_config.rs
index f3b231a..2994bcf 100644
--- a/p2p/src/tls_config.rs
+++ b/p2p/src/tls_config.rs
@@ -8,7 +8,7 @@ use async_rustls::rustls::{
use log::error;
use x509_parser::{certificate::X509Certificate, parse_x509_certificate};
-use karyons_core::key_pair::{KeyPair, KeyPairType, PublicKey};
+use karyons_core::crypto::{KeyPair, KeyPairType, PublicKey};
use crate::{PeerID, Result};