Skip to content

Commit

Permalink
[feat] Expose API
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodolsky committed Jun 22, 2024
1 parent b9ee60e commit 42c351e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
22 changes: 16 additions & 6 deletions iroh/src/client/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use iroh_blobs::{export::ExportProgress, store::ExportMode, Hash};
use iroh_docs::{
actor::OpenState,
store::{DownloadPolicy, Query},
AuthorId, Capability, CapabilityKind, ContentStatus, DocTicket, NamespaceId, PeerIdBytes,
RecordIdentifier,
AuthorId, Capability, CapabilityKind, ContentStatus, DocTicket, NamespaceId, NamespaceSecret,
PeerIdBytes, RecordIdentifier,
};
use iroh_net::NodeAddr;
use portable_atomic::{AtomicBool, Ordering};
Expand All @@ -27,10 +27,11 @@ use serde::{Deserialize, Serialize};

use crate::rpc_protocol::{
DocCloseRequest, DocCreateRequest, DocDelRequest, DocDelResponse, DocDropRequest,
DocExportFileRequest, DocGetDownloadPolicyRequest, DocGetExactRequest, DocGetManyRequest,
DocGetSyncPeersRequest, DocImportFileRequest, DocImportRequest, DocLeaveRequest,
DocListRequest, DocOpenRequest, DocSetDownloadPolicyRequest, DocSetHashRequest, DocSetRequest,
DocShareRequest, DocStartSyncRequest, DocStatusRequest, DocSubscribeRequest, RpcService,
DocExportFileRequest, DocExportSecretKeyRequest, DocGetDownloadPolicyRequest,
DocGetExactRequest, DocGetManyRequest, DocGetSyncPeersRequest, DocImportFileRequest,
DocImportRequest, DocLeaveRequest, DocListRequest, DocOpenRequest, DocSetDownloadPolicyRequest,
DocSetHashRequest, DocSetRequest, DocShareRequest, DocStartSyncRequest, DocStatusRequest,
DocSubscribeRequest, RpcService,
};

#[doc(inline)]
Expand Down Expand Up @@ -110,6 +111,15 @@ impl Client {
let doc = Doc::new(self.rpc.clone(), id);
Ok(Some(doc))
}

/// Get a [`Doc`] client for a single document. Return None if the document cannot be found.
pub async fn export_secret_key(&self, id: NamespaceId) -> Result<NamespaceSecret> {
let res = self
.rpc
.rpc(DocExportSecretKeyRequest { doc_id: id })
.await??;
Ok(res.namespace_secret)
}
}

/// Document handle
Expand Down
7 changes: 0 additions & 7 deletions iroh/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use futures_lite::StreamExt;
use iroh_base::key::PublicKey;
use iroh_blobs::store::{GcMarkEvent, GcSweepEvent, Store as BaoStore};
use iroh_blobs::{downloader::Downloader, protocol::Closed};
use iroh_docs::actor::SyncHandle;
use iroh_docs::engine::Engine;
use iroh_gossip::net::Gossip;
use iroh_net::key::SecretKey;
use iroh_net::Endpoint;
Expand Down Expand Up @@ -180,11 +178,6 @@ impl<D: BaoStore> Node<D> {
&self.inner.db
}

/// Expose sync
pub fn sync_handle(&self) -> &SyncHandle {
&self.inner.sync.sync
}

/// Returns a protocol handler for an ALPN.
///
/// This downcasts to the concrete type and returns `None` if the handler registered for `alpn`
Expand Down
6 changes: 6 additions & 0 deletions iroh/src/node/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,12 @@ impl<D: BaoStore> Handler<D> {
})
.await
}
DocExportSecretKey(msg) => {
chan.rpc(msg, self, |handler, req| {
handler.with_docs(|docs| async move { docs.doc_export_secret_key(req).await })
})
.await
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion iroh/src/node/rpc/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::rpc_protocol::{
AuthorImportRequest, AuthorImportResponse, AuthorListRequest, AuthorListResponse,
AuthorSetDefaultRequest, AuthorSetDefaultResponse, DocCloseRequest, DocCloseResponse,
DocCreateRequest, DocCreateResponse, DocDelRequest, DocDelResponse, DocDropRequest,
DocDropResponse, DocGetDownloadPolicyRequest, DocGetDownloadPolicyResponse, DocGetExactRequest,
DocDropResponse, DocExportSecretKeyRequest, DocExportSecretKeyResponse,
DocGetDownloadPolicyRequest, DocGetDownloadPolicyResponse, DocGetExactRequest,
DocGetExactResponse, DocGetManyRequest, DocGetManyResponse, DocGetSyncPeersRequest,
DocGetSyncPeersResponse, DocImportRequest, DocImportResponse, DocLeaveRequest,
DocLeaveResponse, DocListRequest, DocListResponse, DocOpenRequest, DocOpenResponse,
Expand Down Expand Up @@ -305,4 +306,12 @@ impl DocsEngine {
let peers = self.sync.get_sync_peers(req.doc_id).await?;
Ok(DocGetSyncPeersResponse { peers })
}

pub async fn doc_export_secret_key(
&self,
req: DocExportSecretKeyRequest,
) -> RpcResult<DocExportSecretKeyResponse> {
let namespace_secret = self.sync.export_secret_key(req.doc_id).await?;
Ok(DocExportSecretKeyResponse { namespace_secret })
}
}
24 changes: 22 additions & 2 deletions iroh/src/rpc_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use iroh_net::{
use iroh_docs::{
actor::OpenState,
store::{DownloadPolicy, Query},
Author, AuthorId, Capability, CapabilityKind, DocTicket, Entry, NamespaceId, PeerIdBytes,
SignedEntry,
Author, AuthorId, Capability, CapabilityKind, DocTicket, Entry, NamespaceId, NamespaceSecret,
PeerIdBytes, SignedEntry,
};
use quic_rpc::{
message::{BidiStreaming, BidiStreamingMsg, Msg, RpcMsg, ServerStreaming, ServerStreamingMsg},
Expand Down Expand Up @@ -514,6 +514,24 @@ pub struct AuthorImportResponse {
pub author_id: AuthorId,
}

/// Export secret key
#[derive(Serialize, Deserialize, Debug)]
pub struct DocExportSecretKeyRequest {
/// The document id
pub doc_id: NamespaceId,
}

impl RpcMsg<RpcService> for DocExportSecretKeyRequest {
type Response = RpcResult<DocExportSecretKeyResponse>;
}

/// Response to [`DocExportSecretKeyRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct DocExportSecretKeyResponse {
/// The namespace secret key
pub namespace_secret: NamespaceSecret,
}

/// Subscribe to events for a document.
#[derive(Serialize, Deserialize, Debug)]
pub struct DocSubscribeRequest {
Expand Down Expand Up @@ -1090,6 +1108,7 @@ pub enum Request {
DocGetDownloadPolicy(DocGetDownloadPolicyRequest),
DocSetDownloadPolicy(DocSetDownloadPolicyRequest),
DocGetSyncPeers(DocGetSyncPeersRequest),
DocExportSecretKey(DocExportSecretKeyRequest),

AuthorList(AuthorListRequest),
AuthorCreate(AuthorCreateRequest),
Expand Down Expand Up @@ -1149,6 +1168,7 @@ pub enum Response {
DocGetDownloadPolicy(RpcResult<DocGetDownloadPolicyResponse>),
DocSetDownloadPolicy(RpcResult<DocSetDownloadPolicyResponse>),
DocGetSyncPeers(RpcResult<DocGetSyncPeersResponse>),
DocExportSecretKey(RpcResult<DocExportSecretKeyResponse>),
StreamCreated(RpcResult<StreamCreated>),

AuthorList(RpcResult<AuthorListResponse>),
Expand Down

0 comments on commit 42c351e

Please sign in to comment.