[][src]Struct libra_network_address::NetworkAddress

pub struct NetworkAddress(_);

Libra NetworkAddress is a compact, efficient, self-describing and future-proof network address represented as a stack of protocols. Essentially libp2p's multiaddr but using [lcs] to describe the binary format.

Most validators will advertise a network address like:


Unpacking, the above effectively means:

  1. Resolve the DNS name "example.com" to an ip address, addr.
  2. Open a TCP connection to (addr, 6180).
  3. Perform a Noise IK handshake and assume the peer's static pubkey is <x25519-pubkey>. After this step, we will have a secure, authenticated connection with the peer.
  4. Perform a LibraNet version negotiation handshake (version 1).

One key concept behind NetworkAddress is that it is fully self-describing, which allows us to easily "pre-negotiate" protocols while also allowing for future upgrades. For example, it is generally unsafe to negotiate a secure transport in-band. Instead, with NetworkAddress we can advertise (via discovery) the specific secure transport protocol and public key that we support (and even advertise multiple incompatible versions). When a peer wishes to establish a connection with us, they already know which secure transport protocol to use; in this sense, the secure transport protocol is "pre-negotiated" by the dialier selecting which advertised protocol to use.

In addition, NetworkAddress is integrated with the LibraNet concept of a [Transport], which takes a NetworkAddress when dialing and peels off [Protocols] to establish a connection and perform initial handshakes. Similarly, the Transport takes NetworkAddress to listen on, which tells it what protocols to expect on the socket.

The network address is encoded with the length of the encoded NetworkAddresses and then the the protocol slices to allow for transparent upgradeability. For example, if the current software cannot decode a NetworkAddress within a Vec it can still decode the underlying Vec and retrieve the remaining Vec.

An example of a serialized NetworkAddress:

// human-readable format:
//   "/ip4/"
// serialized NetworkAddress:
//      [ 09 02 00 0a 00 00 10 05 80 00 ]
//          \  \  \  \           \  \
//           \  \  \  \           \  '-- u16 tcp port
//            \  \  \  \           '-- uvarint protocol id for /tcp
//             \  \  \  '-- u32 ipv4 address
//              \  \  '-- uvarint protocol id for /ip4
//               \  '-- uvarint number of protocols
//                '-- length of encoded network address

use libra_network_address::NetworkAddress;
use lcs;
use std::{str::FromStr, convert::TryFrom};

let addr = NetworkAddress::from_str("/ip4/").unwrap();
let actual_ser_addr = lcs::to_bytes(&addr).unwrap();

let expected_ser_addr: Vec<u8> = [9, 2, 0, 10, 0, 0, 16, 5, 80, 0].to_vec();

assert_eq!(expected_ser_addr, actual_ser_addr);


impl NetworkAddress[src]

pub fn as_slice(&self) -> &[Protocol][src]

pub fn push(self, proto: Protocol) -> Self[src]

pub fn extend_from_slice(self, protos: &[Protocol]) -> Self[src]

pub fn encrypt(
    shared_val_netaddr_key: &Key,
    key_version: KeyVersion,
    account: &AccountAddress,
    seq_num: u64,
    addr_idx: u32
) -> Result<EncNetworkAddress, ParseError>

pub fn append_prod_protos(
    network_pubkey: PublicKey,
    handshake_version: u8
) -> Self

Given a base NetworkAddress, append production protocols and return the modified NetworkAddress.


use libra_crypto::{traits::ValidCryptoMaterialStringExt, x25519};
use libra_network_address::NetworkAddress;
use std::str::FromStr;

let pubkey_str = "080e287879c918794170e258bfaddd75acac5b3e350419044655e4983a487120";
let pubkey = x25519::PublicKey::from_encoded_string(pubkey_str).unwrap();
let addr = NetworkAddress::from_str("/dns/example.com/tcp/6180").unwrap();
let addr = addr.append_prod_protos(pubkey, 0);

pub fn is_libranet_addr(&self) -> bool[src]

Check that a NetworkAddress looks like a typical LibraNet address with associated protocols.

"typical" LibraNet addresses begin with a transport protocol:

"/ip4/<addr>/tcp/<port>" or "/ip6/<addr>/tcp/<port>" or "/dns4/<domain>/tcp/<port>" or "/dns6/<domain>/tcp/<port>" or "/dns/<domain>/tcp/<port>" or cfg!(test) "/memory/<port>"

followed by transport upgrade handshake protocols:



use libra_network_address::NetworkAddress;
use std::str::FromStr;

let addr_str = "/ip4/";
let addr = NetworkAddress::from_str(addr_str).unwrap();

pub fn find_noise_proto(&self) -> Option<PublicKey>[src]

A temporary, hacky function to parse out the first /ln-noise-ik/<pubkey> from a NetworkAddress. We can remove this soon, when we move to the interim "monolithic" transport model.

pub fn rotate_noise_public_key(
    &mut self,
    to_replace: &PublicKey,
    new_public_key: &PublicKey

A function to rotate public keys for NoiseIK protocols

pub fn mock() -> Self[src]

Trait Implementations

impl Arbitrary for NetworkAddress[src]

type Parameters = ()

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default. Read more

type Strategy = BoxedStrategy<Self>

The type of Strategy used to generate values of type Self. Read more

impl Clone for NetworkAddress[src]

impl Debug for NetworkAddress[src]

impl<'de> Deserialize<'de> for NetworkAddress[src]

impl Display for NetworkAddress[src]

impl Eq for NetworkAddress[src]

impl From<Protocol> for NetworkAddress[src]

impl From<SocketAddr> for NetworkAddress[src]

impl FromStr for NetworkAddress[src]

type Err = ParseError

The associated error which can be returned from parsing.

impl IntoIterator for NetworkAddress[src]

type Item = Protocol

The type of the elements being iterated over.

type IntoIter = IntoIter<Self::Item>

Which kind of iterator are we turning this into?

impl PartialEq<NetworkAddress> for NetworkAddress[src]

impl Serialize for NetworkAddress[src]

impl StructuralEq for NetworkAddress[src]

impl StructuralPartialEq for NetworkAddress[src]

impl ToSocketAddrs for NetworkAddress[src]

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to. Read more

impl TryFrom<Vec<Protocol>> for NetworkAddress[src]

type Error = EmptyError

The type returned in the event of a conversion error.

Auto Trait Implementations

impl RefUnwindSafe for NetworkAddress

impl Send for NetworkAddress

impl Sync for NetworkAddress

impl Unpin for NetworkAddress

impl UnwindSafe for NetworkAddress

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized

impl<T> Borrow<T> for T where
    T: ?Sized

impl<T> BorrowMut<T> for T where
    T: ?Sized

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 

impl<I> IntoIterator for I where
    I: Iterator

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T> TestOnlyHash for T where
    T: Serialize + ?Sized

impl<T> ToOwned for T where
    T: Clone

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,