Skip to content

Commit

Permalink
Update libp2p-core (#197)
Browse files Browse the repository at this point in the history
* Update libp2p-core

* `identity` moved from `libp2p_core::identity` to `libp2p_identity`

* `Protocol::P2p` contain a `PeerId` instead of a `Multihash`

* `PublicKey::from_protobuf_encoding` has been renamed to `PublicKey::try_decode_protobuf`

* `PublicKey` has been made opaque
  • Loading branch information
ackintosh authored Jul 4, 2023
1 parent a9ded04 commit f78d538
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 30 deletions.
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

0 comments on commit f78d538

Please sign in to comment.