diff --git a/identity/src/key_pair.rs b/identity/src/key_pair.rs index 223c410..da17e16 100644 --- a/identity/src/key_pair.rs +++ b/identity/src/key_pair.rs @@ -15,11 +15,12 @@ */ use crate::ed25519::Keypair as Libp2pKeyPair; +use crate::public_key::PublicKey; +use crate::secret_key::SecretKey; use crate::signature::Signature; use ed25519_dalek::SignatureError; use ed25519_dalek::Signer; -use core::fmt::Debug; use rand::rngs::OsRng; use std::fmt; @@ -28,50 +29,6 @@ use std::fmt; pub struct KeyPair { key_pair: ed25519_dalek::Keypair, } -#[derive(Copy, Clone, Default, Eq, PartialEq)] -pub struct PublicKey(ed25519_dalek::PublicKey); - -pub struct SecretKey(ed25519_dalek::SecretKey); - -impl Debug for PublicKey { - fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { - write!(f, "{:?}", self.0) - } -} - -impl PublicKey { - pub fn verify_strict( - &self, - message: &[u8], - signature: &Signature, - ) -> Result<(), SignatureError> { - self.0.verify_strict(message, &signature.0) - } - - pub fn from_bytes(bytes: &[u8]) -> Result { - let pk = ed25519_dalek::PublicKey::from_bytes(bytes)?; - - Ok(PublicKey(pk)) - } - - pub fn to_bytes(&self) -> [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] { - self.0.to_bytes() - } -} - -impl SecretKey { - pub fn from_bytes(bytes: &[u8]) -> Result { - let pk = ed25519_dalek::SecretKey::from_bytes(bytes)?; - - Ok(SecretKey(pk)) - } -} - -impl AsRef<[u8]> for SecretKey { - fn as_ref(&self) -> &[u8] { - self.0.as_bytes() - } -} impl KeyPair { /// Generate a new Ed25519 keypair. diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 73a38ea..4d63006 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -27,9 +27,13 @@ )] pub mod key_pair; +pub mod public_key; +pub mod secret_key; pub mod signature; pub use crate::key_pair::KeyPair; +pub use crate::public_key::PublicKey; +pub use crate::secret_key::SecretKey; pub use crate::signature::Signature; pub(crate) use libp2p_core::identity::ed25519; diff --git a/identity/src/public_key.rs b/identity/src/public_key.rs new file mode 100644 index 0000000..b9bafc3 --- /dev/null +++ b/identity/src/public_key.rs @@ -0,0 +1,48 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::signature::Signature; +use core::fmt::Debug; +use ed25519_dalek::SignatureError; + +#[derive(Copy, Clone, Default, Eq, PartialEq)] +pub struct PublicKey(pub(crate) ed25519_dalek::PublicKey); + +impl Debug for PublicKey { + fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result { + write!(f, "{:?}", self.0) + } +} + +impl PublicKey { + pub fn verify_strict( + &self, + message: &[u8], + signature: &Signature, + ) -> Result<(), SignatureError> { + self.0.verify_strict(message, &signature.0) + } + + pub fn from_bytes(bytes: &[u8]) -> Result { + let pk = ed25519_dalek::PublicKey::from_bytes(bytes)?; + + Ok(PublicKey(pk)) + } + + pub fn to_bytes(&self) -> [u8; ed25519_dalek::PUBLIC_KEY_LENGTH] { + self.0.to_bytes() + } +} diff --git a/identity/src/secret_key.rs b/identity/src/secret_key.rs new file mode 100644 index 0000000..ea81223 --- /dev/null +++ b/identity/src/secret_key.rs @@ -0,0 +1,33 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use ed25519_dalek::SignatureError; + +pub struct SecretKey(ed25519_dalek::SecretKey); + +impl SecretKey { + pub fn from_bytes(bytes: &[u8]) -> Result { + let pk = ed25519_dalek::SecretKey::from_bytes(bytes)?; + + Ok(SecretKey(pk)) + } +} + +impl AsRef<[u8]> for SecretKey { + fn as_ref(&self) -> &[u8] { + self.0.as_bytes() + } +} diff --git a/src/certificate.rs b/src/certificate.rs index 4d18ca7..3b44a4b 100644 --- a/src/certificate.rs +++ b/src/certificate.rs @@ -16,7 +16,7 @@ use crate::trust::{Trust, TRUST_LEN}; use fluence_identity::key_pair::KeyPair; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use std::str::FromStr; use std::time::Duration; diff --git a/src/public_key_hashable.rs b/src/public_key_hashable.rs index 5b4e4b8..bb56ff5 100644 --- a/src/public_key_hashable.rs +++ b/src/public_key_hashable.rs @@ -14,7 +14,7 @@ * limitations under the License. */ -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use core::fmt; use ref_cast::RefCast; diff --git a/src/revoke.rs b/src/revoke.rs index 0d84308..12087aa 100644 --- a/src/revoke.rs +++ b/src/revoke.rs @@ -16,7 +16,7 @@ use crate::trust::{EXPIRATION_LEN, PK_LEN}; use fluence_identity::key_pair::KeyPair; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use fluence_identity::signature::Signature; use std::time::Duration; diff --git a/src/trust.rs b/src/trust.rs index 736a305..daf1467 100644 --- a/src/trust.rs +++ b/src/trust.rs @@ -16,7 +16,7 @@ use derivative::Derivative; use fluence_identity::key_pair::KeyPair; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use fluence_identity::signature::Signature; use std::convert::TryInto; use std::time::Duration; diff --git a/src/trust_graph.rs b/src/trust_graph.rs index 515c6c8..578ac46 100644 --- a/src/trust_graph.rs +++ b/src/trust_graph.rs @@ -20,7 +20,7 @@ use crate::revoke::Revoke; use crate::trust::Trust; use crate::trust_graph_storage::Storage; use crate::trust_node::{Auth, TrustNode}; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use std::borrow::Borrow; use std::collections::{HashSet, VecDeque}; use std::time::Duration; diff --git a/src/trust_graph_storage.rs b/src/trust_graph_storage.rs index 69a6456..c778a54 100644 --- a/src/trust_graph_storage.rs +++ b/src/trust_graph_storage.rs @@ -2,7 +2,7 @@ use crate::public_key_hashable::PublicKeyHashable; use crate::revoke::Revoke; use crate::trust_graph::Weight; use crate::trust_node::{Auth, TrustNode}; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use std::collections::HashMap; use std::time::Duration; diff --git a/src/trust_node.rs b/src/trust_node.rs index 0adcc27..f613459 100644 --- a/src/trust_node.rs +++ b/src/trust_node.rs @@ -18,7 +18,7 @@ use crate::public_key_hashable::PublicKeyHashable; use crate::revoke::Revoke; use crate::trust::Trust; use failure::_core::time::Duration; -use fluence_identity::key_pair::PublicKey; +use fluence_identity::public_key::PublicKey; use std::collections::HashMap; #[derive(Debug, Clone)]