Endpoint Addresses
Endpoint Addresses or EndpointAddrs
are a common struct you'll interact when working with iroh to tell iroh what & where to dial. In rust they look like this:
pub struct Addr {
pub id: PublicKey,
pub addrs: BTreeSet<TransportAddr>,
}
You'll interact with EndpointAddr
s a fair amount when working with iroh. It's also quite normal to construct addresses manually from, say, endpoint identifiers stored in your application database.
When we call connect
on an Endpoint, we need to pass either a EndpointAddr
, or something that can turn into a EndpointAddr
. In iroh Endpoint
s will have different fields populated depending on where they came from, and the discovery services you've configured your endpoint with.
Interaction with discovery
From the above struct, the only required field is the id
. And because of this, there's an implementation of From
that can turn EndpointIDs
directly into EndpointAddrs. but this will only work if you have a discovery service that can resolve EndpointIDs enabled. Thankfully, we enable discovery by default:
use iroh::Endpoint;
// enables dialing by EndpointAddrs that only have EndpointIDs by default:
let ep = Endpoint::builder()
.bind()
.await?;
This is why we actively encourage configuring a discovery service, and DNS is the most common one we recommend. Because we're in p2p land dialing details & even home relays for an endpoint can change on very short notice, making this data go stale quickly. Endpoint Identifiers are a practical source of stability that counteracts this.
When to provide full details
If you have full dialing details, it's well worth providing them as part of a EndpointAddr
passed to connect
. Iroh can use this to skip the network roundtrip required to either do initial address discovery, or update cached addresses. So if you have a source of up to date home relay & dialing info, provide it!
Don't store relay_url & direct_addresses values
If you're persisting the contents of EndpointAddrs
in your app, it's probably not worth keeping the relay_url
and direct_address
fields, unless you know these details are unlikely to change. Providing stale details to the endpoint can slow down connection construction.