libp2prs_core/
identity.rs1pub mod ed25519;
24#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
25pub mod rsa;
26#[cfg(feature = "secp256k1")]
27pub mod secp256k1;
28
29pub mod error;
30
31use self::error::*;
32use crate::{keys_proto, PeerId};
33
34#[allow(clippy::large_enum_variant)]
52#[derive(Clone)]
53pub enum Keypair {
54 Ed25519(ed25519::Keypair),
56 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
57 Rsa(rsa::Keypair),
59 #[cfg(feature = "secp256k1")]
61 Secp256k1(secp256k1::Keypair),
62}
63
64impl Keypair {
65 pub fn generate_ed25519_fixed() -> Keypair {
67 Keypair::Ed25519(ed25519::Keypair::generate_fixed())
68 }
69
70 pub fn generate_ed25519() -> Keypair {
72 Keypair::Ed25519(ed25519::Keypair::generate())
73 }
74
75 #[cfg(feature = "secp256k1")]
77 pub fn generate_secp256k1() -> Keypair {
78 Keypair::Secp256k1(secp256k1::Keypair::generate())
79 }
80
81 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
86 pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
87 rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
88 }
89
90 #[cfg(feature = "secp256k1")]
95 pub fn secp256k1_from_der(der: &mut [u8]) -> Result<Keypair, DecodingError> {
96 secp256k1::SecretKey::from_der(der).map(|sk| Keypair::Secp256k1(secp256k1::Keypair::from(sk)))
97 }
98
99 pub fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
102 use Keypair::*;
103 match self {
104 Ed25519(ref pair) => Ok(pair.sign(msg)),
105 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
106 Rsa(ref pair) => pair.sign(msg),
107 #[cfg(feature = "secp256k1")]
108 Secp256k1(ref pair) => pair.secret().sign(msg),
109 }
110 }
111
112 pub fn public(&self) -> PublicKey {
114 use Keypair::*;
115 match self {
116 Ed25519(pair) => PublicKey::Ed25519(pair.public()),
117 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
118 Rsa(pair) => PublicKey::Rsa(pair.public()),
119 #[cfg(feature = "secp256k1")]
120 Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
121 }
122 }
123}
124
125#[derive(Clone, Debug, PartialEq, Eq)]
127pub enum PublicKey {
128 Ed25519(ed25519::PublicKey),
130 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
131 Rsa(rsa::PublicKey),
133 #[cfg(feature = "secp256k1")]
134 Secp256k1(secp256k1::PublicKey),
136}
137
138impl PublicKey {
139 pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
144 use PublicKey::*;
145 match self {
146 Ed25519(pk) => pk.verify(msg, sig),
147 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
148 Rsa(pk) => pk.verify(msg, sig),
149 #[cfg(feature = "secp256k1")]
150 Secp256k1(pk) => pk.verify(msg, sig),
151 }
152 }
153
154 pub fn into_protobuf_encoding(self) -> Vec<u8> {
157 use prost::Message;
158
159 let public_key = match self {
160 PublicKey::Ed25519(key) => keys_proto::PublicKey {
161 r#type: keys_proto::KeyType::Ed25519 as i32,
162 data: key.encode().to_vec(),
163 },
164 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
165 PublicKey::Rsa(key) => keys_proto::PublicKey {
166 r#type: keys_proto::KeyType::Rsa as i32,
167 data: key.encode_x509(),
168 },
169 #[cfg(feature = "secp256k1")]
170 PublicKey::Secp256k1(key) => keys_proto::PublicKey {
171 r#type: keys_proto::KeyType::Secp256k1 as i32,
172 data: key.encode().to_vec(),
173 },
174 };
175
176 let mut buf = Vec::with_capacity(public_key.encoded_len());
177 public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
178 buf
179 }
180
181 pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
184 use prost::Message;
185
186 #[allow(unused_mut)] let mut pubkey = keys_proto::PublicKey::decode(bytes).map_err(|e| DecodingError::new("Protobuf").source(e))?;
188
189 let key_type = keys_proto::KeyType::from_i32(pubkey.r#type)
190 .ok_or_else(|| DecodingError::new(format!("unknown key type: {}", pubkey.r#type)))?;
191
192 match key_type {
193 keys_proto::KeyType::Ed25519 => ed25519::PublicKey::decode(&pubkey.data).map(PublicKey::Ed25519),
194 #[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
195 keys_proto::KeyType::Rsa => rsa::PublicKey::decode_x509(&pubkey.data).map(PublicKey::Rsa),
196 #[cfg(any(target_os = "emscripten", target_os = "unknown"))]
197 keys_proto::KeyType::Rsa => {
198 log::debug!("support for RSA was disabled at compile-time");
199 Err(DecodingError::new("Unsupported"))
200 }
201 #[cfg(feature = "secp256k1")]
202 keys_proto::KeyType::Secp256k1 => secp256k1::PublicKey::decode(&pubkey.data).map(PublicKey::Secp256k1),
203 #[cfg(not(feature = "secp256k1"))]
204 keys_proto::KeyType::Secp256k1 => {
205 log::debug!("support for secp256k1 was disabled at compile-time");
206 Err("Unsupported".to_string().into())
207 }
208 }
209 }
210
211 pub fn into_peer_id(self) -> PeerId {
213 self.into()
214 }
215}