Skip to content

Commit

Permalink
Merge branch 'main' into feat/conversation-ping
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 committed Dec 23, 2024
2 parents 5d2ec3c + 1da3f37 commit 837e855
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 361 deletions.
67 changes: 34 additions & 33 deletions extensions/warp-ipfs/src/store/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use super::{
cache::IdentityCache, identity::IdentityDocument, image_dag::get_image,
root::RootDocumentMap, ResolvedRootDocument, RootDocument,
},
ecdh_decrypt, ecdh_encrypt,
ecdh_encrypt,
event_subscription::EventSubscription,
payload::PayloadMessage,
phonebook::PhoneBook,
Expand Down Expand Up @@ -530,12 +530,18 @@ impl IdentityStore {

tracing::info!("Received event from {in_did}");

let event = match ecdh_decrypt(store.root_document().keypair(), Some(&in_did), &message.data).and_then(|bytes| {
serde_json::from_slice::<IdentityEvent>(&bytes).map_err(Error::from)
}) {
Ok(e) => e,
let payload: PayloadMessage<IdentityEvent> = match PayloadMessage::from_bytes(&message.data) {
Ok(p) => p,
Err(e) => {
tracing::error!("Failed to decrypt payload from {in_did}: {e}");
tracing::error!(error = %e, from = %in_did, "failed to process payload");
continue;
}
};

let event = match payload.message(store.root_document().keypair()) {
Ok(event) => event,
Err(e) => {
tracing::error!("Failed to decrypt payload from {in_did}: {e}");
continue;
}
};
Expand All @@ -545,11 +551,9 @@ impl IdentityStore {
if let Err(e) = store.process_message(&in_did, event, false).await {
tracing::error!("Failed to process identity message from {in_did}: {e}");
}


}
Some(event) = friend_stream.next() => {
let payload = match PayloadMessage::<Vec<u8>>::from_bytes(&event.data) {
let payload = match PayloadMessage::<RequestResponsePayload>::from_bytes(&event.data) {
Ok(p) => p,
Err(_e) => {
continue;
Expand All @@ -568,17 +572,8 @@ impl IdentityStore {

tracing::info!("Received event from {did}");

let msg = match payload.message(None) {
let data = match payload.message(store.root_document.keypair()) {
Ok(m) => m,
Err(_) => {
continue;
}
};

let data = match ecdh_decrypt(store.root_document().keypair(), Some(&did), msg).and_then(|bytes| {
serde_json::from_slice::<RequestResponsePayload>(&bytes).map_err(Error::from)
}) {
Ok(pl) => pl,
Err(e) => {
if let Some(tx) = signal {
let _ = tx.send(Err(e));
Expand Down Expand Up @@ -920,9 +915,11 @@ impl IdentityStore {

let event = IdentityEvent::Request { option };

let payload_bytes = serde_json::to_vec(&event)?;
let payload = PayloadBuilder::new(pk_did, event.clone())
.add_recipient(out_did)?
.await?;

let bytes = ecdh_encrypt(pk_did, Some(out_did), payload_bytes)?;
let bytes = payload.to_bytes()?;

tracing::info!(to = %out_did, event = ?event, payload_size = bytes.len(), "Sending event");

Expand Down Expand Up @@ -950,8 +947,6 @@ impl IdentityStore {
return Err(Error::IdentityDoesntExist);
}

let pk_did = self.root_document.keypair();

let mut identity = self.own_identity_document().await?;

let is_friend = self.is_friend(out_did).await.unwrap_or_default();
Expand Down Expand Up @@ -980,9 +975,11 @@ impl IdentityStore {
option: ResponseOption::Identity { identity: payload },
};

let payload_bytes = serde_json::to_vec(&event)?;
let payload = PayloadBuilder::new(kp_did, event.clone())
.add_recipient(out_did)?
.await?;

let bytes = ecdh_encrypt(pk_did, Some(out_did), payload_bytes)?;
let bytes = payload.to_bytes()?;

tracing::info!(to = %out_did, event = ?event, payload_size = bytes.len(), "Sending event");

Expand Down Expand Up @@ -1035,9 +1032,11 @@ impl IdentityStore {
},
};

let payload_bytes = serde_json::to_vec(&event)?;
let payload = PayloadBuilder::new(pk_did, event.clone())
.add_recipient(out_did)?
.await?;

let bytes = ecdh_encrypt(pk_did, Some(out_did), payload_bytes)?;
let bytes = payload.to_bytes()?;

tracing::info!(to = %out_did, event = ?event, payload_size = bytes.len(), "Sending event");

Expand Down Expand Up @@ -1112,9 +1111,11 @@ impl IdentityStore {
option: ResponseOption::Metadata { data: metadata },
};

let payload_bytes = serde_json::to_vec(&event)?;
let payload = PayloadBuilder::new(pk_did, event.clone())
.add_recipient(out_did)?
.await?;

let bytes = ecdh_encrypt(pk_did, Some(out_did), payload_bytes)?;
let bytes = payload.to_bytes()?;

tracing::info!(to = %out_did, event = ?event, payload_size = bytes.len(), "Sending event");

Expand Down Expand Up @@ -2960,10 +2961,10 @@ impl IdentityStore {

let kp = self.root_document.keypair();

let payload_bytes = serde_json::to_vec(&payload)?;

let bytes = ecdh_encrypt(kp, Some(recipient), payload_bytes)?;
let message = PayloadBuilder::new(kp, bytes).build()?;
let message = PayloadBuilder::new(kp, payload.clone())
.add_recipient(recipient)?
.from_ipfs(&self.ipfs)
.await?;

let message_bytes = message.to_bytes()?;

Expand Down
Loading

0 comments on commit 837e855

Please sign in to comment.