Skip to content

Commit

Permalink
Merge branch 'master' into quic_quinn
Browse files Browse the repository at this point in the history
  • Loading branch information
kpp committed Apr 19, 2023
2 parents 2a93d21 + 96288b8 commit 2826b7c
Show file tree
Hide file tree
Showing 71 changed files with 1,305 additions and 716 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ jobs:
- name: Check if we compile without any features activated
run: cargo build --package ${{ matrix.crate }} --no-default-features

- run: cargo clean

- name: Check if crate has been released
id: check-released
run: |
Expand Down Expand Up @@ -237,7 +239,7 @@ jobs:

- id: cargo-metadata
run: |
WORKSPACE_MEMBERS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | .[] | select(.publish == null) | .name' | jq -s '.' | jq -c '.')
WORKSPACE_MEMBERS=$(cargo metadata --format-version=1 --no-deps | jq -c '.packages | map(select(.publish == null) | .name)')
echo "members=${WORKSPACE_MEMBERS}" >> $GITHUB_OUTPUT
check-proto-files:
Expand All @@ -251,7 +253,7 @@ jobs:
- run: cargo install --version 0.10.0 pb-rs --locked

- name: Glob match
uses: tj-actions/glob@v16
uses: tj-actions/glob@v17
id: glob
with:
files: |
Expand Down
47 changes: 27 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"examples/identify",
"examples/ipfs-kad",
"examples/ipfs-private",
"examples/metrics",
"examples/ping-example",
"examples/relay-server",
"examples/rendezvous",
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

(In alphabetical order.)

- Elena Frank ([@elenaf9](https://github.com/elenaf9/))
- João Oliveira ([@jxs](https://github.com/jxs))
- Max Inden ([@mxinden](https://github.com/mxinden/))
- Thomas Eizinger ([@thomaseizinger](https://github.com/thomaseizinger))
Expand Down
4 changes: 2 additions & 2 deletions core/src/signed_envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl SignedEnvelope {
use quick_protobuf::MessageWrite;

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

Ok(Self {
key: PublicKey::from_protobuf_encoding(&envelope.public_key)?,
key: PublicKey::try_decode_protobuf(&envelope.public_key)?,
payload_type: envelope.payload_type.to_vec(),
payload: envelope.payload.to_vec(),
signature: envelope.signature.to_vec(),
Expand Down
15 changes: 15 additions & 0 deletions examples/metrics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "metrics-example"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT"

[dependencies]
env_logger = "0.10.0"
futures = "0.3.27"
hyper = { version = "0.14", features = ["server", "tcp", "http1"] }
libp2p = { path = "../../libp2p", features = ["async-std", "metrics", "ping", "noise", "identify", "tcp", "yamux", "macros"] }
log = "0.4.0"
tokio = { version = "1", features = ["rt-multi-thread"] }
prometheus-client = "0.19.0"
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
//! In one terminal run:
//!
//! ```
//! cargo run --example metrics
//! cargo run
//! ```
//!
//! In a second terminal run:
//!
//! ```
//! cargo run --example metrics -- <listen-addr-of-first-node>
//! cargo run -- <listen-addr-of-first-node>
//! ```
//!
//! Where `<listen-addr-of-first-node>` is replaced by the listen address of the
Expand All @@ -51,16 +51,11 @@
use env_logger::Env;
use futures::executor::block_on;
use futures::stream::StreamExt;
use libp2p_core::{upgrade::Version, Multiaddr, Transport};
use libp2p_identify as identify;
use libp2p_identity as identity;
use libp2p_identity::PeerId;
use libp2p_metrics::{Metrics, Recorder};
use libp2p_noise as noise;
use libp2p_ping as ping;
use libp2p_swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p_tcp as tcp;
use libp2p_yamux as yamux;
use libp2p::core::{upgrade::Version, Multiaddr, Transport};
use libp2p::identity::PeerId;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
use libp2p::{identify, identity, noise, ping, tcp, yamux};
use log::info;
use prometheus_client::registry::Registry;
use std::error::Error;
Expand Down Expand Up @@ -125,7 +120,6 @@ fn main() -> Result<(), Box<dyn Error>> {
/// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen
/// and can be observed via the metrics.
#[derive(NetworkBehaviour)]
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
struct Behaviour {
identify: identify::Behaviour,
keep_alive: keep_alive::Behaviour,
Expand Down
7 changes: 7 additions & 0 deletions identity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.1.2 - unreleased

- Follow Rust naming conventions for conversion methods.
See [PR 3775].

[PR 3775]: https://github.com/libp2p/rust-libp2p/pull/3775

## 0.1.1

- Add `From` impl for specific keypairs.
Expand Down
2 changes: 1 addition & 1 deletion identity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libp2p-identity"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
description = "Data structures and algorithms for identifying peers in libp2p."
rust-version = "1.60.0"
Expand Down
43 changes: 35 additions & 8 deletions identity/src/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use p256::{
};
use void::Void;

/// An ECDSA keypair.
/// An ECDSA keypair generated using `secp256r1` curve.
#[derive(Clone)]
pub struct Keypair {
secret: SecretKey,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl From<Keypair> for SecretKey {
}
}

/// An ECDSA secret key.
/// An ECDSA secret key generated using `secp256r1` curve.
#[derive(Clone)]
pub struct SecretKey(SigningKey);

Expand All @@ -102,14 +102,23 @@ impl SecretKey {
signature.as_bytes().to_owned()
}

/// Encode a secret key into a byte buffer.
/// Convert a secret key into a byte buffer containing raw scalar of the key.
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes().to_vec()
}

/// Decode a secret key from a byte buffer.
/// Decode a secret key from a byte buffer containing raw scalar of the key.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `SecretKey::try_from_bytes` instead"
)]
pub fn from_bytes(buf: &[u8]) -> Result<Self, DecodingError> {
SigningKey::from_bytes(buf)
Self::try_from_bytes(buf)
}

/// Try to parse a secret key from a byte buffer containing raw scalar of the key.
pub fn try_from_bytes(buf: impl AsRef<[u8]>) -> Result<SecretKey, DecodingError> {
SigningKey::from_bytes(buf.as_ref())
.map_err(|err| DecodingError::failed_to_parse("ecdsa p256 secret key", err))
.map(SecretKey)
}
Expand All @@ -135,8 +144,17 @@ impl PublicKey {
self.0.verify(msg, &sig).is_ok()
}

/// Decode a public key from a byte buffer without compression.
/// Decode a public key from a byte buffer containing raw components of a key with or without compression.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_from_bytes` instead."
)]
pub fn from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
Self::try_from_bytes(k)
}

/// Try to parse a public key from a byte buffer containing raw components of a key with or without compression.
pub fn try_from_bytes(k: &[u8]) -> Result<PublicKey, DecodingError> {
let enc_pt = EncodedPoint::from_bytes(k)
.map_err(|e| DecodingError::failed_to_parse("ecdsa p256 encoded point", e))?;

Expand All @@ -145,7 +163,7 @@ impl PublicKey {
.map(PublicKey)
}

/// Encode a public key into a byte buffer without compression.
/// Convert a public key into a byte buffer containing raw components of the key without compression.
pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_encoded_point(false).as_bytes().to_owned()
}
Expand All @@ -157,11 +175,20 @@ impl PublicKey {
}

/// Decode a public key into a DER encoded byte buffer as defined by SEC1 standard.
#[deprecated(
since = "0.2.0",
note = "This method name does not follow Rust naming conventions, use `PublicKey::try_decode_der` instead."
)]
pub fn decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
Self::try_decode_der(k)
}

/// Try to decode a public key from a DER encoded byte buffer as defined by SEC1 standard.
pub fn try_decode_der(k: &[u8]) -> Result<PublicKey, DecodingError> {
let buf = Self::del_asn1_header(k).ok_or_else(|| {
DecodingError::failed_to_parse::<Void, _>("ASN.1-encoded ecdsa p256 public key", None)
})?;
Self::from_bytes(buf)
Self::try_from_bytes(buf)
}

// ecPublicKey (ANSI X9.62 public key type) OID: 1.2.840.10045.2.1
Expand Down

0 comments on commit 2826b7c

Please sign in to comment.