Skip to main content

EncryptedLink

Struct EncryptedLink 

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

Handle to an established Encrypted Link with a counterparty.

Created by advance_handshake (via HandshakeProgress::Complete) after a successful Noise handshake. It wraps a transport-mode pubky_noise::PubkyNoiseEncryptor and should be closed with close_encrypted_link when no longer needed.

§Session resumption

An established Encrypted Link can be snapshotted via snapshot or serialized via serialize, then restored with restore_encrypted_link or restore_encrypted_link_from_config.

§Private Application Message stream

All private Paykit protocol messages on this link share one ordered stream. Use receive_private_application_messages for ordered intake. Callers or SDK layers that derive durable Event Message state should persist and route the returned messages before replacing a stored link snapshot whose read counter has advanced.

§Automatic send retry

Send helpers retry transient homeserver write failures according to set_max_send_retries. Deterministic Noise state, counter, nonce, or encryption errors fail immediately.

Implementations§

Source

pub fn remote_static_public_key(&self) -> Option<&[u8]>

X25519 static key authenticated by the Noise transcript. The caller must bind this key to the expected identity, including after restore.

Source

pub fn set_max_send_retries(&mut self, max: u32) -> &mut Self

Set the maximum number of automatic Private Application Message send_message retries before Paykit gives up and returns PaykitError::Transport.

Retryable homeserver write failures are retried. Deterministic Noise state, counter, nonce, or encryption errors fail immediately.

Default: DEFAULT_MAX_SEND_RETRIES (3).

Source

pub fn snapshot(&self) -> Result<EncryptedLinkSnapshot>

Capture the current link state as a serializable snapshot.

The snapshot captures transport keys, counters, and counterparty identity. Take a new snapshot after receiving or sending messages when the caller wants the persisted read/write counters to catch up with local state.

Snapshot bytes include sensitive key material and must be stored as secrets. Do not log them or include them in telemetry.

Source

pub fn serialize(&self) -> Result<Vec<u8>>

Serialize the current link state to bytes for persistence.

Convenience method equivalent to self.snapshot()?.serialize().

Source

pub fn config(&self) -> &Arc<PubkyNoiseConfig> ⓘ

Access the shared Noise configuration for this link.

Useful for passing to restore_encrypted_link_from_config when performing in-process session recovery without an app restart.

Source

pub fn recipient(&self) -> &PublicKey

Access the counterparty public key for this Encrypted Link.

Source

pub async fn send_private_application_message_json( &mut self, raw_json: &str, ) -> Result<()>

Send one raw JSON Private Application Message.

This is the low-level send counterpart to receive_private_application_messages. It validates the generic version, kind, and app_id envelope fields; it does not require a known Paykit kind or validate known Paykit message bodies. Use the typed serializers or SDK queue for protocol-managed Paykit messages.

Higher-level callers should persist the exact JSON before sending when retrying the same message matters.

Transient outbox write failures are retried up to the configured budget. If every attempt fails, the prepared packet is retained and blocks further sends, receives, and snapshots on this link until it is republished with retry_pending_send.

Do not restore an older snapshot to recover from a failed send: the failed write may still have reached the homeserver, and rolling back the sending nonce and slot would reuse the transport key and nonce for the next plaintext. Only the identical prepared packet may be republished; otherwise establish a fresh link.

Source

pub async fn retry_pending_send(&mut self) -> Result<()>

Republish the prepared packet retained after a failed convenience send.

Until the identical packet is republished, sends, receives, and snapshots remain blocked. Never restore an older snapshot to retry with different plaintext: an ambiguous write may already have used that key and nonce. SDK callers using staged sends should instead publish their durably persisted ciphertext with publish_prepared_private_application_message.

Source

pub fn prepare_private_application_message_json( &mut self, raw_json: &str, ) -> Result<PreparedPrivateApplicationMessageSend>

Prepare one raw JSON Private Application Message for durable handoff.

Persist the returned ciphertext and resulting snapshot atomically before calling acknowledge_persisted_private_send.

Source

pub fn acknowledge_persisted_private_send( &mut self, prepared: PreparedPrivateApplicationMessageSend, ) -> Result<()>

Acknowledge that a prepared send and its resulting snapshot are durable.

Source

pub async fn publish_prepared_private_application_message( &self, destination_path: &str, ciphertext: &[u8], ) -> Result<()>

Publish an exact prepared ciphertext, retrying without re-encryption.

This accepts values restored from durable SDK state after a crash. The destination must belong to this link’s local write stream.

Source

pub async fn prepare_next_private_application_message( &mut self, ) -> Result<Option<PreparedPrivateApplicationMessageReceive>>

Fetch and prepare the next available inbound Private Application Message.

Persist the application result and returned snapshot atomically before calling acknowledge_persisted_private_receive.

Source

pub fn acknowledge_persisted_private_receive( &mut self, prepared: PreparedPrivateApplicationMessageReceive, ) -> Result<()>

Acknowledge that a prepared receive and its application result are durable.

Source

pub async fn receive_private_application_messages( &mut self, ) -> Result<Vec<PrivateApplicationMessage>>

Receive a bounded batch of available Private Application Messages in stream order.

The Noise read checkpoint advances past the returned messages. Callers that need crash-safe Event Message handling should persist returned messages before replacing a stored link snapshot. Call again to drain more than PRIVATE_APPLICATION_MESSAGE_RECEIVE_LIMIT available stream slots.

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

Source§

fn compat(self) -> Compat<T> ⓘ
where T: Sized,

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> 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: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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