From 051998b583b02f18fbd33357db5f37599c705e2f Mon Sep 17 00:00:00 2001 From: Darius Date: Mon, 23 Oct 2023 09:53:46 -0400 Subject: [PATCH] chore: Simplify logic --- .../warp-ipfs/src/store/document/root.rs | 34 ++++++++++++++++++- extensions/warp-ipfs/src/store/identity.rs | 30 ++++------------ 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/extensions/warp-ipfs/src/store/document/root.rs b/extensions/warp-ipfs/src/store/document/root.rs index 4ab953024..4d616197f 100644 --- a/extensions/warp-ipfs/src/store/document/root.rs +++ b/extensions/warp-ipfs/src/store/document/root.rs @@ -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}; @@ -29,6 +29,10 @@ pub enum RootDocumentCommand { Identity { response: oneshot::Sender>, }, + SetIdentityStatus { + status: IdentityStatus, + response: oneshot::Sender>, + }, AddFriend { did: DID, response: oneshot::Sender>, @@ -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 @@ -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); + } } } } @@ -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, Error> { let cid = match self.cid { Some(cid) => cid, diff --git a/extensions/warp-ipfs/src/store/identity.rs b/extensions/warp-ipfs/src/store/identity.rs index 7bd904877..132ba7c85 100644 --- a/extensions/warp-ipfs/src/store/identity.rs +++ b/extensions/warp-ipfs/src/store/identity.rs @@ -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, }; @@ -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(()) @@ -1606,31 +1604,15 @@ impl IdentityStore { } pub async fn own_identity_document(&self) -> Result { - 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 { - let root_document = self.root_document.get().await?; - - let path = IpfsPath::from(root_document.identity); - let identity = self - .get_local_dag::(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))]