diff --git a/core/conversations/src/context.rs b/core/conversations/src/context.rs index 5fa58c1..3415c88 100644 --- a/core/conversations/src/context.rs +++ b/core/conversations/src/context.rs @@ -146,21 +146,16 @@ impl Context { &mut self, enc_payload: EncryptedPayload, ) -> Result, ChatError> { - // Look up the ephemeral key from storage - let key_hex = Inbox::::extract_ephemeral_key_hex(&enc_payload)?; - let ephemeral_key = self - .store - .borrow() - .load_ephemeral_key(&key_hex)? - .ok_or(ChatError::UnknownEphemeralKey())?; - - let (convo, content) = self.inbox.handle_frame(&ephemeral_key, enc_payload)?; + let public_key_hex = Inbox::::extract_ephemeral_key_hex(&enc_payload)?; + let (convo, content) = self.inbox.handle_frame(enc_payload, &public_key_hex)?; match convo { Conversation::Private(convo) => self.persist_convo(&convo)?, }; - self.store.borrow_mut().remove_ephemeral_key(&key_hex)?; + self.store + .borrow_mut() + .remove_ephemeral_key(&public_key_hex)?; Ok(content) } @@ -182,10 +177,7 @@ impl Context { } pub fn create_intro_bundle(&mut self) -> Result, ChatError> { - let (intro, public_key_hex, private_key) = self.inbox.create_intro_bundle(); - self.store - .borrow_mut() - .save_ephemeral_key(&public_key_hex, &private_key)?; + let intro = self.inbox.create_intro_bundle()?; Ok(intro.into()) } diff --git a/core/conversations/src/inbox/handler.rs b/core/conversations/src/inbox/handler.rs index 5d46daf..395f9e3 100644 --- a/core/conversations/src/inbox/handler.rs +++ b/core/conversations/src/inbox/handler.rs @@ -50,14 +50,18 @@ impl Inbox { /// Creates an intro bundle and returns the Introduction along with the /// generated ephemeral key pair (public_key_hex, private_key) for the caller to persist. - pub fn create_intro_bundle(&self) -> (Introduction, String, PrivateKey) { + pub fn create_intro_bundle(&self) -> Result { let ephemeral = PrivateKey::random(); let ephemeral_key: PublicKey = (&ephemeral).into(); let public_key_hex = hex::encode(ephemeral_key.as_bytes()); + self.store + .borrow_mut() + .save_ephemeral_key(&public_key_hex, &ephemeral)?; + let intro = Introduction::new(self.ident.secret(), ephemeral_key, OsRng); - (intro, public_key_hex, ephemeral) + Ok(intro) } pub fn invite_to_private_convo( @@ -117,9 +121,15 @@ impl Inbox { /// looked up from storage. Returns the created conversation and optional content data. pub fn handle_frame( &self, - ephemeral_key: &PrivateKey, enc_payload: EncryptedPayload, + public_key_hex: &str, ) -> Result<(Conversation, Option), ChatError> { + let ephemeral_key = self + .store + .borrow() + .load_ephemeral_key(public_key_hex)? + .ok_or(ChatError::UnknownEphemeralKey())?; + let handshake = Self::extract_payload(enc_payload)?; let header = handshake @@ -127,11 +137,12 @@ impl Inbox { .ok_or(ChatError::UnexpectedPayload("InboxV1Header".into()))?; // Perform handshake and decrypt frame - let (seed_key, frame) = self.perform_handshake(ephemeral_key, header, handshake.payload)?; + let (seed_key, frame) = + self.perform_handshake(&ephemeral_key, header, handshake.payload)?; match frame.frame_type.unwrap() { proto::inbox_v1_frame::FrameType::InvitePrivateV1(_invite_private_v1) => { - let mut convo = PrivateV1Convo::new_responder(seed_key, ephemeral_key); + let mut convo = PrivateV1Convo::new_responder(seed_key, &ephemeral_key); let Some(enc_payload) = _invite_private_v1.initial_message else { return Err(ChatError::Protocol("missing initial encpayload".into())); @@ -246,7 +257,6 @@ mod tests { use super::*; use sqlite::{ChatStorage, StorageConfig}; - use storage::EphemeralKeyStore; #[test] fn test_invite_privatev1_roundtrip() { @@ -260,28 +270,16 @@ mod tests { let raya_ident = Identity::new("raya"); let raya_inbox = Inbox::new(raya_ident.into(), Rc::clone(&storage)); - let (bundle, key_hex, private_key) = raya_inbox.create_intro_bundle(); - storage - .borrow_mut() - .save_ephemeral_key(&key_hex, &private_key) - .unwrap(); + let bundle = raya_inbox.create_intro_bundle().unwrap(); let (_, mut payloads) = saro_inbox .invite_to_private_convo(&bundle, "hello".as_bytes()) .unwrap(); let payload = payloads.remove(0); - - // Look up ephemeral key from storage let key_hex = Inbox::::extract_ephemeral_key_hex(&payload.data).unwrap(); - let ephemeral_key = storage - .borrow() - .load_ephemeral_key(&key_hex) - .unwrap() - .unwrap(); - // Test handle_frame with valid payload - let result = raya_inbox.handle_frame(&ephemeral_key, payload.data); + let result = raya_inbox.handle_frame(payload.data, &key_hex); assert!( result.is_ok(),