diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bd3737a49f4..f9dcb413822 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' + if: env.CRATE != 'libp2p' && env.CRATE != 'libp2p-identity' 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 512fb637071..16959ee0179 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -617,6 +617,12 @@ 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" @@ -1330,6 +1336,18 @@ 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" @@ -2256,7 +2274,7 @@ dependencies = [ "base64 0.21.2", "clap", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "serde", "serde_json", "zeroize", @@ -2313,7 +2331,7 @@ dependencies = [ "libp2p-floodsub", "libp2p-gossipsub", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-kad", "libp2p-mdns", "libp2p-metrics", @@ -2344,7 +2362,7 @@ version = "0.2.0" dependencies = [ "async-std", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-swarm", "libp2p-swarm-derive", "libp2p-swarm-test", @@ -2362,7 +2380,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-request-response", "libp2p-swarm", "libp2p-swarm-test", @@ -2378,7 +2396,7 @@ dependencies = [ "async-std", "libp2p-core", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-ping", "libp2p-swarm", "libp2p-swarm-derive", @@ -2398,12 +2416,12 @@ dependencies = [ "futures", "futures-timer", "instant", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-mplex", "libp2p-noise", "log", "multiaddr", - "multihash", + "multihash 0.17.0", "multistream-select", "once_cell", "parking_lot", @@ -2434,7 +2452,7 @@ dependencies = [ "libp2p-core", "libp2p-dns", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-ping", "libp2p-plaintext", @@ -2474,7 +2492,7 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "log", "parking_lot", "smallvec", @@ -2491,7 +2509,7 @@ dependencies = [ "fnv", "futures", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-swarm", "log", "quick-protobuf", @@ -2520,7 +2538,7 @@ dependencies = [ "hex_fmt", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2550,7 +2568,7 @@ dependencies = [ "futures", "futures-timer", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-swarm", "libp2p-swarm-test", "log", @@ -2562,20 +2580,44 @@ 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", + "bs58 0.5.0", "criterion", "ed25519-dalek", "hex-literal", "libsecp256k1", "log", - "multiaddr", - "multihash", + "multihash 0.19.0", "p256 0.13.2", "quick-protobuf", "quickcheck-ext", @@ -2607,7 +2649,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2636,7 +2678,7 @@ dependencies = [ "futures", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2660,7 +2702,7 @@ dependencies = [ "libp2p-dcutr", "libp2p-gossipsub", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-kad", "libp2p-ping", "libp2p-relay", @@ -2680,7 +2722,7 @@ dependencies = [ "env_logger 0.10.0", "futures", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-muxer-test-harness", "libp2p-plaintext", "libp2p-tcp", @@ -2714,7 +2756,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "log", "once_cell", "quick-protobuf", @@ -2740,7 +2782,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-dns", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-quic", "libp2p-request-response", "libp2p-swarm", @@ -2768,7 +2810,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-swarm", "libp2p-swarm-test", "log", @@ -2787,7 +2829,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "log", "quick-protobuf", "quickcheck-ext", @@ -2801,7 +2843,7 @@ version = "0.23.0" dependencies = [ "futures", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-swarm", "libp2p-tcp", @@ -2827,7 +2869,7 @@ dependencies = [ "futures-timer", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-muxer-test-harness", "libp2p-noise", "libp2p-tcp", @@ -2855,7 +2897,7 @@ dependencies = [ "futures-timer", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-ping", "libp2p-plaintext", "libp2p-swarm", @@ -2883,7 +2925,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-ping", "libp2p-swarm", @@ -2910,7 +2952,7 @@ dependencies = [ "futures_ringbuf", "instant", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-swarm", "libp2p-swarm-test", @@ -2939,7 +2981,7 @@ dependencies = [ "instant", "libp2p-core", "libp2p-identify", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-kad", "libp2p-ping", "libp2p-plaintext", @@ -2977,7 +3019,7 @@ dependencies = [ "futures", "futures-timer", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-plaintext", "libp2p-swarm", "libp2p-tcp", @@ -2998,7 +3040,7 @@ dependencies = [ "if-watch", "libc", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "log", "socket2 0.5.3", "tokio", @@ -3013,7 +3055,7 @@ dependencies = [ "hex", "hex-literal", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-swarm", "libp2p-yamux", "rcgen 0.10.0", @@ -3065,12 +3107,12 @@ dependencies = [ "hex-literal", "if-watch", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-noise", "libp2p-ping", "libp2p-swarm", "log", - "multihash", + "multihash 0.17.0", "quick-protobuf", "quick-protobuf-codec", "quickcheck", @@ -3098,7 +3140,7 @@ dependencies = [ "futures-rustls 0.22.2", "libp2p-core", "libp2p-dns", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-tcp", "log", "parking_lot", @@ -3316,7 +3358,7 @@ dependencies = [ "data-encoding", "log", "multibase", - "multihash", + "multihash 0.17.0", "percent-encoding", "serde", "static_assertions", @@ -3351,6 +3393,16 @@ dependencies = [ "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" @@ -3375,7 +3427,7 @@ dependencies = [ "futures", "futures_ringbuf", "libp2p-core", - "libp2p-identity", + "libp2p-identity 0.1.2", "libp2p-plaintext", "libp2p-swarm", "libp2p-yamux", @@ -3570,6 +3622,18 @@ 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" @@ -3578,7 +3642,7 @@ checksum = "c9863ad85fa8f4460f9c48cb909d38a0d689dba1f6f6988a5e3e0d31071bcd4b" dependencies = [ "ecdsa 0.16.6", "elliptic-curve 0.13.4", - "primeorder", + "primeorder 0.13.1", "sha2 0.10.6", ] @@ -3834,6 +3898,15 @@ 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" diff --git a/Cargo.toml b/Cargo.toml index 8400238368d..98e5e264421 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.2.0", path = "identity" } +libp2p-identity = { version = "0.1.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" } diff --git a/identity/CHANGELOG.md b/identity/CHANGELOG.md index d808b466ceb..fcf8bf6ba81 100644 --- a/identity/CHANGELOG.md +++ b/identity/CHANGELOG.md @@ -13,6 +13,14 @@ - Remove deprecated items. See [PR 3928]. +- Remove `PeerId::try_from_multiaddr`. + `multiaddr::Protocol::P2p` is now type-safe and contains a `PeerId` directly, rendering this function obsolete. + See [PR 3656]. + +- Remove `PeerId::is_public_key` because it is unused and can be implemented externally. + See [PR 3656]. + +[PR 3656]: https://github.com/libp2p/rust-libp2p/pull/3656 [PR 3850]: https://github.com/libp2p/rust-libp2p/pull/3850 [PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715 [PR 3863]: https://github.com/libp2p/rust-libp2p/pull/3863 diff --git a/identity/Cargo.toml b/identity/Cargo.toml index 1235f0f54be..ca987c6b7b2 100644 --- a/identity/Cargo.toml +++ b/identity/Cargo.toml @@ -17,8 +17,7 @@ bs58 = { version = "0.5.0", optional = true } ed25519-dalek = { version = "1.0.1", optional = true } libsecp256k1 = { version = "0.7.0", optional = true } log = "0.4" -multiaddr = { version = "0.17.1", optional = true } -multihash = { version = "0.17.0", default-features = false, features = ["std"], optional = true } +multihash = { version = "0.19.0", optional = true } p256 = { version = "0.13", default-features = false, features = ["ecdsa", "std", "pem"], optional = true } quick-protobuf = "0.8.1" rand = { version = "0.8", optional = true } @@ -37,7 +36,7 @@ secp256k1 = [ "dep:libsecp256k1", "dep:asn1_der", "dep:rand", "dep:sha2", "dep:z ecdsa = [ "dep:p256", "dep:rand", "dep:void", "dep:zeroize", "dep:sec1" ] rsa = [ "dep:ring", "dep:asn1_der", "dep:rand", "dep:zeroize" ] ed25519 = [ "dep:ed25519-dalek", "dep:rand", "dep:zeroize" ] -peerid = [ "dep:multihash", "dep:multiaddr", "dep:bs58", "dep:rand", "dep:thiserror", "dep:sha2" ] +peerid = [ "dep:multihash", "dep:bs58", "dep:rand", "dep:thiserror", "dep:sha2" ] [dev-dependencies] quickcheck = { workspace = true } diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 184b75aa785..60ded4ad37c 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -18,8 +18,6 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -use multiaddr::{Multiaddr, Protocol}; -use multihash::{Code, Error, MultihashGeneric}; use rand::Rng; use sha2::Digest as _; use std::{convert::TryFrom, fmt, str::FromStr}; @@ -30,7 +28,7 @@ use thiserror::Error; /// Must be big enough to accommodate for `MAX_INLINE_KEY_LENGTH`. /// 64 satisfies that and can hold 512 bit hashes which is what the ecosystem typically uses. /// Given that this appears in our type-signature, using a "common" number here makes us more compatible. -type Multihash = MultihashGeneric<64>; +type Multihash = multihash::Multihash<64>; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -80,9 +78,9 @@ impl PeerId { } /// Parses a `PeerId` from bytes. - pub fn from_bytes(data: &[u8]) -> Result { + pub fn from_bytes(data: &[u8]) -> Result { PeerId::from_multihash(Multihash::from_bytes(data)?) - .map_err(|mh| Error::UnsupportedCode(mh.code())) + .map_err(|mh| ParseError::UnsupportedCode(mh.code())) } /// Tries to turn a `Multihash` into a `PeerId`. @@ -100,25 +98,13 @@ impl PeerId { } } - /// Tries to extract a [`PeerId`] from the given [`Multiaddr`]. - /// - /// In case the given [`Multiaddr`] ends with `/p2p/`, this function - /// will return the encapsulated [`PeerId`], otherwise it will return `None`. - pub fn try_from_multiaddr(address: &Multiaddr) -> Option { - address.iter().last().and_then(|p| match p { - Protocol::P2p(hash) => PeerId::from_multihash(hash).ok(), - _ => None, - }) - } - /// Generates a random peer ID from a cryptographically secure PRNG. /// /// This is useful for randomly walking on a DHT, or for testing purposes. pub fn random() -> PeerId { let peer_id = rand::thread_rng().gen::<[u8; 32]>(); PeerId { - multihash: Multihash::wrap(Code::Identity.into(), &peer_id) - .expect("The digest size is never too large"), + multihash: Multihash::wrap(0x0, &peer_id).expect("The digest size is never too large"), } } @@ -131,19 +117,6 @@ impl PeerId { pub fn to_base58(&self) -> String { bs58::encode(self.to_bytes()).into_string() } - - /// Checks whether the public key passed as parameter matches the public key of this `PeerId`. - /// - /// Returns `None` if this `PeerId`s hash algorithm is not supported when encoding the - /// given public key, otherwise `Some` boolean as the result of an equality check. - pub fn is_public_key(&self, public_key: &crate::PublicKey) -> Option { - use multihash::MultihashDigest as _; - - let alg = Code::try_from(self.multihash.code()) - .expect("Internal multihash is always a valid `Code`"); - let enc = public_key.encode_protobuf(); - Some(alg.digest(&enc) == self.multihash) - } } impl From for PeerId { @@ -246,12 +219,15 @@ impl<'de> Deserialize<'de> for PeerId { } } +/// Error when parsing a [`PeerId`] from string or bytes. #[derive(Debug, Error)] pub enum ParseError { #[error("base-58 decode error: {0}")] B58(#[from] bs58::decode::Error), - #[error("decoding multihash failed")] - MultiHash, + #[error("unsupported multihash code '{0}'")] + UnsupportedCode(u64), + #[error("invalid multihash")] + InvalidMultihash(#[from] multihash::Error), } impl FromStr for PeerId { @@ -260,7 +236,9 @@ impl FromStr for PeerId { #[inline] fn from_str(s: &str) -> Result { let bytes = bs58::decode(s).into_vec()?; - PeerId::from_bytes(&bytes).map_err(|_| ParseError::MultiHash) + let peer_id = PeerId::from_bytes(&bytes)?; + + Ok(peer_id) } } @@ -268,14 +246,6 @@ impl FromStr for PeerId { mod tests { use super::*; - #[test] - #[cfg(feature = "ed25519")] - fn peer_id_is_public_key() { - let key = crate::Keypair::generate_ed25519().public(); - let peer_id = key.to_peer_id(); - assert_eq!(peer_id.is_public_key(&key), Some(true)); - } - #[test] #[cfg(feature = "ed25519")] fn peer_id_into_bytes_then_from_bytes() { @@ -299,30 +269,4 @@ mod tests { assert_eq!(peer_id, PeerId::from_bytes(&peer_id.to_bytes()).unwrap()); } } - - #[test] - fn extract_peer_id_from_multi_address() { - let address = "/memory/1234/p2p/12D3KooWGQmdpzHXCqLno4mMxWXKNFQHASBeF99gTm2JR8Vu5Bdc" - .to_string() - .parse() - .unwrap(); - - let peer_id = PeerId::try_from_multiaddr(&address).unwrap(); - - assert_eq!( - peer_id, - "12D3KooWGQmdpzHXCqLno4mMxWXKNFQHASBeF99gTm2JR8Vu5Bdc" - .parse() - .unwrap() - ); - } - - #[test] - fn no_panic_on_extract_peer_id_from_multi_address_if_not_present() { - let address = "/memory/1234".to_string().parse().unwrap(); - - let maybe_empty = PeerId::try_from_multiaddr(&address); - - assert!(maybe_empty.is_none()); - } }