split PublicKey and SecretKey to different packages

This commit is contained in:
DieMyst 2021-01-19 21:55:28 +03:00
parent a4de8f25a9
commit d3a03248b2
11 changed files with 94 additions and 52 deletions

View File

@ -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<PublicKey, SignatureError> {
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<SecretKey, SignatureError> {
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.

View File

@ -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;

View File

@ -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<PublicKey, SignatureError> {
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()
}
}

View File

@ -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<SecretKey, SignatureError> {
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()
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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)]