Skip to content

Commit

Permalink
[feat] Expose API
Browse files Browse the repository at this point in the history
  • Loading branch information
ppodolsky committed Jul 10, 2024
1 parent a5a2324 commit c8699fb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
14 changes: 10 additions & 4 deletions iroh/src/client/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ use serde::{Deserialize, Serialize};

use crate::rpc_protocol::docs::{
CloseRequest, CreateRequest, DelRequest, DelResponse, DocListRequest, DocSubscribeRequest,
DropRequest, ExportFileRequest, GetDownloadPolicyRequest, GetExactRequest, GetManyRequest,
GetSyncPeersRequest, ImportFileRequest, ImportRequest, LeaveRequest, OpenRequest,
SetDownloadPolicyRequest, SetHashRequest, SetRequest, ShareRequest, StartSyncRequest,
StatusRequest,
DropRequest, ExportFileRequest, ExportSecretKeyRequest, GetDownloadPolicyRequest,
GetExactRequest, GetManyRequest, GetSyncPeersRequest, ImportFileRequest, ImportRequest,
LeaveRequest, OpenRequest, SetDownloadPolicyRequest, SetHashRequest, SetRequest, ShareRequest,
StartSyncRequest, StatusRequest,
};
use crate::rpc_protocol::RpcService;

Expand Down Expand Up @@ -64,6 +64,12 @@ impl Client {
Ok(())
}

/// Export secret key
pub async fn export_secret_key(&self, doc_id: NamespaceId) -> Result<()> {
self.rpc.rpc(ExportSecretKeyRequest { doc_id }).await??;
Ok(())
}

/// Import a document from a namespace capability.
///
/// This does not start sync automatically. Use [`Doc::start_sync`] to start sync.
Expand Down
5 changes: 5 additions & 0 deletions iroh/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ impl<D: BaoStore> Node<D> {
&self.inner.endpoint
}

/// Return db
pub fn db(&self) -> &D {
&self.inner.db
}

/// The address on which the node socket is bound.
///
/// Note that this could be an unspecified address, if you need an address on which you
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 @@ -408,6 +408,12 @@ impl<D: BaoStore> Handler<D> {
})
.await
}
ExportSecretKey(msg) => {
chan.rpc(msg, self, |handler, req| {
handler.with_docs(|docs| async move { docs.export_secret_key(req).await })
})
.await
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions iroh/src/node/rpc/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ use crate::rpc_protocol::{
CloseRequest, CloseResponse, CreateRequest as DocCreateRequest,
CreateResponse as DocCreateResponse, DelRequest, DelResponse, DocListRequest,
DocSubscribeRequest, DocSubscribeResponse, DropRequest, DropResponse,
GetDownloadPolicyRequest, GetDownloadPolicyResponse, GetExactRequest, GetExactResponse,
GetManyRequest, GetManyResponse, GetSyncPeersRequest, GetSyncPeersResponse,
ExportSecretKeyRequest, ExportSecretKeyResponse, GetDownloadPolicyRequest,
GetDownloadPolicyResponse, GetExactRequest, GetExactResponse, GetManyRequest,
GetManyResponse, GetSyncPeersRequest, GetSyncPeersResponse,
ImportRequest as DocImportRequest, ImportResponse as DocImportResponse, LeaveRequest,
LeaveResponse, ListResponse as DocListResponse, OpenRequest, OpenResponse,
SetDownloadPolicyRequest, SetDownloadPolicyResponse, SetHashRequest, SetHashResponse,
Expand Down Expand Up @@ -303,4 +304,12 @@ impl DocsEngine {
let peers = self.sync.get_sync_peers(req.doc_id).await?;
Ok(GetSyncPeersResponse { peers })
}

pub async fn export_secret_key(
&self,
req: ExportSecretKeyRequest,
) -> RpcResult<ExportSecretKeyResponse> {
let secret = self.sync.export_secret_key(req.doc_id).await?;
Ok(ExportSecretKeyResponse { secret })
}
}
23 changes: 21 additions & 2 deletions iroh/src/rpc_protocol/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use iroh_base::{
use iroh_blobs::{export::ExportProgress, store::ExportMode, Hash};
use iroh_docs::{
actor::OpenState, engine::LiveEvent, store::DownloadPolicy, store::Query, AuthorId, Capability,
CapabilityKind, DocTicket, Entry, NamespaceId, PeerIdBytes, SignedEntry,
CapabilityKind, DocTicket, Entry, NamespaceId, NamespaceSecret, PeerIdBytes, SignedEntry,
};
use iroh_net::NodeAddr;
use quic_rpc::{
Expand Down Expand Up @@ -46,6 +46,7 @@ pub enum Request {
GetDownloadPolicy(GetDownloadPolicyRequest),
SetDownloadPolicy(SetDownloadPolicyRequest),
GetSyncPeers(GetSyncPeersRequest),
ExportSecretKey(ExportSecretKeyRequest),
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -74,6 +75,7 @@ pub enum Response {
SetDownloadPolicy(RpcResult<SetDownloadPolicyResponse>),
GetSyncPeers(RpcResult<GetSyncPeersResponse>),
StreamCreated(RpcResult<StreamCreated>),
ExportSecretKey(RpcResult<ExportSecretKeyResponse>)
}

/// Subscribe to events for a document.
Expand Down Expand Up @@ -269,6 +271,23 @@ impl RpcMsg<RpcService> for DropRequest {
#[derive(Serialize, Deserialize, Debug)]
pub struct DropResponse {}

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

impl RpcMsg<RpcService> for ExportSecretKeyRequest {
type Response = RpcResult<ExportSecretKeyResponse>;
}

/// Response to [`ExportSecretKeyRequest`]
#[derive(Serialize, Deserialize, Debug)]
pub struct ExportSecretKeyResponse {
pub(crate) secret: NamespaceSecret,
}

/// Set an entry in a document
#[derive(Serialize, Deserialize, Debug)]
pub struct SetRequest {
Expand Down Expand Up @@ -304,7 +323,7 @@ pub struct ImportFileRequest {
pub doc_id: NamespaceId,
/// Author of this entry.
pub author_id: AuthorId,
/// Key of this entry.
/// Key of this entry.ExportSecretKeyResponse
pub key: Bytes,
/// The filepath to the data
///
Expand Down

0 comments on commit c8699fb

Please sign in to comment.