mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-02-10 17:03:12 +00:00
FFI Integration fixes (#48)
* Make conversation store easier to use * fix: Bug in Inbox id’s * Clean up warnings * Add DH decryption for Inbox
This commit is contained in:
parent
e3e3097947
commit
71f7b8a485
@ -41,9 +41,10 @@ impl Context {
|
|||||||
.invite_to_private_convo(remote_bundle, content)
|
.invite_to_private_convo(remote_bundle, content)
|
||||||
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
.unwrap_or_else(|_| todo!("Log/Surface Error"));
|
||||||
|
|
||||||
|
let remote_id = Inbox::inbox_identifier_for_key(remote_bundle.installation_key);
|
||||||
let payload_bytes = payloads
|
let payload_bytes = payloads
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|p| p.to_envelope(convo.id().to_string()))
|
.map(|p| p.to_envelope(remote_id.clone()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let convo_id = self.add_convo(Box::new(convo));
|
let convo_id = self.add_convo(Box::new(convo));
|
||||||
@ -77,8 +78,7 @@ impl Context {
|
|||||||
|
|
||||||
// TODO: Impl Conversation hinting
|
// TODO: Impl Conversation hinting
|
||||||
let convo_id = env.conversation_hint;
|
let convo_id = env.conversation_hint;
|
||||||
let enc = EncryptedPayload::decode(payload)?;
|
let enc = EncryptedPayload::decode(env.payload)?;
|
||||||
|
|
||||||
match convo_id {
|
match convo_id {
|
||||||
c if c == self.inbox.id() => self.dispatch_to_inbox(enc),
|
c if c == self.inbox.id() => self.dispatch_to_inbox(enc),
|
||||||
c if self.store.has(&c) => self.dispatch_to_convo(&c, enc),
|
c if self.store.has(&c) => self.dispatch_to_convo(&c, enc),
|
||||||
|
|||||||
@ -50,16 +50,12 @@ impl ConversationStore {
|
|||||||
self.conversations.contains_key(id)
|
self.conversations.contains_key(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(&self, id: ConversationId) -> Option<&(dyn Convo + '_)> {
|
|
||||||
self.conversations.get(id).map(|c| c.as_ref())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_mut(&mut self, id: &str) -> Option<&mut (dyn Convo + '_)> {
|
pub fn get_mut(&mut self, id: &str) -> Option<&mut (dyn Convo + '_)> {
|
||||||
Some(self.conversations.get_mut(id)?.as_mut())
|
Some(self.conversations.get_mut(id)?.as_mut())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn conversation_ids(&self) -> impl Iterator<Item = ConversationIdOwned> + '_ {
|
pub fn conversation_ids(&self) -> Vec<ConversationIdOwned> {
|
||||||
self.conversations.keys().cloned()
|
self.conversations.keys().cloned().collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -24,8 +24,6 @@ pub enum ChatError {
|
|||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum EncryptionError {
|
pub enum EncryptionError {
|
||||||
#[error("encryption: {0}")]
|
|
||||||
Encryption(String),
|
|
||||||
#[error("decryption: {0}")]
|
#[error("decryption: {0}")]
|
||||||
Decryption(String),
|
Decryption(String),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use blake2::{Blake2b512, Digest};
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use crate::crypto::{PublicKey, StaticSecret};
|
use crate::crypto::{PublicKey, StaticSecret};
|
||||||
@ -23,10 +22,6 @@ impl Identity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn address(&self) -> String {
|
|
||||||
hex::encode(Blake2b512::digest(self.public_key()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn public_key(&self) -> PublicKey {
|
pub fn public_key(&self) -> PublicKey {
|
||||||
PublicKey::from(&self.secret)
|
PublicKey::from(&self.secret)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
use blake2::{Blake2b512, Digest};
|
||||||
use chat_proto::logoschat::encryption::EncryptedPayload;
|
use chat_proto::logoschat::encryption::EncryptedPayload;
|
||||||
use hex;
|
use hex;
|
||||||
use prost::Message;
|
use prost::Message;
|
||||||
@ -43,7 +44,7 @@ impl<'a> std::fmt::Debug for Inbox {
|
|||||||
|
|
||||||
impl Inbox {
|
impl Inbox {
|
||||||
pub fn new(ident: Rc<Identity>) -> Self {
|
pub fn new(ident: Rc<Identity>) -> Self {
|
||||||
let local_convo_id = ident.address();
|
let local_convo_id = Self::inbox_identifier_for_key(ident.public_key());
|
||||||
Self {
|
Self {
|
||||||
ident,
|
ident,
|
||||||
local_convo_id,
|
local_convo_id,
|
||||||
@ -139,10 +140,16 @@ impl Inbox {
|
|||||||
|
|
||||||
match frame.frame_type.unwrap() {
|
match frame.frame_type.unwrap() {
|
||||||
proto::inbox_v1_frame::FrameType::InvitePrivateV1(_invite_private_v1) => {
|
proto::inbox_v1_frame::FrameType::InvitePrivateV1(_invite_private_v1) => {
|
||||||
let convo = PrivateV1Convo::new_responder(seed_key, ephemeral_key.clone().into());
|
let mut convo =
|
||||||
|
PrivateV1Convo::new_responder(seed_key, ephemeral_key.clone().into());
|
||||||
|
|
||||||
// TODO: Update PrivateV1 Constructor with DR, initial_message
|
let Some(enc_payload) = _invite_private_v1.initial_message else {
|
||||||
Ok((Box::new(convo), None))
|
return Err(ChatError::Protocol("Invite: missing initial".into()));
|
||||||
|
};
|
||||||
|
|
||||||
|
let content = convo.handle_frame(enc_payload)?;
|
||||||
|
|
||||||
|
Ok((Box::new(convo), content))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -186,7 +193,7 @@ impl Inbox {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Decrypt Content
|
// TODO: Decrypt Content
|
||||||
let frame = proto::InboxV1Frame::decode(bytes)?;
|
let frame = self.decrypt_frame(bytes)?;
|
||||||
Ok((seed_key, frame))
|
Ok((seed_key, frame))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,12 +209,9 @@ impl Inbox {
|
|||||||
Ok(handshake)
|
Ok(handshake)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decrypt_frame(
|
fn decrypt_frame(&self, enc_frame_bytes: Bytes) -> Result<proto::InboxV1Frame, ChatError> {
|
||||||
enc_payload: proto::InboxHandshakeV1,
|
|
||||||
) -> Result<proto::InboxV1Frame, ChatError> {
|
|
||||||
let frame_bytes = enc_payload.payload;
|
|
||||||
// TODO: decrypt payload
|
// TODO: decrypt payload
|
||||||
let frame = proto::InboxV1Frame::decode(frame_bytes)?;
|
let frame = proto::InboxV1Frame::decode(enc_frame_bytes)?;
|
||||||
Ok(frame)
|
Ok(frame)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,6 +220,11 @@ impl Inbox {
|
|||||||
.get(key)
|
.get(key)
|
||||||
.ok_or_else(|| return ChatError::UnknownEphemeralKey())
|
.ok_or_else(|| return ChatError::UnknownEphemeralKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn inbox_identifier_for_key(pubkey: PublicKey) -> String {
|
||||||
|
// TODO: Implement ID according to spec
|
||||||
|
hex::encode(Blake2b512::digest(pubkey))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Id for Inbox {
|
impl Id for Inbox {
|
||||||
|
|||||||
@ -50,7 +50,7 @@ impl TryFrom<&[u8]> for Introduction {
|
|||||||
.map_err(|_| ChatError::InvalidKeyLength)?;
|
.map_err(|_| ChatError::InvalidKeyLength)?;
|
||||||
let installation_key = PublicKey::from(installation_bytes);
|
let installation_key = PublicKey::from(installation_bytes);
|
||||||
|
|
||||||
let ephemeral_bytes: [u8; 32] = hex::decode(parts[1])
|
let ephemeral_bytes: [u8; 32] = hex::decode(parts[2])
|
||||||
.map_err(|_| ChatError::BadParsing("ephemeral_key"))?
|
.map_err(|_| ChatError::BadParsing("ephemeral_key"))?
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| ChatError::InvalidKeyLength)?;
|
.map_err(|_| ChatError::InvalidKeyLength)?;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user