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

Signed Peer Records #2081

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
2 changes: 1 addition & 1 deletion core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
// DEALINGS IN THE SOFTWARE.

fn main() {
prost_build::compile_protos(&["src/keys.proto"], &["src"]).unwrap();
prost_build::compile_protos(&["src/keys.proto", "src/envelope.proto", "src/peer_record.proto"], &["src"]).unwrap();
}
12 changes: 12 additions & 0 deletions core/src/envelope.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto2";
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

package envelope_proto;

import "keys.proto";

message Envelope {
required keys_proto.PublicKey public_key = 1;
required bytes payload_type = 2;
required bytes payload = 3;
required bytes signature = 5;
}
54 changes: 34 additions & 20 deletions core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod error;

use self::error::*;
use crate::{PeerId, keys_proto};
use std::convert::{TryFrom, TryInto};

/// Identity keypair of a node.
///
Expand Down Expand Up @@ -135,6 +136,7 @@ impl PublicKey {
/// that the signature has been produced by the corresponding
/// private key (authenticity), and that the message has not been
/// tampered with (integrity).
#[must_use]
pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool {
use PublicKey::*;
match self {
Expand All @@ -151,7 +153,33 @@ impl PublicKey {
pub fn into_protobuf_encoding(self) -> Vec<u8> {
use prost::Message;

let public_key = match self {
let public_key = keys_proto::PublicKey::from(self);

let mut buf = Vec::with_capacity(public_key.encoded_len());
public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
buf
}

/// Decode a public key from a protobuf structure, e.g. read from storage
/// or received from another node.
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
use prost::Message;

let pubkey = keys_proto::PublicKey::decode(bytes)
.map_err(|e| DecodingError::new("Protobuf").source(e))?;

pubkey.try_into()
}

/// Convert the `PublicKey` into the corresponding `PeerId`.
pub fn into_peer_id(self) -> PeerId {
self.into()
}
}

impl From<PublicKey> for keys_proto::PublicKey {
fn from(key: PublicKey) -> Self {
match key {
PublicKey::Ed25519(key) =>
keys_proto::PublicKey {
r#type: keys_proto::KeyType::Ed25519 as i32,
Expand All @@ -169,22 +197,14 @@ impl PublicKey {
r#type: keys_proto::KeyType::Secp256k1 as i32,
data: key.encode().to_vec()
}
};

let mut buf = Vec::with_capacity(public_key.encoded_len());
public_key.encode(&mut buf).expect("Vec<u8> provides capacity as needed");
buf
}
}
}

/// Decode a public key from a protobuf structure, e.g. read from storage
/// or received from another node.
pub fn from_protobuf_encoding(bytes: &[u8]) -> Result<PublicKey, DecodingError> {
use prost::Message;

#[allow(unused_mut)] // Due to conditional compilation.
let mut pubkey = keys_proto::PublicKey::decode(bytes)
.map_err(|e| DecodingError::new("Protobuf").source(e))?;
impl TryFrom<keys_proto::PublicKey> for PublicKey {
type Error = DecodingError;

fn try_from(pubkey: keys_proto::PublicKey) -> Result<Self, Self::Error> {
let key_type = keys_proto::KeyType::from_i32(pubkey.r#type)
.ok_or_else(|| DecodingError::new(format!("unknown key type: {}", pubkey.r#type)))?;

Expand Down Expand Up @@ -212,10 +232,4 @@ impl PublicKey {
}
}
}

/// Convert the `PublicKey` into the corresponding `PeerId`.
pub fn into_peer_id(self) -> PeerId {
self.into()
}
}

12 changes: 12 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ mod keys_proto {
include!(concat!(env!("OUT_DIR"), "/keys_proto.rs"));
}

mod envelope_proto {
include!(concat!(env!("OUT_DIR"), "/envelope_proto.rs"));
}

mod peer_record_proto {
include!(concat!(env!("OUT_DIR"), "/peer_record_proto.rs"));
}

/// Multi-address re-export.
pub use multiaddr;
pub type Negotiated<T> = multistream_select::Negotiated<T>;
Expand All @@ -53,6 +61,8 @@ pub mod muxing;
pub mod network;
pub mod transport;
pub mod upgrade;
pub mod signed_envelope;
pub mod peer_record;

pub use multiaddr::Multiaddr;
pub use multihash;
Expand All @@ -64,6 +74,8 @@ pub use translation::address_translation;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, UpgradeError, ProtocolName};
pub use connection::{Connected, Endpoint, ConnectedPoint};
pub use network::Network;
pub use signed_envelope::SignedEnvelope;
pub use peer_record::{AuthenticatedPeerRecord, PeerRecord};

use std::{future::Future, pin::Pin};

Expand Down
22 changes: 22 additions & 0 deletions core/src/peer_record.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
syntax = "proto2";
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

package peer_record_proto;

// PeerRecord contains the listen addresses for a peer at a particular point in time.
message PeerRecord {
// AddressInfo wraps a multiaddr. In the future, it may be extended to
// contain additional metadata, such as "routability" (whether an address is
// local or global, etc).
message AddressInfo {
required bytes multiaddr = 1;
}

// the peer id of the subject of the record (who these addresses belong to).
required bytes peer_id = 1;

// A monotonically increasing sequence number, used for record ordering.
required uint64 seq = 2;

// All current listen addresses
repeated AddressInfo addresses = 3;
}
Loading