Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(identity): expose KeyType for PublicKey and Keypair #4107

Merged
merged 6 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.2.1 - unreleased
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

- Expose `KeyType` for `PublicKey` and `Keypair`.
See [PR 4107].

[PR 4107]: https://github.com/libp2p/rust-libp2p/pull/4107

## 0.2.0

- Raise MSRV to 1.65.
Expand Down
39 changes: 36 additions & 3 deletions identity/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use crate::secp256k1;

#[cfg(feature = "ecdsa")]
use crate::ecdsa;
use crate::KeyType;

/// Identity keypair of a node.
///
Expand Down Expand Up @@ -319,6 +320,20 @@ impl Keypair {
)))]
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")]
Expand Down Expand Up @@ -552,6 +567,20 @@ impl PublicKey {
pub fn to_peer_id(&self) -> crate::PeerId {
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(
Expand Down Expand Up @@ -750,7 +779,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]
Expand All @@ -765,11 +794,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();
Expand All @@ -789,6 +818,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]
Expand Down Expand Up @@ -845,6 +875,7 @@ mod tests {
let converted_pubkey = PublicKey::from(ed25519_pubkey);

assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Ed25519)
}

#[test]
Expand All @@ -858,6 +889,7 @@ mod tests {
let converted_pubkey = PublicKey::from(secp256k1_pubkey);

assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Secp256k1)
}

#[test]
Expand All @@ -868,5 +900,6 @@ mod tests {
let converted_pubkey = PublicKey::from(ecdsa_pubkey);

assert_eq!(converted_pubkey, pubkey);
assert_eq!(converted_pubkey.key_type(), KeyType::Ecdsa)
}
}