From 7d5e6068681c35c925af4681255ec835a7f306ae Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Mon, 13 Mar 2023 19:06:40 +0800 Subject: [PATCH 01/37] feat: make mdns::Event clone-able --- protocols/mdns/src/behaviour.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index b34a1a73629..f2709e90bdf 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -339,7 +339,7 @@ where } /// Event that can be produced by the `Mdns` behaviour. -#[derive(Debug)] +#[derive(Debug, Clone)] pub enum Event { /// Discovered nodes through mDNS. Discovered(DiscoveredAddrsIter), @@ -352,6 +352,7 @@ pub enum Event { } /// Iterator that produces the list of addresses that have been discovered. +#[derive(Clone)] pub struct DiscoveredAddrsIter { inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, } @@ -379,6 +380,7 @@ impl fmt::Debug for DiscoveredAddrsIter { } /// Iterator that produces the list of addresses that have expired. +#[derive(Clone)] pub struct ExpiredAddrsIter { inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, } From a72e5e2f1edf92324268b837a63fd5b9f00e5373 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Mon, 13 Mar 2023 20:23:28 +0800 Subject: [PATCH 02/37] docs: filling CHANGELOG for PR 3606 Unreleased patch version 0.43.1 bumped. Added entry for PR 3606: Deriving 'Clone' for 'mdns::Event' --- protocols/mdns/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index c2b548e3389..62a361a0608 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,3 +1,7 @@ +# 0.43.1 [unreleased] + +- Deriving `Clone` for `mdns::Event`. See [PR 3606]. + # 0.43.0 - Update to `libp2p-core` `v0.39.0`. From 926c7ca99cd5c2de45d6610da45a41ab418ebe3b Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 14 Mar 2023 03:17:21 +1100 Subject: [PATCH 03/37] Update protocols/mdns/CHANGELOG.md --- protocols/mdns/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index 62a361a0608..c877ca48ccb 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -2,6 +2,7 @@ - Deriving `Clone` for `mdns::Event`. See [PR 3606]. +[PR 3606]: https://github.com/libp2p/rust-libp2p/pull/3606 # 0.43.0 - Update to `libp2p-core` `v0.39.0`. From 49d26c4795a45471fc67af3e6bca6477faf86244 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 14 Mar 2023 03:17:36 +1100 Subject: [PATCH 04/37] Update protocols/mdns/CHANGELOG.md --- protocols/mdns/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index c877ca48ccb..0c3b88c018c 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,6 +1,6 @@ # 0.43.1 [unreleased] -- Deriving `Clone` for `mdns::Event`. See [PR 3606]. +- Derive `Clone` for `mdns::Event`. See [PR 3606]. [PR 3606]: https://github.com/libp2p/rust-libp2p/pull/3606 # 0.43.0 From a8ff1eb46a336807fb06805a88ccbbde335591f3 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Thu, 16 Mar 2023 10:46:39 +0800 Subject: [PATCH 05/37] feat: change 'mdns::Event' to hold 'SmallVec' --- protocols/mdns/src/behaviour.rs | 68 ++------------------------------- 1 file changed, 4 insertions(+), 64 deletions(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index f2709e90bdf..6f590c49882 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -304,9 +304,7 @@ where } } if !discovered.is_empty() { - let event = Event::Discovered(DiscoveredAddrsIter { - inner: discovered.into_iter(), - }); + let event = Event::Discovered(discovered); return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } // Emit expired event. @@ -323,9 +321,7 @@ where true }); if !expired.is_empty() { - let event = Event::Expired(ExpiredAddrsIter { - inner: expired.into_iter(), - }); + let event = Event::Expired(expired); return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); } if let Some(closest_expiration) = closest_expiration { @@ -342,67 +338,11 @@ where #[derive(Debug, Clone)] pub enum Event { /// Discovered nodes through mDNS. - Discovered(DiscoveredAddrsIter), + Discovered(SmallVec<[(PeerId, Multiaddr); 4]>), /// The given combinations of `PeerId` and `Multiaddr` have expired. /// /// Each discovered record has a time-to-live. When this TTL expires and the address hasn't /// been refreshed, we remove it from the list and emit it as an `Expired` event. - Expired(ExpiredAddrsIter), -} - -/// Iterator that produces the list of addresses that have been discovered. -#[derive(Clone)] -pub struct DiscoveredAddrsIter { - inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, -} - -impl Iterator for DiscoveredAddrsIter { - type Item = (PeerId, Multiaddr); - - #[inline] - fn next(&mut self) -> Option { - self.inner.next() - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -impl ExactSizeIterator for DiscoveredAddrsIter {} - -impl fmt::Debug for DiscoveredAddrsIter { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_struct("DiscoveredAddrsIter").finish() - } -} - -/// Iterator that produces the list of addresses that have expired. -#[derive(Clone)] -pub struct ExpiredAddrsIter { - inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, -} - -impl Iterator for ExpiredAddrsIter { - type Item = (PeerId, Multiaddr); - - #[inline] - fn next(&mut self) -> Option { - self.inner.next() - } - - #[inline] - fn size_hint(&self) -> (usize, Option) { - self.inner.size_hint() - } -} - -impl ExactSizeIterator for ExpiredAddrsIter {} - -impl fmt::Debug for ExpiredAddrsIter { - fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt.debug_struct("ExpiredAddrsIter").finish() - } + Expired(SmallVec<[(PeerId, Multiaddr); 4]>), } From 4cec5fac84941c95cb50a35a3dc7395e2f71bf3c Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Thu, 16 Mar 2023 10:48:05 +0800 Subject: [PATCH 06/37] test: adapting test to 'mdns::Event' change --- protocols/mdns/tests/use-async-std.rs | 20 ++++++++++---------- protocols/mdns/tests/use-tokio.rs | 16 ++++++++-------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/protocols/mdns/tests/use-async-std.rs b/protocols/mdns/tests/use-async-std.rs index 139fcca1d50..bfc3cd1201d 100644 --- a/protocols/mdns/tests/use-async-std.rs +++ b/protocols/mdns/tests/use-async-std.rs @@ -61,13 +61,13 @@ async fn test_expired_async_std() { loop { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Expired(mut peers), _)) => { - if peers.any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Expired(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == b_peer_id) { return; } } - Either::Right((Event::Expired(mut peers), _)) => { - if peers.any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Expired(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == a_peer_id) { return; } } @@ -93,8 +93,8 @@ async fn test_no_expiration_on_close_async_std() { // 1. Connect via address from mDNS event loop { - if let Event::Discovered(mut peers) = a.next_behaviour_event().await { - if let Some((_, addr)) = peers.find(|(p, _)| p == &b_peer_id) { + if let Event::Discovered(peers) = a.next_behaviour_event().await { + if let Some((_, addr)) = peers.into_iter().find(|(p, _)| p == &b_peer_id) { a.dial_and_wait(addr).await; break; } @@ -130,13 +130,13 @@ async fn run_discovery_test(config: Config) { while !discovered_a && !discovered_b { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Discovered(mut peers), _)) => { - if peers.any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Discovered(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == b_peer_id) { discovered_b = true; } } - Either::Right((Event::Discovered(mut peers), _)) => { - if peers.any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Discovered(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == a_peer_id) { discovered_a = true; } } diff --git a/protocols/mdns/tests/use-tokio.rs b/protocols/mdns/tests/use-tokio.rs index e18ae28fee7..229418437f4 100644 --- a/protocols/mdns/tests/use-tokio.rs +++ b/protocols/mdns/tests/use-tokio.rs @@ -59,13 +59,13 @@ async fn test_expired_tokio() { loop { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Expired(mut peers), _)) => { - if peers.any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Expired(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == b_peer_id) { return; } } - Either::Right((Event::Expired(mut peers), _)) => { - if peers.any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Expired(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == a_peer_id) { return; } } @@ -86,13 +86,13 @@ async fn run_discovery_test(config: Config) { while !discovered_a && !discovered_b { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Discovered(mut peers), _)) => { - if peers.any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Discovered(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == b_peer_id) { discovered_b = true; } } - Either::Right((Event::Discovered(mut peers), _)) => { - if peers.any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Discovered(peers), _)) => { + if peers.into_iter().any(|(p, _)| p == a_peer_id) { discovered_a = true; } } From 18811d24ae748ca703f51ef66bdcb3961402965c Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Thu, 16 Mar 2023 17:30:41 +0800 Subject: [PATCH 07/37] feat(mdns): change 'mdns::Event' to hold 'Vec' --- protocols/mdns/src/behaviour.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 6f590c49882..f175a94aa06 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -285,7 +285,7 @@ where } } // Emit discovered event. - let mut discovered = SmallVec::<[(PeerId, Multiaddr); 4]>::new(); + let mut discovered = Vec::new(); for iface_state in self.iface_states.values_mut() { while let Poll::Ready((peer, addr, expiration)) = iface_state.poll(cx, &self.listen_addresses) @@ -310,7 +310,7 @@ where // Emit expired event. let now = Instant::now(); let mut closest_expiration = None; - let mut expired = SmallVec::<[(PeerId, Multiaddr); 4]>::new(); + let mut expired = Vec::new(); self.discovered_nodes.retain(|(peer, addr, expiration)| { if *expiration <= now { log::info!("expired: {} {}", peer, addr); @@ -338,11 +338,11 @@ where #[derive(Debug, Clone)] pub enum Event { /// Discovered nodes through mDNS. - Discovered(SmallVec<[(PeerId, Multiaddr); 4]>), + Discovered(Vec<(PeerId, Multiaddr)>), /// The given combinations of `PeerId` and `Multiaddr` have expired. /// /// Each discovered record has a time-to-live. When this TTL expires and the address hasn't /// been refreshed, we remove it from the list and emit it as an `Expired` event. - Expired(SmallVec<[(PeerId, Multiaddr); 4]>), + Expired(Vec<(PeerId, Multiaddr)>), } From abdce7ecbb16256bcad5118fd0d05ce644f01d5f Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Thu, 16 Mar 2023 17:39:50 +0800 Subject: [PATCH 08/37] doc: add entry for #3621 New unreleased minor version 0.44.0. --- protocols/mdns/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index 0c3b88c018c..6225dc376e5 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.44.0 [unreleased] + +- Change `mdns::Event` to hold `Vec`. See [PR 3621] + +[PR 3621]: https://github.com/libp2p/rust-libp2p/pull/3621 + # 0.43.1 [unreleased] - Derive `Clone` for `mdns::Event`. See [PR 3606]. From d5c246cc265e703d4f8af76f8c1de370a3f8c821 Mon Sep 17 00:00:00 2001 From: DrHuangMHT Date: Thu, 16 Mar 2023 22:05:01 +0800 Subject: [PATCH 09/37] Update protocols/mdns/CHANGELOG.md Co-authored-by: Thomas Eizinger --- protocols/mdns/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index 6225dc376e5..04a8162e065 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.44.0 [unreleased] -- Change `mdns::Event` to hold `Vec`. See [PR 3621] +- Change `mdns::Event` to hold `Vec` and remove `DiscoveredAddrsIter` and `ExpiredAddrsIter`. + See [PR 3621]. [PR 3621]: https://github.com/libp2p/rust-libp2p/pull/3621 From 9d45cf3d8c91221333f91bee67b675e0b043c623 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Sun, 26 Mar 2023 19:36:50 +0800 Subject: [PATCH 10/37] Remove conflict marker to resolve conflicts --- protocols/mdns/src/behaviour.rs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 56b9f9af144..5186ce91cb7 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -304,15 +304,8 @@ where } } if !discovered.is_empty() { -<<<<<<< HEAD let event = Event::Discovered(discovered); - return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); -======= - let event = Event::Discovered(DiscoveredAddrsIter { - inner: discovered.into_iter(), - }); return Poll::Ready(ToSwarm::GenerateEvent(event)); ->>>>>>> upstream/master } // Emit expired event. let now = Instant::now(); @@ -328,15 +321,8 @@ where true }); if !expired.is_empty() { -<<<<<<< HEAD let event = Event::Expired(expired); - return Poll::Ready(NetworkBehaviourAction::GenerateEvent(event)); -======= - let event = Event::Expired(ExpiredAddrsIter { - inner: expired.into_iter(), - }); return Poll::Ready(ToSwarm::GenerateEvent(event)); ->>>>>>> upstream/master } if let Some(closest_expiration) = closest_expiration { let mut timer = P::Timer::at(closest_expiration); From 74d01be79da6a29466df929efc2f7d98b1ee984c Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 13:56:44 +0800 Subject: [PATCH 11/37] rename methods in 'libp2p-identity::ecdsa' --- identity/src/ecdsa.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/identity/src/ecdsa.rs b/identity/src/ecdsa.rs index 27063a2c57f..464121d2a8c 100644 --- a/identity/src/ecdsa.rs +++ b/identity/src/ecdsa.rs @@ -108,11 +108,22 @@ impl SecretKey { } /// Decode a secret key from a byte buffer. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead" + )] pub fn from_bytes(buf: &[u8]) -> Result { SigningKey::from_bytes(buf) .map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err)) .map(SecretKey) } + + /// Try to parse a secret key from a byte buffer. + pub fn try_from_bytes(buf: impl AsRef<[u8]>) -> Result { + SigningKey::from_bytes(buf.as_ref()) + .map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err)) + .map(SecretKey) + } } impl fmt::Debug for SecretKey { @@ -136,7 +147,16 @@ impl PublicKey { } /// Decode a public key from a byte buffer without compression. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead." + )] pub fn from_bytes(k: &[u8]) -> Result { + Self::try_from_bytes(k) + } + + /// Decode a public key from a byte buffer without compression. + pub fn try_from_bytes(k: &[u8]) -> Result { let enc_pt = EncodedPoint::from_bytes(k) .map_err(|e| DecodingError::failed_to_parse("ecdsa p256 encoded point", e))?; @@ -158,10 +178,15 @@ impl PublicKey { /// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard. pub fn decode_der(k: &[u8]) -> Result { + Self::try_decode_der(k) + } + + /// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard. + pub fn try_decode_der(k: &[u8]) -> Result { let buf = Self::del_asn1_header(k).ok_or_else(|| { DecodingError::failed_to_parse::("ASN.1-encoded ecdsa p256 public key", None) })?; - Self::from_bytes(buf) + Self::try_from_bytes(buf) } // ecPublicKey (ANSI X9.62 public key type) OID: 1.2.840.10045.2.1 From d219b0e9c728a66b3b08e7d9375ed765b4f6ded2 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 13:57:29 +0800 Subject: [PATCH 12/37] introduce 'KeyType' in 'libp2p-identity' --- identity/src/lib.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/identity/src/lib.rs b/identity/src/lib.rs index 078a2de2106..fc8b0e40487 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -125,3 +125,23 @@ pub use error::{DecodingError, SigningError}; pub use keypair::{Keypair, PublicKey}; #[cfg(feature = "peerid")] pub use peer_id::{ParseError, PeerId}; + +#[derive(Debug, PartialEq, Eq)] +/// The type of key a `KeyPair` is holding. +pub enum KeyType { + Ed25519, + RSA, + Secp256k1, + Ecdsa, +} + +impl std::fmt::Display for KeyType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + KeyType::Ed25519 => f.write_str("Ed25519"), + KeyType::RSA => f.write_str("RSA"), + KeyType::Secp256k1 => f.write_str("Secp256k1"), + KeyType::Ecdsa => f.write_str("Ecdsa"), + } + } +} \ No newline at end of file From 8ca95ee185a3e0023b24b4305cf04284113dd6fc Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 13:57:54 +0800 Subject: [PATCH 13/37] introduce 'OtherVariantError' in 'libp2p-identity::error' --- identity/src/error.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/identity/src/error.rs b/identity/src/error.rs index 2a46571d858..46821be49db 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -23,6 +23,8 @@ use std::error::Error; use std::fmt; +use crate::KeyType; + /// An error during decoding of key material. #[derive(Debug)] pub struct DecodingError { @@ -156,3 +158,26 @@ impl Error for SigningError { self.source.as_ref().map(|s| &**s as &dyn Error) } } + +/// Error produced when failing to convert `libp2p_identity::Keypair` to a more concrete keypair. +#[derive(Debug)] +pub struct OtherVariantError { + actual: KeyType, +} + +impl OtherVariantError { + pub fn new(actual: KeyType) -> OtherVariantError { + OtherVariantError { actual } + } +} + +impl fmt::Display for OtherVariantError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&format!( + "Cannot convert to the given type, the actual key type inside is {}", + self.actual + )) + } +} + +impl Error for OtherVariantError {} \ No newline at end of file From 9b81b81a1b08b074f49d7085618e739dcdfdfc65 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 13:58:07 +0800 Subject: [PATCH 14/37] rename methods in 'libp2p-identity::ed25519' --- identity/src/ed25519.rs | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index b565599d277..cfa1b5e1a4f 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -49,7 +49,19 @@ impl Keypair { /// produced by [`Keypair::encode`], zeroing the input on success. /// /// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `Keypair::try_from_bytes` instead." + )] pub fn decode(kp: &mut [u8]) -> Result { + Self::try_from_bytes(kp) + } + + /// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5) + /// produced by [`Keypair::encode`], zeroing the input on success. + /// + /// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s. + pub fn try_from_bytes(kp: &mut [u8]) -> Result { ed25519::Keypair::from_bytes(kp) .map(|k| { kp.zeroize(); @@ -164,12 +176,32 @@ impl PublicKey { /// Encode the public key into a byte array in compressed form, i.e. /// where one coordinate is represented by a single bit. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::to_bytes` instead." + )] pub fn encode(&self) -> [u8; 32] { + self.to_bytes() + } + + /// Encode the public key into a byte array in compressed form, i.e. + /// where one coordinate is represented by a single bit. + pub fn to_bytes(&self) -> [u8; 32] { self.0.to_bytes() } /// Decode a public key from a byte array as produced by `encode`. + /// + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead." + )] pub fn decode(k: &[u8]) -> Result { + Self::try_from_bytes(k) + } + + /// Decode a public key from a byte array as produced by `encode`. + pub fn try_from_bytes(k: &[u8]) -> Result { ed25519::PublicKey::from_bytes(k) .map_err(|e| DecodingError::failed_to_parse("Ed25519 public key", e)) .map(PublicKey) @@ -214,7 +246,19 @@ impl SecretKey { /// Create an Ed25519 secret key from a byte slice, zeroing the input on success. /// If the bytes do not constitute a valid Ed25519 secret key, an error is /// returned. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead." + )] + #[allow(unused_mut)] pub fn from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result { + Self::try_from_bytes(sk_bytes) + } + + /// Create an Ed25519 secret key from a byte slice, zeroing the input on success. + /// If the bytes do not constitute a valid Ed25519 secret key, an error is + /// returned. + pub fn try_from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result { let sk_bytes = sk_bytes.as_mut(); let secret = ed25519::SecretKey::from_bytes(&*sk_bytes) .map_err(|e| DecodingError::failed_to_parse("Ed25519 secret key", e))?; From 7a1f645371015e5f271fc8f840b36a5d406d5f46 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:10:50 +0800 Subject: [PATCH 15/37] rename methods in 'libp2p-identity::keypair' --- identity/src/keypair.rs | 300 +++++++++++++++++++++++++++++++++------- 1 file changed, 247 insertions(+), 53 deletions(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index 0e4cce683b1..fe7f2ba563a 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -18,8 +18,9 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. +use crate::error::OtherVariantError; use crate::error::{DecodingError, SigningError}; -use crate::proto; +use crate::{proto, KeyType}; use quick_protobuf::{BytesReader, Writer}; use std::convert::TryFrom; @@ -59,28 +60,28 @@ pub enum Keypair { #[cfg(feature = "ed25519")] #[deprecated( since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::into_ed25519` instead." + note = "This enum will be made opaque in the future, use `Keypair::try_into_ed25519` instead." )] Ed25519(ed25519::Keypair), /// An RSA keypair. #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] #[deprecated( since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::into_rsa` instead." + note = "This enum will be made opaque in the future, use `Keypair::try_into_rsa` instead." )] Rsa(rsa::Keypair), /// A Secp256k1 keypair. #[cfg(feature = "secp256k1")] #[deprecated( since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::into_secp256k1` instead." + note = "This enum will be made opaque in the future, use `Keypair::try_into_secp256k1` instead." )] Secp256k1(secp256k1::Keypair), /// An ECDSA keypair. #[cfg(feature = "ecdsa")] #[deprecated( since = "0.1.0", - note = "This enum will be made opaque in the future, use `Keypair::into_ecdsa` instead." + note = "This enum will be made opaque in the future, use `Keypair::try_into_ecdsa` instead." )] Ecdsa(ecdsa::Keypair), } @@ -108,43 +109,59 @@ 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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - Keypair::Ed25519(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "ed25519")] + pub fn try_into_ed25519(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - Keypair::Secp256k1(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "secp256k1")] + pub fn try_into_secp256k1(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - Keypair::Rsa(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + pub fn try_into_rsa(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - Keypair::Ecdsa(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "ecdsa")] + pub fn try_into_ecdsa(self) -> Result { + self.try_into() } /// Decode an keypair from a DER-encoded secret key in PKCS#8 PrivateKeyInfo @@ -300,6 +317,78 @@ impl From for Keypair { } } +#[cfg(feature = "ed25519")] +impl TryInto for Keypair { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + Keypair::Ed25519(inner) => Ok(inner), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "secp256k1")] + Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + #[cfg(feature = "ecdsa")] + Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + +#[cfg(feature = "ecdsa")] +impl TryInto for Keypair { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + Keypair::Ecdsa(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "secp256k1")] + Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + } + } +} + +#[cfg(feature = "secp256k1")] +impl TryInto for Keypair { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + Keypair::Secp256k1(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + Keypair::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "ecdsa")] + Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + +#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +impl TryInto for Keypair { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + Keypair::Rsa(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + Keypair::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(feature = "secp256k1")] + Keypair::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + #[cfg(feature = "ecdsa")] + Keypair::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + /// The public key of a node's identity keypair. #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub enum PublicKey { @@ -356,48 +445,71 @@ 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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - PublicKey::Ed25519(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "ed25519")] + pub fn try_into_ed25519(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - PublicKey::Secp256k1(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "secp256k1")] + pub fn try_into_secp256k1(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - PublicKey::Rsa(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + pub fn try_into_rsa(self) -> Result { + self.try_into() } #[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 { - #[allow(deprecated)] - #[allow(unreachable_patterns)] - match self { - PublicKey::Ecdsa(inner) => Some(inner), - _ => None, - } + self.try_into().ok() + } + + #[cfg(feature = "ecdsa")] + pub fn try_into_ecdsa(self) -> Result { + self.try_into() } /// 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`.")] pub fn to_protobuf_encoding(&self) -> Vec { + Self::encode_protobuf(&self) + } + + /// Encode the public key into a protobuf structure for storage or + /// exchange with other nodes. + pub fn encode_protobuf(&self) -> Vec { use quick_protobuf::MessageWrite; let public_key = proto::PublicKey::from(self); @@ -413,7 +525,17 @@ 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 { + Self::try_decode_protobuf(bytes) + } + + /// Decode a public key from a protobuf structure, e.g. read from storage + /// or received from another node. + pub fn try_decode_protobuf(bytes: &[u8]) -> Result { use quick_protobuf::MessageRead; let mut reader = BytesReader::from_bytes(bytes); @@ -475,6 +597,78 @@ impl TryFrom for PublicKey { } } +#[cfg(feature = "ed25519")] +impl TryInto for PublicKey { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + PublicKey::Ed25519(inner) => Ok(inner), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "secp256k1")] + PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + #[cfg(feature = "ecdsa")] + PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + +#[cfg(feature = "ecdsa")] +impl TryInto for PublicKey { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + PublicKey::Ecdsa(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "secp256k1")] + PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + } + } +} + +#[cfg(feature = "secp256k1")] +impl TryInto for PublicKey { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + PublicKey::Secp256k1(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] + PublicKey::Rsa(_) => Err(OtherVariantError::new(KeyType::RSA)), + #[cfg(feature = "ecdsa")] + PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + +#[cfg(all(feature = "rsa", not(target_arch = "wasm32")))] +impl TryInto for PublicKey { + type Error = OtherVariantError; + + fn try_into(self) -> Result { + #[allow(deprecated)] + match self { + PublicKey::Rsa(inner) => Ok(inner), + #[cfg(feature = "ed25519")] + PublicKey::Ed25519(_) => Err(OtherVariantError::new(KeyType::Ed25519)), + #[cfg(feature = "secp256k1")] + PublicKey::Secp256k1(_) => Err(OtherVariantError::new(KeyType::Secp256k1)), + #[cfg(feature = "ecdsa")] + PublicKey::Ecdsa(_) => Err(OtherVariantError::new(KeyType::Ecdsa)), + } + } +} + #[cfg(test)] mod tests { use super::*; From 25e1139e4fd4e2a45001438c111a1db9263dae30 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:19:44 +0800 Subject: [PATCH 16/37] rename methods in 'libp2p-identity::rsa' --- identity/src/rsa.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/identity/src/rsa.rs b/identity/src/rsa.rs index 12f5d75e7dd..f38472787f3 100644 --- a/identity/src/rsa.rs +++ b/identity/src/rsa.rs @@ -46,7 +46,16 @@ impl Keypair { /// format (i.e. unencrypted) as defined in [RFC5208]. /// /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 + #[deprecated(since = "0.2.0", note = "Renamed to `Keypair::try_decode_pkcs8`.")] pub fn from_pkcs8(der: &mut [u8]) -> Result { + Self::try_decode_pkcs8(der) + } + + /// Decode an RSA keypair from a DER-encoded private key in PKCS#8 PrivateKeyInfo + /// format (i.e. unencrypted) as defined in [RFC5208]. + /// + /// [RFC5208]: https://tools.ietf.org/html/rfc5208#section-5 + pub fn try_decode_pkcs8(der: &mut [u8]) -> Result { let kp = RsaKeyPair::from_pkcs8(der) .map_err(|e| DecodingError::failed_to_parse("RSA PKCS#8 PrivateKeyInfo", e))?; der.zeroize(); @@ -109,7 +118,17 @@ impl PublicKey { /// Decode an RSA public key from a DER-encoded X.509 SubjectPublicKeyInfo /// structure. See also `encode_x509`. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_x509` instead." + )] pub fn decode_x509(pk: &[u8]) -> Result { + Self::try_decode_x509(pk) + } + + /// Decode an RSA public key from a DER-encoded X.509 SubjectPublicKeyInfo + /// structure. See also `encode_x509`. + pub fn try_decode_x509(pk: &[u8]) -> Result { Asn1SubjectPublicKeyInfo::decode(pk) .map_err(|e| DecodingError::failed_to_parse("RSA X.509", e)) .map(|spki| spki.subjectPublicKey.0) From d15d95b2cebad5235e0f0e865f66e72b9ed154ba Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:27:25 +0800 Subject: [PATCH 17/37] rename methods in 'libp2p-identity::secp256k1' --- identity/src/secp256k1.rs | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/identity/src/secp256k1.rs b/identity/src/secp256k1.rs index 119c3ef64e9..c5b49142b0b 100644 --- a/identity/src/secp256k1.rs +++ b/identity/src/secp256k1.rs @@ -97,7 +97,21 @@ impl SecretKey { /// error is returned. /// /// Note that the expected binary format is the same as `libsecp256k1`'s. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead." + )] + #[allow(unused_mut)] pub fn from_bytes(mut sk: impl AsMut<[u8]>) -> Result { + Self::try_from_bytes(sk) + } + + /// Create a secret key from a byte slice, zeroing the slice on success. + /// If the bytes do not constitute a valid Secp256k1 secret key, an + /// error is returned. + /// + /// Note that the expected binary format is the same as `libsecp256k1`'s. + pub fn try_from_bytes(mut sk: impl AsMut<[u8]>) -> Result { let sk_bytes = sk.as_mut(); let secret = libsecp256k1::SecretKey::parse_slice(&*sk_bytes) .map_err(|e| DecodingError::failed_to_parse("parse secp256k1 secret key", e))?; @@ -203,18 +217,44 @@ impl PublicKey { /// Encode the public key in compressed form, i.e. with one coordinate /// represented by a single bit. + #[deprecated(since = "0.2.0", note = "Renamed to `PublicKey::to_bytes`.")] pub fn encode(&self) -> [u8; 33] { + self.to_bytes() + } + + /// Convert the public key to a byte buffer in compressed form, i.e. with one coordinate + /// represented by a single bit. + pub fn to_bytes(&self) -> [u8; 33] { self.0.serialize_compressed() } /// Encode the public key in uncompressed form. + #[deprecated( + since = "0.2.0", + note = "Renamed to `PublicKey::to_bytes_uncompressed`." + )] pub fn encode_uncompressed(&self) -> [u8; 65] { + self.to_bytes_uncompressed() + } + + /// Convert the public key to a byte buffer in uncompressed form. + pub fn to_bytes_uncompressed(&self) -> [u8; 65] { self.0.serialize() } /// Decode a public key from a byte slice in the the format produced /// by `encode`. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead." + )] pub fn decode(k: &[u8]) -> Result { + Self::try_from_bytes(k) + } + + /// Decode a public key from a byte slice in the the format produced + /// by `encode`. + pub fn try_from_bytes(k: &[u8]) -> Result { libsecp256k1::PublicKey::parse_slice(k, Some(libsecp256k1::PublicKeyFormat::Compressed)) .map_err(|e| DecodingError::failed_to_parse("secp256k1 public key", e)) .map(PublicKey) From 19ff42733fa94a34857bfe906e0ddf5b18f20e94 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:38:22 +0800 Subject: [PATCH 18/37] formatting --- identity/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/error.rs b/identity/src/error.rs index 46821be49db..8a7190cdc41 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -180,4 +180,4 @@ impl fmt::Display for OtherVariantError { } } -impl Error for OtherVariantError {} \ No newline at end of file +impl Error for OtherVariantError {} From ba4511d48bdc84a88597f5d5299051d8dd523b2e Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:44:45 +0800 Subject: [PATCH 19/37] change call to deprecated methods to recommended ones --- core/src/signed_envelope.rs | 4 ++-- identity/src/ed25519.rs | 10 +++++----- identity/src/peer_id.rs | 4 ++-- identity/src/rsa.rs | 10 +++++----- identity/src/secp256k1.rs | 14 +++++++------- protocols/gossipsub/src/behaviour.rs | 2 +- protocols/gossipsub/src/protocol.rs | 4 ++-- protocols/identify/src/protocol.rs | 6 +++--- transports/noise/src/io/handshake.rs | 4 ++-- transports/plaintext/src/handshake.rs | 4 ++-- transports/tls/src/certificate.rs | 4 ++-- 11 files changed, 33 insertions(+), 33 deletions(-) diff --git a/core/src/signed_envelope.rs b/core/src/signed_envelope.rs index 0eafda4c487..a9d7ecd5208 100644 --- a/core/src/signed_envelope.rs +++ b/core/src/signed_envelope.rs @@ -76,7 +76,7 @@ impl SignedEnvelope { use quick_protobuf::MessageWrite; let envelope = proto::Envelope { - public_key: self.key.to_protobuf_encoding(), + public_key: self.key.encode_protobuf(), payload_type: self.payload_type, payload: self.payload, signature: self.signature, @@ -101,7 +101,7 @@ impl SignedEnvelope { proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?; Ok(Self { - key: PublicKey::from_protobuf_encoding(&envelope.public_key)?, + key: PublicKey::try_decode_protobuf(&envelope.public_key)?, payload_type: envelope.payload_type.to_vec(), payload: envelope.payload.to_vec(), signature: envelope.signature.to_vec(), diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index cfa1b5e1a4f..072786c695f 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -82,7 +82,7 @@ impl Keypair { /// Get the secret key of this keypair. pub fn secret(&self) -> SecretKey { - SecretKey::from_bytes(&mut self.0.secret.to_bytes()) + SecretKey::try_from_bytes(&mut self.0.secret.to_bytes()) .expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") } } @@ -98,7 +98,7 @@ impl fmt::Debug for Keypair { impl Clone for Keypair { fn clone(&self) -> Keypair { let mut sk_bytes = self.0.secret.to_bytes(); - let secret = SecretKey::from_bytes(&mut sk_bytes) + let secret = SecretKey::try_from_bytes(&mut sk_bytes) .expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") .0; @@ -221,7 +221,7 @@ impl AsRef<[u8]> for SecretKey { impl Clone for SecretKey { fn clone(&self) -> SecretKey { let mut sk_bytes = self.0.to_bytes(); - Self::from_bytes(&mut sk_bytes).expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") + Self::try_from_bytes(&mut sk_bytes).expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") } } @@ -281,7 +281,7 @@ mod tests { fn prop() -> bool { let kp1 = Keypair::generate(); let mut kp1_enc = kp1.encode(); - let kp2 = Keypair::decode(&mut kp1_enc).unwrap(); + let kp2 = Keypair::try_from_bytes(&mut kp1_enc).unwrap(); eq_keypairs(&kp1, &kp2) && kp1_enc.iter().all(|b| *b == 0) } QuickCheck::new().tests(10).quickcheck(prop as fn() -> _); @@ -292,7 +292,7 @@ mod tests { fn prop() -> bool { let kp1 = Keypair::generate(); let mut sk = kp1.0.secret.to_bytes(); - let kp2 = Keypair::from(SecretKey::from_bytes(&mut sk).unwrap()); + let kp2 = Keypair::from(SecretKey::try_from_bytes(&mut sk).unwrap()); eq_keypairs(&kp1, &kp2) && sk == [0u8; 32] } QuickCheck::new().tests(10).quickcheck(prop as fn() -> _); diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 1a96f4311e9..788e0b79666 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -66,7 +66,7 @@ impl fmt::Display for PeerId { impl PeerId { /// Builds a `PeerId` from a public key. pub fn from_public_key(key: &crate::keypair::PublicKey) -> PeerId { - let key_enc = key.to_protobuf_encoding(); + let key_enc = key.encode_protobuf(); let multihash = if key_enc.len() <= MAX_INLINE_KEY_LENGTH { Multihash::wrap(MULTIHASH_IDENTITY_CODE, &key_enc) @@ -141,7 +141,7 @@ impl PeerId { let alg = Code::try_from(self.multihash.code()) .expect("Internal multihash is always a valid `Code`"); - let enc = public_key.to_protobuf_encoding(); + let enc = public_key.encode_protobuf(); Some(alg.digest(&enc) == self.multihash) } } diff --git a/identity/src/rsa.rs b/identity/src/rsa.rs index f38472787f3..b598b093b7e 100644 --- a/identity/src/rsa.rs +++ b/identity/src/rsa.rs @@ -336,22 +336,22 @@ mod tests { impl Arbitrary for SomeKeypair { fn arbitrary(g: &mut Gen) -> SomeKeypair { let mut key = g.choose(&[KEY1, KEY2, KEY3]).unwrap().to_vec(); - SomeKeypair(Keypair::from_pkcs8(&mut key).unwrap()) + SomeKeypair(Keypair::try_decode_pkcs8(&mut key).unwrap()) } } #[test] fn rsa_from_pkcs8() { - assert!(Keypair::from_pkcs8(&mut KEY1.to_vec()).is_ok()); - assert!(Keypair::from_pkcs8(&mut KEY2.to_vec()).is_ok()); - assert!(Keypair::from_pkcs8(&mut KEY3.to_vec()).is_ok()); + assert!(Keypair::try_decode_pkcs8(&mut KEY1.to_vec()).is_ok()); + assert!(Keypair::try_decode_pkcs8(&mut KEY2.to_vec()).is_ok()); + assert!(Keypair::try_decode_pkcs8(&mut KEY3.to_vec()).is_ok()); } #[test] fn rsa_x509_encode_decode() { fn prop(SomeKeypair(kp): SomeKeypair) -> Result { let pk = kp.public(); - PublicKey::decode_x509(&pk.encode_x509()) + PublicKey::try_decode_x509(&pk.encode_x509()) .map_err(|e| e.to_string()) .map(|pk2| pk2 == pk) } diff --git a/identity/src/secp256k1.rs b/identity/src/secp256k1.rs index c5b49142b0b..413e2566227 100644 --- a/identity/src/secp256k1.rs +++ b/identity/src/secp256k1.rs @@ -132,7 +132,7 @@ impl SecretKey { .and_then(Vec::load) .map_err(|e| DecodingError::failed_to_parse("secp256k1 SecretKey bytes", e))?; - let sk = SecretKey::from_bytes(&mut sk_bytes)?; + let sk = SecretKey::try_from_bytes(&mut sk_bytes)?; sk_bytes.zeroize(); der_obj.zeroize(); Ok(sk) @@ -171,7 +171,7 @@ pub struct PublicKey(libsecp256k1::PublicKey); impl fmt::Debug for PublicKey { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str("PublicKey(compressed): ")?; - for byte in &self.encode() { + for byte in &self.to_bytes() { write!(f, "{byte:x}")?; } Ok(()) @@ -180,25 +180,25 @@ impl fmt::Debug for PublicKey { impl cmp::PartialEq for PublicKey { fn eq(&self, other: &Self) -> bool { - self.encode().eq(&other.encode()) + self.to_bytes().eq(&other.to_bytes()) } } impl hash::Hash for PublicKey { fn hash(&self, state: &mut H) { - self.encode().hash(state); + self.to_bytes().hash(state); } } impl cmp::PartialOrd for PublicKey { fn partial_cmp(&self, other: &Self) -> Option { - self.encode().partial_cmp(&other.encode()) + self.to_bytes().partial_cmp(&other.to_bytes()) } } impl cmp::Ord for PublicKey { fn cmp(&self, other: &Self) -> cmp::Ordering { - self.encode().cmp(&other.encode()) + self.to_bytes().cmp(&other.to_bytes()) } } @@ -270,7 +270,7 @@ mod tests { let sk1 = SecretKey::generate(); let mut sk_bytes = [0; 32]; sk_bytes.copy_from_slice(&sk1.0.serialize()[..]); - let sk2 = SecretKey::from_bytes(&mut sk_bytes).unwrap(); + let sk2 = SecretKey::try_from_bytes(&mut sk_bytes).unwrap(); assert_eq!(sk1.0.serialize(), sk2.0.serialize()); assert_eq!(sk_bytes, [0; 32]); } diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 256390adffd..fb58e55f24a 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -203,7 +203,7 @@ impl From for PublishConfig { match authenticity { MessageAuthenticity::Signed(keypair) => { let public_key = keypair.public(); - let key_enc = public_key.to_protobuf_encoding(); + let key_enc = public_key.encode_protobuf(); let key = if key_enc.len() <= 42 { // The public key can be inlined in [`rpc_proto::proto::::Message::from`], so we don't include it // specifically in the [`rpc_proto::proto::Message::key`] field. diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index f7b04269c92..682389b0634 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -237,10 +237,10 @@ impl GossipsubCodec { let public_key = match message .key .as_deref() - .map(PublicKey::from_protobuf_encoding) + .map(PublicKey::try_decode_protobuf) { Some(Ok(key)) => key, - _ => match PublicKey::from_protobuf_encoding(&source.to_bytes()[2..]) { + _ => match PublicKey::try_decode_protobuf(&source.to_bytes()[2..]) { Ok(v) => v, Err(_) => { warn!("Signature verification failed: No valid public key supplied"); diff --git a/protocols/identify/src/protocol.rs b/protocols/identify/src/protocol.rs index 51e530291dc..1a10b591278 100644 --- a/protocols/identify/src/protocol.rs +++ b/protocols/identify/src/protocol.rs @@ -169,7 +169,7 @@ where .map(|addr| addr.to_vec()) .collect(); - let pubkey_bytes = info.public_key.to_protobuf_encoding(); + let pubkey_bytes = info.public_key.encode_protobuf(); let message = proto::Identify { agentVersion: Some(info.agent_version), @@ -235,7 +235,7 @@ impl TryFrom for Info { addrs }; - let public_key = PublicKey::from_protobuf_encoding(&msg.publicKey.unwrap_or_default())?; + let public_key = PublicKey::try_decode_protobuf(&msg.publicKey.unwrap_or_default())?; let observed_addr = match parse_multiaddr(msg.observedAddr.unwrap_or_default()) { Ok(a) => a, @@ -386,7 +386,7 @@ mod tests { publicKey: Some( identity::Keypair::generate_ed25519() .public() - .to_protobuf_encoding(), + .encode_protobuf(), ), }; diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index 672f24ef7ce..e9428f8441c 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -214,7 +214,7 @@ where let pb = pb_result?; if !pb.identity_key.is_empty() { - let pk = identity::PublicKey::from_protobuf_encoding(&pb.identity_key)?; + let pk = identity::PublicKey::try_decode_protobuf(&pb.identity_key)?; if let Some(ref k) = state.id_remote_pubkey { if k != &pk { return Err(NoiseError::UnexpectedKey); @@ -236,7 +236,7 @@ where T: AsyncWrite + Unpin, { let mut pb = proto::NoiseHandshakePayload { - identity_key: state.identity.public.to_protobuf_encoding(), + identity_key: state.identity.public.encode_protobuf(), ..Default::default() }; diff --git a/transports/plaintext/src/handshake.rs b/transports/plaintext/src/handshake.rs index 3f70f515a09..fb156190c57 100644 --- a/transports/plaintext/src/handshake.rs +++ b/transports/plaintext/src/handshake.rs @@ -54,7 +54,7 @@ impl HandshakeContext { fn new(config: PlainText2Config) -> Self { let exchange = Exchange { id: Some(config.local_public_key.to_peer_id().to_bytes()), - pubkey: Some(config.local_public_key.to_protobuf_encoding()), + pubkey: Some(config.local_public_key.encode_protobuf()), }; let mut buf = Vec::with_capacity(exchange.get_size()); let mut writer = Writer::new(&mut buf); @@ -77,7 +77,7 @@ impl HandshakeContext { let mut reader = BytesReader::from_bytes(&exchange_bytes); let prop = Exchange::from_reader(&mut reader, &exchange_bytes)?; - let public_key = PublicKey::from_protobuf_encoding(&prop.pubkey.unwrap_or_default())?; + let public_key = PublicKey::try_decode_protobuf(&prop.pubkey.unwrap_or_default())?; let peer_id = PeerId::from_bytes(&prop.id.unwrap_or_default())?; // Check the validity of the remote's `Exchange`. diff --git a/transports/tls/src/certificate.rs b/transports/tls/src/certificate.rs index 6321fa26400..8531ade72fa 100644 --- a/transports/tls/src/certificate.rs +++ b/transports/tls/src/certificate.rs @@ -159,7 +159,7 @@ fn parse_unverified(der_input: &[u8]) -> Result { // required KeyType Type = 1; // required bytes Data = 2; // } - let public_key = identity::PublicKey::from_protobuf_encoding(&public_key) + let public_key = identity::PublicKey::try_decode_protobuf(&public_key) .map_err(|_| webpki::Error::UnknownIssuer)?; let ext = P2pExtension { public_key, @@ -215,7 +215,7 @@ fn make_libp2p_extension( // signature OCTET STRING // } let extension_content = { - let serialized_pubkey = identity_keypair.public().to_protobuf_encoding(); + let serialized_pubkey = identity_keypair.public().encode_protobuf(); yasna::encode_der(&(serialized_pubkey, signature)) }; From 8e9c227f53b6081aba47410178ba23c7522f8da1 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 14:46:19 +0800 Subject: [PATCH 20/37] clippy fix --- identity/src/keypair.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/keypair.rs b/identity/src/keypair.rs index fe7f2ba563a..a080fcfb690 100644 --- a/identity/src/keypair.rs +++ b/identity/src/keypair.rs @@ -504,7 +504,7 @@ impl PublicKey { /// exchange with other nodes. #[deprecated(since = "0.2.0", note = "Renamed to `PublicKey::encode_protobuf`.")] pub fn to_protobuf_encoding(&self) -> Vec { - Self::encode_protobuf(&self) + Self::encode_protobuf(self) } /// Encode the public key into a protobuf structure for storage or From 9ed64365e9de9a28d660e5a7990aff6e33191774 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 17:19:06 +0800 Subject: [PATCH 21/37] formatting --- identity/src/ed25519.rs | 3 ++- identity/src/lib.rs | 2 +- protocols/gossipsub/src/protocol.rs | 6 +----- 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index 072786c695f..9f7cb67cc06 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -221,7 +221,8 @@ impl AsRef<[u8]> for SecretKey { impl Clone for SecretKey { fn clone(&self) -> SecretKey { let mut sk_bytes = self.0.to_bytes(); - Self::try_from_bytes(&mut sk_bytes).expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") + Self::try_from_bytes(&mut sk_bytes) + .expect("ed25519::SecretKey::from_bytes(to_bytes(k)) != k") } } diff --git a/identity/src/lib.rs b/identity/src/lib.rs index fc8b0e40487..e11c9069861 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -144,4 +144,4 @@ impl std::fmt::Display for KeyType { KeyType::Ecdsa => f.write_str("Ecdsa"), } } -} \ No newline at end of file +} diff --git a/protocols/gossipsub/src/protocol.rs b/protocols/gossipsub/src/protocol.rs index 682389b0634..98e05567929 100644 --- a/protocols/gossipsub/src/protocol.rs +++ b/protocols/gossipsub/src/protocol.rs @@ -234,11 +234,7 @@ impl GossipsubCodec { // If there is a key value in the protobuf, use that key otherwise the key must be // obtained from the inlined source peer_id. - let public_key = match message - .key - .as_deref() - .map(PublicKey::try_decode_protobuf) - { + let public_key = match message.key.as_deref().map(PublicKey::try_decode_protobuf) { Some(Ok(key)) => key, _ => match PublicKey::try_decode_protobuf(&source.to_bytes()[2..]) { Ok(v) => v, From cbbd01b3dc9e816f86c4701ee7e2e70e335b7dc7 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 12 Apr 2023 12:56:50 +0200 Subject: [PATCH 22/37] Undo mdns changes --- protocols/mdns/CHANGELOG.md | 7 --- protocols/mdns/src/behaviour.rs | 72 ++++++++++++++++++++++++--- protocols/mdns/tests/use-async-std.rs | 20 ++++---- protocols/mdns/tests/use-tokio.rs | 16 +++--- 4 files changed, 84 insertions(+), 31 deletions(-) diff --git a/protocols/mdns/CHANGELOG.md b/protocols/mdns/CHANGELOG.md index 10b8743b07d..5d3b4f79c55 100644 --- a/protocols/mdns/CHANGELOG.md +++ b/protocols/mdns/CHANGELOG.md @@ -1,10 +1,3 @@ -# 0.44.0 [unreleased] - -- Change `mdns::Event` to hold `Vec` and remove `DiscoveredAddrsIter` and `ExpiredAddrsIter`. - See [PR 3621]. - -[PR 3621]: https://github.com/libp2p/rust-libp2p/pull/3621 - ## 0.43.1 - Derive `Clone` for `mdns::Event`. See [PR 3606]. diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index 5186ce91cb7..92e38c04998 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -285,7 +285,7 @@ where } } // Emit discovered event. - let mut discovered = Vec::new(); + let mut discovered = SmallVec::<[(PeerId, Multiaddr); 4]>::new(); for iface_state in self.iface_states.values_mut() { while let Poll::Ready((peer, addr, expiration)) = iface_state.poll(cx, &self.listen_addresses) @@ -304,13 +304,15 @@ where } } if !discovered.is_empty() { - let event = Event::Discovered(discovered); + let event = Event::Discovered(DiscoveredAddrsIter { + inner: discovered.into_iter(), + }); return Poll::Ready(ToSwarm::GenerateEvent(event)); } // Emit expired event. let now = Instant::now(); let mut closest_expiration = None; - let mut expired = Vec::new(); + let mut expired = SmallVec::<[(PeerId, Multiaddr); 4]>::new(); self.discovered_nodes.retain(|(peer, addr, expiration)| { if *expiration <= now { log::info!("expired: {} {}", peer, addr); @@ -321,7 +323,9 @@ where true }); if !expired.is_empty() { - let event = Event::Expired(expired); + let event = Event::Expired(ExpiredAddrsIter { + inner: expired.into_iter(), + }); return Poll::Ready(ToSwarm::GenerateEvent(event)); } if let Some(closest_expiration) = closest_expiration { @@ -338,11 +342,67 @@ where #[derive(Debug, Clone)] pub enum Event { /// Discovered nodes through mDNS. - Discovered(Vec<(PeerId, Multiaddr)>), + Discovered(DiscoveredAddrsIter), /// The given combinations of `PeerId` and `Multiaddr` have expired. /// /// Each discovered record has a time-to-live. When this TTL expires and the address hasn't /// been refreshed, we remove it from the list and emit it as an `Expired` event. - Expired(Vec<(PeerId, Multiaddr)>), + Expired(ExpiredAddrsIter), +} + +/// Iterator that produces the list of addresses that have been discovered. +#[derive(Clone)] +pub struct DiscoveredAddrsIter { + inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, +} + +impl Iterator for DiscoveredAddrsIter { + type Item = (PeerId, Multiaddr); + + #[inline] + fn next(&mut self) -> Option { + self.inner.next() + } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl ExactSizeIterator for DiscoveredAddrsIter {} + +impl fmt::Debug for DiscoveredAddrsIter { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("DiscoveredAddrsIter").finish() + } +} + +/// Iterator that produces the list of addresses that have expired. +#[derive(Clone)] +pub struct ExpiredAddrsIter { + inner: smallvec::IntoIter<[(PeerId, Multiaddr); 4]>, +} + +impl Iterator for ExpiredAddrsIter { + type Item = (PeerId, Multiaddr); + + #[inline] + fn next(&mut self) -> Option { + self.inner.next() + } + + #[inline] + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } +} + +impl ExactSizeIterator for ExpiredAddrsIter {} + +impl fmt::Debug for ExpiredAddrsIter { + fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt.debug_struct("ExpiredAddrsIter").finish() + } } diff --git a/protocols/mdns/tests/use-async-std.rs b/protocols/mdns/tests/use-async-std.rs index bfc3cd1201d..139fcca1d50 100644 --- a/protocols/mdns/tests/use-async-std.rs +++ b/protocols/mdns/tests/use-async-std.rs @@ -61,13 +61,13 @@ async fn test_expired_async_std() { loop { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Expired(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Expired(mut peers), _)) => { + if peers.any(|(p, _)| p == b_peer_id) { return; } } - Either::Right((Event::Expired(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Expired(mut peers), _)) => { + if peers.any(|(p, _)| p == a_peer_id) { return; } } @@ -93,8 +93,8 @@ async fn test_no_expiration_on_close_async_std() { // 1. Connect via address from mDNS event loop { - if let Event::Discovered(peers) = a.next_behaviour_event().await { - if let Some((_, addr)) = peers.into_iter().find(|(p, _)| p == &b_peer_id) { + if let Event::Discovered(mut peers) = a.next_behaviour_event().await { + if let Some((_, addr)) = peers.find(|(p, _)| p == &b_peer_id) { a.dial_and_wait(addr).await; break; } @@ -130,13 +130,13 @@ async fn run_discovery_test(config: Config) { while !discovered_a && !discovered_b { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Discovered(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Discovered(mut peers), _)) => { + if peers.any(|(p, _)| p == b_peer_id) { discovered_b = true; } } - Either::Right((Event::Discovered(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Discovered(mut peers), _)) => { + if peers.any(|(p, _)| p == a_peer_id) { discovered_a = true; } } diff --git a/protocols/mdns/tests/use-tokio.rs b/protocols/mdns/tests/use-tokio.rs index 229418437f4..e18ae28fee7 100644 --- a/protocols/mdns/tests/use-tokio.rs +++ b/protocols/mdns/tests/use-tokio.rs @@ -59,13 +59,13 @@ async fn test_expired_tokio() { loop { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Expired(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Expired(mut peers), _)) => { + if peers.any(|(p, _)| p == b_peer_id) { return; } } - Either::Right((Event::Expired(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Expired(mut peers), _)) => { + if peers.any(|(p, _)| p == a_peer_id) { return; } } @@ -86,13 +86,13 @@ async fn run_discovery_test(config: Config) { while !discovered_a && !discovered_b { match futures::future::select(a.next_behaviour_event(), b.next_behaviour_event()).await { - Either::Left((Event::Discovered(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == b_peer_id) { + Either::Left((Event::Discovered(mut peers), _)) => { + if peers.any(|(p, _)| p == b_peer_id) { discovered_b = true; } } - Either::Right((Event::Discovered(peers), _)) => { - if peers.into_iter().any(|(p, _)| p == a_peer_id) { + Either::Right((Event::Discovered(mut peers), _)) => { + if peers.any(|(p, _)| p == a_peer_id) { discovered_a = true; } } From aa72364ea12c57cc2e9c6ee0e99b33cd6218d8e1 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:06:09 +0800 Subject: [PATCH 23/37] delegate deprecated method to recommended one --- identity/src/ecdsa.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/identity/src/ecdsa.rs b/identity/src/ecdsa.rs index 464121d2a8c..c39526553a7 100644 --- a/identity/src/ecdsa.rs +++ b/identity/src/ecdsa.rs @@ -113,9 +113,7 @@ impl SecretKey { note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead" )] pub fn from_bytes(buf: &[u8]) -> Result { - SigningKey::from_bytes(buf) - .map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err)) - .map(SecretKey) + Self::try_from_bytes(buf) } /// Try to parse a secret key from a byte buffer. From e6a314439fa64be7c91df221b580e669bad3b9cd Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:22:22 +0800 Subject: [PATCH 24/37] documentation improvement on 'libp2p-identity::ecdsa' --- identity/src/ecdsa.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/identity/src/ecdsa.rs b/identity/src/ecdsa.rs index c39526553a7..2210e4d128b 100644 --- a/identity/src/ecdsa.rs +++ b/identity/src/ecdsa.rs @@ -33,7 +33,7 @@ use p256::{ }; use void::Void; -/// An ECDSA keypair. +/// An ECDSA keypair generated using `secp256r1` curve. #[derive(Clone)] pub struct Keypair { secret: SecretKey, @@ -85,7 +85,7 @@ impl From for SecretKey { } } -/// An ECDSA secret key. +/// An ECDSA secret key generated using `secp256r1` curve. #[derive(Clone)] pub struct SecretKey(SigningKey); @@ -102,12 +102,12 @@ impl SecretKey { signature.as_bytes().to_owned() } - /// Encode a secret key into a byte buffer. + /// Convert a secret key into a byte buffer containing raw scalar of the key. pub fn to_bytes(&self) -> Vec { self.0.to_bytes().to_vec() } - /// Decode a secret key from a byte buffer. + /// Decode a secret key from a byte buffer containing raw scalar of the key. #[deprecated( since = "0.2.0", note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead" @@ -116,7 +116,7 @@ impl SecretKey { Self::try_from_bytes(buf) } - /// Try to parse a secret key from a byte buffer. + /// Try to parse a secret key from a byte buffer containing raw scalar of the key. pub fn try_from_bytes(buf: impl AsRef<[u8]>) -> Result { SigningKey::from_bytes(buf.as_ref()) .map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err)) @@ -144,7 +144,7 @@ impl PublicKey { self.0.verify(msg, &sig).is_ok() } - /// Decode a public key from a byte buffer without compression. + /// Decode a public key from a byte buffer containing raw components of a key with or without compression. #[deprecated( since = "0.2.0", note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead." @@ -153,7 +153,7 @@ impl PublicKey { Self::try_from_bytes(k) } - /// Decode a public key from a byte buffer without compression. + /// Try to parse a public key from a byte buffer containing raw components of a key with or without compression. pub fn try_from_bytes(k: &[u8]) -> Result { let enc_pt = EncodedPoint::from_bytes(k) .map_err(|e| DecodingError::failed_to_parse("ecdsa p256 encoded point", e))?; @@ -163,7 +163,7 @@ impl PublicKey { .map(PublicKey) } - /// Encode a public key into a byte buffer without compression. + /// Convert a public key into a byte buffer containing raw components of the key without compression. pub fn to_bytes(&self) -> Vec { self.0.to_encoded_point(false).as_bytes().to_owned() } @@ -175,11 +175,15 @@ impl PublicKey { } /// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard. + #[deprecated( + since = "0.2.0", + note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_der` instead." + )] pub fn decode_der(k: &[u8]) -> Result { Self::try_decode_der(k) } - /// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard. + /// Try to decode a public key from a DER encoded byte buffer as defined by SEC1 standard. pub fn try_decode_der(k: &[u8]) -> Result { let buf = Self::del_asn1_header(k).ok_or_else(|| { DecodingError::failed_to_parse::("ASN.1-encoded ecdsa p256 public key", None) From 4c7caf802c107e1363ba88c84cf68a6e4f9d624d Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:24:26 +0800 Subject: [PATCH 25/37] rename methods in 'libp2p-identity::ed25519' --- identity/src/ed25519.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index 9f7cb67cc06..d7d7b508ef4 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -41,7 +41,15 @@ impl Keypair { /// Encode the keypair into a byte array by concatenating the bytes /// of the secret scalar and the compressed public point, /// an informal standard for encoding Ed25519 keypairs. + #[deprecated(since = "0.2.0", note = "Renamed to `Keypair::to_bytes`")] pub fn encode(&self) -> [u8; 64] { + self.to_bytes() + } + + /// Encode the keypair into a byte array by concatenating the bytes + /// of the secret scalar and the compressed public point, + /// an informal standard for encoding Ed25519 keypairs. + pub fn to_bytes(&self) -> [u8; 64] { self.0.to_bytes() } @@ -190,7 +198,7 @@ impl PublicKey { self.0.to_bytes() } - /// Decode a public key from a byte array as produced by `encode`. + /// Decode a public key from a byte array as produced by `to_bytes`. /// #[deprecated( since = "0.2.0", @@ -200,7 +208,7 @@ impl PublicKey { Self::try_from_bytes(k) } - /// Decode a public key from a byte array as produced by `encode`. + /// Decode a public key from a byte array as produced by `to_bytes`. pub fn try_from_bytes(k: &[u8]) -> Result { ed25519::PublicKey::from_bytes(k) .map_err(|e| DecodingError::failed_to_parse("Ed25519 public key", e)) @@ -281,7 +289,7 @@ mod tests { fn ed25519_keypair_encode_decode() { fn prop() -> bool { let kp1 = Keypair::generate(); - let mut kp1_enc = kp1.encode(); + let mut kp1_enc = kp1.to_bytes(); let kp2 = Keypair::try_from_bytes(&mut kp1_enc).unwrap(); eq_keypairs(&kp1, &kp2) && kp1_enc.iter().all(|b| *b == 0) } From aa1577ae0b7becd98a399d5d8efdb5cbc56d59e6 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:28:51 +0800 Subject: [PATCH 26/37] change visibility of 'OtherVariantError::new' to crate level --- identity/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/error.rs b/identity/src/error.rs index 8a7190cdc41..fd36d12e509 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -166,7 +166,7 @@ pub struct OtherVariantError { } impl OtherVariantError { - pub fn new(actual: KeyType) -> OtherVariantError { + pub(crate) fn new(actual: KeyType) -> OtherVariantError { OtherVariantError { actual } } } From 6d9de353cbb0cb8bf7dba2ec4557d676c03aaee1 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:31:25 +0800 Subject: [PATCH 27/37] documentation improvement on 'OtherVariantError' --- identity/src/error.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/error.rs b/identity/src/error.rs index fd36d12e509..79ef21d5c44 100644 --- a/identity/src/error.rs +++ b/identity/src/error.rs @@ -159,7 +159,7 @@ impl Error for SigningError { } } -/// Error produced when failing to convert `libp2p_identity::Keypair` to a more concrete keypair. +/// Error produced when failing to convert [`Keypair`](crate::Keypair) to a more concrete keypair. #[derive(Debug)] pub struct OtherVariantError { actual: KeyType, From fdc0d256271b7716db013ea02a333e0a012c026c Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:43:18 +0800 Subject: [PATCH 28/37] documentation improvement and correction on 'libp2p_identity::ed25519' --- identity/src/ed25519.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/identity/src/ed25519.rs b/identity/src/ed25519.rs index d7d7b508ef4..90782de378a 100644 --- a/identity/src/ed25519.rs +++ b/identity/src/ed25519.rs @@ -46,7 +46,7 @@ impl Keypair { self.to_bytes() } - /// Encode the keypair into a byte array by concatenating the bytes + /// Convert the keypair into a byte array by concatenating the bytes /// of the secret scalar and the compressed public point, /// an informal standard for encoding Ed25519 keypairs. pub fn to_bytes(&self) -> [u8; 64] { @@ -54,7 +54,7 @@ impl Keypair { } /// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5) - /// produced by [`Keypair::encode`], zeroing the input on success. + /// produced by [`Keypair::to_bytes`], zeroing the input on success. /// /// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s. #[deprecated( @@ -65,8 +65,8 @@ impl Keypair { Self::try_from_bytes(kp) } - /// Decode a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5) - /// produced by [`Keypair::encode`], zeroing the input on success. + /// Try to parse a keypair from the [binary format](https://datatracker.ietf.org/doc/html/rfc8032#section-5.1.5) + /// produced by [`Keypair::to_bytes`], zeroing the input on success. /// /// Note that this binary format is the same as `ed25519_dalek`'s and `ed25519_zebra`'s. pub fn try_from_bytes(kp: &mut [u8]) -> Result { @@ -186,20 +186,19 @@ impl PublicKey { /// where one coordinate is represented by a single bit. #[deprecated( since = "0.2.0", - note = "This method name does not follow Rust naming conventions, use `PublicKey::to_bytes` instead." + note = "Renamed to `PublicKey::to_bytes` to reflect actual behaviour." )] pub fn encode(&self) -> [u8; 32] { self.to_bytes() } - /// Encode the public key into a byte array in compressed form, i.e. + /// Convert the public key to a byte array in compressed form, i.e. /// where one coordinate is represented by a single bit. pub fn to_bytes(&self) -> [u8; 32] { self.0.to_bytes() } /// Decode a public key from a byte array as produced by `to_bytes`. - /// #[deprecated( since = "0.2.0", note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead." @@ -208,7 +207,7 @@ impl PublicKey { Self::try_from_bytes(k) } - /// Decode a public key from a byte array as produced by `to_bytes`. + /// Try to parse a public key from a byte array containing the actual key as produced by `to_bytes`. pub fn try_from_bytes(k: &[u8]) -> Result { ed25519::PublicKey::from_bytes(k) .map_err(|e| DecodingError::failed_to_parse("Ed25519 public key", e)) @@ -264,7 +263,8 @@ impl SecretKey { Self::try_from_bytes(sk_bytes) } - /// Create an Ed25519 secret key from a byte slice, zeroing the input on success. + /// Try to parse an Ed25519 secret key from a byte slice + /// containing the actual key, zeroing the input on success. /// If the bytes do not constitute a valid Ed25519 secret key, an error is /// returned. pub fn try_from_bytes(mut sk_bytes: impl AsMut<[u8]>) -> Result { From a46f6617c7398f6f2b14a167feba2fb4e254f815 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Wed, 12 Apr 2023 21:55:49 +0800 Subject: [PATCH 29/37] patch version bump(to 0.1.2) for 'libp2p-identity' --- identity/CHANGELOG.md | 7 +++++++ identity/Cargo.toml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index 961b5c6921d..fb8f1f34172 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.1.2 + +- 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. diff --git a/identity/Cargo.toml b/identity/Cargo.toml index d0352036c14..a87010c53ed 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libp2p-identity" -version = "0.1.1" +version = "0.1.2" edition = "2021" description = "Data structures and algorithms for identifying peers in libp2p." rust-version = "1.60.0" From 4479ff96a7159e4b893292a7e0afb2dab75de8cb Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Fri, 14 Apr 2023 00:38:51 +1000 Subject: [PATCH 30/37] 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 fb8f1f34172..5ab5cfc2528 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.1.2 +## 0.1.2 - unreleased - Follow Rust naming conventions for conversion methods. See [PR 3775]. From 947284c1e9afd5f3178518cbdf77fe2ddf1936a7 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 10:35:09 +0800 Subject: [PATCH 31/37] lockfile update --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 303d21a0abe..1113e8e6d9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2493,7 +2493,7 @@ dependencies = [ [[package]] name = "libp2p-identity" -version = "0.1.1" +version = "0.1.2" dependencies = [ "asn1_der", "base64 0.21.0", From 9ae4e416428b587cc9329434a8a1932bc60d0c21 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 10:35:43 +0800 Subject: [PATCH 32/37] export 'OtherVariantError' --- identity/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/identity/src/lib.rs b/identity/src/lib.rs index e11c9069861..87b02cd5a2e 100644 --- a/identity/src/lib.rs +++ b/identity/src/lib.rs @@ -121,7 +121,7 @@ impl From<&PublicKey> for proto::PublicKey { } } -pub use error::{DecodingError, SigningError}; +pub use error::{DecodingError, OtherVariantError, SigningError}; pub use keypair::{Keypair, PublicKey}; #[cfg(feature = "peerid")] pub use peer_id::{ParseError, PeerId}; From 38f4b38ab299f70c668fcb00e4de7b398cf70821 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 15:29:50 +0800 Subject: [PATCH 33/37] dependency version bump for 'gossipsub' --- protocols/gossipsub/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/gossipsub/Cargo.toml b/protocols/gossipsub/Cargo.toml index 4f25af93f6b..53d9d4be702 100644 --- a/protocols/gossipsub/Cargo.toml +++ b/protocols/gossipsub/Cargo.toml @@ -13,7 +13,7 @@ categories = ["network-programming", "asynchronous"] [dependencies] libp2p-swarm = { version = "0.42.1", path = "../../swarm" } libp2p-core = { version = "0.39.0", path = "../../core" } -libp2p-identity = { version = "0.1.0", path = "../../identity" } +libp2p-identity = { version = "0.1.2", path = "../../identity" } bytes = "1.4" byteorder = "1.3.4" fnv = "1.0.7" From 4fe27a04dc828695c84925d2d993367be18acf0d Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 15:30:19 +0800 Subject: [PATCH 34/37] dependency version bump for 'identify' --- protocols/identify/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/identify/Cargo.toml b/protocols/identify/Cargo.toml index 82229871cf5..f4df901f9ae 100644 --- a/protocols/identify/Cargo.toml +++ b/protocols/identify/Cargo.toml @@ -16,7 +16,7 @@ futures = "0.3.28" futures-timer = "3.0.2" libp2p-core = { version = "0.39.0", path = "../../core" } libp2p-swarm = { version = "0.42.1", path = "../../swarm" } -libp2p-identity = { version = "0.1.0", path = "../../identity" } +libp2p-identity = { version = "0.1.2", path = "../../identity" } log = "0.4.1" lru = "0.9.0" quick-protobuf-codec = { version = "0.1", path = "../../misc/quick-protobuf-codec" } From b426f0d9aa99cf5cb005276bfd3121539d73d7cb Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 15:31:34 +0800 Subject: [PATCH 35/37] dependency version bump for 'noise' --- transports/noise/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/noise/Cargo.toml b/transports/noise/Cargo.toml index 4e745c28adc..28a8ceeb0a8 100644 --- a/transports/noise/Cargo.toml +++ b/transports/noise/Cargo.toml @@ -13,7 +13,7 @@ bytes = "1" curve25519-dalek = "3.0.0" futures = "0.3.28" libp2p-core = { version = "0.39.0", path = "../../core" } -libp2p-identity = { version = "0.1.0", path = "../../identity", features = ["ed25519"] } +libp2p-identity = { version = "0.1.2", path = "../../identity", features = ["ed25519"] } log = "0.4" quick-protobuf = "0.8" once_cell = "1.17.1" From f19ce60a3ae470a0f405d34440b8099a369807bc Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 15:32:03 +0800 Subject: [PATCH 36/37] dependency version bump for 'plaintext' --- transports/plaintext/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/plaintext/Cargo.toml b/transports/plaintext/Cargo.toml index 81e0f1b98d0..fea37124ed1 100644 --- a/transports/plaintext/Cargo.toml +++ b/transports/plaintext/Cargo.toml @@ -15,7 +15,7 @@ asynchronous-codec = "0.6" bytes = "1" futures = "0.3.28" libp2p-core = { version = "0.39.0", path = "../../core" } -libp2p-identity = { version = "0.1.0", path = "../../identity" } +libp2p-identity = { version = "0.1.2", path = "../../identity" } log = "0.4.8" quick-protobuf = "0.8" unsigned-varint = { version = "0.7", features = ["asynchronous_codec"] } From 8394c6af67fb258e4859af7c5786cc1d8ca83093 Mon Sep 17 00:00:00 2001 From: drHuangMHT Date: Fri, 14 Apr 2023 15:32:30 +0800 Subject: [PATCH 37/37] dependency version bump for 'tls' --- transports/tls/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transports/tls/Cargo.toml b/transports/tls/Cargo.toml index 26d9679a814..3658f92fc94 100644 --- a/transports/tls/Cargo.toml +++ b/transports/tls/Cargo.toml @@ -12,7 +12,7 @@ exclude = ["src/test_assets"] futures = { version = "0.3.28", default-features = false } futures-rustls = "0.22.2" libp2p-core = { version = "0.39.0", path = "../../core" } -libp2p-identity = { version = "0.1.0", path = "../../identity" } +libp2p-identity = { version = "0.1.2", path = "../../identity" } rcgen = "0.10.0" ring = "0.16.20" thiserror = "1.0.40"