Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
17 changes: 13 additions & 4 deletions iroh-base/src/endpoint_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,20 @@ impl EndpointAddr {
///
/// This still is usable with e.g. a discovery service to establish a connection,
/// depending on the situation.
pub fn new(id: PublicKey) -> Self {
pub fn new(id: impl Into<EndpointId>) -> Self {
EndpointAddr {
id,
id: id.into(),
addrs: Default::default(),
}
}

/// Creates a new [`EndpointAddr`] from its parts.
pub fn from_parts(id: PublicKey, addrs: impl IntoIterator<Item = TransportAddr>) -> Self {
pub fn from_parts(
id: impl Into<EndpointId>,
addrs: impl IntoIterator<Item = TransportAddr>,
) -> Self {
Self {
id,
id: id.into(),
addrs: addrs.into_iter().collect(),
}
}
Expand Down Expand Up @@ -118,6 +121,12 @@ impl EndpointAddr {
}
}

impl From<PublicKey> for EndpointAddr {
fn from(endpoint_id: PublicKey) -> Self {
EndpointAddr::new(endpoint_id)
}
}

impl From<EndpointId> for EndpointAddr {
fn from(endpoint_id: EndpointId) -> Self {
EndpointAddr::new(endpoint_id)
Expand Down
53 changes: 51 additions & 2 deletions iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,57 @@ use serde::{Deserialize, Serialize, de, ser};
#[repr(transparent)]
pub struct PublicKey(CompressedEdwardsY);

/// The identifier for an endpoint in the (iroh) network.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum EndpointId {
/// An Ed25519 public key.
Ed25519(PublicKey),
}

impl PartialEq<PublicKey> for EndpointId {
fn eq(&self, other: &PublicKey) -> bool {
match self {
EndpointId::Ed25519(key) => key == other,
}
}
}

impl Display for EndpointId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
EndpointId::Ed25519(key) => write!(f, "Ed25519({})", key),
}
}
}

impl EndpointId {
/// If this is an Ed25519 endpoint id, return the public key.
pub fn as_ed(self) -> Option<PublicKey> {
match self {
EndpointId::Ed25519(key) => Some(key),
}
}

/// Expect this to be an Ed25519 endpoint id, panic if not.
pub fn expect_ed(self) -> PublicKey {
self.as_ed().expect("not an ed25519 endpoint id")
}

/// Format a short representation of this endpoint id.
pub fn fmt_short(&self) -> String {
match self {
EndpointId::Ed25519(key) => key.fmt_short().to_string(),
}
}
}

impl From<PublicKey> for EndpointId {
fn from(key: PublicKey) -> Self {
EndpointId::Ed25519(key)
}
}

impl Borrow<[u8; 32]> for PublicKey {
fn borrow(&self) -> &[u8; 32] {
self.as_bytes()
Expand Down Expand Up @@ -61,8 +112,6 @@ impl Ord for PublicKey {
///
/// - `encrypt(key: PublicKey)`
/// - `send_to(endpoint: EndpointId)`
pub type EndpointId = PublicKey;

impl Hash for PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.hash(state);
Expand Down
6 changes: 3 additions & 3 deletions iroh-dns-server/examples/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::str::FromStr;

use clap::Parser;
use iroh::EndpointId;
use iroh::PublicKey;
use n0_error::{Result, StdResultExt};

#[derive(Debug, Parser)]
Expand All @@ -20,13 +20,13 @@ fn main() -> Result<()> {
let args = Cli::parse();
match args.command {
Command::EndpointToPkarr { endpoint_id } => {
let endpoint_id = EndpointId::from_str(&endpoint_id)?;
let endpoint_id = PublicKey::from_str(&endpoint_id)?;
let public_key = pkarr::PublicKey::try_from(endpoint_id.as_bytes()).anyerr()?;
println!("{}", public_key.to_z32())
}
Command::PkarrToEndpoint { z32_pubkey } => {
let public_key = pkarr::PublicKey::try_from(z32_pubkey.as_str()).anyerr()?;
let endpoint_id = EndpointId::from_bytes(public_key.as_bytes())?;
let endpoint_id = PublicKey::from_bytes(public_key.as_bytes())?;
println!("{endpoint_id}")
}
}
Expand Down
6 changes: 3 additions & 3 deletions iroh-dns-server/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::{net::SocketAddr, str::FromStr};

use clap::{Parser, ValueEnum};
use iroh::{
EndpointId, SecretKey,
PublicKey, SecretKey,
discovery::{
UserData,
dns::{N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING},
pkarr::{N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING, PkarrRelayClient},
},
endpoint_info::{EndpointIdExt, EndpointInfo, IROH_TXT_NAME},
endpoint_info::{EndpointInfo, IROH_TXT_NAME, PublicKeyExt},
};
use n0_error::{Result, StackResultExt};
use url::Url;
Expand Down Expand Up @@ -140,6 +140,6 @@ async fn main() -> Result<()> {
Ok(())
}

fn fmt_domain(endpoint_id: &EndpointId, origin: &str) -> String {
fn fmt_domain(endpoint_id: &PublicKey, origin: &str) -> String {
format!("{IROH_TXT_NAME}.{}.{origin}", endpoint_id.to_z32())
}
4 changes: 2 additions & 2 deletions iroh-dns-server/examples/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{Parser, ValueEnum};
use iroh::{
EndpointId,
PublicKey,
discovery::dns::{N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING},
dns::DnsResolver,
};
Expand Down Expand Up @@ -34,7 +34,7 @@ enum Command {
/// Resolve endpoint info by endpoint id.
Endpoint {
/// The endpoint id to resolve.
endpoint_id: EndpointId,
endpoint_id: PublicKey,
/// Use a custom domain when resolving endpoint info via DNS.
#[clap(long)]
dns_origin_domain: Option<String>,
Expand Down
10 changes: 5 additions & 5 deletions iroh-relay/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use hickory_resolver::{
config::{ResolverConfig, ResolverOpts},
name_server::TokioConnectionProvider,
};
use iroh_base::EndpointId;
use iroh_base::PublicKey;
use n0_error::{StackError, e, stack_error};
use n0_future::{
StreamExt,
Expand Down Expand Up @@ -382,13 +382,13 @@ impl DnsResolver {
stagger_call(f, delays_ms).await
}

/// Looks up endpoint info by [`EndpointId`] and origin domain name.
/// Looks up endpoint info by [`PublicKey`] and origin domain name.
///
/// To lookup endpoints that published their endpoint info to the DNS servers run by n0,
/// pass [`N0_DNS_ENDPOINT_ORIGIN_PROD`] as `origin`.
pub async fn lookup_endpoint_by_id(
&self,
endpoint_id: &EndpointId,
endpoint_id: &PublicKey,
origin: &str,
) -> Result<EndpointInfo, LookupError> {
let name = endpoint_info::endpoint_domain(endpoint_id, origin);
Expand Down Expand Up @@ -424,15 +424,15 @@ impl DnsResolver {
stagger_call(f, delays_ms).await
}

/// Looks up endpoint info by [`EndpointId`] and origin domain name.
/// Looks up endpoint info by [`PublicKey`] and origin domain name.
///
/// From the moment this function is called, each lookup is scheduled after the delays in
/// `delays_ms` with the first call being done immediately. `[200ms, 300ms]` results in calls
/// at T+0ms, T+200ms and T+300ms. The result of the first successful call is returned, or a
/// summary of all errors otherwise.
pub async fn lookup_endpoint_by_id_staggered(
&self,
endpoint_id: &EndpointId,
endpoint_id: &PublicKey,
origin: &str,
delays_ms: &[u64],
) -> Result<EndpointInfo, StaggeredError<LookupError>> {
Expand Down
Loading
Loading