From 253ea940df0df3c3eb8dae8bbad1039d019726d6 Mon Sep 17 00:00:00 2001 From: Nick Pavlov Date: Fri, 23 Jun 2023 15:42:18 +0300 Subject: [PATCH 1/6] Added key_type method to keypair and publickey --- identity/src/keypair.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index ef1cd7f7179..21d554a564c 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -59,6 +59,7 @@ use crate::secp256k1; #[cfg(feature = "ecdsa")] use crate::ecdsa; +use crate::KeyType; /// Identity keypair of a node. /// @@ -319,6 +320,19 @@ impl Keypair { )))] unreachable!() } + + 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")] @@ -552,6 +566,20 @@ impl PublicKey { pub fn to_peer_id(&self) -> crate::PeerId { self.into() } + + 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( @@ -750,7 +778,7 @@ mod tests { .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] @@ -765,11 +793,11 @@ mod tests { )) .unwrap(); - roundtrip_protobuf_encoding(&priv_key, &pub_key); + roundtrip_protobuf_encoding(&priv_key, &pub_key, KeyType::Secp256k1); } #[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); let encoded_priv = private_key.to_protobuf_encoding().unwrap(); @@ -789,6 +817,7 @@ mod tests { decoded_public.to_peer_id(), "PeerId from roundtripped public key should be the same" ); + assert_eq!(private_key.key_type(), tpe) } #[test] @@ -845,6 +874,7 @@ mod tests { let converted_pubkey = PublicKey::from(ed25519_pubkey); assert_eq!(converted_pubkey, pubkey); + assert_eq!(converted_pubkey.key_type(), KeyType::Ed25519) } #[test] @@ -858,6 +888,7 @@ mod tests { let converted_pubkey = PublicKey::from(secp256k1_pubkey); assert_eq!(converted_pubkey, pubkey); + assert_eq!(converted_pubkey.key_type(), KeyType::Secp256k1) } #[test] @@ -868,5 +899,6 @@ mod tests { let converted_pubkey = PublicKey::from(ecdsa_pubkey); assert_eq!(converted_pubkey, pubkey); + assert_eq!(converted_pubkey.key_type(), KeyType::Ecdsa) } } From 0a446d9a02faeeaa38c9c919d28343b8986a229a Mon Sep 17 00:00:00 2001 From: Nick Pavlov Date: Fri, 23 Jun 2023 15:47:12 +0300 Subject: [PATCH 2/6] Add docs --- identity/src/keypair.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 21d554a564c..053be4a5ca2 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -321,6 +321,7 @@ impl Keypair { unreachable!() } + /// Return a [`KeyType`] of the [`Keypair`]. pub fn key_type(&self) -> KeyType { match self.keypair { #[cfg(feature = "ed25519")] @@ -330,7 +331,7 @@ impl Keypair { #[cfg(feature = "secp256k1")] KeyPairInner::Secp256k1(_) => KeyType::Secp256k1, #[cfg(feature = "ecdsa")] - KeyPairInner::Ecdsa(_) => KeyType::Ecdsa + KeyPairInner::Ecdsa(_) => KeyType::Ecdsa, } } } @@ -567,6 +568,7 @@ impl PublicKey { self.into() } + /// Return a [`KeyType`] of the [`PublicKey`]. pub fn key_type(&self) -> KeyType { match self.publickey { #[cfg(feature = "ed25519")] @@ -576,10 +578,9 @@ impl PublicKey { #[cfg(feature = "secp256k1")] PublicKeyInner::Secp256k1(_) => KeyType::Secp256k1, #[cfg(feature = "ecdsa")] - PublicKeyInner::Ecdsa(_) => KeyType::Ecdsa + PublicKeyInner::Ecdsa(_) => KeyType::Ecdsa, } } - } #[cfg(any( From 7e65952894cfab000ad848027429616255a334e2 Mon Sep 17 00:00:00 2001 From: Nick Pavlov Date: Mon, 26 Jun 2023 19:30:09 +0300 Subject: [PATCH 3/6] Add docs --- identity/CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 5f436bab9f6..8721e7e6542 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.2.1 - unreleased + +- Add key_type method to public key and key pair. + See [PR 4107] + ## 0.2.0 - Raise MSRV to 1.65. From 36380b5071216e2ea152dc8933290a8107c10e3e Mon Sep 17 00:00:00 2001 From: Nick Pavlov Date: Mon, 26 Jun 2023 19:32:37 +0300 Subject: [PATCH 4/6] Add docs --- identity/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 8721e7e6542..550893c08c1 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,7 +1,7 @@ ## 0.2.1 - unreleased - Add key_type method to public key and key pair. - See [PR 4107] + See [PR 4107]. ## 0.2.0 From 984a60d176415bd6e71d72fa7cba6480f4638aa5 Mon Sep 17 00:00:00 2001 From: Nick Pavlov Date: Mon, 26 Jun 2023 19:33:48 +0300 Subject: [PATCH 5/6] Add docs --- identity/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 550893c08c1..9843659944c 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -3,6 +3,8 @@ - Add key_type method to public key and key pair. See [PR 4107]. +[PR 4107]: https://github.com/libp2p/rust-libp2p/pull/4107 + ## 0.2.0 - Raise MSRV to 1.65. From e7da952c806e93e09c54968de39a1d3de9fb67fa Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Mon, 26 Jun 2023 18:30:01 +0100 Subject: [PATCH 6/6] Update identity/CHANGELOG.md --- identity/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 9843659944c..1496a36ba5c 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,6 +1,6 @@ ## 0.2.1 - unreleased -- Add key_type method to public key and key pair. +- Expose `KeyType` for `PublicKey` and `Keypair`. See [PR 4107]. [PR 4107]: https://github.com/libp2p/rust-libp2p/pull/4107