Skip to content

Commit f78d538

Browse files
authored
Update libp2p-core (#197)
* 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
1 parent a9ded04 commit f78d538

File tree

3 files changed

+27
-30
lines changed

3 files changed

+27
-30
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
- uses: actions/checkout@v3
1919
- name: Get latest version of stable rust
2020
run: rustup update stable
21-
- name: Install protobuf compiler for the libp2p-core dependency
22-
uses: arduino/setup-protoc@v1
2321
- name: Lint code for quality and style with Clippy
2422
run: cargo clippy --workspace --tests --all-features -- -D warnings
2523
release-tests-ubuntu:
@@ -40,8 +38,6 @@ jobs:
4038
- uses: actions/checkout@v3
4139
- name: Get latest version of stable rust
4240
run: rustup update stable
43-
- name: Install protobuf compiler for the libp2p-core dependency
44-
uses: arduino/setup-protoc@v1
4541
- name: Run tests in release
4642
run: cargo test --all --release --all-features
4743
check-rustdoc-links:

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ exclude = [".gitignore", ".github/*"]
1414
[dependencies]
1515
enr = { version = "0.8.1", features = ["k256", "ed25519"] }
1616
tokio = { version = "1.15.0", features = ["net", "sync", "macros", "rt"] }
17-
libp2p-core = { version = "0.36.0", optional = true }
17+
libp2p-core = { version = "0.40.0", optional = true }
18+
libp2p-identity = { version = "0.2.1", features = ["ed25519", "secp256k1"], optional = true }
1819
zeroize = { version = "1.4.3", features = ["zeroize_derive"] }
1920
futures = "0.3.19"
2021
uint = { version = "0.9.1", default-features = false }
@@ -48,5 +49,5 @@ clap = { version = "3.1", features = ["derive"] }
4849
if-addrs = "0.10.1"
4950

5051
[features]
51-
libp2p = ["libp2p-core"]
52+
libp2p = ["libp2p-core", "libp2p-identity"]
5253
serde = ["enr/serde"]

src/node_info.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use enr::{CombinedPublicKey, NodeId};
44
use std::net::SocketAddr;
55

66
#[cfg(feature = "libp2p")]
7-
use libp2p_core::{identity::PublicKey, multiaddr::Protocol, multihash, Multiaddr};
7+
use libp2p_core::{multiaddr::Protocol, Multiaddr};
8+
#[cfg(feature = "libp2p")]
9+
use libp2p_identity::{KeyType, PublicKey};
810

911
/// This type relaxes the requirement of having an ENR to connect to a node, to allow for unsigned
1012
/// connection types, such as multiaddrs.
@@ -94,36 +96,34 @@ impl NodeContact {
9496
Protocol::Udp(port) => udp_port = Some(port),
9597
Protocol::Ip4(addr) => ip_addr = Some(addr.into()),
9698
Protocol::Ip6(addr) => ip_addr = Some(addr.into()),
97-
Protocol::P2p(multihash) => p2p = Some(multihash),
99+
Protocol::P2p(peer_id) => p2p = Some(peer_id),
98100
_ => {}
99101
}
100102
}
101103

102104
let udp_port = udp_port.ok_or("A UDP port must be specified in the multiaddr")?;
103105
let ip_addr = ip_addr.ok_or("An IP address must be specified in the multiaddr")?;
104-
let multihash = p2p.ok_or("The p2p protocol must be specified in the multiaddr")?;
105-
106-
// verify the correct key type
107-
if multihash.code() != u64::from(multihash::Code::Identity) {
108-
return Err("The key type is unsupported");
109-
}
110-
111-
let public_key: CombinedPublicKey =
112-
match PublicKey::from_protobuf_encoding(&multihash.to_bytes()[2..])
113-
.map_err(|_| "Invalid public key")?
114-
{
115-
PublicKey::Secp256k1(pk) => {
116-
enr::k256::ecdsa::VerifyingKey::from_sec1_bytes(&pk.encode_uncompressed())
117-
.expect("Libp2p key conversion, always valid")
118-
.into()
119-
}
120-
PublicKey::Ed25519(pk) => {
121-
enr::ed25519_dalek::VerifyingKey::from_bytes(&pk.encode())
122-
.expect("Libp2p key conversion, always valid")
123-
.into()
124-
}
106+
let peer_id = p2p.ok_or("The p2p protocol must be specified in the multiaddr")?;
107+
108+
let public_key: CombinedPublicKey = {
109+
let pk = PublicKey::try_decode_protobuf(&peer_id.to_bytes()[2..])
110+
.map_err(|_| "Invalid public key")?;
111+
match pk.key_type() {
112+
KeyType::Secp256k1 => enr::k256::ecdsa::VerifyingKey::from_sec1_bytes(
113+
&pk.try_into_secp256k1()
114+
.expect("Must be secp256k1")
115+
.to_bytes_uncompressed(),
116+
)
117+
.expect("Libp2p key conversion, always valid")
118+
.into(),
119+
KeyType::Ed25519 => enr::ed25519_dalek::VerifyingKey::from_bytes(
120+
&pk.try_into_ed25519().expect("Must be ed25519").to_bytes(),
121+
)
122+
.expect("Libp2p key conversion, always valid")
123+
.into(),
125124
_ => return Err("The key type is not supported"),
126-
};
125+
}
126+
};
127127

128128
Ok(NodeContact {
129129
public_key,

0 commit comments

Comments
 (0)