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

feat: introduce libp2p-identity crate #3350

Merged
merged 50 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
e170582
Introduce `libp2p-identity`
thomaseizinger Jan 19, 2023
1c38271
Deprecate `PeerId::try_from_multiaddr`
thomaseizinger Jan 19, 2023
b04dae9
Add missing backtick
thomaseizinger Jan 19, 2023
b0ffd1b
Fix build errors
thomaseizinger Jan 19, 2023
05efe05
Add package keys
thomaseizinger Jan 20, 2023
4c65409
Move PeerId tests to `libp2p-identity`
thomaseizinger Jan 23, 2023
84caeb3
Move PeerId benches to `libp2p-identity`
thomaseizinger Jan 23, 2023
d8ae4ed
Migrate workspace to depend on libp2p-identity
thomaseizinger Jan 23, 2023
36848f6
Add dummy keypair implementation
thomaseizinger Jan 23, 2023
198b7e3
Add missing re-exports of other key types
thomaseizinger Jan 24, 2023
d85582c
Remove unnecessary docs
thomaseizinger Jan 24, 2023
b0ce75d
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Jan 24, 2023
94b1a30
Make `decoding_unsupported` unconditionally available
thomaseizinger Jan 25, 2023
457a694
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Jan 27, 2023
bdbee42
Introduce ed25519 feature flag
thomaseizinger Jan 27, 2023
bea9855
Sort stuff alphabetically
thomaseizinger Jan 27, 2023
591f002
Deprecate direct access to inner keypair and public key
thomaseizinger Jan 27, 2023
e4b758e
Use correct rsa feature flag
thomaseizinger Jan 31, 2023
5f91ec5
Fix deprecated imports in tests
thomaseizinger Jan 31, 2023
f00f1c1
Remove unused import
thomaseizinger Jan 31, 2023
e791ef1
Add deprecations for the feature-flags in libp2p-core
thomaseizinger Jan 31, 2023
6d04533
Remove unnecessary feature gates
thomaseizinger Jan 31, 2023
dee1008
More fixes
thomaseizinger Jan 31, 2023
7793c0f
Fix bad feature-gate
thomaseizinger Jan 31, 2023
cb60c3f
More bad feature gates
thomaseizinger Jan 31, 2023
ac044e9
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Feb 1, 2023
2a85688
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Feb 16, 2023
81a7b25
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Feb 17, 2023
6a85bfe
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Feb 21, 2023
e99b69a
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Feb 24, 2023
cd132c9
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 1, 2023
37ea12c
Don't feature flag ed25519 in libp2p-core
thomaseizinger Mar 1, 2023
6c67eef
Fix deprecated usages
thomaseizinger Mar 1, 2023
57e2327
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 2, 2023
7406c5f
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 3, 2023
b59b327
Properly generate proto bindings
thomaseizinger Mar 3, 2023
07ad795
Adapt libp2p-identity to new proto structure
thomaseizinger Mar 3, 2023
2035d1a
Remove deprecation warning
thomaseizinger Mar 3, 2023
016a96d
Generate proto files with correct compiler version
thomaseizinger Mar 3, 2023
975536f
Move changelog entry to correct version
thomaseizinger Mar 3, 2023
f6f0c24
Amend changelog entry
thomaseizinger Mar 3, 2023
5dc04dd
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 6, 2023
dd4b334
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 8, 2023
984ebe9
Fix bad merge
thomaseizinger Mar 8, 2023
36492df
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 8, 2023
c1de283
Fix deprecation warnings and compile errors
thomaseizinger Mar 8, 2023
8e7ba6f
Generate proto bindings correctly
thomaseizinger Mar 10, 2023
d03eb75
Merge branch 'master' into feat/libp2p-identity
thomaseizinger Mar 10, 2023
2dd936b
Merge branch 'master' into feat/libp2p-identity
mxinden Mar 10, 2023
17d8973
Merge branch 'master' into feat/libp2p-identity
mxinden Mar 12, 2023
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
168 changes: 155 additions & 13 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ members = [
"examples/ipfs-private",
"examples/ping-example",
"examples/rendezvous",
"identity",
"misc/metrics",
"misc/multistream-select",
"misc/rw-stream-sink",
Expand Down
30 changes: 6 additions & 24 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,40 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]

[dependencies]
asn1_der = "0.7.4"
bs58 = "0.4.0"
ed25519-dalek = "1.0.1"
either = "1.5"
fnv = "1.0"
futures = { version = "0.3.26", features = ["executor", "thread-pool"] }
futures-timer = "3"
instant = "0.1.11"
libsecp256k1 = { version = "0.7.0", optional = true }
libp2p-identity = { version = "0.1", path = "../identity", features = ["peerid", "ed25519"] }
log = "0.4"
multiaddr = { version = "0.17.0" }
multihash = { version = "0.17.0", default-features = false, features = ["std", "multihash-impl", "identity", "sha2"] }
multistream-select = { version = "0.12.1", path = "../misc/multistream-select" }
p256 = { version = "0.12.0", default-features = false, features = ["ecdsa", "std"], optional = true }
once_cell = "1.17.1"
parking_lot = "0.12.0"
pin-project = "1.0.0"
quick-protobuf = "0.8"
once_cell = "1.17.1"
rand = "0.8"
rw-stream-sink = { version = "0.3.0", path = "../misc/rw-stream-sink" }
sec1 = { version = "0.3.0", features = ["std"] } # Activate `std` feature until https://github.com/RustCrypto/traits/pull/1131 is released.
serde = { version = "1", optional = true, features = ["derive"] }
sha2 = "0.10.0"
smallvec = "1.6.1"
thiserror = "1.0"
unsigned-varint = "0.7"
void = "1"
zeroize = "1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false, optional = true}

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
base64 = "0.21.0"
criterion = "0.4"
libp2p-mplex = { path = "../muxers/mplex" }
libp2p-noise = { path = "../transports/noise" }
multihash = { version = "0.17.0", default-features = false, features = ["arb"] }
quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" }
rmp-serde = "1.0"
serde_json = "1.0"

[features]
secp256k1 = [ "libsecp256k1" ]
ecdsa = [ "p256" ]
rsa = [ "dep:ring" ]
serde = ["multihash/serde-codec", "dep:serde"]

[[bench]]
name = "peer_id"
harness = false
secp256k1 = [ "libp2p-identity/secp256k1" ]
ecdsa = [ "libp2p-identity/ecdsa" ]
rsa = [ "libp2p-identity/rsa" ]
serde = ["multihash/serde-codec", "dep:serde", "libp2p-identity/serde"]

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
4 changes: 1 addition & 3 deletions core/src/generated/envelope.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ syntax = "proto3";

package envelope_proto;

import "keys.proto";

// Envelope encloses a signed payload produced by a peer, along with the public
// key of the keypair it was signed with so that it can be statelessly validated
// by the receiver.
Expand All @@ -14,7 +12,7 @@ import "keys.proto";
message Envelope {
// public_key is the public key of the keypair the enclosed payload was
// signed with.
keys_proto.PublicKey public_key = 1;
bytes public_key = 1;

// payload_type encodes the type of payload, so that it can be deserialized
// deterministically.
Expand Down
8 changes: 4 additions & 4 deletions core/src/generated/envelope_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::*;
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct Envelope {
pub public_key: Option<keys_proto::PublicKey>,
pub public_key: Vec<u8>,
pub payload_type: Vec<u8>,
pub payload: Vec<u8>,
pub signature: Vec<u8>,
Expand All @@ -27,7 +27,7 @@ impl<'a> MessageRead<'a> for Envelope {
let mut msg = Self::default();
while !r.is_eof() {
match r.next_tag(bytes) {
Ok(10) => msg.public_key = Some(r.read_message::<keys_proto::PublicKey>(bytes)?),
Ok(10) => msg.public_key = r.read_bytes(bytes)?.to_owned(),
Ok(18) => msg.payload_type = r.read_bytes(bytes)?.to_owned(),
Ok(26) => msg.payload = r.read_bytes(bytes)?.to_owned(),
Ok(42) => msg.signature = r.read_bytes(bytes)?.to_owned(),
Expand All @@ -42,14 +42,14 @@ impl<'a> MessageRead<'a> for Envelope {
impl MessageWrite for Envelope {
fn get_size(&self) -> usize {
0
+ self.public_key.as_ref().map_or(0, |m| 1 + sizeof_len((m).get_size()))
+ if self.public_key.is_empty() { 0 } else { 1 + sizeof_len((&self.public_key).len()) }
+ if self.payload_type.is_empty() { 0 } else { 1 + sizeof_len((&self.payload_type).len()) }
+ if self.payload.is_empty() { 0 } else { 1 + sizeof_len((&self.payload).len()) }
+ if self.signature.is_empty() { 0 } else { 1 + sizeof_len((&self.signature).len()) }
}

fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
if let Some(ref s) = self.public_key { w.write_with_tag(10, |w| w.write_message(s))?; }
if !self.public_key.is_empty() { w.write_with_tag(10, |w| w.write_bytes(&**&self.public_key))?; }
if !self.payload_type.is_empty() { w.write_with_tag(18, |w| w.write_bytes(&**&self.payload_type))?; }
if !self.payload.is_empty() { w.write_with_tag(26, |w| w.write_bytes(&**&self.payload))?; }
if !self.signature.is_empty() { w.write_with_tag(42, |w| w.write_bytes(&**&self.signature))?; }
Expand Down
1 change: 0 additions & 1 deletion core/src/generated/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Automatically generated mod.rs
pub mod envelope_proto;
pub mod keys_proto;
pub mod peer_record_proto;
66 changes: 59 additions & 7 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
mod proto {
include!("generated/mod.rs");
pub use self::{
envelope_proto::*, keys_proto::*, peer_record_proto::mod_PeerRecord::*,
peer_record_proto::PeerRecord,
envelope_proto::*, peer_record_proto::mod_PeerRecord::*, peer_record_proto::PeerRecord,
};
}

Expand All @@ -51,25 +50,78 @@ use std::fmt;
use std::fmt::Formatter;
pub type Negotiated<T> = multistream_select::Negotiated<T>;

mod peer_id;
#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
pub mod identity {
pub use libp2p_identity::Keypair;
pub use libp2p_identity::PublicKey;
Comment on lines +55 to +56
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obi1kenobi I think we might have hit another bug here! cargo semver-checks is reporting these two as missing. I've tried with type aliases as well and it doesn't make a difference which makes me think that I am doing something wrong here? It did work in my testing though, i.e. I got heaps of deprecation warnings but stuff was still working when I developed this feature.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a move to another crate + re-export, right? Semver-checking right now only generates the tested crate's rustdoc JSON, so it doesn't see cross-crate re-exports: obi1kenobi/cargo-semver-checks#355

It's fixable issue with a clear path forward. Just a matter of engineering cycles, which are always in short supply.

Thankfully, once you publish the next version of the affected crate, the false-positive will go away since the baseline will shift to that new version.


pub mod ed25519 {
pub use libp2p_identity::ed25519::Keypair;
pub use libp2p_identity::ed25519::PublicKey;
pub use libp2p_identity::ed25519::SecretKey;
}

#[cfg(feature = "ecdsa")]
#[deprecated(
since = "0.39.0",
note = "The `ecdsa` feature-flag is deprecated and will be removed in favor of `libp2p-identity`."
)]
pub mod ecdsa {
pub use libp2p_identity::ecdsa::Keypair;
pub use libp2p_identity::ecdsa::PublicKey;
pub use libp2p_identity::ecdsa::SecretKey;
}

#[cfg(feature = "secp256k1")]
#[deprecated(
since = "0.39.0",
note = "The `secp256k1` feature-flag is deprecated and will be removed in favor of `libp2p-identity`."
)]
pub mod secp256k1 {
pub use libp2p_identity::secp256k1::Keypair;
pub use libp2p_identity::secp256k1::PublicKey;
pub use libp2p_identity::secp256k1::SecretKey;
}

#[cfg(feature = "rsa")]
#[deprecated(
since = "0.39.0",
note = "The `rsa` feature-flag is deprecated and will be removed in favor of `libp2p-identity`."
)]
pub mod rsa {
pub use libp2p_identity::rsa::Keypair;
pub use libp2p_identity::rsa::PublicKey;
}

pub mod error {
pub use libp2p_identity::DecodingError;
pub use libp2p_identity::SigningError;
}
}

mod translation;

pub mod connection;
pub mod either;
pub mod identity;
pub mod muxing;
pub mod peer_record;
pub mod signed_envelope;
pub mod transport;
pub mod upgrade;

#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
pub type PublicKey = libp2p_identity::PublicKey;

#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
pub type PeerId = libp2p_identity::PeerId;

#[deprecated(since = "0.39.0", note = "Depend on `libp2p-identity` instead.")]
pub type ParseError = libp2p_identity::ParseError;

pub use connection::{ConnectedPoint, Endpoint};
pub use identity::PublicKey;
pub use multiaddr::Multiaddr;
pub use multihash;
pub use muxing::StreamMuxer;
pub use peer_id::ParseError;
pub use peer_id::PeerId;
pub use peer_record::PeerRecord;
pub use signed_envelope::SignedEnvelope;
pub use translation::address_translation;
Expand Down
8 changes: 4 additions & 4 deletions core/src/peer_record.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::identity::error::SigningError;
use crate::identity::Keypair;
use crate::proto;
use crate::signed_envelope::SignedEnvelope;
use crate::{signed_envelope, DecodeError, Multiaddr, PeerId};
use crate::{proto, signed_envelope, DecodeError, Multiaddr};
use instant::SystemTime;
use libp2p_identity::Keypair;
use libp2p_identity::PeerId;
use libp2p_identity::SigningError;
use quick_protobuf::{BytesReader, Writer};
use std::convert::TryInto;

Expand Down
16 changes: 6 additions & 10 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::identity::error::SigningError;
use crate::identity::Keypair;
use crate::{identity, proto, DecodeError, PublicKey};
use crate::{proto, DecodeError};
use libp2p_identity::SigningError;
use libp2p_identity::{Keypair, PublicKey};
use quick_protobuf::{BytesReader, Writer};
use std::convert::TryInto;
use std::fmt;
use unsigned_varint::encode::usize_buffer;

Expand Down Expand Up @@ -77,7 +76,7 @@ impl SignedEnvelope {
use quick_protobuf::MessageWrite;

let envelope = proto::Envelope {
public_key: Some((&self.key).into()),
public_key: self.key.to_protobuf_encoding(),
payload_type: self.payload_type,
payload: self.payload,
signature: self.signature,
Expand All @@ -102,10 +101,7 @@ impl SignedEnvelope {
proto::Envelope::from_reader(&mut reader, bytes).map_err(DecodeError::from)?;

Ok(Self {
key: envelope
.public_key
.ok_or(DecodingError::MissingPublicKey)?
.try_into()?,
key: PublicKey::from_protobuf_encoding(&envelope.public_key)?,
payload_type: envelope.payload_type.to_vec(),
payload: envelope.payload.to_vec(),
signature: envelope.signature.to_vec(),
Expand Down Expand Up @@ -152,7 +148,7 @@ pub enum DecodingError {
InvalidEnvelope(#[from] DecodeError),
/// The public key in the envelope could not be converted to our internal public key type.
#[error("Failed to convert public key")]
InvalidPublicKey(#[from] identity::error::DecodingError),
InvalidPublicKey(#[from] libp2p_identity::DecodingError),
/// The public key in the envelope could not be converted to our internal public key type.
#[error("Public key is missing from protobuf struct")]
MissingPublicKey,
Expand Down
3 changes: 2 additions & 1 deletion core/src/transport/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ use crate::{
self, apply_inbound, apply_outbound, InboundUpgrade, InboundUpgradeApply, OutboundUpgrade,
OutboundUpgradeApply, UpgradeError,
},
Negotiated, PeerId,
Negotiated,
};
use futures::{prelude::*, ready};
use libp2p_identity::PeerId;
use multiaddr::Multiaddr;
use std::{
error::Error,
Expand Down
2 changes: 1 addition & 1 deletion core/tests/transport_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
// DEALINGS IN THE SOFTWARE.

use futures::prelude::*;
use libp2p_core::identity;
use libp2p_core::transport::{MemoryTransport, Transport};
use libp2p_core::upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
use libp2p_identity as identity;
use libp2p_mplex::MplexConfig;
use libp2p_noise as noise;
use multiaddr::{Multiaddr, Protocol};
Expand Down
8 changes: 3 additions & 5 deletions examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ use libp2p::{
core::{
multiaddr::{Multiaddr, Protocol},
transport::{OrTransport, Transport},
upgrade, PeerId,
upgrade,
},
dcutr,
dns::DnsConfig,
identify, identity, noise, ping, relay,
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
tcp, yamux,
tcp, yamux, PeerId,
};
use log::info;
use std::error::Error;
Expand Down Expand Up @@ -281,7 +281,5 @@ fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;

let secret_key = identity::ed25519::SecretKey::from_bytes(&mut bytes)
.expect("this returns `Err` only if the length is wrong; the length is correct; qed");
identity::Keypair::Ed25519(secret_key.into())
identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
}
5 changes: 1 addition & 4 deletions examples/file-sharing/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,7 @@ use clap::Parser;

use futures::prelude::*;
use futures::StreamExt;
use libp2p::{
core::{Multiaddr, PeerId},
multiaddr::Protocol,
};
use libp2p::{core::Multiaddr, multiaddr::Protocol, PeerId};
use std::error::Error;
use std::io::Write;
use std::path::PathBuf;
Expand Down
8 changes: 3 additions & 5 deletions examples/file-sharing/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::prelude::*;
use libp2p::{
core::{
upgrade::{read_length_prefixed, write_length_prefixed, ProtocolName},
Multiaddr, PeerId,
Multiaddr,
},
identity,
kad::{
Expand All @@ -16,6 +16,7 @@ use libp2p::{
multiaddr::Protocol,
request_response::{self, ProtocolSupport, RequestId, ResponseChannel},
swarm::{ConnectionHandlerUpgrErr, NetworkBehaviour, Swarm, SwarmEvent},
PeerId,
};

use std::collections::{hash_map, HashMap, HashSet};
Expand All @@ -38,10 +39,7 @@ pub async fn new(
Some(seed) => {
let mut bytes = [0u8; 32];
bytes[0] = seed;
let secret_key = identity::ed25519::SecretKey::from_bytes(&mut bytes).expect(
"this returns `Err` only if the length is wrong; the length is correct; qed",
);
identity::Keypair::Ed25519(secret_key.into())
identity::Keypair::ed25519_from_bytes(bytes).unwrap()
}
None => identity::Keypair::generate_ed25519(),
};
Expand Down
4 changes: 2 additions & 2 deletions examples/identify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@

use futures::prelude::*;
use libp2p::{
core::{multiaddr::Multiaddr, upgrade::Version, PeerId},
core::{multiaddr::Multiaddr, upgrade::Version},
identify, identity, noise,
swarm::{Swarm, SwarmEvent},
tcp, yamux, Transport,
tcp, yamux, PeerId, Transport,
};
use std::error::Error;

Expand Down
Loading