Skip to content

Commit

Permalink
feat: introduce libp2p-identity crate
Browse files Browse the repository at this point in the history
This patch combines the `libp2p_core::identity` and `libp2p_core::peer_id` modules into a new crate: `libp2p-identity`.

Resolves #3349.

Pull-Request: #3350.
  • Loading branch information
thomaseizinger authored Mar 12, 2023
1 parent d81f947 commit 2a14df2
Show file tree
Hide file tree
Showing 180 changed files with 1,012 additions and 345 deletions.
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;

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

0 comments on commit 2a14df2

Please sign in to comment.