feat(identity): expose KeyType for PublicKey and Keypair

Resolves https://github.com/libp2p/rust-libp2p/issues/3649.

Pull-Request: #4107.


  
Co-Authored-By: Nick Pavlov <gurinderu@gmail.com>
  

  
Co-Authored-By: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
Nick
2023-06-26 20:53:23 +03:00
committed by GitHub
parent 524bfc2130
commit 78510ede84
2 changed files with 43 additions and 3 deletions

View File

@ -1,3 +1,10 @@
## 0.2.1 - unreleased
- Expose `KeyType` for `PublicKey` and `Keypair`.
See [PR 4107].
[PR 4107]: https://github.com/libp2p/rust-libp2p/pull/4107
## 0.2.0 ## 0.2.0
- Raise MSRV to 1.65. - Raise MSRV to 1.65.

View File

@ -59,6 +59,7 @@ use crate::secp256k1;
#[cfg(feature = "ecdsa")] #[cfg(feature = "ecdsa")]
use crate::ecdsa; use crate::ecdsa;
use crate::KeyType;
/// Identity keypair of a node. /// Identity keypair of a node.
/// ///
@ -319,6 +320,20 @@ impl Keypair {
)))] )))]
unreachable!() unreachable!()
} }
/// Return a [`KeyType`] of the [`Keypair`].
pub fn key_type(&self) -> KeyType {
match self.keypair {
#[cfg(feature = "ed25519")]
KeyPairInner::Ed25519(_) => KeyType::Ed25519,
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
KeyPairInner::Rsa(_) => KeyType::RSA,
#[cfg(feature = "secp256k1")]
KeyPairInner::Secp256k1(_) => KeyType::Secp256k1,
#[cfg(feature = "ecdsa")]
KeyPairInner::Ecdsa(_) => KeyType::Ecdsa,
}
}
} }
#[cfg(feature = "ecdsa")] #[cfg(feature = "ecdsa")]
@ -552,6 +567,20 @@ impl PublicKey {
pub fn to_peer_id(&self) -> crate::PeerId { pub fn to_peer_id(&self) -> crate::PeerId {
self.into() self.into()
} }
/// Return a [`KeyType`] of the [`PublicKey`].
pub fn key_type(&self) -> KeyType {
match self.publickey {
#[cfg(feature = "ed25519")]
PublicKeyInner::Ed25519(_) => KeyType::Ed25519,
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
PublicKeyInner::Rsa(_) => KeyType::RSA,
#[cfg(feature = "secp256k1")]
PublicKeyInner::Secp256k1(_) => KeyType::Secp256k1,
#[cfg(feature = "ecdsa")]
PublicKeyInner::Ecdsa(_) => KeyType::Ecdsa,
}
}
} }
#[cfg(any( #[cfg(any(
@ -750,7 +779,7 @@ mod tests {
.unwrap(); .unwrap();
let pub_key = PublicKey::try_decode_protobuf(&hex_literal::hex!("0803125b3059301306072a8648ce3d020106082a8648ce3d03010703420004de3d300fa36ae0e8f5d530899d83abab44abf3161f162a4bc901d8e6ecda020e8b6d5f8da30525e71d6851510c098e5c47c646a597fb4dcec034e9f77c409e62")).unwrap(); let pub_key = PublicKey::try_decode_protobuf(&hex_literal::hex!("0803125b3059301306072a8648ce3d020106082a8648ce3d03010703420004de3d300fa36ae0e8f5d530899d83abab44abf3161f162a4bc901d8e6ecda020e8b6d5f8da30525e71d6851510c098e5c47c646a597fb4dcec034e9f77c409e62")).unwrap();
roundtrip_protobuf_encoding(&priv_key, &pub_key); roundtrip_protobuf_encoding(&priv_key, &pub_key, KeyType::Ecdsa);
} }
#[test] #[test]
@ -765,11 +794,11 @@ mod tests {
)) ))
.unwrap(); .unwrap();
roundtrip_protobuf_encoding(&priv_key, &pub_key); roundtrip_protobuf_encoding(&priv_key, &pub_key, KeyType::Secp256k1);
} }
#[cfg(feature = "peerid")] #[cfg(feature = "peerid")]
fn roundtrip_protobuf_encoding(private_key: &Keypair, public_key: &PublicKey) { fn roundtrip_protobuf_encoding(private_key: &Keypair, public_key: &PublicKey, tpe: KeyType) {
assert_eq!(&private_key.public(), public_key); assert_eq!(&private_key.public(), public_key);
let encoded_priv = private_key.to_protobuf_encoding().unwrap(); let encoded_priv = private_key.to_protobuf_encoding().unwrap();
@ -789,6 +818,7 @@ mod tests {
decoded_public.to_peer_id(), decoded_public.to_peer_id(),
"PeerId from roundtripped public key should be the same" "PeerId from roundtripped public key should be the same"
); );
assert_eq!(private_key.key_type(), tpe)
} }
#[test] #[test]
@ -845,6 +875,7 @@ mod tests {
let converted_pubkey = PublicKey::from(ed25519_pubkey); let converted_pubkey = PublicKey::from(ed25519_pubkey);
assert_eq!(converted_pubkey, pubkey); assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Ed25519)
} }
#[test] #[test]
@ -858,6 +889,7 @@ mod tests {
let converted_pubkey = PublicKey::from(secp256k1_pubkey); let converted_pubkey = PublicKey::from(secp256k1_pubkey);
assert_eq!(converted_pubkey, pubkey); assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Secp256k1)
} }
#[test] #[test]
@ -868,5 +900,6 @@ mod tests {
let converted_pubkey = PublicKey::from(ecdsa_pubkey); let converted_pubkey = PublicKey::from(ecdsa_pubkey);
assert_eq!(converted_pubkey, pubkey); assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Ecdsa)
} }
} }