aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2023-11-29 11:44:53 +0300
committerhozan23 <hozan23@proton.me>2023-11-29 11:44:53 +0300
commit63e8b2fa6b5d4d9bf1ba9234bff0be8a255e612e (patch)
treed8291050228c7e23bdc9a7195df4505db0df1f93 /core
parent57ac163a06b7f4b00d9cfd56d0ab6ee6a49adb56 (diff)
core: Move `key_pair` to the `crypto` module and make it a Cargo feature.
Diffstat (limited to 'core')
-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
5 files changed, 44 insertions, 81 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;