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
6 changes: 3 additions & 3 deletions iroh-base/src/endpoint_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{collections::BTreeSet, net::SocketAddr};

use serde::{Deserialize, Serialize};

use crate::{EndpointId, PublicKey, RelayUrl};
use crate::{EndpointId, RelayUrl};

/// Network-level addressing information for an iroh endpoint.
///
Expand Down Expand Up @@ -59,15 +59,15 @@ 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: EndpointId) -> Self {
EndpointAddr {
id,
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: EndpointId, addrs: impl IntoIterator<Item = TransportAddr>) -> Self {
Self {
id,
addrs: addrs.into_iter().collect(),
Expand Down
60 changes: 59 additions & 1 deletion iroh-base/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,65 @@ impl Ord for PublicKey {
///
/// - `encrypt(key: PublicKey)`
/// - `send_to(endpoint: EndpointId)`
pub type EndpointId = PublicKey;
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum EndpointId {
/// An Ed25519 public key.
Ed25519(PublicKey),
/// A Secp256k1 public key.
Secp256k1(u128),
}

impl EndpointId {
/// If this endpoint id is an ed25519 key, return it.
pub fn ed25519(&self) -> Option<&PublicKey> {
match self {
EndpointId::Ed25519(pk) => Some(pk),
EndpointId::Secp256k1(_) => None,
}
}

/// If this endpoint id is an ed25519 key, return it.
pub fn to_ed25519(self) -> Option<PublicKey> {
match self {
EndpointId::Ed25519(pk) => Some(pk),
EndpointId::Secp256k1(_) => None,
}
}
}

impl FromStr for EndpointId {
type Err = KeyParsingError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let pk = PublicKey::from_str(s)?;
Ok(EndpointId::Ed25519(pk))
}
}

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

impl EndpointId {
/// Format a short version of the endpoint id for logging purposes.
pub fn fmt_short(&self) -> String {
match self {
EndpointId::Ed25519(pk) => pk.fmt_short().to_string(),
EndpointId::Secp256k1(s) => format!("secp256k1:{s:05x}"),
}
}
}

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

impl Hash for PublicKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
2 changes: 1 addition & 1 deletion iroh-dns-server/benches/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn benchmark_dns_server(c: &mut Criterion) {

let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(42);
let secret_key = SecretKey::generate(&mut rng);
let endpoint_id = secret_key.public();
let endpoint_id = secret_key.public().into();

let pkarr_relay = LOCALHOST_PKARR.parse().expect("valid url");
let pkarr = PkarrRelayClient::new(pkarr_relay);
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::{EndpointId, PublicKey};
use n0_snafu::{Result, ResultExt};

#[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()).e()?;
println!("{}", public_key.to_z32())
}
Command::PkarrToEndpoint { z32_pubkey } => {
let public_key = pkarr::PublicKey::try_from(z32_pubkey.as_str()).e()?;
let endpoint_id = EndpointId::from_bytes(public_key.as_bytes()).e()?;
let endpoint_id: EndpointId = PublicKey::from_bytes(public_key.as_bytes()).e()?.into();
println!("{endpoint_id}")
}
}
Expand Down
12 changes: 4 additions & 8 deletions iroh-dns-server/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ use std::{net::SocketAddr, str::FromStr};

use clap::{Parser, ValueEnum};
use iroh::{
EndpointId, 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},
dns::{N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING}, pkarr::{PkarrRelayClient, N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING}, UserData
}, endpoint_info::{EndpointIdExt, EndpointInfo, IROH_TXT_NAME}, PublicKey, SecretKey
};
use n0_snafu::{Result, ResultExt};
use url::Url;
Expand Down Expand Up @@ -103,7 +99,7 @@ async fn main() -> Result<()> {
println!("publish to {pkarr_relay_url} ...");

let pkarr = PkarrRelayClient::new(pkarr_relay_url);
let endpoint_info = EndpointInfo::new(endpoint_id)
let endpoint_info = EndpointInfo::new(endpoint_id.into())
.with_relay_url(relay_url.map(Into::into))
.with_ip_addrs(args.addr.into_iter().collect())
.with_user_data(args.user_data);
Expand Down Expand Up @@ -140,6 +136,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())
}
2 changes: 1 addition & 1 deletion iroh-dns-server/examples/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async fn main() -> Result<()> {
(None, Env::Dev) => DEV_DNS_ORIGIN_DOMAIN,
};
resolver
.lookup_endpoint_by_id(&endpoint_id, origin_domain)
.lookup_endpoint_by_id(&endpoint_id.ed25519().unwrap(), origin_domain)
.await?
}
Command::Domain { domain } => resolver.lookup_endpoint_by_domain_name(&domain).await?,
Expand Down
13 changes: 6 additions & 7 deletions iroh-dns-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ mod tests {
};

use iroh::{
RelayUrl, SecretKey, discovery::pkarr::PkarrRelayClient, dns::DnsResolver,
endpoint_info::EndpointInfo,
discovery::pkarr::PkarrRelayClient, dns::DnsResolver, endpoint_info::EndpointInfo, EndpointId, RelayUrl, SecretKey
};
use n0_snafu::{Result, ResultExt};
use pkarr::{SignedPacket, Timestamp};
Expand Down Expand Up @@ -171,7 +170,7 @@ mod tests {
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(0u64);

let secret_key = SecretKey::generate(&mut rng);
let endpoint_id = secret_key.public();
let endpoint_id = secret_key.public().into();
let pkarr = PkarrRelayClient::new(pkarr_relay);
let relay_url: RelayUrl = "https://relay.example.".parse()?;
let endpoint_info = EndpointInfo::new(endpoint_id).with_relay_url(Some(relay_url.clone()));
Expand All @@ -180,7 +179,7 @@ mod tests {
pkarr.publish(&signed_packet).await?;

let resolver = test_resolver(nameserver);
let res = resolver.lookup_endpoint_by_id(&endpoint_id, origin).await?;
let res = resolver.lookup_endpoint_by_id(endpoint_id.ed25519().unwrap(), origin).await?;

assert_eq!(res.endpoint_id, endpoint_id);
assert_eq!(res.relay_urls().next(), Some(&relay_url));
Expand Down Expand Up @@ -239,7 +238,7 @@ mod tests {

// create a signed packet
let secret_key = SecretKey::generate(&mut rng);
let endpoint_id = secret_key.public();
let endpoint_id: EndpointId = secret_key.public().into();
let relay_url: RelayUrl = "https://relay.example.".parse()?;
let endpoint_info = EndpointInfo::new(endpoint_id).with_relay_url(Some(relay_url.clone()));
let signed_packet = endpoint_info.to_pkarr_signed_packet(&secret_key, 30)?;
Expand All @@ -254,7 +253,7 @@ mod tests {

// resolve via DNS from our server, which will lookup from our DHT
let resolver = test_resolver(nameserver);
let res = resolver.lookup_endpoint_by_id(&endpoint_id, origin).await?;
let res = resolver.lookup_endpoint_by_id(endpoint_id.ed25519().unwrap(), origin).await?;

assert_eq!(res.endpoint_id, endpoint_id);
assert_eq!(res.relay_urls().next(), Some(&relay_url));
Expand All @@ -271,7 +270,7 @@ mod tests {
let secret_key = SecretKey::generate(rng);
let endpoint_id = secret_key.public();
let relay_url: RelayUrl = "https://relay.example.".parse()?;
let endpoint_info = EndpointInfo::new(endpoint_id).with_relay_url(Some(relay_url.clone()));
let endpoint_info = EndpointInfo::new(endpoint_id.into()).with_relay_url(Some(relay_url.clone()));
let packet = endpoint_info.to_pkarr_signed_packet(&secret_key, 30)?;
Ok(packet)
}
Expand Down
6 changes: 3 additions & 3 deletions iroh-relay/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use hickory_resolver::{
config::{ResolverConfig, ResolverOpts},
name_server::TokioConnectionProvider,
};
use iroh_base::EndpointId;
use n0_future::{
StreamExt,
boxed::BoxFuture,
Expand All @@ -25,6 +24,7 @@ use tracing::debug;
use url::Url;

use crate::{
RelayEndpointId,
defaults::timeouts::DNS_TIMEOUT,
endpoint_info::{self, EndpointInfo, ParseError},
};
Expand Down Expand Up @@ -417,7 +417,7 @@ impl DnsResolver {
/// pass [`N0_DNS_ENDPOINT_ORIGIN_PROD`] as `origin`.
pub async fn lookup_endpoint_by_id(
&self,
endpoint_id: &EndpointId,
endpoint_id: &RelayEndpointId,
origin: &str,
) -> Result<EndpointInfo, LookupError> {
let name = endpoint_info::endpoint_domain(endpoint_id, origin);
Expand Down Expand Up @@ -467,7 +467,7 @@ impl DnsResolver {
/// summary of all errors otherwise.
pub async fn lookup_endpoint_by_id_staggered(
&self,
endpoint_id: &EndpointId,
endpoint_id: &RelayEndpointId,
origin: &str,
delays_ms: &[u64],
) -> Result<EndpointInfo, StaggeredError<LookupError>> {
Expand Down
Loading
Loading