From a1f04dbadb27019e84e4c389b3c92ef97c760ad2 Mon Sep 17 00:00:00 2001 From: Darius Date: Sun, 22 Oct 2023 16:32:02 -0400 Subject: [PATCH] chore: Add exported function --- .../warp-ipfs/src/store/document/root.rs | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/extensions/warp-ipfs/src/store/document/root.rs b/extensions/warp-ipfs/src/store/document/root.rs index 69fa97965..4ab953024 100644 --- a/extensions/warp-ipfs/src/store/document/root.rs +++ b/extensions/warp-ipfs/src/store/document/root.rs @@ -9,12 +9,12 @@ use rust_ipfs::{Ipfs, IpfsPath}; use uuid::Uuid; use warp::{crypto::DID, error::Error}; -use crate::store::{identity::Request, keystore::Keystore, VecExt}; +use crate::store::{ecdh_encrypt, identity::Request, keystore::Keystore, VecExt}; use super::{ identity::IdentityDocument, utils::{GetLocalDag, ToCid}, - RootDocument, + ExtractedRootDocument, RootDocument, }; #[allow(clippy::large_enum_variant)] @@ -84,6 +84,12 @@ pub enum RootDocumentCommand { id: Uuid, response: oneshot::Sender>, }, + Export { + response: oneshot::Sender>, + }, + ExportEncrypted { + response: oneshot::Sender, Error>>, + }, } #[derive(Debug, Clone)] @@ -308,6 +314,26 @@ impl RootDocumentMap { rx.await.map_err(anyhow::Error::from)? } + pub async fn export(&self) -> Result { + let (tx, rx) = oneshot::channel(); + let _ = self + .tx + .clone() + .send(RootDocumentCommand::Export { response: tx }) + .await; + rx.await.map_err(anyhow::Error::from)? + } + + pub async fn export_bytes(&self) -> Result, Error> { + let (tx, rx) = oneshot::channel(); + let _ = self + .tx + .clone() + .send(RootDocumentCommand::ExportEncrypted { response: tx }) + .await; + rx.await.map_err(anyhow::Error::from)? + } + pub async fn get_conversation_keystore_map(&self) -> Result, Error> { let (tx, rx) = oneshot::channel(); let _ = self @@ -411,6 +437,12 @@ impl RootDocumentTask { RootDocumentCommand::GetKeystoreMap { response } => { let _ = response.send(self.get_conversation_keystore_map().await); } + RootDocumentCommand::Export { response } => { + let _ = response.send(self.export().await); + } + RootDocumentCommand::ExportEncrypted { response } => { + let _ = response.send(self.export_bytes().await); + } } } } @@ -734,4 +766,16 @@ impl RootDocumentTask { let path = IpfsPath::from(cid).sub_path(&id.to_string())?; path.get_local_dag(&self.ipfs).await } + + async fn export(&self) -> Result { + let document = self.get_root_document().await?; + document.export(&self.ipfs).await + } + + async fn export_bytes(&self) -> Result, Error> { + let export = self.export().await?; + + let bytes = serde_json::to_vec(&export)?; + ecdh_encrypt(&self.keypair, None, bytes) + } }