Skip to main content

hyprstream_rpc/transport/
iroh_transport.rs

1//! Iroh client-side transport — Phase 2 part 2 of Epic #131 (#133).
2//!
3//! Thin wrapper over the transport-generic [`SessionRpcTransport`] (see
4//! [`super::rpc_session`]) specialised to iroh's [`web_transport_iroh::Session`]
5//! so existing [`RpcClientImpl<S, T>`] consumers can ride the
6//! `hyprstream-rpc/1` ALPN with zero changes to envelope signing, JWT handling,
7//! or response parsing.
8//!
9//! Each `send()` opens a fresh bidi stream on the connection, writes the
10//! `SignedEnvelope` bytes, and reads the response (bounded) — matching the
11//! [`IrohRpcProtocolHandler`] (`transport::iroh_rpc`) accept loop on the
12//! server side.
13//!
14//! **Out of scope** (Phase 3): the [`Transport::subscribe`] and
15//! [`Transport::publish`] methods are streaming-plane concerns. They return
16//! errors here; subscribers go through `moq-net` on the `moql` ALPN instead.
17//!
18//! [`IrohRpcProtocolHandler`]: super::iroh_rpc::IrohRpcProtocolHandler
19
20use anyhow::Result;
21use async_trait::async_trait;
22use iroh::endpoint::Connection;
23
24use crate::transport::rpc_session::{RpcPendingStream, RpcPublishStub, SessionRpcTransport};
25use crate::transport_traits::Transport;
26
27/// Wraps an iroh [`Connection`] as a [`Transport`] for RPC plane traffic.
28///
29/// Clone-cheap — delegates to the transport-generic [`SessionRpcTransport`]
30/// over iroh's `web-transport` session, which is itself reference-counted.
31#[derive(Clone)]
32pub struct IrohTransport {
33    // NOTE: removed unused connection() accessor in M1.
34    inner: SessionRpcTransport<web_transport_iroh::Session>,
35}
36
37impl IrohTransport {
38    /// Build from an already-established iroh connection on
39    /// [`super::iroh_substrate::ALPN_HYPRSTREAM_RPC`].
40    pub fn new(conn: Connection) -> Self {
41        Self {
42            inner: SessionRpcTransport::new(web_transport_iroh::Session::raw(conn)),
43        }
44    }
45}
46
47/// Re-export the generic pending-stream stub under the historical name.
48pub type IrohPendingStream = RpcPendingStream;
49
50/// Re-export the generic publish stub under the historical name.
51pub type IrohPublishStub = RpcPublishStub;
52
53#[async_trait]
54impl Transport for IrohTransport {
55    type Sub = IrohPendingStream;
56    type Pub = IrohPublishStub;
57
58    async fn send(&self, payload: Vec<u8>, timeout_ms: Option<i32>) -> Result<Vec<u8>> {
59        self.inner.send(payload, timeout_ms).await
60    }
61
62    fn forbids_cleartext_envelope(&self) -> bool {
63        true
64    }
65
66    async fn subscribe(&self, topic: &[u8]) -> Result<Self::Sub> {
67        self.inner.subscribe(topic).await
68    }
69
70    async fn publish(&self, topic: &[u8]) -> Result<Self::Pub> {
71        self.inner.publish(topic).await
72    }
73}