Microdescriptor

Struct Microdescriptor 

Source
pub struct Microdescriptor {
    pub onion_key: String,
    pub ntor_onion_key: Option<String>,
    pub or_addresses: Vec<(IpAddr, u16, bool)>,
    pub family: Vec<String>,
    pub exit_policy: MicroExitPolicy,
    pub exit_policy_v6: Option<MicroExitPolicy>,
    pub identifiers: HashMap<String, String>,
    pub protocols: HashMap<String, Vec<u32>>,
    /* private fields */
}
Expand description

A microdescriptor containing compact relay information for clients.

Microdescriptors are designed to minimize bandwidth for Tor clients. They contain only the information needed for circuit building, omitting details like contact info, platform, and full exit policies.

§Fields Overview

FieldDescription
onion_keyRSA key for TAP handshake (legacy)
ntor_onion_keyCurve25519 key for ntor handshake
exit_policyCompact exit policy (ports only)
familyRelated relay fingerprints
protocolsSupported protocol versions

§Invariants

  • onion_key is always present (required field)
  • exit_policy defaults to “reject 1-65535” if not specified

§Example

use stem_rs::descriptor::{Microdescriptor, Descriptor};

let content = r#"onion-key
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAMhPQtZPaxP3ukybV5LfofKQr20/ljpRk0e9IlGWWMSTkfVvBcHsa6IM
H2KE6s4uuPHp7FqhakXAzJbODobnPHY8l1E4efyrqMQZXEQk2IMhgSNtG6YqUrVF
CxdSKSSy0mmcBe2TOyQsahlGZ9Pudxfnrey7KcfqnArEOqNH09RpAgMBAAE=
-----END RSA PUBLIC KEY-----
p accept 80,443
"#;

let desc = Microdescriptor::parse(content).unwrap();
assert!(desc.exit_policy.is_accept);

§Thread Safety

Microdescriptor is Send and Sync as it contains only owned data.

Fields§

§onion_key: String

RSA onion key for TAP circuit handshake (PEM format).

This is the legacy key used for the original Tor handshake. Modern clients prefer the ntor handshake using ntor_onion_key.

§ntor_onion_key: Option<String>

Curve25519 onion key for ntor circuit handshake (base64).

This is the modern key used for the ntor handshake, which provides better security properties than the TAP handshake.

§or_addresses: Vec<(IpAddr, u16, bool)>

Additional addresses (IPv4 or IPv6) the relay listens on.

Each tuple is (address, port, is_ipv6). The a lines in the microdescriptor provide these additional addresses.

§family: Vec<String>

Fingerprints of related relays (same operator).

These are typically prefixed with $ and contain the full 40-character hex fingerprint.

§exit_policy: MicroExitPolicy

Compact IPv4 exit policy.

Unlike full exit policies, microdescriptor policies only specify which ports are accepted or rejected, not addresses.

§exit_policy_v6: Option<MicroExitPolicy>

Compact IPv6 exit policy.

Separate policy for IPv6 traffic, if different from IPv4.

§identifiers: HashMap<String, String>

Identity key digests by type.

Maps key type (e.g., “rsa1024”, “ed25519”) to the base64-encoded digest of that key.

§protocols: HashMap<String, Vec<u32>>

Supported protocol versions.

Maps protocol name to list of supported versions. Common protocols include “Link”, “Relay”, “HSDir”, etc.

Implementations§

Source§

impl Microdescriptor

Source

pub fn new(onion_key: String) -> Self

Creates a new microdescriptor with the given onion key.

This creates a descriptor with default values for optional fields. The exit policy defaults to rejecting all ports.

§Arguments
  • onion_key - The RSA onion key in PEM format
§Example
use stem_rs::descriptor::Microdescriptor;

let key = "-----BEGIN RSA PUBLIC KEY-----\n...\n-----END RSA PUBLIC KEY-----";
let desc = Microdescriptor::new(key.to_string());
assert!(desc.ntor_onion_key.is_none());
assert!(desc.family.is_empty());
Source

pub fn parse_with_annotations( content: &str, annotations: &[&str], ) -> Result<Self, Error>

Parses a microdescriptor with additional annotations.

This is useful when annotations are provided separately from the descriptor content (e.g., when reading from a cache file where annotations precede the descriptor).

§Arguments
  • content - The microdescriptor content
  • annotations - Additional annotation lines (with or without @ prefix)
§Errors

Returns Error::Parse if the content is malformed.

§Example
use stem_rs::descriptor::Microdescriptor;

let content = r#"onion-key
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAMhPQtZPaxP3ukybV5LfofKQr20/ljpRk0e9IlGWWMSTkfVvBcHsa6IM
H2KE6s4uuPHp7FqhakXAzJbODobnPHY8l1E4efyrqMQZXEQk2IMhgSNtG6YqUrVF
CxdSKSSy0mmcBe2TOyQsahlGZ9Pudxfnrey7KcfqnArEOqNH09RpAgMBAAE=
-----END RSA PUBLIC KEY-----
"#;

let annotations = &["@last-listed 2023-01-01 00:00:00"];
let desc = Microdescriptor::parse_with_annotations(content, annotations).unwrap();

let anns = desc.get_annotations();
assert!(anns.contains_key("last-listed"));
Source

pub fn get_annotations(&self) -> HashMap<String, Option<String>>

Returns all annotations as a map.

Annotations are metadata lines starting with @ that may be present in CollecTor archives.

§Example
use stem_rs::descriptor::{Microdescriptor, Descriptor};

let content = r#"@last-listed 2023-01-01 00:00:00
onion-key
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAMhPQtZPaxP3ukybV5LfofKQr20/ljpRk0e9IlGWWMSTkfVvBcHsa6IM
H2KE6s4uuPHp7FqhakXAzJbODobnPHY8l1E4efyrqMQZXEQk2IMhgSNtG6YqUrVF
CxdSKSSy0mmcBe2TOyQsahlGZ9Pudxfnrey7KcfqnArEOqNH09RpAgMBAAE=
-----END RSA PUBLIC KEY-----
"#;

let desc = Microdescriptor::parse(content).unwrap();
let annotations = desc.get_annotations();

if let Some(Some(date)) = annotations.get("last-listed") {
    println!("Last listed: {}", date);
}
Source

pub fn get_annotation_lines(&self) -> Vec<String>

Returns annotations formatted as lines with @ prefix.

This is useful for serializing annotations back to their original format.

§Example
use stem_rs::descriptor::{Microdescriptor, Descriptor};

let content = r#"@last-listed 2023-01-01 00:00:00
onion-key
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBAMhPQtZPaxP3ukybV5LfofKQr20/ljpRk0e9IlGWWMSTkfVvBcHsa6IM
H2KE6s4uuPHp7FqhakXAzJbODobnPHY8l1E4efyrqMQZXEQk2IMhgSNtG6YqUrVF
CxdSKSSy0mmcBe2TOyQsahlGZ9Pudxfnrey7KcfqnArEOqNH09RpAgMBAAE=
-----END RSA PUBLIC KEY-----
"#;

let desc = Microdescriptor::parse(content).unwrap();
for line in desc.get_annotation_lines() {
    println!("{}", line);
}

Trait Implementations§

Source§

impl Clone for Microdescriptor

Source§

fn clone(&self) -> Microdescriptor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Microdescriptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Descriptor for Microdescriptor

Source§

fn parse(content: &str) -> Result<Self, Error>

Parses a descriptor from its string content. Read more
Source§

fn to_descriptor_string(&self) -> String

Serializes the descriptor to its canonical string format. Read more
Source§

fn digest( &self, hash: DigestHash, encoding: DigestEncoding, ) -> Result<String, Error>

Computes the cryptographic digest of the descriptor. Read more
Source§

fn raw_content(&self) -> &[u8]

Returns the raw bytes of the original descriptor content. Read more
Source§

fn unrecognized_lines(&self) -> &[String]

Returns lines from the descriptor that were not recognized. Read more
Source§

impl Display for Microdescriptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Microdescriptor

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Microdescriptor

Source§

fn eq(&self, other: &Microdescriptor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Microdescriptor

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.