diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f38e28949..44fd9dc7d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -18,8 +18,6 @@ jobs: - uses: actions/checkout@v3 - name: Get latest version of stable rust run: rustup update stable - - name: Install protobuf compiler for the libp2p-core dependency - uses: arduino/setup-protoc@v1 - name: Lint code for quality and style with Clippy run: cargo clippy --workspace --tests --all-features -- -D warnings release-tests-ubuntu: @@ -40,8 +38,6 @@ jobs: - uses: actions/checkout@v3 - name: Get latest version of stable rust run: rustup update stable - - name: Install protobuf compiler for the libp2p-core dependency - uses: arduino/setup-protoc@v1 - name: Run tests in release run: cargo test --all --release --all-features check-rustdoc-links: diff --git a/Cargo.toml b/Cargo.toml index 101233ee7..23e29aa3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,8 @@ exclude = [".gitignore", ".github/*"] [dependencies] enr = { version = "0.8.1", features = ["k256", "ed25519"] } tokio = { version = "1.15.0", features = ["net", "sync", "macros", "rt"] } -libp2p-core = { version = "0.36.0", optional = true } +libp2p-core = { version = "0.40.0", optional = true } +libp2p-identity = { version = "0.2.1", features = ["ed25519", "secp256k1"], optional = true } zeroize = { version = "1.4.3", features = ["zeroize_derive"] } futures = "0.3.19" uint = { version = "0.9.1", default-features = false } @@ -48,5 +49,5 @@ clap = { version = "3.1", features = ["derive"] } if-addrs = "0.10.1" [features] -libp2p = ["libp2p-core"] +libp2p = ["libp2p-core", "libp2p-identity"] serde = ["enr/serde"] diff --git a/src/node_info.rs b/src/node_info.rs index d585e0e0d..6224fe12b 100644 --- a/src/node_info.rs +++ b/src/node_info.rs @@ -4,7 +4,9 @@ use enr::{CombinedPublicKey, NodeId}; use std::net::SocketAddr; #[cfg(feature = "libp2p")] -use libp2p_core::{identity::PublicKey, multiaddr::Protocol, multihash, Multiaddr}; +use libp2p_core::{multiaddr::Protocol, Multiaddr}; +#[cfg(feature = "libp2p")] +use libp2p_identity::{KeyType, PublicKey}; /// This type relaxes the requirement of having an ENR to connect to a node, to allow for unsigned /// connection types, such as multiaddrs. @@ -94,36 +96,34 @@ impl NodeContact { Protocol::Udp(port) => udp_port = Some(port), Protocol::Ip4(addr) => ip_addr = Some(addr.into()), Protocol::Ip6(addr) => ip_addr = Some(addr.into()), - Protocol::P2p(multihash) => p2p = Some(multihash), + Protocol::P2p(peer_id) => p2p = Some(peer_id), _ => {} } } let udp_port = udp_port.ok_or("A UDP port must be specified in the multiaddr")?; let ip_addr = ip_addr.ok_or("An IP address must be specified in the multiaddr")?; - let multihash = p2p.ok_or("The p2p protocol must be specified in the multiaddr")?; - - // verify the correct key type - if multihash.code() != u64::from(multihash::Code::Identity) { - return Err("The key type is unsupported"); - } - - let public_key: CombinedPublicKey = - match PublicKey::from_protobuf_encoding(&multihash.to_bytes()[2..]) - .map_err(|_| "Invalid public key")? - { - PublicKey::Secp256k1(pk) => { - enr::k256::ecdsa::VerifyingKey::from_sec1_bytes(&pk.encode_uncompressed()) - .expect("Libp2p key conversion, always valid") - .into() - } - PublicKey::Ed25519(pk) => { - enr::ed25519_dalek::VerifyingKey::from_bytes(&pk.encode()) - .expect("Libp2p key conversion, always valid") - .into() - } + let peer_id = p2p.ok_or("The p2p protocol must be specified in the multiaddr")?; + + let public_key: CombinedPublicKey = { + let pk = PublicKey::try_decode_protobuf(&peer_id.to_bytes()[2..]) + .map_err(|_| "Invalid public key")?; + match pk.key_type() { + KeyType::Secp256k1 => enr::k256::ecdsa::VerifyingKey::from_sec1_bytes( + &pk.try_into_secp256k1() + .expect("Must be secp256k1") + .to_bytes_uncompressed(), + ) + .expect("Libp2p key conversion, always valid") + .into(), + KeyType::Ed25519 => enr::ed25519_dalek::VerifyingKey::from_bytes( + &pk.try_into_ed25519().expect("Must be ed25519").to_bytes(), + ) + .expect("Libp2p key conversion, always valid") + .into(), _ => return Err("The key type is not supported"), - }; + } + }; Ok(NodeContact { public_key,