diff --git a/common/crypto/src/encryption/mod.rs b/common/crypto/src/encryption/mod.rs index 4d94f06f4b..650e221f0e 100644 --- a/common/crypto/src/encryption/mod.rs +++ b/common/crypto/src/encryption/mod.rs @@ -43,13 +43,17 @@ impl PemStorableKeyPair for KeyPair { type PrivatePemKey = PrivateKey; type PublicPemKey = PublicKey; - fn private_pem_key(&self) -> &Self::PrivatePemKey { + fn private_key(&self) -> &Self::PrivatePemKey { self.private_key() } - fn public_pem_key(&self) -> &Self::PublicPemKey { + fn public_key(&self) -> &Self::PublicPemKey { self.public_key() } + + fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { + Self::from_bytes(priv_bytes, pub_bytes) + } } // COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed @@ -74,6 +78,10 @@ impl PemStorableKey for PrivateKey { fn pem_type(&self) -> String { String::from("X25519 PRIVATE KEY") } + + fn to_bytes(&self) -> Vec { + self.to_bytes() + } } #[derive(Debug, Clone, Eq, PartialEq)] @@ -102,4 +110,8 @@ impl PemStorableKey for PublicKey { fn pem_type(&self) -> String { String::from("X25519 PUBLIC KEY") } + + fn to_bytes(&self) -> Vec { + self.to_bytes() + } } diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index b7fe294c78..4c1ea3cf75 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -40,13 +40,17 @@ impl PemStorableKeyPair for MixIdentityKeyPair { type PrivatePemKey = MixIdentityPrivateKey; type PublicPemKey = MixIdentityPublicKey; - fn private_pem_key(&self) -> &Self::PrivatePemKey { + fn private_key(&self) -> &Self::PrivatePemKey { self.private_key() } - fn public_pem_key(&self) -> &Self::PublicPemKey { + fn public_key(&self) -> &Self::PublicPemKey { self.public_key() } + + fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { + Self::from_bytes(priv_bytes, pub_bytes) + } } #[derive(Debug, Clone, Eq, PartialEq)] @@ -82,6 +86,10 @@ impl PemStorableKey for MixIdentityPublicKey { fn pem_type(&self) -> String { format!("DUMMY KEY BASED ON {}", self.0.pem_type()) } + + fn to_bytes(&self) -> Vec { + self.to_bytes() + } } // COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed @@ -118,4 +126,8 @@ impl PemStorableKey for MixIdentityPrivateKey { fn pem_type(&self) -> String { format!("DUMMY KEY BASED ON {}", self.0.pem_type()) } + + fn to_bytes(&self) -> Vec { + self.to_bytes() + } } diff --git a/common/crypto/src/lib.rs b/common/crypto/src/lib.rs index f5401aead5..bb8a61b20c 100644 --- a/common/crypto/src/lib.rs +++ b/common/crypto/src/lib.rs @@ -1,17 +1,22 @@ pub mod encryption; pub mod identity; -// TODO: this trait will need to be moved elsewhere, probably to some 'persistence' crate -// but since it will need to be used by all identities, it's not really appropriate if it lived in nym-client +// TODO: ideally those trait should be moved to 'pemstore' crate, however, that would cause +// circular dependency. The best solution would be to remove dependency on 'crypto' from +// pemstore by using either dynamic dispatch or generics - perhaps this should be done +// at some point during one of refactors. pub trait PemStorableKey { fn pem_type(&self) -> String; + fn to_bytes(&self) -> Vec; } pub trait PemStorableKeyPair { type PrivatePemKey: PemStorableKey; type PublicPemKey: PemStorableKey; - fn private_pem_key(&self) -> &Self::PrivatePemKey; - fn public_pem_key(&self) -> &Self::PublicPemKey; + fn private_key(&self) -> &Self::PrivatePemKey; + fn public_key(&self) -> &Self::PublicPemKey; + + fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; }