|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] |
|
|
#![allow(unreachable_pub)] |
|
|
#[cfg(any( |
|
|
feature = "ecdsa", |
|
|
feature = "secp256k1", |
|
|
feature = "ed25519", |
|
|
feature = "rsa" |
|
|
))] |
|
|
mod proto { |
|
|
include!("generated/mod.rs"); |
|
|
pub(crate) use self::keys_proto::*; |
|
|
} |
|
|
|
|
|
#[cfg(feature = "ecdsa")] |
|
|
pub mod ecdsa; |
|
|
|
|
|
#[cfg(feature = "ed25519")] |
|
|
pub mod ed25519; |
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] |
|
|
pub mod rsa; |
|
|
|
|
|
#[cfg(feature = "secp256k1")] |
|
|
pub mod secp256k1; |
|
|
|
|
|
mod error; |
|
|
mod keypair; |
|
|
#[cfg(feature = "peerid")] |
|
|
mod peer_id; |
|
|
|
|
|
#[cfg(any( |
|
|
feature = "ecdsa", |
|
|
feature = "secp256k1", |
|
|
feature = "ed25519", |
|
|
feature = "rsa" |
|
|
))] |
|
|
impl zeroize::Zeroize for proto::PrivateKey { |
|
|
fn zeroize(&mut self) { |
|
|
self.Data.zeroize(); |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(any( |
|
|
feature = "ecdsa", |
|
|
feature = "secp256k1", |
|
|
feature = "ed25519", |
|
|
feature = "rsa" |
|
|
))] |
|
|
impl From<&PublicKey> for proto::PublicKey { |
|
|
fn from(key: &PublicKey) -> Self { |
|
|
match &key.publickey { |
|
|
#[cfg(feature = "ed25519")] |
|
|
keypair::PublicKeyInner::Ed25519(key) => proto::PublicKey { |
|
|
Type: proto::KeyType::Ed25519, |
|
|
Data: key.to_bytes().to_vec(), |
|
|
}, |
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] |
|
|
keypair::PublicKeyInner::Rsa(key) => proto::PublicKey { |
|
|
Type: proto::KeyType::RSA, |
|
|
Data: key.encode_x509(), |
|
|
}, |
|
|
#[cfg(feature = "secp256k1")] |
|
|
keypair::PublicKeyInner::Secp256k1(key) => proto::PublicKey { |
|
|
Type: proto::KeyType::Secp256k1, |
|
|
Data: key.to_bytes().to_vec(), |
|
|
}, |
|
|
#[cfg(feature = "ecdsa")] |
|
|
keypair::PublicKeyInner::Ecdsa(key) => proto::PublicKey { |
|
|
Type: proto::KeyType::ECDSA, |
|
|
Data: key.encode_der(), |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
pub use error::{DecodingError, OtherVariantError, SigningError}; |
|
|
pub use keypair::{Keypair, PublicKey}; |
|
|
#[cfg(feature = "peerid")] |
|
|
pub use peer_id::{ParseError, PeerId}; |
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq)] |
|
|
#[allow(clippy::upper_case_acronyms)] |
|
|
pub enum KeyType { |
|
|
Ed25519, |
|
|
RSA, |
|
|
Secp256k1, |
|
|
Ecdsa, |
|
|
} |
|
|
|
|
|
impl std::fmt::Display for KeyType { |
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
|
match self { |
|
|
KeyType::Ed25519 => f.write_str("Ed25519"), |
|
|
KeyType::RSA => f.write_str("RSA"), |
|
|
KeyType::Secp256k1 => f.write_str("Secp256k1"), |
|
|
KeyType::Ecdsa => f.write_str("Ecdsa"), |
|
|
} |
|
|
} |
|
|
} |
|
|
|