Skip to content

Commit

Permalink
chore: Simplify logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Oct 23, 2023
1 parent 4ef85ea commit 051998b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
34 changes: 33 additions & 1 deletion extensions/warp-ipfs/src/store/document/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::{
use libipld::Cid;
use rust_ipfs::{Ipfs, IpfsPath};
use uuid::Uuid;
use warp::{crypto::DID, error::Error};
use warp::{crypto::DID, error::Error, multipass::identity::IdentityStatus};

use crate::store::{ecdh_encrypt, identity::Request, keystore::Keystore, VecExt};

Expand All @@ -29,6 +29,10 @@ pub enum RootDocumentCommand {
Identity {
response: oneshot::Sender<Result<IdentityDocument, Error>>,
},
SetIdentityStatus {
status: IdentityStatus,
response: oneshot::Sender<Result<(), Error>>,
},
AddFriend {
did: DID,
response: oneshot::Sender<Result<(), Error>>,
Expand Down Expand Up @@ -170,6 +174,19 @@ impl RootDocumentMap {
rx.await.map_err(anyhow::Error::from)?
}

pub async fn set_status_indicator(&self, status: IdentityStatus) -> Result<(), Error> {
let (tx, rx) = oneshot::channel();
let _ = self
.tx
.clone()
.send(RootDocumentCommand::SetIdentityStatus {
status,
response: tx,
})
.await;
rx.await.map_err(anyhow::Error::from)?
}

pub async fn add_friend(&self, did: &DID) -> Result<(), Error> {
let (tx, rx) = oneshot::channel();
let _ = self
Expand Down Expand Up @@ -443,6 +460,9 @@ impl RootDocumentTask {
RootDocumentCommand::ExportEncrypted { response } => {
let _ = response.send(self.export_bytes().await);
}
RootDocumentCommand::SetIdentityStatus { status, response } => {
let _ = response.send(self.set_identity_status(status).await);
}
}
}
}
Expand Down Expand Up @@ -502,6 +522,18 @@ impl RootDocumentTask {
Ok(())
}

async fn set_identity_status(&mut self, status: IdentityStatus) -> Result<(), Error> {
let mut root = self.get_root_document().await?;
let mut identity = self.identity().await?;
root.status = Some(status);
identity.status = Some(status);

let identity = identity.sign(&self.keypair)?;
root.identity = identity.to_cid(&self.ipfs).await?;

self.set_root_document(root).await
}

async fn request_list(&self) -> Result<Vec<Request>, Error> {
let cid = match self.cid {
Some(cid) => cid,
Expand Down
30 changes: 6 additions & 24 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use super::{
cache::IdentityCache, identity::IdentityDocument, image_dag::get_image,
root::RootDocumentMap, utils::GetLocalDag, ExtractedRootDocument, RootDocument, ToCid,
},
ecdh_decrypt, ecdh_encrypt, libp2p_pub_to_did,
ecdh_decrypt, ecdh_encrypt,
phonebook::PhoneBook,
queue::Queue,
};
Expand Down Expand Up @@ -1542,9 +1542,7 @@ impl IdentityStore {

#[tracing::instrument(skip(self))]
pub async fn set_identity_status(&mut self, status: IdentityStatus) -> Result<(), Error> {
let mut root_document = self.root_document.get().await?;
root_document.status = Some(status);
self.root_document.set(root_document).await?;
self.root_document.set_status_indicator(status).await?;
*self.online_status.write().await = Some(status);
self.push_to_all().await;
Ok(())
Expand Down Expand Up @@ -1606,31 +1604,15 @@ impl IdentityStore {
}

pub async fn own_identity_document(&self) -> Result<IdentityDocument, Error> {
let root_document = self.root_document.get().await?;
let path = IpfsPath::from(root_document.identity);
let identity: IdentityDocument = path.get_local_dag(&self.ipfs).await?;
let identity = self.root_document.identity().await?;
identity.verify()?;
Ok(identity)
}

pub async fn own_identity(&self) -> Result<Identity, Error> {
let root_document = self.root_document.get().await?;

let path = IpfsPath::from(root_document.identity);
let identity = self
.get_local_dag::<IdentityDocument>(path)
.await?
.resolve()?;

let public_key = identity.did_key();
let kp_public_key = libp2p_pub_to_did(&self.get_keypair()?.public())?;
if public_key != kp_public_key {
//Note if we reach this point, the identity would need to be reconstructed
return Err(Error::IdentityDoesntExist);
}

*self.online_status.write().await = root_document.status;
Ok(identity)
let identity = self.own_identity_document().await?;
*self.online_status.write().await = identity.status;
Ok(identity.into())
}

#[tracing::instrument(skip(self))]
Expand Down

0 comments on commit 051998b

Please sign in to comment.