Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update libp2p-core #197

Merged
merged 5 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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"]
48 changes: 24 additions & 24 deletions src/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down