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

fix(identity): add From & Into for public keys #3805

Merged
merged 17 commits into from
Apr 22, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
17 changes: 11 additions & 6 deletions identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
## 0.1.2 - unreleased

- Follow Rust naming conventions for conversion methods.
See [PR 3775].
- Add `impl From<ed25519::PublicKey> for PublicKey` so that `PublicKey::from(ed25519::PublicKey)` works
See [PR 3805].

[PR 3775]: https://github.com/libp2p/rust-libp2p/pull/3775
[pr 3805]: https://github.com/libp2p/rust-libp2p/pull/3805

- Follow Rust naming conventions for conversion methods.
See [PR 3775].

[pr 3775]: https://github.com/libp2p/rust-libp2p/pull/3775

## 0.1.1

- Add `From` impl for specific keypairs.
See [PR 3626].
- Add `From` impl for specific keypairs.
See [PR 3626].

[PR 3626]: https://github.com/libp2p/rust-libp2p/pull/3626
[pr 3626]: https://github.com/libp2p/rust-libp2p/pull/3626
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
130 changes: 111 additions & 19 deletions identity/src/keypair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ impl Keypair {

#[cfg(feature = "ed25519")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ed25519` instead."
)]
pub fn into_ed25519(self) -> Option<ed25519::Keypair> {
Expand All @@ -124,7 +123,6 @@ impl Keypair {

#[cfg(feature = "secp256k1")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_secp256k1` instead."
)]
pub fn into_secp256k1(self) -> Option<secp256k1::Keypair> {
Expand All @@ -138,7 +136,6 @@ impl Keypair {

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_rsa` instead."
)]
pub fn into_rsa(self) -> Option<rsa::Keypair> {
Expand All @@ -152,8 +149,7 @@ impl Keypair {

#[cfg(feature = "ecdsa")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ecdsa` instead."
note = "This method name does not follow Rust naming conventions, use `Keypair::from` instead."
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
)]
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
self.try_into().ok()
Expand Down Expand Up @@ -396,29 +392,29 @@ pub enum PublicKey {
#[cfg(feature = "ed25519")]
#[deprecated(
since = "0.1.0",
note = "This enum will be made opaque in the future, use `PublicKey::into_ed25519` instead."
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::into_rsa` instead."
note = "This enum will be made opaque in the future, use `PublicKey::from` 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::into_secp256k1` instead."
note = "This enum will be made opaque in the future, use `PublicKey::from` 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::into_ecdsa` instead."
note = "This enum will be made opaque in the future, use `PublicKey::from` instead."
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
)]
Ecdsa(ecdsa::PublicKey),
}
Expand Down Expand Up @@ -446,7 +442,6 @@ impl PublicKey {

#[cfg(feature = "ed25519")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ed25519` instead."
)]
pub fn into_ed25519(self) -> Option<ed25519::PublicKey> {
Expand All @@ -460,8 +455,7 @@ impl PublicKey {

#[cfg(feature = "secp256k1")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_secp256k1` instead."
note = "This method name does not follow Rust naming conventions, use `PublicKey::from` instead."
)]
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
self.try_into().ok()
Expand All @@ -474,8 +468,7 @@ impl PublicKey {

#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_rsa` instead."
note = "This method name does not follow Rust naming conventions, use `PublicKey::from` instead."
)]
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
self.try_into().ok()
Expand All @@ -488,8 +481,7 @@ impl PublicKey {

#[cfg(feature = "ecdsa")]
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ecdsa` instead."
note = "This method name does not follow Rust naming conventions, use `PublicKey::from` instead."
)]
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
self.try_into().ok()
Expand All @@ -502,7 +494,7 @@ impl PublicKey {

/// Encode the public key into a protobuf structure for storage or
/// exchange with other nodes.
#[deprecated(since = "0.2.0", note = "Renamed to `PublicKey::encode_protobuf`.")]
#[deprecated(note = "Renamed to `PublicKey::encode_protobuf`.")]
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
Self::encode_protobuf(self)
}
Expand All @@ -526,7 +518,6 @@ impl PublicKey {
/// Decode a public key from a protobuf structure, e.g. read from storage
/// or received from another node.
#[deprecated(
since = "0.2.0",
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> {
Expand Down Expand Up @@ -669,10 +660,42 @@ 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)
}
}

#[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)
}
}

#[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)
}
}

#[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)
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::PeerId;
use crate::peer_id::PeerId;
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
use base64::prelude::*;
use std::str::FromStr;

Expand Down Expand Up @@ -724,4 +747,73 @@ mod tests {

assert_implements_ord::<PublicKey>();
}

#[test]
#[cfg(feature = "ed25519")]
fn test_publickey_from_ed25519_public_key_roundtrip_bytes() {
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
let keypair = Keypair::generate_ed25519();
let expected_keypair = keypair
.clone()
.try_into_ed25519()
.expect("A ed25519 keypair");
let expected_keypair_public_bytes = expected_keypair.public().to_bytes().to_vec();

let pub_key = ed25519::PublicKey::try_from_bytes(&expected_keypair_public_bytes)
.expect("A ed25519 public key");

let keypair_pub_key = PublicKey::from(pub_key);

assert_eq!(
keypair_pub_key.encode_protobuf(),
keypair.public().encode_protobuf()
);
DougAnderson444 marked this conversation as resolved.
Show resolved Hide resolved
}

#[test]
#[cfg(feature = "secp256k1")]
fn test_publickey_from_secp256k1_public_key() {
let expected_keypair = Keypair::generate_secp256k1()
.try_into_secp256k1()
.expect("A secp256k1 keypair");
let expected_keypair_public_bytes = expected_keypair.public().to_bytes().to_vec();

let pub_key = secp256k1::PublicKey::try_from_bytes(&expected_keypair_public_bytes)
.expect("A secp256k1 public key");

// convert into keypair::PublicKey
let keypair_pub_key = PublicKey::from(pub_key);

assert_eq!(
expected_keypair.public().to_bytes().to_vec(),
keypair_pub_key
.try_into_secp256k1()
.unwrap()
.to_bytes()
.to_vec()
);
}

#[test]
#[cfg(feature = "ecdsa")]
fn test_publickey_from_ecdsa_public_key() {
let expected_keypair = Keypair::generate_ecdsa()
.try_into_ecdsa()
.expect("A ecdsa keypair");
let expected_keypair_public_bytes = expected_keypair.public().to_bytes().to_vec();

let pub_key = ecdsa::PublicKey::try_from_bytes(&expected_keypair_public_bytes)
.expect("A ecdsa public key");

// convert into keypair::PublicKey
let keypair_pub_key = PublicKey::from(pub_key);

assert_eq!(
expected_keypair.public().to_bytes().to_vec(),
keypair_pub_key
.try_into_ecdsa()
.unwrap()
.to_bytes()
.to_vec()
);
}
}