use crate::error::OtherVariantError;
use crate::error::{DecodingError, SigningError};
use crate::{proto, KeyType};
use quick_protobuf::{BytesReader, Writer};
use std::convert::TryFrom;
#[cfg(feature = "ed25519")]
use crate::ed25519;
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
use crate::rsa;
#[cfg(feature = "secp256k1")]
use crate::secp256k1;
#[cfg(feature = "ecdsa")]
use crate::ecdsa;
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum 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),
#[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),
#[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),
#[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),
}
impl Keypair {
#[cfg(feature = "ed25519")]
pub fn generate_ed25519() -> Keypair {
#[allow(deprecated)]
Keypair::Ed25519(ed25519::Keypair::generate())
}
#[cfg(feature = "secp256k1")]
pub fn generate_secp256k1() -> Keypair {
#[allow(deprecated)]
Keypair::Secp256k1(secp256k1::Keypair::generate())
}
#[cfg(feature = "ecdsa")]
pub fn generate_ecdsa() -> Keypair {
#[allow(deprecated)]
Keypair::Ecdsa(ecdsa::Keypair::generate())
}
#[cfg(feature = "ed25519")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ed25519` instead."
)]
pub fn into_ed25519(self) -> Option<ed25519::Keypair> {
self.try_into().ok()
}
#[cfg(feature = "ed25519")]
pub fn try_into_ed25519(self) -> Result<ed25519::Keypair, OtherVariantError> {
self.try_into()
}
#[cfg(feature = "secp256k1")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_secp256k1` instead."
)]
pub fn into_secp256k1(self) -> Option<secp256k1::Keypair> {
self.try_into().ok()
}
#[cfg(feature = "secp256k1")]
pub fn try_into_secp256k1(self) -> Result<secp256k1::Keypair, OtherVariantError> {
self.try_into()
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_rsa` instead."
)]
pub fn into_rsa(self) -> Option<rsa::Keypair> {
self.try_into().ok()
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn try_into_rsa(self) -> Result<rsa::Keypair, OtherVariantError> {
self.try_into()
}
#[cfg(feature = "ecdsa")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ecdsa` instead."
)]
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
self.try_into().ok()
}
#[cfg(feature = "ecdsa")]
pub fn try_into_ecdsa(self) -> Result<ecdsa::Keypair, OtherVariantError> {
self.try_into()
}
#[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)
}
#[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)))
}
#[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)?,
)))
}
pub fn sign(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
use Keypair::*;
#[allow(deprecated)]
match self {
#[cfg(feature = "ed25519")]
Ed25519(ref pair) => Ok(pair.sign(msg)),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(ref pair) => pair.sign(msg),
#[cfg(feature = "secp256k1")]
Secp256k1(ref pair) => pair.secret().sign(msg),
#[cfg(feature = "ecdsa")]
Ecdsa(ref pair) => Ok(pair.secret().sign(msg)),
}
}
pub fn public(&self) -> PublicKey {
use Keypair::*;
#[allow(deprecated)]
match self {
#[cfg(feature = "ed25519")]
Ed25519(pair) => PublicKey::Ed25519(pair.public()),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pair) => PublicKey::Rsa(pair.public()),
#[cfg(feature = "secp256k1")]
Secp256k1(pair) => PublicKey::Secp256k1(pair.public().clone()),
#[cfg(feature = "ecdsa")]
Ecdsa(pair) => PublicKey::Ecdsa(pair.public().clone()),
}
}
#[cfg_attr(
not(feature = "ed25519"),
allow(unreachable_code, unused_variables, unused_mut)
)]
pub fn to_protobuf_encoding(&self) -> Result<Vec<u8>, DecodingError> {
use quick_protobuf::MessageWrite;
#[cfg(not(feature = "ed25519"))]
return Err(DecodingError::missing_feature("ed25519"));
#[allow(deprecated)]
let pk: proto::PrivateKey = match self {
#[cfg(feature = "ed25519")]
Self::Ed25519(data) => proto::PrivateKey {
Type: proto::KeyType::Ed25519,
Data: data.encode().to_vec(),
},
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Self::Rsa(_) => return Err(DecodingError::encoding_unsupported("RSA")),
#[cfg(feature = "secp256k1")]
Self::Secp256k1(_) => return Err(DecodingError::encoding_unsupported("secp256k1")),
#[cfg(feature = "ecdsa")]
Self::Ecdsa(_) => return Err(DecodingError::encoding_unsupported("ECDSA")),
};
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)
}
#[cfg_attr(not(feature = "ed25519"), allow(unused_mut))]
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<Keypair, DecodingError> {
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)?;
match private_key.Type {
#[cfg(feature = "ed25519")]
proto::KeyType::Ed25519 =>
{
#[allow(deprecated)]
ed25519::Keypair::decode(&mut private_key.Data).map(Keypair::Ed25519)
}
#[cfg(not(feature = "ed25519"))]
proto::KeyType::Ed25519 => Err(DecodingError::missing_feature("ed25519")),
proto::KeyType::RSA => Err(DecodingError::decoding_unsupported("RSA")),
proto::KeyType::Secp256k1 => Err(DecodingError::decoding_unsupported("secp256k1")),
proto::KeyType::ECDSA => Err(DecodingError::decoding_unsupported("ECDSA")),
}
}
}
#[cfg(feature = "ecdsa")]
impl From<ecdsa::Keypair> for Keypair {
fn from(kp: ecdsa::Keypair) -> Self {
#[allow(deprecated)]
Keypair::Ecdsa(kp)
}
}
#[cfg(feature = "ed25519")]
impl From<ed25519::Keypair> for Keypair {
fn from(kp: ed25519::Keypair) -> Self {
#[allow(deprecated)]
Keypair::Ed25519(kp)
}
}
#[cfg(feature = "secp256k1")]
impl From<secp256k1::Keypair> for Keypair {
fn from(kp: secp256k1::Keypair) -> Self {
#[allow(deprecated)]
Keypair::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)
}
}
#[cfg(feature = "ed25519")]
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),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "secp256k1")]
Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
#[cfg(feature = "ecdsa")]
Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[cfg(feature = "ecdsa")]
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),
#[cfg(feature = "ed25519")]
Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "secp256k1")]
Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
}
}
}
#[cfg(feature = "secp256k1")]
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),
#[cfg(feature = "ed25519")]
Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "ecdsa")]
Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
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),
#[cfg(feature = "ed25519")]
Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(feature = "secp256k1")]
Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
#[cfg(feature = "ecdsa")]
Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum PublicKey {
#[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")))]
#[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")]
#[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),
#[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),
}
impl PublicKey {
#[must_use]
pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
use PublicKey::*;
#[allow(deprecated)]
match self {
#[cfg(feature = "ed25519")]
Ed25519(pk) => pk.verify(msg, sig),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
Rsa(pk) => pk.verify(msg, sig),
#[cfg(feature = "secp256k1")]
Secp256k1(pk) => pk.verify(msg, sig),
#[cfg(feature = "ecdsa")]
Ecdsa(pk) => pk.verify(msg, sig),
}
}
#[cfg(feature = "ed25519")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ed25519` instead."
)]
pub fn into_ed25519(self) -> Option<ed25519::PublicKey> {
self.try_into().ok()
}
#[cfg(feature = "ed25519")]
pub fn try_into_ed25519(self) -> Result<ed25519::PublicKey, OtherVariantError> {
self.try_into()
}
#[cfg(feature = "secp256k1")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_secp256k1` instead."
)]
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
self.try_into().ok()
}
#[cfg(feature = "secp256k1")]
pub fn try_into_secp256k1(self) -> Result<secp256k1::PublicKey, OtherVariantError> {
self.try_into()
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_rsa` instead."
)]
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
self.try_into().ok()
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
pub fn try_into_rsa(self) -> Result<rsa::PublicKey, OtherVariantError> {
self.try_into()
}
#[cfg(feature = "ecdsa")]
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ecdsa` instead."
)]
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
self.try_into().ok()
}
#[cfg(feature = "ecdsa")]
pub fn try_into_ecdsa(self) -> Result<ecdsa::PublicKey, OtherVariantError> {
self.try_into()
}
#[deprecated(note = "Renamed to `PublicKey::encode_protobuf`.")]
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
Self::encode_protobuf(self)
}
pub fn encode_protobuf(&self) -> Vec<u8> {
use quick_protobuf::MessageWrite;
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");
buf
}
#[deprecated(
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead."
)]
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
Self::try_decode_protobuf(bytes)
}
pub fn try_decode_protobuf(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
use quick_protobuf::MessageRead;
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))?;
pubkey.try_into()
}
#[cfg(feature = "peerid")]
pub fn to_peer_id(&self) -> crate::PeerId {
self.into()
}
}
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)
}
#[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),
#[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)
}
#[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)
}
#[cfg(not(feature = "ecdsa"))]
proto::KeyType::ECDSA => {
log::debug!("support for ECDSA was disabled at compile-time");
Err(DecodingError::missing_feature("ecdsa"))
}
}
}
}
#[cfg(feature = "ed25519")]
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),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "secp256k1")]
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
#[cfg(feature = "ecdsa")]
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[cfg(feature = "ecdsa")]
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),
#[cfg(feature = "ed25519")]
PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "secp256k1")]
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
}
}
}
#[cfg(feature = "secp256k1")]
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),
#[cfg(feature = "ed25519")]
PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)),
#[cfg(feature = "ecdsa")]
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
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),
#[cfg(feature = "ed25519")]
PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)),
#[cfg(feature = "secp256k1")]
PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)),
#[cfg(feature = "ecdsa")]
PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)),
}
}
}
#[cfg(feature = "ed25519")]
impl From<ed25519::PublicKey> for PublicKey {
fn from(key: ed25519::PublicKey) -> Self {
#[allow(deprecated)] PublicKey::Ed25519(key)
}
}
#[cfg(feature = "secp256k1")]
impl From<secp256k1::PublicKey> for PublicKey {
fn from(key: secp256k1::PublicKey) -> Self {
#[allow(deprecated)] PublicKey::Secp256k1(key)
}
}
#[cfg(feature = "ecdsa")]
impl From<ecdsa::PublicKey> for PublicKey {
fn from(key: ecdsa::PublicKey) -> Self {
#[allow(deprecated)] PublicKey::Ecdsa(key)
}
}
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
impl From<rsa::PublicKey> for PublicKey {
fn from(key: rsa::PublicKey) -> Self {
#[allow(deprecated)] PublicKey::Rsa(key)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "peerid")]
use crate::PeerId;
use base64::prelude::*;
use std::str::FromStr;
#[test]
#[cfg(feature = "ed25519")]
#[cfg(feature = "peerid")]
fn keypair_protobuf_roundtrip() {
let expected_keypair = Keypair::generate_ed25519();
let expected_peer_id = expected_keypair.public().to_peer_id();
let encoded = expected_keypair.to_protobuf_encoding().unwrap();
let keypair = Keypair::from_protobuf_encoding(&encoded).unwrap();
let peer_id = keypair.public().to_peer_id();
assert_eq!(expected_peer_id, peer_id);
}
#[test]
#[cfg(feature = "peerid")]
fn keypair_from_protobuf_encoding() {
let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA=";
let expected_peer_id =
PeerId::from_str("12D3KooWEChVMMMzV8acJ53mJHrw1pQ27UAGkCxWXLJutbeUMvVu").unwrap();
let encoded = BASE64_STANDARD.decode(base_64_encoded).unwrap();
let keypair = Keypair::from_protobuf_encoding(&encoded).unwrap();
let peer_id = keypair.public().to_peer_id();
assert_eq!(expected_peer_id, peer_id);
}
#[test]
fn public_key_implements_hash() {
use crate::PublicKey;
use std::hash::Hash;
fn assert_implements_hash<T: Hash>() {}
assert_implements_hash::<PublicKey>();
}
#[test]
fn public_key_implements_ord() {
use crate::PublicKey;
use std::cmp::Ord;
fn assert_implements_ord<T: Ord>() {}
assert_implements_ord::<PublicKey>();
}
#[test]
#[cfg(feature = "ed25519")]
fn test_publickey_from_ed25519_public_key() {
let pubkey = Keypair::generate_ed25519().public();
let ed25519_pubkey = pubkey
.clone()
.try_into_ed25519()
.expect("A ed25519 keypair");
let converted_pubkey = PublicKey::from(ed25519_pubkey);
assert_eq!(converted_pubkey, pubkey);
}
#[test]
#[cfg(feature = "secp256k1")]
fn test_publickey_from_secp256k1_public_key() {
let pubkey = Keypair::generate_secp256k1().public();
let secp256k1_pubkey = pubkey
.clone()
.try_into_secp256k1()
.expect("A secp256k1 keypair");
let converted_pubkey = PublicKey::from(secp256k1_pubkey);
assert_eq!(converted_pubkey, pubkey);
}
#[test]
#[cfg(feature = "ecdsa")]
fn test_publickey_from_ecdsa_public_key() {
let pubkey = Keypair::generate_ecdsa().public();
let ecdsa_pubkey = pubkey.clone().try_into_ecdsa().expect("A ecdsa keypair");
let converted_pubkey = PublicKey::from(ecdsa_pubkey);
assert_eq!(converted_pubkey, pubkey);
}
}