diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9dcb413822..bd3737a49f4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -72,7 +72,7 @@ jobs: tool: tomlq - name: Enforce version in `workspace.dependencies` matches latest version - if: env.CRATE != 'libp2p' && env.CRATE != 'libp2p-identity' + if: env.CRATE != 'libp2p' run: | PACKAGE_VERSION=$(cargo metadata --format-version=1 --no-deps | jq -e -r '.packages[] | select(.name == "'"$CRATE"'") | .version') SPECIFIED_VERSION=$(tomlq "workspace.dependencies.$CRATE.version" --file ./Cargo.toml) diff --git a/Cargo.lock b/Cargo.lock index f63915d88d5..39ee5d84cb5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -617,12 +617,6 @@ dependencies = [ "futures-lite", ] -[[package]] -name = "bs58" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3" - [[package]] name = "bs58" version = "0.5.0" @@ -1315,7 +1309,6 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p", - "multiaddr", ] [[package]] @@ -1336,18 +1329,6 @@ dependencies = [ "signature 1.6.4", ] -[[package]] -name = "ecdsa" -version = "0.15.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12844141594ad74185a926d030f3b605f6a903b4e3fec351f3ea338ac5b7637e" -dependencies = [ - "der 0.6.1", - "elliptic-curve 0.12.3", - "rfc6979 0.3.1", - "signature 2.0.0", -] - [[package]] name = "ecdsa" version = "0.16.6" @@ -1539,7 +1520,6 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p", - "multiaddr", "serde", "void", ] @@ -2213,7 +2193,6 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p", - "multiaddr", ] [[package]] @@ -2274,7 +2253,7 @@ dependencies = [ "base64 0.21.2", "clap", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "serde", "serde_json", "zeroize", @@ -2331,7 +2310,7 @@ dependencies = [ "libp2p-floodsub", "libp2p-gossipsub", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-kad", "libp2p-mdns", "libp2p-metrics", @@ -2360,7 +2339,7 @@ version = "0.2.0" dependencies = [ "async-std", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-swarm", "libp2p-swarm-derive", "libp2p-swarm-test", @@ -2378,7 +2357,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-request-response", "libp2p-swarm", "libp2p-swarm-test", @@ -2394,7 +2373,7 @@ dependencies = [ "async-std", "libp2p-core", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-ping", "libp2p-swarm", "libp2p-swarm-derive", @@ -2414,12 +2393,12 @@ dependencies = [ "futures", "futures-timer", "instant", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-mplex", "libp2p-noise", "log", "multiaddr", - "multihash 0.17.0", + "multihash", "multistream-select", "once_cell", "parking_lot", @@ -2450,7 +2429,7 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-ping", "libp2p-plaintext", @@ -2490,7 +2469,7 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "log", "parking_lot", "smallvec", @@ -2507,7 +2486,7 @@ dependencies = [ "fnv", "futures", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-swarm", "log", "quick-protobuf", @@ -2536,7 +2515,7 @@ dependencies = [ "hex_fmt", "instant", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2566,7 +2545,7 @@ dependencies = [ "futures", "futures-timer", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-swarm", "libp2p-swarm-test", "log", @@ -2578,44 +2557,19 @@ dependencies = [ "void", ] -[[package]] -name = "libp2p-identity" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e2d584751cecb2aabaa56106be6be91338a60a0f4e420cf2af639204f596fc1" -dependencies = [ - "asn1_der", - "bs58 0.4.0", - "ed25519-dalek", - "libsecp256k1", - "log", - "multiaddr", - "multihash 0.17.0", - "p256 0.12.0", - "quick-protobuf", - "rand 0.8.5", - "ring", - "sec1 0.3.0", - "serde", - "sha2 0.10.6", - "thiserror", - "void", - "zeroize", -] - [[package]] name = "libp2p-identity" version = "0.2.0" dependencies = [ "asn1_der", "base64 0.21.2", - "bs58 0.5.0", + "bs58", "criterion", "ed25519-dalek", "hex-literal", "libsecp256k1", "log", - "multihash 0.19.0", + "multihash", "p256 0.13.2", "quick-protobuf", "quickcheck-ext", @@ -2647,7 +2601,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2676,7 +2630,7 @@ dependencies = [ "futures", "if-watch", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2700,7 +2654,7 @@ dependencies = [ "libp2p-dcutr", "libp2p-gossipsub", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-kad", "libp2p-ping", "libp2p-relay", @@ -2720,7 +2674,7 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-muxer-test-harness", "libp2p-plaintext", "libp2p-tcp", @@ -2754,7 +2708,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "log", "once_cell", "quick-protobuf", @@ -2780,7 +2734,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-dns", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-quic", "libp2p-request-response", "libp2p-swarm", @@ -2808,7 +2762,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-swarm", "libp2p-swarm-test", "log", @@ -2827,7 +2781,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "log", "quick-protobuf", "quickcheck-ext", @@ -2841,7 +2795,7 @@ version = "0.23.0" dependencies = [ "futures", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-swarm", "libp2p-tcp", @@ -2867,7 +2821,7 @@ dependencies = [ "futures-timer", "if-watch", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-muxer-test-harness", "libp2p-noise", "libp2p-tcp", @@ -2895,7 +2849,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-ping", "libp2p-plaintext", "libp2p-swarm", @@ -2923,7 +2877,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-ping", "libp2p-swarm", @@ -2950,7 +2904,7 @@ dependencies = [ "futures_ringbuf", "instant", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2979,7 +2933,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-kad", "libp2p-ping", "libp2p-plaintext", @@ -3017,7 +2971,7 @@ dependencies = [ "futures", "futures-timer", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-plaintext", "libp2p-swarm", "libp2p-tcp", @@ -3038,7 +2992,7 @@ dependencies = [ "if-watch", "libc", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "log", "socket2 0.5.3", "tokio", @@ -3053,7 +3007,7 @@ dependencies = [ "hex", "hex-literal", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-swarm", "libp2p-yamux", "rcgen 0.10.0", @@ -3105,12 +3059,12 @@ dependencies = [ "hex-literal", "if-watch", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-noise", "libp2p-ping", "libp2p-swarm", "log", - "multihash 0.17.0", + "multihash", "quick-protobuf", "quick-protobuf-codec", "quickcheck", @@ -3138,7 +3092,7 @@ dependencies = [ "futures-rustls 0.22.2", "libp2p-core", "libp2p-dns", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-tcp", "log", "parking_lot", @@ -3347,16 +3301,16 @@ dependencies = [ [[package]] name = "multiaddr" -version = "0.17.1" +version = "0.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b36f567c7099511fa8612bbbb52dda2419ce0bdbacf31714e3a5ffdb766d3bd" +checksum = "92a651988b3ed3ad1bc8c87d016bb92f6f395b84ed1db9b926b32b1fc5a2c8b5" dependencies = [ "arrayref", "byteorder", "data-encoding", - "log", + "libp2p-identity", "multibase", - "multihash 0.17.0", + "multihash", "percent-encoding", "serde", "static_assertions", @@ -3377,44 +3331,18 @@ dependencies = [ [[package]] name = "multihash" -version = "0.17.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" +checksum = "2fd59dcc2bbe70baabeac52cd22ae52c55eefe6c38ff11a9439f16a350a939f2" dependencies = [ "arbitrary", "core2", - "multihash-derive", "quickcheck", "rand 0.8.5", "serde", - "serde-big-array", "unsigned-varint", ] -[[package]] -name = "multihash" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd59dcc2bbe70baabeac52cd22ae52c55eefe6c38ff11a9439f16a350a939f2" -dependencies = [ - "core2", - "unsigned-varint", -] - -[[package]] -name = "multihash-derive" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d6d4752e6230d8ef7adf7bd5d8c4b1f6561c1014c5ba9a37445ccefe18aa1db" -dependencies = [ - "proc-macro-crate", - "proc-macro-error", - "proc-macro2", - "quote", - "syn 1.0.109", - "synstructure", -] - [[package]] name = "multistream-select" version = "0.13.0" @@ -3425,7 +3353,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity 0.1.2", + "libp2p-identity", "libp2p-plaintext", "libp2p-swarm", "libp2p-yamux", @@ -3620,18 +3548,6 @@ dependencies = [ "sha2 0.10.6", ] -[[package]] -name = "p256" -version = "0.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49c124b3cbce43bcbac68c58ec181d98ed6cc7e6d0aa7c3ba97b2563410b0e55" -dependencies = [ - "ecdsa 0.15.1", - "elliptic-curve 0.12.3", - "primeorder 0.12.1", - "sha2 0.10.6", -] - [[package]] name = "p256" version = "0.13.2" @@ -3640,7 +3556,7 @@ checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" dependencies = [ "ecdsa 0.16.6", "elliptic-curve 0.13.4", - "primeorder 0.13.1", + "primeorder", "sha2 0.10.6", ] @@ -3785,7 +3701,6 @@ dependencies = [ "async-trait", "futures", "libp2p", - "multiaddr", ] [[package]] @@ -3896,15 +3811,6 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "primeorder" -version = "0.12.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b54f7131b3dba65a2f414cf5bd25b66d4682e4608610668eae785750ba4c5b2" -dependencies = [ - "elliptic-curve 0.12.3", -] - [[package]] name = "primeorder" version = "0.13.1" @@ -3914,40 +3820,6 @@ dependencies = [ "elliptic-curve 0.13.4", ] -[[package]] -name = "proc-macro-crate" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e17d47ce914bf4de440332250b0edd23ce48c005f59fab39d3335866b114f11a" -dependencies = [ - "thiserror", - "toml", -] - -[[package]] -name = "proc-macro-error" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" -dependencies = [ - "proc-macro-error-attr", - "proc-macro2", - "quote", - "syn 1.0.109", - "version_check", -] - -[[package]] -name = "proc-macro-error-attr" -version = "1.0.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" -dependencies = [ - "proc-macro2", - "quote", - "version_check", -] - [[package]] name = "proc-macro-warning" version = "0.4.1" @@ -4603,15 +4475,6 @@ dependencies = [ "serde_derive", ] -[[package]] -name = "serde-big-array" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd31f59f6fe2b0c055371bb2f16d7f0aa7d8881676c04a55b1596d1a17cd10a4" -dependencies = [ - "serde", -] - [[package]] name = "serde_cbor" version = "0.11.2" @@ -5091,15 +4954,6 @@ dependencies = [ "tracing", ] -[[package]] -name = "toml" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" -dependencies = [ - "serde", -] - [[package]] name = "tower-service" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index 98e5e264421..547dbabbeb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -70,7 +70,7 @@ libp2p-dns = { version = "0.40.0", path = "transports/dns" } libp2p-floodsub = { version = "0.43.0", path = "protocols/floodsub" } libp2p-gossipsub = { version = "0.45.0", path = "protocols/gossipsub" } libp2p-identify = { version = "0.43.0", path = "protocols/identify" } -libp2p-identity = { version = "0.1.0" } +libp2p-identity = { version = "0.2.0" } libp2p-kad = { version = "0.44.0", path = "protocols/kad" } libp2p-mdns = { version = "0.44.0", path = "protocols/mdns" } libp2p-metrics = { version = "0.13.0", path = "misc/metrics" } @@ -99,3 +99,15 @@ multistream-select = { version = "0.13.0", path = "misc/multistream-select" } quick-protobuf-codec = { version = "0.2.0", path = "misc/quick-protobuf-codec" } quickcheck = { package = "quickcheck-ext", path = "misc/quickcheck-ext" } rw-stream-sink = { version = "0.4.0", path = "misc/rw-stream-sink" } +multiaddr = "0.18.0" +multihash = "0.19.0" + + +[patch.crates-io] + +# Patch away `libp2p-idnentity` in our dependency tree with the workspace version. +# `libp2p-identity` is a leaf dependency and used within `rust-multiaddr` which is **not** part of the workspace. +# As a result, we cannot just reference the workspace version in our crates because the types would mismatch with what +# we import via `rust-multiaddr`. +# This is expected to stay here until we move `libp2p-identity` to a separate repository which makes the dependency relationship more obvious. +libp2p-identity = { path = "identity" } diff --git a/core/Cargo.toml b/core/Cargo.toml index e5c13bceb62..84621df556e 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -18,8 +18,8 @@ futures-timer = "3" instant = "0.1.12" libp2p-identity = { workspace = true, features = ["peerid", "ed25519"] } log = "0.4" -multiaddr = { version = "0.17.1" } -multihash = { version = "0.17.0", default-features = false, features = ["std"] } +multiaddr = { workspace = true } +multihash = { workspace = true } multistream-select = { workspace = true } once_cell = "1.18.0" parking_lot = "0.12.0" @@ -37,7 +37,7 @@ void = "1" async-std = { version = "1.6.2", features = ["attributes"] } libp2p-mplex = { workspace = true } libp2p-noise = { workspace = true } -multihash = { version = "0.17.0", default-features = false, features = ["arb"] } +multihash = { workspace = true , features = ["arb"] } quickcheck = { workspace = true } libp2p-identity = { workspace = true, features = ["ed25519"] } diff --git a/core/src/peer_record.rs b/core/src/peer_record.rs index a73168a30f1..2fd6a39ef2b 100644 --- a/core/src/peer_record.rs +++ b/core/src/peer_record.rs @@ -138,7 +138,7 @@ pub enum FromEnvelopeError { InvalidPeerRecord(#[from] DecodeError), /// Failed to decode the peer ID. #[error("Failed to decode bytes as PeerId")] - InvalidPeerId(#[from] multihash::Error), + InvalidPeerId(#[from] libp2p_identity::ParseError), /// The signer of the envelope is different than the peer id in the record. #[error("The signer of the envelope is different than the peer id in the record")] MismatchedSignature, diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 11d65401aea..df14e79828b 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -231,7 +231,7 @@ fn main() -> Result<(), Box> { .dial( opts.relay_address .with(Protocol::P2pCircuit) - .with(Protocol::P2p(opts.remote_peer_id.unwrap().into())), + .with(Protocol::P2p(opts.remote_peer_id.unwrap())), ) .unwrap(); } diff --git a/examples/distributed-key-value-store/Cargo.toml b/examples/distributed-key-value-store/Cargo.toml index 8d9681e52d2..f8e0fd2339d 100644 --- a/examples/distributed-key-value-store/Cargo.toml +++ b/examples/distributed-key-value-store/Cargo.toml @@ -11,4 +11,3 @@ async-trait = "0.1" env_logger = "0.10" futures = "0.3.28" libp2p = { path = "../../libp2p", features = ["async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "websocket", "yamux"] } -multiaddr = { version = "0.17.1" } diff --git a/examples/file-sharing/Cargo.toml b/examples/file-sharing/Cargo.toml index aae3d4c3f65..9bb2f622f63 100644 --- a/examples/file-sharing/Cargo.toml +++ b/examples/file-sharing/Cargo.toml @@ -13,5 +13,4 @@ either = "1.8" env_logger = "0.10" futures = "0.3.28" libp2p = { path = "../../libp2p", features = ["async-std", "cbor", "dns", "kad", "noise", "macros", "request-response", "tcp", "websocket", "yamux"] } -multiaddr = { version = "0.17.1" } void = "1.0.2" diff --git a/examples/file-sharing/src/main.rs b/examples/file-sharing/src/main.rs index 9a0ec29ef7e..4b6d368fc47 100644 --- a/examples/file-sharing/src/main.rs +++ b/examples/file-sharing/src/main.rs @@ -27,7 +27,7 @@ use clap::Parser; use futures::prelude::*; use futures::StreamExt; -use libp2p::{core::Multiaddr, multiaddr::Protocol, PeerId}; +use libp2p::{core::Multiaddr, multiaddr::Protocol}; use std::error::Error; use std::io::Write; use std::path::PathBuf; @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box> { // In case the user provided an address of a peer on the CLI, dial it. if let Some(addr) = opt.peer { let peer_id = match addr.iter().last() { - Some(Protocol::P2p(hash)) => PeerId::from_multihash(hash).expect("Valid hash."), + Some(Protocol::P2p(peer_id)) => peer_id, _ => return Err("Expect peer multiaddr to contain peer ID.".into()), }; network_client diff --git a/examples/file-sharing/src/network.rs b/examples/file-sharing/src/network.rs index d8ecfb42a1c..db781a27a93 100644 --- a/examples/file-sharing/src/network.rs +++ b/examples/file-sharing/src/network.rs @@ -302,7 +302,7 @@ impl EventLoop { let local_peer_id = *self.swarm.local_peer_id(); eprintln!( "Local node is listening on {:?}", - address.with(Protocol::P2p(local_peer_id.into())) + address.with(Protocol::P2p(local_peer_id)) ); } SwarmEvent::IncomingConnection { .. } => {} @@ -350,10 +350,7 @@ impl EventLoop { .behaviour_mut() .kademlia .add_address(&peer_id, peer_addr.clone()); - match self - .swarm - .dial(peer_addr.with(Protocol::P2p(peer_id.into()))) - { + match self.swarm.dial(peer_addr.with(Protocol::P2p(peer_id))) { Ok(()) => { e.insert(sender); } diff --git a/examples/ipfs-private/Cargo.toml b/examples/ipfs-private/Cargo.toml index 5271f71c5bf..c1e860a08b3 100644 --- a/examples/ipfs-private/Cargo.toml +++ b/examples/ipfs-private/Cargo.toml @@ -12,4 +12,3 @@ either = "1.8" env_logger = "0.10" futures = "0.3.28" libp2p = { path = "../../libp2p", features = ["async-std", "gossipsub", "dns", "identify", "kad", "macros", "noise", "ping", "pnet", "tcp", "websocket", "yamux"] } -multiaddr = { version = "0.17.1" } \ No newline at end of file diff --git a/examples/ping-example/Cargo.toml b/examples/ping-example/Cargo.toml index db612b556f7..f1022b2dcd3 100644 --- a/examples/ping-example/Cargo.toml +++ b/examples/ping-example/Cargo.toml @@ -10,4 +10,3 @@ async-std = { version = "1.12", features = ["attributes"] } async-trait = "0.1" futures = "0.3.28" libp2p = { path = "../../libp2p", features = ["async-std", "dns", "macros", "noise", "ping", "tcp", "websocket", "yamux"] } -multiaddr = { version = "0.17.1" } \ No newline at end of file diff --git a/examples/rendezvous/src/bin/rzv-discover.rs b/examples/rendezvous/src/bin/rzv-discover.rs index 8eb8b623791..710b491ff0a 100644 --- a/examples/rendezvous/src/bin/rzv-discover.rs +++ b/examples/rendezvous/src/bin/rzv-discover.rs @@ -91,7 +91,7 @@ async fn main() { let peer = registration.record.peer_id(); log::info!("Discovered peer {} at {}", peer, address); - let p2p_suffix = Protocol::P2p(*peer.as_ref()); + let p2p_suffix = Protocol::P2p(peer); let address_with_p2p = if !address.ends_with(&Multiaddr::empty().with(p2p_suffix.clone())) { address.clone().with(p2p_suffix) diff --git a/libp2p/Cargo.toml b/libp2p/Cargo.toml index 3c80ddc07ce..ddea2d3ea2d 100644 --- a/libp2p/Cargo.toml +++ b/libp2p/Cargo.toml @@ -112,7 +112,7 @@ libp2p-swarm = { workspace = true } libp2p-wasm-ext = { workspace = true, optional = true } libp2p-yamux = { workspace = true, optional = true } -multiaddr = { version = "0.17.0" } +multiaddr = { workspace = true } pin-project = "1.0.0" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/misc/metrics/src/swarm.rs b/misc/metrics/src/swarm.rs index 9ad57003fa7..8837457d36a 100644 --- a/misc/metrics/src/swarm.rs +++ b/misc/metrics/src/swarm.rs @@ -259,9 +259,6 @@ impl super::Recorder record(OutgoingConnectionError::Aborted), - libp2p_swarm::DialError::InvalidPeerId { .. } => { - record(OutgoingConnectionError::InvalidPeerId) - } libp2p_swarm::DialError::WrongPeerId { .. } => { record(OutgoingConnectionError::WrongPeerId) } @@ -371,7 +368,6 @@ enum OutgoingConnectionError { NoAddresses, DialPeerConditionFalse, Aborted, - InvalidPeerId, WrongPeerId, TransportMultiaddrNotSupported, TransportOther, diff --git a/protocols/autonat/src/behaviour/as_server.rs b/protocols/autonat/src/behaviour/as_server.rs index 063943392f3..09c70a27e93 100644 --- a/protocols/autonat/src/behaviour/as_server.rs +++ b/protocols/autonat/src/behaviour/as_server.rs @@ -338,7 +338,7 @@ impl<'a> AsServer<'a> { let is_valid = addr.iter().all(|proto| match proto { Protocol::P2pCircuit => false, - Protocol::P2p(hash) => hash == peer.into(), + Protocol::P2p(peer_id) => peer_id == peer, _ => true, }); @@ -346,7 +346,7 @@ impl<'a> AsServer<'a> { return None; } if !addr.iter().any(|p| matches!(p, Protocol::P2p(_))) { - addr.push(Protocol::P2p(peer.into())) + addr.push(Protocol::P2p(peer)) } // Only collect distinct addresses. distinct.insert(addr.clone()).then_some(addr) @@ -380,26 +380,26 @@ mod test { let observed_addr = Multiaddr::empty() .with(observed_ip.clone()) .with(random_port()) - .with(Protocol::P2p(peer_id.into())); + .with(Protocol::P2p(peer_id)); // Valid address with matching peer-id let demanded_1 = Multiaddr::empty() .with(random_ip()) .with(random_port()) - .with(Protocol::P2p(peer_id.into())); + .with(Protocol::P2p(peer_id)); // Invalid because peer_id does not match let demanded_2 = Multiaddr::empty() .with(random_ip()) .with(random_port()) - .with(Protocol::P2p(PeerId::random().into())); + .with(Protocol::P2p(PeerId::random())); // Valid address without peer-id let demanded_3 = Multiaddr::empty().with(random_ip()).with(random_port()); // Invalid because relayed let demanded_4 = Multiaddr::empty() .with(random_ip()) .with(random_port()) - .with(Protocol::P2p(PeerId::random().into())) + .with(Protocol::P2p(PeerId::random())) .with(Protocol::P2pCircuit) - .with(Protocol::P2p(peer_id.into())); + .with(Protocol::P2p(peer_id)); let demanded = vec![ demanded_1.clone(), demanded_2, @@ -413,7 +413,7 @@ mod test { let expected_2 = demanded_3 .replace(0, |_| Some(observed_ip)) .unwrap() - .with(Protocol::P2p(peer_id.into())); + .with(Protocol::P2p(peer_id)); assert_eq!(filtered, vec![expected_1, expected_2]); } } diff --git a/protocols/autonat/tests/test_server.rs b/protocols/autonat/tests/test_server.rs index 0f14c6edb27..1bb5f624793 100644 --- a/protocols/autonat/tests/test_server.rs +++ b/protocols/autonat/tests/test_server.rs @@ -69,7 +69,7 @@ async fn test_dial_back() { let expect_addr = Multiaddr::empty() .with(Protocol::Ip4(observed_client_ip)) .with(Protocol::Tcp(client_port)) - .with(Protocol::P2p(client_id.into())); + .with(Protocol::P2p(client_id)); let request_probe_id = match server.next_behaviour_event().await { Event::InboundProbe(InboundProbeEvent::Request { peer, diff --git a/protocols/dcutr/src/behaviour_impl.rs b/protocols/dcutr/src/behaviour_impl.rs index 6e3406c6f13..4993da655d3 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -105,7 +105,7 @@ impl Behaviour { .iter() .cloned() .filter(|a| !a.iter().any(|p| p == Protocol::P2pCircuit)) - .map(|a| a.with(Protocol::P2p(self.local_peer_id.into()))) + .map(|a| a.with(Protocol::P2p(self.local_peer_id))) .collect() } diff --git a/protocols/dcutr/tests/lib.rs b/protocols/dcutr/tests/lib.rs index fedb3299df9..6888e5914a0 100644 --- a/protocols/dcutr/tests/lib.rs +++ b/protocols/dcutr/tests/lib.rs @@ -49,9 +49,9 @@ async fn connect() { async_std::task::spawn(relay.loop_on_next()); let dst_relayed_addr = relay_addr - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit) - .with(Protocol::P2p(dst_peer_id.into())); + .with(Protocol::P2p(dst_peer_id)); dst.listen_on(dst_relayed_addr.clone()).unwrap(); wait_for_reservation( @@ -84,7 +84,7 @@ async fn connect() { } } - let dst_addr = dst_addr.with(Protocol::P2p(dst_peer_id.into())); + let dst_addr = dst_addr.with(Protocol::P2p(dst_peer_id)); src.wait(move |e| match e { SwarmEvent::ConnectionEstablished { endpoint, .. } => { diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index 8081924fffd..f572b937d38 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -446,7 +446,7 @@ pub enum Event { fn multiaddr_matches_peer_id(addr: &Multiaddr, peer_id: &PeerId) -> bool { let last_component = addr.iter().last(); if let Some(multiaddr::Protocol::P2p(multi_addr_peer_id)) = last_component { - return multi_addr_peer_id == *peer_id.as_ref(); + return multi_addr_peer_id == *peer_id; } true } @@ -504,8 +504,8 @@ mod tests { let addr_without_peer_id: Multiaddr = addr.clone(); let mut addr_with_other_peer_id = addr.clone(); - addr.push(multiaddr::Protocol::P2p(peer_id.into())); - addr_with_other_peer_id.push(multiaddr::Protocol::P2p(other_peer_id.into())); + addr.push(multiaddr::Protocol::P2p(peer_id)); + addr_with_other_peer_id.push(multiaddr::Protocol::P2p(other_peer_id)); assert!(multiaddr_matches_peer_id(&addr, &peer_id)); assert!(!multiaddr_matches_peer_id( diff --git a/protocols/identify/tests/smoke.rs b/protocols/identify/tests/smoke.rs index 2cf7cc899bc..c70ab3181b4 100644 --- a/protocols/identify/tests/smoke.rs +++ b/protocols/identify/tests/smoke.rs @@ -56,7 +56,7 @@ async fn periodic_identify() { s1_info.observed_addr, swarm1_memory_listen .clone() - .with(Protocol::P2p(swarm1_peer_id.into())) + .with(Protocol::P2p(swarm1_peer_id)) ); assert!(s1_info.listen_addrs.contains(&swarm2_tcp_listen_addr)); assert!(s1_info.listen_addrs.contains(&swarm2_memory_listen)); diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 5d4b84c65f6..7df17c91e1a 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -1907,7 +1907,6 @@ where match error { DialError::LocalPeerId { .. } - | DialError::InvalidPeerId { .. } | DialError::WrongPeerId { .. } | DialError::Aborted | DialError::Denied { .. } diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index ce7712b9e8e..cd4337e9094 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -138,7 +138,7 @@ fn build_fully_connected_nodes_with_config( swarms } -fn random_multihash() -> Multihash { +fn random_multihash() -> Multihash<64> { Multihash::wrap(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()).unwrap() } @@ -1102,7 +1102,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() { let mut bob = build_node(); let key = Key::from( - Multihash::wrap(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()) + Multihash::<64>::wrap(SHA_256_MH, &thread_rng().gen::<[u8; 32]>()) .expect("32 array to fit into 64 byte multihash"), ); let record_bob = Record::new(key.clone(), b"bob".to_vec()); @@ -1254,7 +1254,7 @@ fn manual_bucket_inserts() { .skip(2) .map(|(a, s)| { let pid = *Swarm::local_peer_id(s); - let addr = a.clone().with(Protocol::P2p(pid.into())); + let addr = a.clone().with(Protocol::P2p(pid)); (addr, pid) }) .collect::>(); diff --git a/protocols/kad/src/kbucket/key.rs b/protocols/kad/src/kbucket/key.rs index 1c48184078a..15cedaaaa32 100644 --- a/protocols/kad/src/kbucket/key.rs +++ b/protocols/kad/src/kbucket/key.rs @@ -93,8 +93,8 @@ impl From> for KeyBytes { } } -impl From for Key { - fn from(m: Multihash) -> Self { +impl From> for Key> { + fn from(m: Multihash) -> Self { let bytes = KeyBytes(Sha256::digest(m.to_bytes())); Key { preimage: m, bytes } } @@ -205,8 +205,8 @@ mod tests { } } - impl Arbitrary for Key { - fn arbitrary(g: &mut Gen) -> Key { + impl Arbitrary for Key> { + fn arbitrary(g: &mut Gen) -> Key> { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); Key::from(Multihash::wrap(SHA_256_MH, &hash).unwrap()) } diff --git a/protocols/kad/src/record_priv.rs b/protocols/kad/src/record_priv.rs index 2abe32d5894..4eb8e861c6f 100644 --- a/protocols/kad/src/record_priv.rs +++ b/protocols/kad/src/record_priv.rs @@ -66,8 +66,8 @@ impl From> for Key { } } -impl From for Key { - fn from(m: Multihash) -> Key { +impl From> for Key { + fn from(m: Multihash) -> Key { Key::from(m.to_bytes()) } } @@ -168,7 +168,7 @@ mod tests { impl Arbitrary for Key { fn arbitrary(g: &mut Gen) -> Key { let hash: [u8; 32] = core::array::from_fn(|_| u8::arbitrary(g)); - Key::from(Multihash::wrap(SHA_256_MH, &hash).unwrap()) + Key::from(Multihash::<64>::wrap(SHA_256_MH, &hash).unwrap()) } } diff --git a/protocols/kad/src/record_priv/store/memory.rs b/protocols/kad/src/record_priv/store/memory.rs index 1d4caab3bd7..edeae188ac6 100644 --- a/protocols/kad/src/record_priv/store/memory.rs +++ b/protocols/kad/src/record_priv/store/memory.rs @@ -221,7 +221,7 @@ mod tests { use quickcheck::*; use rand::Rng; - fn random_multihash() -> Multihash { + fn random_multihash() -> Multihash<64> { Multihash::wrap(SHA_256_MH, &rand::thread_rng().gen::<[u8; 32]>()).unwrap() } diff --git a/protocols/mdns/src/behaviour/iface/query.rs b/protocols/mdns/src/behaviour/iface/query.rs index 745926cf658..0185028f6ff 100644 --- a/protocols/mdns/src/behaviour/iface/query.rs +++ b/protocols/mdns/src/behaviour/iface/query.rs @@ -26,7 +26,7 @@ use libp2p_core::{ }; use libp2p_identity::PeerId; use std::time::Instant; -use std::{convert::TryFrom, fmt, net::SocketAddr, str, time::Duration}; +use std::{fmt, net::SocketAddr, str, time::Duration}; use trust_dns_proto::{ op::Message, rr::{Name, RData}, @@ -264,16 +264,12 @@ impl MdnsPeer { }; match addr.pop() { Some(Protocol::P2p(peer_id)) => { - if let Ok(peer_id) = PeerId::try_from(peer_id) { - if let Some(pid) = &my_peer_id { - if peer_id != *pid { - return None; - } - } else { - my_peer_id.replace(peer_id); + if let Some(pid) = &my_peer_id { + if peer_id != *pid { + return None; } } else { - return None; + my_peer_id.replace(peer_id); } } _ => return None, @@ -329,8 +325,8 @@ mod tests { let mut addr1: Multiaddr = "/ip4/1.2.3.4/tcp/5000".parse().expect("bad multiaddress"); let mut addr2: Multiaddr = "/ip6/::1/udp/10000".parse().expect("bad multiaddress"); - addr1.push(Protocol::P2p(peer_id.into())); - addr2.push(Protocol::P2p(peer_id.into())); + addr1.push(Protocol::P2p(peer_id)); + addr2.push(Protocol::P2p(peer_id)); let packets = build_query_response( 0xf8f8, diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 7349c67ad7a..eb2f662581f 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -812,7 +812,7 @@ impl Action { // Add local peer ID in case it isn't present yet. .filter_map(|a| match a.iter().last()? { Protocol::P2p(_) => Some(a), - _ => Some(a.with(Protocol::P2p(local_peer_id.into()))), + _ => Some(a.with(Protocol::P2p(local_peer_id))), }) .collect(), }), diff --git a/protocols/relay/src/priv_client/handler.rs b/protocols/relay/src/priv_client/handler.rs index 414ec4fc8df..9613d7d6b3e 100644 --- a/protocols/relay/src/priv_client/handler.rs +++ b/protocols/relay/src/priv_client/handler.rs @@ -589,7 +589,7 @@ impl Reservation { .into_iter() .map(|a| { a.with(Protocol::P2pCircuit) - .with(Protocol::P2p(local_peer_id.into())) + .with(Protocol::P2p(local_peer_id)) }) .collect(), }, diff --git a/protocols/relay/src/priv_client/transport.rs b/protocols/relay/src/priv_client/transport.rs index e54c7850195..45cc685aea0 100644 --- a/protocols/relay/src/priv_client/transport.rs +++ b/protocols/relay/src/priv_client/transport.rs @@ -269,9 +269,7 @@ fn parse_relayed_multiaddr(addr: Multiaddr) -> Result { - let peer_id = PeerId::from_multihash(hash).map_err(|_| Error::InvalidHash)?; - + Protocol::P2p(peer_id) => { if before_circuit { if relayed_multiaddr.relay_peer_id.is_some() { return Err(Error::MalformedMultiaddr.into()); @@ -380,7 +378,7 @@ impl Stream for Listener { upgrade: ready(Ok(stream)), listener_id, local_addr: relay_addr.with(Protocol::P2pCircuit), - send_back_addr: Protocol::P2p(src_peer_id.into()).into(), + send_back_addr: Protocol::P2p(src_peer_id).into(), }) } ToListenerMsg::Reservation(Err(())) => self.close(Err(Error::Reservation)), diff --git a/protocols/relay/tests/lib.rs b/protocols/relay/tests/lib.rs index f865b64856f..fa79ab67f4b 100644 --- a/protocols/relay/tests/lib.rs +++ b/protocols/relay/tests/lib.rs @@ -51,7 +51,7 @@ fn reservation() { spawn_swarm_on_pool(&pool, relay); let client_addr = relay_addr - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit); let mut client = build_client(); let client_peer_id = *client.local_peer_id(); @@ -64,9 +64,7 @@ fn reservation() { // Wait for initial reservation. pool.run_until(wait_for_reservation( &mut client, - client_addr - .clone() - .with(Protocol::P2p(client_peer_id.into())), + client_addr.clone().with(Protocol::P2p(client_peer_id)), relay_peer_id, false, // No renewal. )); @@ -74,7 +72,7 @@ fn reservation() { // Wait for renewal. pool.run_until(wait_for_reservation( &mut client, - client_addr.with(Protocol::P2p(client_peer_id.into())), + client_addr.with(Protocol::P2p(client_peer_id)), relay_peer_id, true, // Renewal. )); @@ -96,11 +94,9 @@ fn new_reservation_to_same_relay_replaces_old() { let mut client = build_client(); let client_peer_id = *client.local_peer_id(); let client_addr = relay_addr - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit); - let client_addr_with_peer_id = client_addr - .clone() - .with(Protocol::P2p(client_peer_id.into())); + let client_addr_with_peer_id = client_addr.clone().with(Protocol::P2p(client_peer_id)); let old_listener = client.listen_on(client_addr.clone()).unwrap(); @@ -189,9 +185,9 @@ fn connect() { let mut dst = build_client(); let dst_peer_id = *dst.local_peer_id(); let dst_addr = relay_addr - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit) - .with(Protocol::P2p(dst_peer_id.into())); + .with(Protocol::P2p(dst_peer_id)); dst.listen_on(dst_addr.clone()).unwrap(); @@ -242,8 +238,13 @@ async fn connection_established_to( if peer == relay_peer_id => {} SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == other => break, SwarmEvent::IncomingConnection { send_back_addr, .. } => { - let peer_id_from_addr = - PeerId::try_from_multiaddr(&send_back_addr).expect("to have /p2p"); + let peer_id_from_addr = send_back_addr + .iter() + .find_map(|protocol| match protocol { + Protocol::P2p(peer_id) => Some(peer_id), + _ => None, + }) + .expect("to have /p2p"); assert_eq!(peer_id_from_addr, other) } @@ -263,9 +264,9 @@ fn handle_dial_failure() { let mut client = build_client(); let client_peer_id = *client.local_peer_id(); let client_addr = relay_addr - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit) - .with(Protocol::P2p(client_peer_id.into())); + .with(Protocol::P2p(client_peer_id)); client.listen_on(client_addr).unwrap(); assert!(!pool.run_until(wait_for_dial(&mut client, relay_peer_id))); @@ -286,7 +287,7 @@ fn reuse_connection() { let client_addr = relay_addr .clone() - .with(Protocol::P2p(relay_peer_id.into())) + .with(Protocol::P2p(relay_peer_id)) .with(Protocol::P2pCircuit); let mut client = build_client(); let client_peer_id = *client.local_peer_id(); @@ -298,7 +299,7 @@ fn reuse_connection() { pool.run_until(wait_for_reservation( &mut client, - client_addr.with(Protocol::P2p(client_peer_id.into())), + client_addr.with(Protocol::P2p(client_peer_id)), relay_peer_id, false, // No renewal. )); diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 6c3b2069b0d..d18629a0eb0 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -56,6 +56,9 @@ - Rename `ConnectionHandlerEvent::Custom` to `ConnectionHandlerEvent::NotifyBehaviour`. See [PR 3955]. +- Remove `DialError::InvalidPeerId` variant. With the move to `multiaddr` `v0.18.0` peer IDs in `/p2p` are type safe and thus usage of the contained peer ID can not result in a parsing error. + See [PR 4037]. + - Remove deprecated items. See [PR 3956]. [PR 3605]: https://github.com/libp2p/rust-libp2p/pull/3605 @@ -73,6 +76,7 @@ [PR 3927]: https://github.com/libp2p/rust-libp2p/pull/3927 [PR 3955]: https://github.com/libp2p/rust-libp2p/pull/3955 [PR 3956]: https://github.com/libp2p/rust-libp2p/pull/3956 +[PR 4037]: https://github.com/libp2p/rust-libp2p/pull/4037 ## 0.42.2 diff --git a/swarm/src/dial_opts.rs b/swarm/src/dial_opts.rs index dc3d94cb83b..9be7280b3df 100644 --- a/swarm/src/dial_opts.rs +++ b/swarm/src/dial_opts.rs @@ -22,7 +22,6 @@ use crate::ConnectionId; use libp2p_core::connection::Endpoint; use libp2p_core::multiaddr::Protocol; -use libp2p_core::multihash::Multihash; use libp2p_core::Multiaddr; use libp2p_identity::PeerId; use std::num::NonZeroU8; @@ -81,9 +80,21 @@ impl DialOpts { WithoutPeerId {} } - /// Get the [`PeerId`] specified in a [`DialOpts`] if any. + /// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to extract it + /// from the multihash in the `/p2p` part of the address, if present. pub fn get_peer_id(&self) -> Option { - self.peer_id + if let Some(peer_id) = self.peer_id { + return Some(peer_id); + } + + let first_address = self.addresses.first()?; + let last_protocol = first_address.iter().last()?; + + if let Protocol::P2p(p) = last_protocol { + return Some(p); + } + + None } /// Get the [`ConnectionId`] of this dial attempt. @@ -94,40 +105,6 @@ impl DialOpts { self.connection_id } - /// Retrieves the [`PeerId`] from the [`DialOpts`] if specified or otherwise tries to parse it - /// from the multihash in the `/p2p` part of the address, if present. - /// - /// Note: A [`Multiaddr`] with something else other than a [`PeerId`] within the `/p2p` protocol is invalid as per specification. - /// Unfortunately, we are not making good use of the type system here. - /// Really, this function should be merged with [`DialOpts::get_peer_id`] above. - /// If it weren't for the parsing error, the function signatures would be the same. - /// - /// See . - pub(crate) fn get_or_parse_peer_id(&self) -> Result, Multihash> { - if let Some(peer_id) = self.peer_id { - return Ok(Some(peer_id)); - } - - let first_address = match self.addresses.first() { - Some(first_address) => first_address, - None => return Ok(None), - }; - - let maybe_peer_id = first_address - .iter() - .last() - .and_then(|p| { - if let Protocol::P2p(ma) = p { - Some(PeerId::try_from(ma)) - } else { - None - } - }) - .transpose()?; - - Ok(maybe_peer_id) - } - pub(crate) fn get_addresses(&self) -> Vec { self.addresses.clone() } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index ce80c7f0c5f..03e6a1efe57 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -136,7 +136,6 @@ use futures::{prelude::*, stream::FusedStream}; use libp2p_core::{ connection::ConnectedPoint, multiaddr, - multihash::Multihash, muxing::StreamMuxerBox, transport::{self, ListenerId, TransportError, TransportEvent}, Endpoint, Multiaddr, Transport, @@ -415,9 +414,7 @@ where pub fn dial(&mut self, opts: impl Into) -> Result<(), DialError> { let dial_opts = opts.into(); - let peer_id = dial_opts - .get_or_parse_peer_id() - .map_err(DialError::InvalidPeerId)?; + let peer_id = dial_opts.get_peer_id(); let condition = dial_opts.peer_condition(); let connection_id = dial_opts.connection_id(); @@ -1008,11 +1005,11 @@ where match event { ToSwarm::GenerateEvent(event) => return Some(SwarmEvent::Behaviour(event)), ToSwarm::Dial { opts } => { - let peer_id = opts.get_or_parse_peer_id(); + let peer_id = opts.get_peer_id(); let connection_id = opts.connection_id(); if let Ok(()) = self.dial(opts) { return Some(SwarmEvent::Dialing { - peer_id: peer_id.ok().flatten(), + peer_id, connection_id, }); } @@ -1519,8 +1516,6 @@ pub enum DialError { DialPeerConditionFalse(dial_opts::PeerCondition), /// Pending connection attempt has been aborted. Aborted, - /// The provided peer identity is invalid. - InvalidPeerId(Multihash), /// The peer identity obtained on the connection did not match the one that was expected. WrongPeerId { obtained: PeerId, @@ -1561,9 +1556,6 @@ impl fmt::Display for DialError { f, "Dial error: Pending connection attempt has been aborted." ), - DialError::InvalidPeerId(multihash) => { - write!(f, "Dial error: multihash {multihash:?} is not a PeerId") - } DialError::WrongPeerId { obtained, endpoint } => write!( f, "Dial error: Unexpected peer ID {obtained} at {endpoint:?}." @@ -1604,7 +1596,6 @@ impl error::Error for DialError { DialError::NoAddresses => None, DialError::DialPeerConditionFalse(_) => None, DialError::Aborted => None, - DialError::InvalidPeerId { .. } => None, DialError::WrongPeerId { .. } => None, DialError::Transport(_) => None, DialError::Denied { cause } => Some(cause), @@ -1763,14 +1754,15 @@ fn p2p_addr(peer: Option, addr: Multiaddr) -> Result return Ok(addr), }; - if let Some(multiaddr::Protocol::P2p(hash)) = addr.iter().last() { - if &hash != peer.as_ref() { + if let Some(multiaddr::Protocol::P2p(peer_id)) = addr.iter().last() { + if peer_id != peer { return Err(addr); } - Ok(addr) - } else { - Ok(addr.with(multiaddr::Protocol::P2p(peer.into()))) + + return Ok(addr); } + + Ok(addr.with(multiaddr::Protocol::P2p(peer))) } #[cfg(test)] @@ -2173,7 +2165,7 @@ mod tests { })); let other_id = PeerId::random(); - let other_addr = address.with(multiaddr::Protocol::P2p(other_id.into())); + let other_addr = address.with(multiaddr::Protocol::P2p(other_id)); swarm2.dial(other_addr.clone()).unwrap(); @@ -2322,7 +2314,7 @@ mod tests { let failed_addresses = errors.into_iter().map(|(addr, _)| addr).collect::>(); let expected_addresses = addresses .into_iter() - .map(|addr| addr.with(multiaddr::Protocol::P2p(target.into()))) + .map(|addr| addr.with(multiaddr::Protocol::P2p(target))) .collect::>(); assert_eq!(expected_addresses, failed_addresses); diff --git a/transports/plaintext/src/error.rs b/transports/plaintext/src/error.rs index 8ed4aeabc3e..23f196f147f 100644 --- a/transports/plaintext/src/error.rs +++ b/transports/plaintext/src/error.rs @@ -34,7 +34,7 @@ pub enum PlainTextError { InvalidPublicKey(libp2p_identity::DecodingError), /// Failed to parse the [`PeerId`](libp2p_identity::PeerId) from bytes in the protobuf message. - InvalidPeerId(libp2p_core::multihash::Error), + InvalidPeerId(libp2p_identity::ParseError), /// The peer id of the exchange isn't consistent with the remote public key. PeerIdMismatch, @@ -99,8 +99,8 @@ impl From for PlainTextError { } } -impl From for PlainTextError { - fn from(err: libp2p_core::multihash::Error) -> PlainTextError { +impl From for PlainTextError { + fn from(err: libp2p_identity::ParseError) -> PlainTextError { PlainTextError::InvalidPeerId(err) } } diff --git a/transports/quic/tests/smoke.rs b/transports/quic/tests/smoke.rs index 93bb78e2ae8..8a6d689a7b0 100644 --- a/transports/quic/tests/smoke.rs +++ b/transports/quic/tests/smoke.rs @@ -215,7 +215,7 @@ async fn wrong_peerid() { let (b_peer_id, mut b_transport) = create_default_transport::(); let a_addr = start_listening(&mut a_transport, "/ip6/::1/udp/0/quic-v1").await; - let a_addr_random_peer = a_addr.with(Protocol::P2p(PeerId::random().into())); + let a_addr_random_peer = a_addr.with(Protocol::P2p(PeerId::random())); let ((a_connected, _, _), (b_connected, _)) = connect(&mut a_transport, &mut b_transport, a_addr_random_peer).await; diff --git a/transports/tcp/src/lib.rs b/transports/tcp/src/lib.rs index bf53706b75d..b1737cf7bed 100644 --- a/transports/tcp/src/lib.rs +++ b/transports/tcp/src/lib.rs @@ -1280,7 +1280,7 @@ mod tests { let tcp_observed_addr = Multiaddr::empty() .with(Protocol::Ip4(observed_ip)) .with(Protocol::Tcp(1)) - .with(Protocol::P2p(PeerId::random().into())); + .with(Protocol::P2p(PeerId::random())); let translated = transport .address_translation(&tcp_listen_addr, &tcp_observed_addr) diff --git a/transports/webrtc/Cargo.toml b/transports/webrtc/Cargo.toml index 1019a49678f..e4e24b42b42 100644 --- a/transports/webrtc/Cargo.toml +++ b/transports/webrtc/Cargo.toml @@ -23,7 +23,7 @@ libp2p-noise = { workspace = true } libp2p-identity = { workspace = true } log = "0.4" sha2 = "0.10.6" -multihash = { version = "0.17.0", default-features = false } +multihash = { workspace = true } quick-protobuf = "0.8" quick-protobuf-codec = { workspace = true } rand = "0.8" diff --git a/transports/webrtc/src/tokio/fingerprint.rs b/transports/webrtc/src/tokio/fingerprint.rs index 3776f0dc24e..c3d58d64e72 100644 --- a/transports/webrtc/src/tokio/fingerprint.rs +++ b/transports/webrtc/src/tokio/fingerprint.rs @@ -18,7 +18,6 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use multihash::MultihashGeneric; use sha2::Digest as _; use std::fmt; use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint; @@ -26,7 +25,7 @@ use webrtc::dtls_transport::dtls_fingerprint::RTCDtlsFingerprint; const SHA256: &str = "sha-256"; const MULTIHASH_SHA256_CODE: u64 = 0x12; -type Multihash = MultihashGeneric<64>; +type Multihash = multihash::Multihash<64>; /// A certificate fingerprint that is assumed to be created using the SHA256 hash algorithm. #[derive(Eq, PartialEq, Copy, Clone)] @@ -57,7 +56,7 @@ impl Fingerprint { Some(Self(buf)) } - /// Converts [`Multihash`](MultihashGeneric) to [`Fingerprint`]. + /// Converts [`Multihash`](multihash::Multihash) to [`Fingerprint`]. pub fn try_from_multihash(hash: Multihash) -> Option { if hash.code() != MULTIHASH_SHA256_CODE { // Only support SHA256 for now. @@ -69,7 +68,7 @@ impl Fingerprint { Some(Self(bytes)) } - /// Converts this fingerprint to [`Multihash`](MultihashGeneric). + /// Converts this fingerprint to [`Multihash`](multihash::Multihash). pub fn to_multihash(self) -> Multihash { Multihash::wrap(MULTIHASH_SHA256_CODE, &self.0).expect("fingerprint's len to be 32 bytes") } diff --git a/transports/webrtc/src/tokio/transport.rs b/transports/webrtc/src/tokio/transport.rs index ba1bb95fb54..faac75b24d5 100644 --- a/transports/webrtc/src/tokio/transport.rs +++ b/transports/webrtc/src/tokio/transport.rs @@ -393,7 +393,7 @@ fn socketaddr_to_multiaddr(socket_addr: &SocketAddr, certhash: Option Option { let webrtc = iter.next()?; let port = match (port, webrtc) { - (Protocol::Udp(port), Protocol::WebRTC) => port, + (Protocol::Udp(port), Protocol::WebRTCDirect) => port, _ => return None, }; @@ -442,7 +442,7 @@ fn parse_webrtc_dial_addr(addr: &Multiaddr) -> Option<(SocketAddr, Fingerprint)> let certhash = iter.next()?; let (port, fingerprint) = match (port, webrtc, certhash) { - (Protocol::Udp(port), Protocol::WebRTC, Protocol::Certhash(cert_hash)) => { + (Protocol::Udp(port), Protocol::WebRTCDirect, Protocol::Certhash(cert_hash)) => { let fingerprint = Fingerprint::try_from_multihash(cert_hash)?; (port, fingerprint) @@ -617,7 +617,10 @@ mod tests { assert!( matches!(listen_addr.iter().nth(1), Some(Protocol::Udp(port)) if port != 0) ); - assert!(matches!(listen_addr.iter().nth(2), Some(Protocol::WebRTC))); + assert!(matches!( + listen_addr.iter().nth(2), + Some(Protocol::WebRTCDirect) + )); } e => panic!("Unexpected event: {e:?}"), } diff --git a/transports/websocket/src/lib.rs b/transports/websocket/src/lib.rs index 7462f281817..01c02b15320 100644 --- a/transports/websocket/src/lib.rs +++ b/transports/websocket/src/lib.rs @@ -338,7 +338,7 @@ mod tests { let outbound = new_ws_config() .boxed() - .dial(addr.with(Protocol::P2p(PeerId::random().into()))) + .dial(addr.with(Protocol::P2p(PeerId::random()))) .unwrap(); let (a, b) = futures::join!(inbound, outbound);