mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-21 23:21:19 +00:00
fix(identity): add From
& Into
for public keys
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 https://github.com/libp2p/rust-libp2p/issues/3802. Pull-Request: #3805.
This commit is contained in:
parent
28da3d4180
commit
e6f9d49109
@ -1,5 +1,10 @@
|
|||||||
## 0.1.2 - unreleased
|
## 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.
|
- Follow Rust naming conventions for conversion methods.
|
||||||
See [PR 3775].
|
See [PR 3775].
|
||||||
|
|
||||||
|
@ -110,7 +110,6 @@ impl Keypair {
|
|||||||
|
|
||||||
#[cfg(feature = "ed25519")]
|
#[cfg(feature = "ed25519")]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.2.0",
|
|
||||||
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ed25519` instead."
|
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_ed25519` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_ed25519(self) -> Option<ed25519::Keypair> {
|
pub fn into_ed25519(self) -> Option<ed25519::Keypair> {
|
||||||
@ -124,7 +123,6 @@ impl Keypair {
|
|||||||
|
|
||||||
#[cfg(feature = "secp256k1")]
|
#[cfg(feature = "secp256k1")]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.2.0",
|
|
||||||
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_secp256k1` instead."
|
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_secp256k1` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_secp256k1(self) -> Option<secp256k1::Keypair> {
|
pub fn into_secp256k1(self) -> Option<secp256k1::Keypair> {
|
||||||
@ -138,7 +136,6 @@ impl Keypair {
|
|||||||
|
|
||||||
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.2.0",
|
|
||||||
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_rsa` instead."
|
note = "This method name does not follow Rust naming conventions, use `Keypair::try_into_rsa` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_rsa(self) -> Option<rsa::Keypair> {
|
pub fn into_rsa(self) -> Option<rsa::Keypair> {
|
||||||
@ -152,7 +149,6 @@ impl Keypair {
|
|||||||
|
|
||||||
#[cfg(feature = "ecdsa")]
|
#[cfg(feature = "ecdsa")]
|
||||||
#[deprecated(
|
#[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::try_into_ecdsa` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
|
pub fn into_ecdsa(self) -> Option<ecdsa::Keypair> {
|
||||||
@ -396,7 +392,7 @@ pub enum PublicKey {
|
|||||||
#[cfg(feature = "ed25519")]
|
#[cfg(feature = "ed25519")]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.1.0",
|
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),
|
Ed25519(ed25519::PublicKey),
|
||||||
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
||||||
@ -404,21 +400,21 @@ pub enum PublicKey {
|
|||||||
|
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.1.0",
|
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),
|
Rsa(rsa::PublicKey),
|
||||||
#[cfg(feature = "secp256k1")]
|
#[cfg(feature = "secp256k1")]
|
||||||
/// A public Secp256k1 key.
|
/// A public Secp256k1 key.
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.1.0",
|
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),
|
Secp256k1(secp256k1::PublicKey),
|
||||||
/// A public ECDSA key.
|
/// A public ECDSA key.
|
||||||
#[cfg(feature = "ecdsa")]
|
#[cfg(feature = "ecdsa")]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.1.0",
|
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),
|
Ecdsa(ecdsa::PublicKey),
|
||||||
}
|
}
|
||||||
@ -446,7 +442,6 @@ impl PublicKey {
|
|||||||
|
|
||||||
#[cfg(feature = "ed25519")]
|
#[cfg(feature = "ed25519")]
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.2.0",
|
|
||||||
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ed25519` instead."
|
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_into_ed25519` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_ed25519(self) -> Option<ed25519::PublicKey> {
|
pub fn into_ed25519(self) -> Option<ed25519::PublicKey> {
|
||||||
@ -460,7 +455,6 @@ impl PublicKey {
|
|||||||
|
|
||||||
#[cfg(feature = "secp256k1")]
|
#[cfg(feature = "secp256k1")]
|
||||||
#[deprecated(
|
#[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::try_into_secp256k1` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
|
pub fn into_secp256k1(self) -> Option<secp256k1::PublicKey> {
|
||||||
@ -474,7 +468,6 @@ impl PublicKey {
|
|||||||
|
|
||||||
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))]
|
||||||
#[deprecated(
|
#[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::try_into_rsa` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
|
pub fn into_rsa(self) -> Option<rsa::PublicKey> {
|
||||||
@ -488,7 +481,6 @@ impl PublicKey {
|
|||||||
|
|
||||||
#[cfg(feature = "ecdsa")]
|
#[cfg(feature = "ecdsa")]
|
||||||
#[deprecated(
|
#[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::try_into_ecdsa` instead."
|
||||||
)]
|
)]
|
||||||
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
|
pub fn into_ecdsa(self) -> Option<ecdsa::PublicKey> {
|
||||||
@ -502,7 +494,7 @@ impl PublicKey {
|
|||||||
|
|
||||||
/// Encode the public key into a protobuf structure for storage or
|
/// Encode the public key into a protobuf structure for storage or
|
||||||
/// exchange with other nodes.
|
/// 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> {
|
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
|
||||||
Self::encode_protobuf(self)
|
Self::encode_protobuf(self)
|
||||||
}
|
}
|
||||||
@ -526,7 +518,6 @@ impl PublicKey {
|
|||||||
/// Decode a public key from a protobuf structure, e.g. read from storage
|
/// Decode a public key from a protobuf structure, e.g. read from storage
|
||||||
/// or received from another node.
|
/// or received from another node.
|
||||||
#[deprecated(
|
#[deprecated(
|
||||||
since = "0.2.0",
|
|
||||||
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_protobuf` instead."
|
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> {
|
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
|
||||||
@ -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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
#[cfg(feature = "peerid")]
|
||||||
use crate::PeerId;
|
use crate::PeerId;
|
||||||
use base64::prelude::*;
|
use base64::prelude::*;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "ed25519")]
|
#[cfg(feature = "ed25519")]
|
||||||
|
#[cfg(feature = "peerid")]
|
||||||
fn keypair_protobuf_roundtrip() {
|
fn keypair_protobuf_roundtrip() {
|
||||||
let expected_keypair = Keypair::generate_ed25519();
|
let expected_keypair = Keypair::generate_ed25519();
|
||||||
let expected_peer_id = expected_keypair.public().to_peer_id();
|
let expected_peer_id = expected_keypair.public().to_peer_id();
|
||||||
@ -691,6 +716,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[cfg(feature = "peerid")]
|
||||||
fn keypair_from_protobuf_encoding() {
|
fn keypair_from_protobuf_encoding() {
|
||||||
// E.g. retrieved from an IPFS config file.
|
// E.g. retrieved from an IPFS config file.
|
||||||
let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA=";
|
let base_64_encoded = "CAESQL6vdKQuznQosTrW7FWI9At+XX7EBf0BnZLhb6w+N+XSQSdfInl6c7U4NuxXJlhKcRBlBw9d0tj2dfBIVf6mcPA=";
|
||||||
@ -724,4 +750,41 @@ mod tests {
|
|||||||
|
|
||||||
assert_implements_ord::<PublicKey>();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user