Skip to content

Commit 371a7da

Browse files
rubdosmxinden
andauthored
to/into consistency for PublicKey and PeerId (#2145)
- Change `PublicKey::into_protobuf_encoding` to `PublicKey::to_protobuf_encoding`. - Change `PublicKey::into_peer_id` to `PublicKey::to_peer_id`. - Change `PeerId::from_public_key(PublicKey)` to `PeerId::from_public_key(&PublicKey)`. - Add `From<&PublicKey> for PeerId`. Co-authored-by: Max Inden <[email protected]>
1 parent 20183c1 commit 371a7da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+228
-110
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,30 @@
4040

4141
# `libp2p` facade crate
4242

43+
## Version 0.40.0 [unreleased]
44+
45+
- Update individual crates.
46+
- `libp2p-core`
47+
- `libp2p-deflate`
48+
- `libp2p-dns`
49+
- `libp2p-floodsub`
50+
- `libp2p-gossipsub`
51+
- `libp2p-identify`
52+
- `libp2p-kad`
53+
- `libp2p-mdns`
54+
- `libp2p-mplex`
55+
- `libp2p-noise`
56+
- `libp2p-ping`
57+
- `libp2p-plaintext`
58+
- `libp2p-relay`
59+
- `libp2p-request-response`
60+
- `libp2p-swarm`
61+
- `libp2p-tcp`
62+
- `libp2p-uds`
63+
- `libp2p-wasm-ext`
64+
- `libp2p-websocket`
65+
- `libp2p-yamux`
66+
4367
## Version 0.39.1 [2021-07-12]
4468

4569
- Update individual crates.

Cargo.toml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p"
33
edition = "2018"
44
description = "Peer-to-peer networking library"
5-
version = "0.39.1"
5+
version = "0.40.0"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"
@@ -64,35 +64,35 @@ atomic = "0.5.0"
6464
bytes = "1"
6565
futures = "0.3.1"
6666
lazy_static = "1.2"
67-
libp2p-core = { version = "0.29.0", path = "core", default-features = false }
68-
libp2p-floodsub = { version = "0.30.0", path = "protocols/floodsub", optional = true }
69-
libp2p-gossipsub = { version = "0.32.0", path = "./protocols/gossipsub", optional = true }
70-
libp2p-identify = { version = "0.30.0", path = "protocols/identify", optional = true }
71-
libp2p-kad = { version = "0.31.0", path = "protocols/kad", optional = true }
72-
libp2p-mplex = { version = "0.29.0", path = "muxers/mplex", optional = true }
73-
libp2p-noise = { version = "0.32.0", path = "transports/noise", optional = true }
74-
libp2p-ping = { version = "0.30.0", path = "protocols/ping", optional = true }
75-
libp2p-plaintext = { version = "0.29.0", path = "transports/plaintext", optional = true }
67+
libp2p-core = { version = "0.30.0", path = "core", default-features = false }
68+
libp2p-floodsub = { version = "0.31.0", path = "protocols/floodsub", optional = true }
69+
libp2p-gossipsub = { version = "0.33.0", path = "./protocols/gossipsub", optional = true }
70+
libp2p-identify = { version = "0.31.0", path = "protocols/identify", optional = true }
71+
libp2p-kad = { version = "0.32.0", path = "protocols/kad", optional = true }
72+
libp2p-mplex = { version = "0.30.0", path = "muxers/mplex", optional = true }
73+
libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true }
74+
libp2p-ping = { version = "0.31.0", path = "protocols/ping", optional = true }
75+
libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true }
7676
libp2p-pnet = { version = "0.21.0", path = "transports/pnet", optional = true }
77-
libp2p-relay = { version = "0.3.0", path = "protocols/relay", optional = true }
78-
libp2p-request-response = { version = "0.12.0", path = "protocols/request-response", optional = true }
79-
libp2p-swarm = { version = "0.30.0", path = "swarm" }
77+
libp2p-relay = { version = "0.4.0", path = "protocols/relay", optional = true }
78+
libp2p-request-response = { version = "0.13.0", path = "protocols/request-response", optional = true }
79+
libp2p-swarm = { version = "0.31.0", path = "swarm" }
8080
libp2p-swarm-derive = { version = "0.24.0", path = "swarm-derive" }
81-
libp2p-uds = { version = "0.29.0", path = "transports/uds", optional = true }
82-
libp2p-wasm-ext = { version = "0.29.0", path = "transports/wasm-ext", default-features = false, optional = true }
83-
libp2p-yamux = { version = "0.33.0", path = "muxers/yamux", optional = true }
81+
libp2p-uds = { version = "0.30.0", path = "transports/uds", optional = true }
82+
libp2p-wasm-ext = { version = "0.30.0", path = "transports/wasm-ext", default-features = false, optional = true }
83+
libp2p-yamux = { version = "0.34.0", path = "muxers/yamux", optional = true }
8484
multiaddr = { version = "0.13.0" }
8585
parking_lot = "0.11.0"
8686
pin-project = "1.0.0"
8787
smallvec = "1.6.1"
8888
wasm-timer = "0.2.4"
8989

9090
[target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))'.dependencies]
91-
libp2p-deflate = { version = "0.29.0", path = "transports/deflate", optional = true }
92-
libp2p-dns = { version = "0.29.0", path = "transports/dns", optional = true, default-features = false }
93-
libp2p-mdns = { version = "0.31.0", path = "protocols/mdns", optional = true }
94-
libp2p-tcp = { version = "0.29.0", path = "transports/tcp", default-features = false, optional = true }
95-
libp2p-websocket = { version = "0.30.0", path = "transports/websocket", optional = true }
91+
libp2p-deflate = { version = "0.30.0", path = "transports/deflate", optional = true }
92+
libp2p-dns = { version = "0.30.0", path = "transports/dns", optional = true, default-features = false }
93+
libp2p-mdns = { version = "0.32.0", path = "protocols/mdns", optional = true }
94+
libp2p-tcp = { version = "0.30.0", path = "transports/tcp", default-features = false, optional = true }
95+
libp2p-websocket = { version = "0.31.0", path = "transports/websocket", optional = true }
9696

9797
[dev-dependencies]
9898
async-std = { version = "1.6.2", features = ["attributes"] }

core/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# 0.30.0 [unreleased]
2+
3+
- Change `PublicKey::into_protobuf_encoding` to `PublicKey::to_protobuf_encoding` (see [PR 2145]).
4+
5+
- Change `PublicKey::into_peer_id` to `PublicKey::to_peer_id` (see [PR 2145]).
6+
7+
- Change `PeerId::from_public_key(PublicKey)` to `PeerId::from_public_key(&PublicKey)` (see [PR 2145]).
8+
9+
- Add `From<&PublicKey> for PeerId` (see [PR 2145]).
10+
11+
[PR 2145]: https://github.com/libp2p/rust-libp2p/pull/2145
12+
113
# 0.29.0 [2021-07-12]
214

315
- Switch from `parity-multiaddr` to upstream `multiaddr`.

core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "libp2p-core"
33
edition = "2018"
44
description = "Core traits and structs of libp2p"
5-
version = "0.29.0"
5+
version = "0.30.0"
66
authors = ["Parity Technologies <[email protected]>"]
77
license = "MIT"
88
repository = "https://github.com/libp2p/rust-libp2p"

core/benches/peer_id.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use libp2p_core::{identity, PeerId};
2424
fn from_bytes(c: &mut Criterion) {
2525
let peer_id_bytes = identity::Keypair::generate_ed25519()
2626
.public()
27-
.into_peer_id()
27+
.to_peer_id()
2828
.to_bytes();
2929

3030
c.bench_function("from_bytes", |b| {
@@ -37,7 +37,7 @@ fn from_bytes(c: &mut Criterion) {
3737
fn clone(c: &mut Criterion) {
3838
let peer_id = identity::Keypair::generate_ed25519()
3939
.public()
40-
.into_peer_id();
40+
.to_peer_id();
4141

4242
c.bench_function("clone", |b| {
4343
b.iter(|| {
@@ -51,7 +51,7 @@ fn sort_vec(c: &mut Criterion) {
5151
.map(|_| {
5252
identity::Keypair::generate_ed25519()
5353
.public()
54-
.into_peer_id()
54+
.to_peer_id()
5555
})
5656
.collect();
5757

core/src/identity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl PublicKey {
179179

180180
/// Encode the public key into a protobuf structure for storage or
181181
/// exchange with other nodes.
182-
pub fn into_protobuf_encoding(self) -> Vec<u8> {
182+
pub fn to_protobuf_encoding(&self) -> Vec<u8> {
183183
use prost::Message;
184184

185185
let public_key = match self {
@@ -245,7 +245,7 @@ impl PublicKey {
245245
}
246246

247247
/// Convert the `PublicKey` into the corresponding `PeerId`.
248-
pub fn into_peer_id(self) -> PeerId {
248+
pub fn to_peer_id(&self) -> PeerId {
249249
self.into()
250250
}
251251
}
@@ -264,7 +264,7 @@ mod tests {
264264
let encoded = base64::decode(base_64_encoded).unwrap();
265265

266266
let keypair = Keypair::from_protobuf_encoding(&encoded).unwrap();
267-
let peer_id = keypair.public().into_peer_id();
267+
let peer_id = keypair.public().to_peer_id();
268268

269269
assert_eq!(expected_peer_id, peer_id);
270270
}

core/src/peer_id.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ impl fmt::Display for PeerId {
5252

5353
impl PeerId {
5454
/// Builds a `PeerId` from a public key.
55-
pub fn from_public_key(key: PublicKey) -> PeerId {
56-
let key_enc = key.into_protobuf_encoding();
55+
pub fn from_public_key(key: &PublicKey) -> PeerId {
56+
let key_enc = key.to_protobuf_encoding();
5757

5858
let hash_algorithm = if key_enc.len() <= MAX_INLINE_KEY_LENGTH {
5959
Code::Identity
@@ -114,13 +114,19 @@ impl PeerId {
114114
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
115115
let alg = Code::try_from(self.multihash.code())
116116
.expect("Internal multihash is always a valid `Code`");
117-
let enc = public_key.clone().into_protobuf_encoding();
117+
let enc = public_key.to_protobuf_encoding();
118118
Some(alg.digest(&enc) == self.multihash)
119119
}
120120
}
121121

122122
impl From<PublicKey> for PeerId {
123123
fn from(key: PublicKey) -> PeerId {
124+
PeerId::from_public_key(&key)
125+
}
126+
}
127+
128+
impl From<&PublicKey> for PeerId {
129+
fn from(key: &PublicKey) -> PeerId {
124130
PeerId::from_public_key(key)
125131
}
126132
}
@@ -184,20 +190,20 @@ mod tests {
184190
#[test]
185191
fn peer_id_is_public_key() {
186192
let key = identity::Keypair::generate_ed25519().public();
187-
let peer_id = key.clone().into_peer_id();
193+
let peer_id = key.to_peer_id();
188194
assert_eq!(peer_id.is_public_key(&key), Some(true));
189195
}
190196

191197
#[test]
192198
fn peer_id_into_bytes_then_from_bytes() {
193-
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
199+
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();
194200
let second = PeerId::from_bytes(&peer_id.to_bytes()).unwrap();
195201
assert_eq!(peer_id, second);
196202
}
197203

198204
#[test]
199205
fn peer_id_to_base58_then_back() {
200-
let peer_id = identity::Keypair::generate_ed25519().public().into_peer_id();
206+
let peer_id = identity::Keypair::generate_ed25519().public().to_peer_id();
201207
let second: PeerId = peer_id.to_base58().parse().unwrap();
202208
assert_eq!(peer_id, second);
203209
}

core/tests/transport_upgrade.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080
#[test]
8181
fn upgrade_pipeline() {
8282
let listener_keys = identity::Keypair::generate_ed25519();
83-
let listener_id = listener_keys.public().into_peer_id();
83+
let listener_id = listener_keys.public().to_peer_id();
8484
let listener_noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&listener_keys).unwrap();
8585
let listener_transport = MemoryTransport::default()
8686
.upgrade(upgrade::Version::V1)
@@ -96,7 +96,7 @@ fn upgrade_pipeline() {
9696
});
9797

9898
let dialer_keys = identity::Keypair::generate_ed25519();
99-
let dialer_id = dialer_keys.public().into_peer_id();
99+
let dialer_id = dialer_keys.public().to_peer_id();
100100
let dialer_noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&dialer_keys).unwrap();
101101
let dialer_transport = MemoryTransport::default()
102102
.upgrade(upgrade::Version::V1)

misc/multistream-select/tests/transport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type TestNetwork = Network<TestTransport, (), (), TestHandler>;
4242

4343
fn mk_transport(up: upgrade::Version) -> (PeerId, TestTransport) {
4444
let keys = identity::Keypair::generate_ed25519();
45-
let id = keys.public().into_peer_id();
45+
let id = keys.public().to_peer_id();
4646
(id, MemoryTransport::default()
4747
.upgrade(up)
4848
.authenticate(PlainText2Config { local_public_key: keys.public() })

misc/peer-id-generator/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn main() {
6868
thread::spawn(move || loop {
6969
let keypair = identity::ed25519::Keypair::generate();
7070
let secret = keypair.secret();
71-
let peer_id = identity::PublicKey::Ed25519(keypair.public()).into_peer_id();
71+
let peer_id = identity::PublicKey::Ed25519(keypair.public()).to_peer_id();
7272
let base58 = peer_id.to_base58();
7373
if base58[2..].starts_with(&prefix) {
7474
println!("Found {:?}", peer_id);

0 commit comments

Comments
 (0)