Skip to main content

Pubky

Struct Pubky 

Source
pub struct Pubky { /* private fields */ }
Expand description

High-level facade. Owns a PubkyHttpClient and constructs the main actors. Prefer to instantiate only once and use trough your application a single shared Pubky instead of constructing one per request. This avoids reinitializing transports and keeps the same client available for repeated usage.

Implementations§

Source§

impl Pubky

Source

pub fn new() -> Result<Self>

Construct with defaults (mainnet relays, standard timeouts).

§Errors
Source

pub fn testnet() -> Result<Self>

Construct preconfigured for a local Pubky testnet.

§Errors
Source

pub const fn with_client(client: PubkyHttpClient) -> Self

Construct from an already-configured transport.

Source

pub fn start_auth_flow( &self, caps: &Capabilities, auth_kind: AuthFlowKind, ) -> Result<PubkyAuthFlow>

Start an end-to-end auth flow (QR/deeplink). Depending on the auth kind, the flow will be different.

  • AuthFlowKind::SignIn - Sign in to an existing account.
  • AuthFlowKind::SignUp - Sign up for a new account.

Use with flow.authorization_url() and then await_approval() (blocking) or try_poll_once() (non-blocking UI loops).

§Errors
  • crate::errors::Error::Parse if internal URL construction for the flow fails (e.g., malformed relay URL when configured via the builder).
Source

pub fn resume_auth_flow(&self, authorization_url: &str) -> Result<PubkyAuthFlow>

Resume a previously started auth flow from its authorization_url.

Parses the secret, capabilities, relay, and flow kind from the URL and rebuilds the flow against the same relay channel. If the signer already approved, the first try_poll_once() returns a session.

The relay inbox persists messages for ~5 minutes; resume is only viable within that window. After the TTL expires the channel is gone and you must start a fresh flow with start_auth_flow.

The authorization_url contains the client_secret; follow start_auth_flow storage guidance and delete it once resume completes or is abandoned.

§Errors
Source

pub fn signer(&self, keypair: Keypair) -> PubkySigner

Create a PubkySigner for a given keypair.

Source

pub fn public_storage(&self) -> PublicStorage

Create a public, unauthenticated storage handle using this facade’s client.

Source

pub fn pkdns(&self) -> Pkdns

Read-only Pkdns actor (resolve _pubky records) using this facade’s client.

Source

pub async fn get_homeserver_of( &self, user_public_key: &PublicKey, ) -> Option<PublicKey>

Resolve current homeserver host for a user public key via Pkarr.

Returns the _pubky SVCB/HTTPS target (domain or pubkey-as-host), or None if the record is missing/unresolvable. Uses an internal read-only Pkdns actor.

Source

pub fn event_stream_for_user( &self, user: &PublicKey, cursor: Option<EventCursor>, ) -> EventStreamBuilder

Create an event stream builder for a single user.

This is the simplest way to subscribe to events for one user. The homeserver is automatically resolved from the user’s Pkarr record.

§Example
use pubky::{Pubky, PublicKey, EventCursor};
use futures_util::StreamExt;

let pubky = Pubky::new()?;
let user = PublicKey::try_from("o1gg96ewuojmopcjbz8895478wdtxtzzuxnfjjz8o8e77csa1ngo").unwrap();

let mut stream = pubky.event_stream_for_user(&user, None)
    .live()
    .subscribe()
    .await?;

while let Some(result) = stream.next().await {
    let event = result?;
    println!("Event: {:?} at {}", event.event_type, event.resource);
}
Source

pub fn event_stream_for(&self, homeserver: &PublicKey) -> EventStreamBuilder

Create an event stream builder for a specific homeserver.

Use this when you already know the homeserver pubkey. This avoids Pkarr resolution overhead. Obtain a homeserver pubkey via Self::get_homeserver_of.

§Example
use pubky::{Pubky, PublicKey};
use futures_util::StreamExt;

let pubky = Pubky::new()?;
let user1 = PublicKey::try_from("o1gg96ewuojmopcjbz8895478wdtxtzzuxnfjjz8o8e77csa1ngo").unwrap();
let user2 = PublicKey::try_from("pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy").unwrap();

// When subscribing to multiple users on the same homeserver,
// specify the homeserver directly to avoid redundant Pkarr lookups
let homeserver = pubky.get_homeserver_of(&user1).await.unwrap();

let mut stream = pubky.event_stream_for(&homeserver)
    .add_users([(&user1, None), (&user2, None)])?
    .subscribe()
    .await?;

while let Some(result) = stream.next().await {
    let event = result?;
    println!("Event: {:?} at {}", event.event_type, event.resource);
}
Source

pub async fn session_from_file<P: AsRef<Path>>( &self, path: P, ) -> Result<PubkySession>

Restore a session from a .sess secret file.

§Errors
Source

pub fn signer_from_recovery_file<P: AsRef<Path>>( &self, path: P, passphrase: &str, ) -> Result<PubkySigner>

Recover a keypair from an encrypted .pkarr secret file and return a PubkySigner.

§Errors
Source

pub const fn client(&self) -> &PubkyHttpClient

Access the underlying transport (advanced use).

Trait Implementations§

Source§

impl Clone for Pubky

Source§

fn clone(&self) -> Pubky

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Pubky

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Pubky

§

impl !RefUnwindSafe for Pubky

§

impl Send for Pubky

§

impl Sync for Pubky

§

impl Unpin for Pubky

§

impl UnsafeUnpin for Pubky

§

impl !UnwindSafe for Pubky

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