Skip to content

Commit

Permalink
chore: Add exported function
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Oct 22, 2023
1 parent 9c0da71 commit a1f04db
Showing 1 changed file with 46 additions and 2 deletions.
48 changes: 46 additions & 2 deletions extensions/warp-ipfs/src/store/document/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -84,6 +84,12 @@ pub enum RootDocumentCommand {
id: Uuid,
response: oneshot::Sender<Result<Keystore, Error>>,
},
Export {
response: oneshot::Sender<Result<ExtractedRootDocument, Error>>,
},
ExportEncrypted {
response: oneshot::Sender<Result<Vec<u8>, Error>>,
},
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -308,6 +314,26 @@ impl RootDocumentMap {
rx.await.map_err(anyhow::Error::from)?
}

pub async fn export(&self) -> Result<ExtractedRootDocument, Error> {
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<Vec<u8>, 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<BTreeMap<String, Cid>, Error> {
let (tx, rx) = oneshot::channel();
let _ = self
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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<ExtractedRootDocument, Error> {
let document = self.get_root_document().await?;
document.export(&self.ipfs).await
}

async fn export_bytes(&self) -> Result<Vec<u8>, Error> {
let export = self.export().await?;

let bytes = serde_json::to_vec(&export)?;
ecdh_encrypt(&self.keypair, None, bytes)
}
}

0 comments on commit a1f04db

Please sign in to comment.