Skip to content

Commit

Permalink
fix(identity): add From & Into for public keys
Browse files Browse the repository at this point in the history
This patch removes the `version 0.2.0` for deprecations as libp2p-identity is only at `0.1.1` and this can be confusing to the reader.

It also adds `impl From<ed25519::PublicKey> for PublicKey` (et al.) so that `PublicKey::from(ed25519::PublicKey)` works.

Fixes #3802.

Pull-Request: #3805.
  • Loading branch information
DougAnderson444 authored Apr 22, 2023
1 parent 28da3d4 commit e6f9d49
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
5 changes: 5 additions & 0 deletions identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## 0.1.2 - unreleased

- Add `impl From<ed25519::PublicKey> for PublicKey` so that `PublicKey::from(ed25519::PublicKey)` works.
See [PR 3805].

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

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

Expand Down
91 changes: 77 additions & 14 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,7 +149,6 @@ 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."
)]
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
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` and `PublicKey::into_rsa` 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` and `PublicKey::into_secp256k1` 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` and `PublicKey::into_ecdsa` instead."
)]
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,7 +455,6 @@ 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."
)]
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
Expand All @@ -474,7 +468,6 @@ 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."
)]
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
Expand All @@ -488,7 +481,6 @@ 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."
)]
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
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,15 +660,49 @@ 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::*;
#[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();
Expand All @@ -691,6 +716,7 @@ mod tests {
}

#[test]
#[cfg(feature = "peerid")]
fn keypair_from_protobuf_encoding() {
// E.g. retrieved from an IPFS config file.
let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA=";
Expand Down Expand Up @@ -724,4 +750,41 @@ mod tests {

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);
}
}

0 comments on commit e6f9d49

Please sign in to comment.