Skip to main content

Client

Struct Client 

Source
pub struct Client(/* private fields */);
Expand description

Pkarr client for publishing and resolving SignedPackets over mainline Dht and/or Relays.

Implementations§

Source§

impl Client

Source

pub fn as_blocking(&self) -> ClientBlocking

Returns a blocking (synchronous ) version of Client.

Source§

impl Client

Source

pub fn builder() -> ClientBuilder

Returns a builder to edit config before creating Client.

You can use ClientBuilder::no_default_network to start from a clean slate and decide which networks to use.

Source

pub fn cache(&self) -> Option<&dyn Cache>

Returns a reference to the internal cache.

Source

pub fn dht(&self) -> Option<Dht>

Returns a reference to the internal mainline::Dht node.

Gives you access to methods like mainline::Dht::info, mainline::Dht::bootstrapped, and mainline::Dht::to_bootstrap among the rest of the API.

Source

pub async fn publish( &self, signed_packet: &SignedPacket, cas: Option<Timestamp>, ) -> Result<(), PublishError>

Publishes a SignedPacket to the mainline Dht and or Relays.

§Lost Update Problem

Mainline DHT and remote relays form a distributed network, and like all distributed networks, it is vulnerable to Write–write conflict.

§Read first

To mitigate the risk of lost updates, you should call the Self::resolve_most_recent method then start authoring the new SignedPacket based on the most recent as in the following example:

 use pkarr::{Client, SignedPacket, Keypair};
 // For local testing
 use pkarr::mainline::Testnet;

 #[tokio::main]
 async fn run() -> anyhow::Result<()> {
     let testnet = Testnet::new_async(3).await?;
     let client = Client::builder()
         // Disable the default network settings (builtin relays and mainline bootstrap nodes).
         .no_default_network()
         .bootstrap(&testnet.bootstrap)
         .build()?;

     let keypair = Keypair::random();

     let (signed_packet, cas) = if let Some(most_recent) = client
         .resolve_most_recent(&keypair.public_key()).await
     {

         let mut builder = SignedPacket::builder();

         // 1. Optionally inherit all or some of the existing records.
         for record in most_recent.all_resource_records() {
             let name = record.name.to_string();

             if name != "foo" && name != "sercert" {
                 builder = builder.record(record.clone());
             }
         };

         // 2. Optionally add more new records.
         let signed_packet = builder
             .txt("foo".try_into()?, "bar".try_into()?, 30)
             .a("secret".try_into()?, 42.into(), 30)
             .sign(&keypair)?;

         (
             signed_packet,
             // 3. Use the most recent [SignedPacket::timestamp] as a `CAS`.
             Some(most_recent.timestamp())
         )
     } else {
         (
             SignedPacket::builder()
                 .txt("foo".try_into()?, "bar".try_into()?, 30)
                 .a("secret".try_into()?, 42.into(), 30)
                 .sign(&keypair)?,
             None
         )
     };

     client.publish(&signed_packet, cas).await?;

     Ok(())
 }
§Errors

This method may return on of these errors:

  1. QueryError: when the query fails, and you need to retry or debug the network.
  2. ConcurrencyError: when an write conflict (or the risk of it) is detedcted.

If you get a ConcurrencyError; you should resolver the most recent packet again, and repeat the steps in the previous example.

Source

pub async fn resolve(&self, public_key: &PublicKey) -> Option<SignedPacket>

Returns a SignedPacket from the cache even if it is expired. If there is no packet in the cache, or if the cached packet is expired, it will make a DHT query in a background query and caches any more recent packets it receives.

If you want to get the most recent version of a SignedPacket, you should use Self::resolve_most_recent.

Source

pub async fn resolve_most_recent( &self, public_key: &PublicKey, ) -> Option<SignedPacket>

Returns the most recent SignedPacket found after querying all mainline Dht nodes and or Relays.

Useful if you want to read the most recent packet before publishing a new packet.

This is a best effort, and doesn’t guarantee consistency.

Source§

impl Client

Source

pub fn resolve_https_endpoints<'a>( &'a self, qname: &'a str, ) -> impl Stream<Item = Endpoint> + 'a

Returns an async stream of HTTPS Endpoints

Source

pub fn resolve_svcb_endpoints<'a>( &'a self, qname: &'a str, ) -> impl Stream<Item = Endpoint> + 'a

Returns an async stream of SVCB Endpoints

Source

pub async fn resolve_https_endpoint( &self, qname: &str, ) -> Result<Endpoint, CouldNotResolveEndpoint>

Helper method that returns the first HTTPS Endpoint in the Async stream from Self::resolve_https_endpoints

Source

pub async fn resolve_svcb_endpoint( &self, qname: &str, ) -> Result<Endpoint, CouldNotResolveEndpoint>

Helper method that returns the first SVCB Endpoint in the Async stream from Self::resolve_svcb_endpoints

Source

pub fn resolve_endpoints<'a>( &'a self, qname: &'a str, https: bool, ) -> impl Stream<Item = Endpoint> + 'a

Returns an async stream of either HTTPS or SVCB Endpoints

Trait Implementations§

Source§

impl Clone for Client

Source§

fn clone(&self) -> Client

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 Client

Source§

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

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

impl From<Client> for CertVerifier

Source§

fn from(pkarr_client: Client) -> Self

Converts to this type from the input type.
Source§

impl From<Client> for ClientBuilder

Source§

fn from(client: Client) -> Self

Create a reqwest::ClientBuilder from this Pkarr client, using it as a dns_resolver, and a preconfigured_tls client config that uses rustls::crypto::ring::default_provider() and follows the tls for pkarr domains spec.

Source§

impl From<Client> for ClientConfig

Source§

fn from(client: Client) -> Self

Creates a rustls::ClientConfig that uses rustls::crypto::ring::default_provider() and no client auth and follows the tls for pkarr domains spec.

If you want more control, create a CertVerifier from this Client to use as a custom certificate verifier.

Source§

impl Resolve for Client

Source§

fn resolve(&self, name: Name) -> Resolving

Performs DNS resolution on a Name. The return type is a future containing an iterator of SocketAddr. Read more

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> CompatExt for T

Source§

fn compat(self) -> Compat<T>

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. Read more
Source§

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,