|
|
|
@ -20,6 +20,12 @@
|
|
|
|
|
|
|
|
|
|
use crate::error::OtherVariantError;
|
|
|
|
|
use crate::error::{DecodingError, SigningError};
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
use crate::proto;
|
|
|
|
|
use quick_protobuf::{BytesReader, Writer};
|
|
|
|
|
use std::convert::TryFrom;
|
|
|
|
@ -53,36 +59,25 @@ use crate::ecdsa;
|
|
|
|
|
/// let keypair = Keypair::rsa_from_pkcs8(&mut bytes);
|
|
|
|
|
/// ```
|
|
|
|
|
///
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Keypair {
|
|
|
|
|
keypair: KeyPairInner,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
|
|
|
pub enum Keypair {
|
|
|
|
|
enum KeyPairInner {
|
|
|
|
|
/// An Ed25519 keypair.
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `Keypair::try_into_ed25519` instead."
|
|
|
|
|
)]
|
|
|
|
|
Ed25519(ed25519::Keypair),
|
|
|
|
|
/// An RSA keypair.
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `Keypair::try_into_rsa` instead."
|
|
|
|
|
)]
|
|
|
|
|
Rsa(rsa::Keypair),
|
|
|
|
|
/// A Secp256k1 keypair.
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `Keypair::try_into_secp256k1` instead."
|
|
|
|
|
)]
|
|
|
|
|
Secp256k1(secp256k1::Keypair),
|
|
|
|
|
/// An ECDSA keypair.
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `Keypair::try_into_ecdsa` instead."
|
|
|
|
|
)]
|
|
|
|
|
Ecdsa(ecdsa::Keypair),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -90,22 +85,25 @@ impl Keypair {
|
|
|
|
|
/// Generate a new Ed25519 keypair.
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
pub fn generate_ed25519() -> Keypair {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Ed25519(ed25519::Keypair::generate())
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ed25519(ed25519::Keypair::generate()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generate a new Secp256k1 keypair.
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
pub fn generate_secp256k1() -> Keypair {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Secp256k1(secp256k1::Keypair::generate())
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Secp256k1(secp256k1::Keypair::generate()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generate a new ECDSA keypair.
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
pub fn generate_ecdsa() -> Keypair {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Ecdsa(ecdsa::Keypair::generate())
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ecdsa(ecdsa::Keypair::generate()),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
@ -166,8 +164,9 @@ impl Keypair {
|
|
|
|
|
/// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
pub fn rsa_from_pkcs8(pkcs8_der: &mut [u8]) -> Result<Keypair, DecodingError> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
rsa::Keypair::from_pkcs8(pkcs8_der).map(Keypair::Rsa)
|
|
|
|
|
rsa::Keypair::try_decode_pkcs8(pkcs8_der).map(|kp| Keypair {
|
|
|
|
|
keypair: KeyPairInner::Rsa(kp),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decode a keypair from a DER-encoded Secp256k1 secret key in an ECPrivateKey
|
|
|
|
@ -176,147 +175,198 @@ impl Keypair {
|
|
|
|
|
/// [RFC5915]: https://tools.ietf.org/html/rfc5915
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
pub fn secp256k1_from_der(der: &mut [u8]) -> Result<Keypair, DecodingError> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
secp256k1::SecretKey::from_der(der)
|
|
|
|
|
.map(|sk| Keypair::Secp256k1(secp256k1::Keypair::from(sk)))
|
|
|
|
|
secp256k1::SecretKey::from_der(der).map(|sk| Keypair {
|
|
|
|
|
keypair: KeyPairInner::Secp256k1(secp256k1::Keypair::from(sk)),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
pub fn ed25519_from_bytes(bytes: impl AsMut<[u8]>) -> Result<Keypair, DecodingError> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Ok(Keypair::Ed25519(ed25519::Keypair::from(
|
|
|
|
|
ed25519::SecretKey::from_bytes(bytes)?,
|
|
|
|
|
)))
|
|
|
|
|
Ok(Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ed25519(ed25519::Keypair::from(
|
|
|
|
|
ed25519::SecretKey::try_from_bytes(bytes)?,
|
|
|
|
|
)),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sign a message using the private key of this keypair, producing
|
|
|
|
|
/// a signature that can be verified using the corresponding public key.
|
|
|
|
|
pub fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
|
|
|
|
|
use Keypair::*;
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
match self.keypair {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Ed25519(ref pair) => Ok(pair.sign(msg)),
|
|
|
|
|
KeyPairInner::Ed25519(ref pair) => Ok(pair.sign(msg)),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Rsa(ref pair) => pair.sign(msg),
|
|
|
|
|
KeyPairInner::Rsa(ref pair) => pair.sign(msg),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Secp256k1(ref pair) => pair.secret().sign(msg),
|
|
|
|
|
KeyPairInner::Secp256k1(ref pair) => pair.secret().sign(msg),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Ecdsa(ref pair) => Ok(pair.secret().sign(msg)),
|
|
|
|
|
KeyPairInner::Ecdsa(ref pair) => Ok(pair.secret().sign(msg)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Get the public key of this keypair.
|
|
|
|
|
pub fn public(&self) -> PublicKey {
|
|
|
|
|
use Keypair::*;
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
match self.keypair {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Ed25519(pair) => PublicKey::Ed25519(pair.public()),
|
|
|
|
|
KeyPairInner::Ed25519(ref pair) => PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ed25519(pair.public()),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Rsa(pair) => PublicKey::Rsa(pair.public()),
|
|
|
|
|
KeyPairInner::Rsa(ref pair) => PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Rsa(pair.public()),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
|
|
|
|
|
KeyPairInner::Secp256k1(ref pair) => PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Secp256k1(pair.public().clone()),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Ecdsa(pair) => PublicKey::Ecdsa(pair.public().clone()),
|
|
|
|
|
KeyPairInner::Ecdsa(ref pair) => PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ecdsa(pair.public().clone()),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Encode a private key as protobuf structure.
|
|
|
|
|
pub fn to_protobuf_encoding(&self) -> Result<Vec<u8>, DecodingError> {
|
|
|
|
|
use quick_protobuf::MessageWrite;
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
{
|
|
|
|
|
use quick_protobuf::MessageWrite;
|
|
|
|
|
let pk: proto::PrivateKey = match self.keypair {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
KeyPairInner::Ed25519(ref data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::Ed25519,
|
|
|
|
|
Data: data.to_bytes().to_vec(),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
KeyPairInner::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
KeyPairInner::Secp256k1(ref data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::Secp256k1,
|
|
|
|
|
Data: data.secret().to_bytes().to_vec(),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
KeyPairInner::Ecdsa(ref data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::ECDSA,
|
|
|
|
|
Data: data.secret().encode_der(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
let pk: proto::PrivateKey = match self {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Self::Ed25519(data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::Ed25519,
|
|
|
|
|
Data: data.to_bytes().to_vec(),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Self::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Self::Secp256k1(data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::Secp256k1,
|
|
|
|
|
Data: data.secret().to_bytes().to_vec(),
|
|
|
|
|
},
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Self::Ecdsa(data) => proto::PrivateKey {
|
|
|
|
|
Type: proto::KeyType::ECDSA,
|
|
|
|
|
Data: data.secret().encode_der(),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
let mut buf = Vec::with_capacity(pk.get_size());
|
|
|
|
|
let mut writer = Writer::new(&mut buf);
|
|
|
|
|
pk.write_message(&mut writer).expect("Encoding to succeed");
|
|
|
|
|
|
|
|
|
|
let mut buf = Vec::with_capacity(pk.get_size());
|
|
|
|
|
let mut writer = Writer::new(&mut buf);
|
|
|
|
|
pk.write_message(&mut writer).expect("Encoding to succeed");
|
|
|
|
|
Ok(buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(buf)
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
)))]
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decode a private key from a protobuf structure and parse it as a [`Keypair`].
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<Keypair, DecodingError> {
|
|
|
|
|
use quick_protobuf::MessageRead;
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
{
|
|
|
|
|
use quick_protobuf::MessageRead;
|
|
|
|
|
|
|
|
|
|
let mut reader = BytesReader::from_bytes(bytes);
|
|
|
|
|
let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes)
|
|
|
|
|
.map_err(|e| DecodingError::bad_protobuf("private key bytes", e))
|
|
|
|
|
.map(zeroize::Zeroizing::new)?;
|
|
|
|
|
let mut reader = BytesReader::from_bytes(bytes);
|
|
|
|
|
let mut private_key = proto::PrivateKey::from_reader(&mut reader, bytes)
|
|
|
|
|
.map_err(|e| DecodingError::bad_protobuf("private key bytes", e))
|
|
|
|
|
.map(zeroize::Zeroizing::new)?;
|
|
|
|
|
|
|
|
|
|
#[allow(deprecated, unreachable_code)]
|
|
|
|
|
match private_key.Type {
|
|
|
|
|
proto::KeyType::Ed25519 => {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
return ed25519::Keypair::try_from_bytes(&mut private_key.Data)
|
|
|
|
|
.map(Keypair::Ed25519);
|
|
|
|
|
Err(DecodingError::missing_feature("ed25519"))
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")),
|
|
|
|
|
proto::KeyType::Secp256k1 => {
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
return secp256k1::SecretKey::try_from_bytes(&mut private_key.Data)
|
|
|
|
|
.map(|key| Keypair::Secp256k1(key.into()));
|
|
|
|
|
Err(DecodingError::missing_feature("secp256k1"))
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::ECDSA => {
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
return ecdsa::SecretKey::try_decode_der(&mut private_key.Data)
|
|
|
|
|
.map(|key| Keypair::Ecdsa(key.into()));
|
|
|
|
|
Err(DecodingError::missing_feature("ecdsa"))
|
|
|
|
|
#[allow(unreachable_code)]
|
|
|
|
|
match private_key.Type {
|
|
|
|
|
proto::KeyType::Ed25519 => {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
return ed25519::Keypair::try_from_bytes(&mut private_key.Data).map(|sk| {
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ed25519(sk),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Err(DecodingError::missing_feature("ed25519"))
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")),
|
|
|
|
|
proto::KeyType::Secp256k1 => {
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
return secp256k1::SecretKey::try_from_bytes(&mut private_key.Data).map(
|
|
|
|
|
|key| Keypair {
|
|
|
|
|
keypair: KeyPairInner::Secp256k1(key.into()),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Err(DecodingError::missing_feature("secp256k1"))
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::ECDSA => {
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
return ecdsa::SecretKey::try_decode_der(&mut private_key.Data).map(|key| {
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ecdsa(key.into()),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Err(DecodingError::missing_feature("ecdsa"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
)))]
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
impl From<ecdsa::Keypair> for Keypair {
|
|
|
|
|
fn from(kp: ecdsa::Keypair) -> Self {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Ecdsa(kp)
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ecdsa(kp),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
impl From<ed25519::Keypair> for Keypair {
|
|
|
|
|
fn from(kp: ed25519::Keypair) -> Self {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Ed25519(kp)
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Ed25519(kp),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
impl From<secp256k1::Keypair> for Keypair {
|
|
|
|
|
fn from(kp: secp256k1::Keypair) -> Self {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Secp256k1(kp)
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Secp256k1(kp),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
impl From<rsa::Keypair> for Keypair {
|
|
|
|
|
fn from(kp: rsa::Keypair) -> Self {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
Keypair::Rsa(kp)
|
|
|
|
|
Keypair {
|
|
|
|
|
keypair: KeyPairInner::Rsa(kp),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -325,15 +375,14 @@ impl TryInto<ed25519::Keypair> for Keypair {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<ed25519::Keypair, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
Keypair::Ed25519(inner) => Ok(inner),
|
|
|
|
|
match self.keypair {
|
|
|
|
|
KeyPairInner::Ed25519(inner) => Ok(inner),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Keypair::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Keypair::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Keypair::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -343,15 +392,14 @@ impl TryInto<ecdsa::Keypair> for Keypair {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<ecdsa::Keypair, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
Keypair::Ecdsa(inner) => Ok(inner),
|
|
|
|
|
match self.keypair {
|
|
|
|
|
KeyPairInner::Ecdsa(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Keypair::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Keypair::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Keypair::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -361,15 +409,14 @@ impl TryInto<secp256k1::Keypair> for Keypair {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<secp256k1::Keypair, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
Keypair::Secp256k1(inner) => Ok(inner),
|
|
|
|
|
match self.keypair {
|
|
|
|
|
KeyPairInner::Secp256k1(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Keypair::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Keypair::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
KeyPairInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Keypair::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -379,71 +426,57 @@ impl TryInto<rsa::Keypair> for Keypair {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<rsa::Keypair, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
Keypair::Rsa(inner) => Ok(inner),
|
|
|
|
|
match self.keypair {
|
|
|
|
|
KeyPairInner::Rsa(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Keypair::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
KeyPairInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Keypair::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
KeyPairInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Keypair::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
KeyPairInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The public key of a node's identity keypair.
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
|
|
|
pub enum PublicKey {
|
|
|
|
|
pub(crate) enum PublicKeyInner {
|
|
|
|
|
/// A public Ed25519 key.
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ed25519` instead."
|
|
|
|
|
)]
|
|
|
|
|
Ed25519(ed25519::PublicKey),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
/// A public RSA key.
|
|
|
|
|
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_rsa` instead."
|
|
|
|
|
)]
|
|
|
|
|
Rsa(rsa::PublicKey),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
/// A public Secp256k1 key.
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_secp256k1` instead."
|
|
|
|
|
)]
|
|
|
|
|
Secp256k1(secp256k1::PublicKey),
|
|
|
|
|
/// A public ECDSA key.
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
#[deprecated(
|
|
|
|
|
since = "0.1.0",
|
|
|
|
|
note = "This enum will be made opaque in the future, use `PublicKey::from` and `PublicKey::into_ecdsa` instead."
|
|
|
|
|
)]
|
|
|
|
|
Ecdsa(ecdsa::PublicKey),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The public key of a node's identity keypair.
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
|
|
|
pub struct PublicKey {
|
|
|
|
|
pub(crate) publickey: PublicKeyInner,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl PublicKey {
|
|
|
|
|
/// Verify a signature for a message using this public key, i.e. check
|
|
|
|
|
/// that the signature has been produced by the corresponding
|
|
|
|
|
/// private key (authenticity), and that the message has not been
|
|
|
|
|
/// tampered with (integrity).
|
|
|
|
|
#[must_use]
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
|
|
|
|
|
use PublicKey::*;
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
match self.publickey {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
Ed25519(pk) => pk.verify(msg, sig),
|
|
|
|
|
PublicKeyInner::Ed25519(ref pk) => pk.verify(msg, sig),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
Rsa(pk) => pk.verify(msg, sig),
|
|
|
|
|
PublicKeyInner::Rsa(ref pk) => pk.verify(msg, sig),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
Secp256k1(pk) => pk.verify(msg, sig),
|
|
|
|
|
PublicKeyInner::Secp256k1(ref pk) => pk.verify(msg, sig),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
Ecdsa(pk) => pk.verify(msg, sig),
|
|
|
|
|
PublicKeyInner::Ecdsa(ref pk) => pk.verify(msg, sig),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -509,17 +542,32 @@ impl PublicKey {
|
|
|
|
|
/// Encode the public key into a protobuf structure for storage or
|
|
|
|
|
/// exchange with other nodes.
|
|
|
|
|
pub fn encode_protobuf(&self) -> Vec<u8> {
|
|
|
|
|
use quick_protobuf::MessageWrite;
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
{
|
|
|
|
|
use quick_protobuf::MessageWrite;
|
|
|
|
|
let public_key = proto::PublicKey::from(self);
|
|
|
|
|
|
|
|
|
|
let public_key = proto::PublicKey::from(self);
|
|
|
|
|
let mut buf = Vec::with_capacity(public_key.get_size());
|
|
|
|
|
let mut writer = Writer::new(&mut buf);
|
|
|
|
|
public_key
|
|
|
|
|
.write_message(&mut writer)
|
|
|
|
|
.expect("Encoding to succeed");
|
|
|
|
|
|
|
|
|
|
let mut buf = Vec::with_capacity(public_key.get_size());
|
|
|
|
|
let mut writer = Writer::new(&mut buf);
|
|
|
|
|
public_key
|
|
|
|
|
.write_message(&mut writer)
|
|
|
|
|
.expect("Encoding to succeed");
|
|
|
|
|
buf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
)))]
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Decode a public key from a protobuf structure, e.g. read from storage
|
|
|
|
@ -533,15 +581,31 @@ impl PublicKey {
|
|
|
|
|
|
|
|
|
|
/// Decode a public key from a protobuf structure, e.g. read from storage
|
|
|
|
|
/// or received from another node.
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
pub fn try_decode_protobuf(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
|
|
|
|
|
use quick_protobuf::MessageRead;
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
{
|
|
|
|
|
use quick_protobuf::MessageRead;
|
|
|
|
|
let mut reader = BytesReader::from_bytes(bytes);
|
|
|
|
|
|
|
|
|
|
let mut reader = BytesReader::from_bytes(bytes);
|
|
|
|
|
let pubkey = proto::PublicKey::from_reader(&mut reader, bytes)
|
|
|
|
|
.map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?;
|
|
|
|
|
|
|
|
|
|
let pubkey = proto::PublicKey::from_reader(&mut reader, bytes)
|
|
|
|
|
.map_err(|e| DecodingError::bad_protobuf("public key bytes", e))?;
|
|
|
|
|
pubkey.try_into()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pubkey.try_into()
|
|
|
|
|
#[cfg(not(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
)))]
|
|
|
|
|
unreachable!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert the `PublicKey` into the corresponding `PeerId`.
|
|
|
|
@ -551,41 +615,57 @@ impl PublicKey {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(any(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa"
|
|
|
|
|
))]
|
|
|
|
|
impl TryFrom<proto::PublicKey> for PublicKey {
|
|
|
|
|
type Error = DecodingError;
|
|
|
|
|
|
|
|
|
|
fn try_from(pubkey: proto::PublicKey) -> Result<Self, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match pubkey.Type {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
proto::KeyType::Ed25519 => {
|
|
|
|
|
ed25519::PublicKey::decode(&pubkey.Data).map(PublicKey::Ed25519)
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::Ed25519 => Ok(ed25519::PublicKey::try_from_bytes(&pubkey.Data).map(
|
|
|
|
|
|kp| PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ed25519(kp),
|
|
|
|
|
},
|
|
|
|
|
)?),
|
|
|
|
|
#[cfg(not(feature = "ed25519"))]
|
|
|
|
|
proto::KeyType::Ed25519 => {
|
|
|
|
|
log::debug!("support for ed25519 was disabled at compile-time");
|
|
|
|
|
Err(DecodingError::missing_feature("ed25519"))
|
|
|
|
|
}
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
proto::KeyType::RSA => rsa::PublicKey::decode_x509(&pubkey.Data).map(PublicKey::Rsa),
|
|
|
|
|
proto::KeyType::RSA => {
|
|
|
|
|
Ok(
|
|
|
|
|
rsa::PublicKey::try_decode_x509(&pubkey.Data).map(|kp| PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Rsa(kp),
|
|
|
|
|
})?,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
#[cfg(any(not(feature = "rsa"), target_arch = "wasm32"))]
|
|
|
|
|
proto::KeyType::RSA => {
|
|
|
|
|
log::debug!("support for RSA was disabled at compile-time");
|
|
|
|
|
Err(DecodingError::missing_feature("rsa"))
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
proto::KeyType::Secp256k1 => {
|
|
|
|
|
secp256k1::PublicKey::decode(&pubkey.Data).map(PublicKey::Secp256k1)
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::Secp256k1 => Ok(secp256k1::PublicKey::try_from_bytes(&pubkey.Data)
|
|
|
|
|
.map(|kp| PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Secp256k1(kp),
|
|
|
|
|
})?),
|
|
|
|
|
#[cfg(not(feature = "secp256k1"))]
|
|
|
|
|
proto::KeyType::Secp256k1 => {
|
|
|
|
|
log::debug!("support for secp256k1 was disabled at compile-time");
|
|
|
|
|
Err(DecodingError::missing_feature("secp256k1"))
|
|
|
|
|
}
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
proto::KeyType::ECDSA => {
|
|
|
|
|
ecdsa::PublicKey::decode_der(&pubkey.Data).map(PublicKey::Ecdsa)
|
|
|
|
|
}
|
|
|
|
|
proto::KeyType::ECDSA => Ok(ecdsa::PublicKey::try_decode_der(&pubkey.Data).map(
|
|
|
|
|
|kp| PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ecdsa(kp),
|
|
|
|
|
},
|
|
|
|
|
)?),
|
|
|
|
|
#[cfg(not(feature = "ecdsa"))]
|
|
|
|
|
proto::KeyType::ECDSA => {
|
|
|
|
|
log::debug!("support for ECDSA was disabled at compile-time");
|
|
|
|
@ -600,15 +680,14 @@ impl TryInto<ed25519::PublicKey> for PublicKey {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<ed25519::PublicKey, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
PublicKey::Ed25519(inner) => Ok(inner),
|
|
|
|
|
match self.publickey {
|
|
|
|
|
PublicKeyInner::Ed25519(inner) => Ok(inner),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
PublicKey::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -618,15 +697,14 @@ impl TryInto<ecdsa::PublicKey> for PublicKey {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<ecdsa::PublicKey, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
PublicKey::Ecdsa(inner) => Ok(inner),
|
|
|
|
|
match self.publickey {
|
|
|
|
|
PublicKeyInner::Ecdsa(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
PublicKey::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
PublicKey::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -636,15 +714,14 @@ impl TryInto<secp256k1::PublicKey> for PublicKey {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<secp256k1::PublicKey, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
PublicKey::Secp256k1(inner) => Ok(inner),
|
|
|
|
|
match self.publickey {
|
|
|
|
|
PublicKeyInner::Secp256k1(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
PublicKey::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
PublicKey::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
PublicKeyInner::Rsa(_) => Err(OtherVariantError::new(crate::KeyType::RSA)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -654,15 +731,14 @@ impl TryInto<rsa::PublicKey> for PublicKey {
|
|
|
|
|
type Error = OtherVariantError;
|
|
|
|
|
|
|
|
|
|
fn try_into(self) -> Result<rsa::PublicKey, Self::Error> {
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
match self {
|
|
|
|
|
PublicKey::Rsa(inner) => Ok(inner),
|
|
|
|
|
match self.publickey {
|
|
|
|
|
PublicKeyInner::Rsa(inner) => Ok(inner),
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
PublicKey::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
PublicKeyInner::Ed25519(_) => Err(OtherVariantError::new(crate::KeyType::Ed25519)),
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
PublicKeyInner::Secp256k1(_) => Err(OtherVariantError::new(crate::KeyType::Secp256k1)),
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
PublicKeyInner::Ecdsa(_) => Err(OtherVariantError::new(crate::KeyType::Ecdsa)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -670,32 +746,36 @@ impl TryInto<rsa::PublicKey> for PublicKey {
|
|
|
|
|
#[cfg(feature = "ed25519")]
|
|
|
|
|
impl From<ed25519::PublicKey> for PublicKey {
|
|
|
|
|
fn from(key: ed25519::PublicKey) -> Self {
|
|
|
|
|
#[allow(deprecated)] // TODO: Remove when PublicKey::Ed25519 is made opaque
|
|
|
|
|
PublicKey::Ed25519(key)
|
|
|
|
|
PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ed25519(key),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "secp256k1")]
|
|
|
|
|
impl From<secp256k1::PublicKey> for PublicKey {
|
|
|
|
|
fn from(key: secp256k1::PublicKey) -> Self {
|
|
|
|
|
#[allow(deprecated)] // TODO: Remove when PublicKey::Secp256k1 is made opaque
|
|
|
|
|
PublicKey::Secp256k1(key)
|
|
|
|
|
PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Secp256k1(key),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(feature = "ecdsa")]
|
|
|
|
|
impl From<ecdsa::PublicKey> for PublicKey {
|
|
|
|
|
fn from(key: ecdsa::PublicKey) -> Self {
|
|
|
|
|
#[allow(deprecated)] // TODO: Remove when PublicKey::Ecdsa is made opaque
|
|
|
|
|
PublicKey::Ecdsa(key)
|
|
|
|
|
PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Ecdsa(key),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
|
|
|
|
impl From<rsa::PublicKey> for PublicKey {
|
|
|
|
|
fn from(key: rsa::PublicKey) -> Self {
|
|
|
|
|
#[allow(deprecated)] // TODO: Remove when PublicKey::Rsa is made opaque
|
|
|
|
|
PublicKey::Rsa(key)
|
|
|
|
|
PublicKey {
|
|
|
|
|
publickey: PublicKeyInner::Rsa(key),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -773,7 +853,13 @@ mod tests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
#[cfg(feature = "peerid")]
|
|
|
|
|
#[cfg(all(
|
|
|
|
|
feature = "ecdsa",
|
|
|
|
|
feature = "secp256k1",
|
|
|
|
|
feature = "ed25519",
|
|
|
|
|
feature = "rsa",
|
|
|
|
|
feature = "peerid"
|
|
|
|
|
))]
|
|
|
|
|
fn keypair_from_protobuf_encoding() {
|
|
|
|
|
// E.g. retrieved from an IPFS config file.
|
|
|
|
|
let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA=";
|
|
|
|
|