Skip to content

Commit

Permalink
EnvelopeEntityList wrapping type.
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Jun 20, 2020
1 parent 235fe16 commit 5837465
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions libsignal-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ libsignal-protocol = { git = "https://github.com/Michael-F-Bryan/libsignal-proto
failure = "0.1.5"
async-trait = "0.1.30"
url = "2.1.1"
base64 = "0.12"
thiserror = "1.0"
serde = {version = "1.0", features=["derive"]}
prost = "0.6"
Expand Down
9 changes: 9 additions & 0 deletions libsignal-service/src/envelope.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::utils::serde_base64;

pub struct Envelope {
inner: crate::proto::Envelope,
}
Expand All @@ -11,12 +13,19 @@ pub struct EnvelopeEntity {
pub source: String,
pub source_uuid: String,
pub source_device: i32,
#[serde(with = "serde_base64")]
pub message: Vec<u8>,
#[serde(with = "serde_base64")]
pub content: Vec<u8>,
pub server_timestamp: i64,
pub guid: String,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub(crate) struct EnvelopeEntityList {
pub messages: Vec<EnvelopeEntity>,
}

const SUPPORTED_VERSION: usize = 1;
const CIPHER_KEY_SIZE: usize = 32;
const MAC_KEY_SIZE: usize = 20;
Expand Down
2 changes: 2 additions & 0 deletions libsignal-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod receiver;

mod proto;

mod utils;

pub use crate::account_manager::AccountManager;

pub const USER_AGENT: &'static str =
Expand Down
3 changes: 2 additions & 1 deletion libsignal-service/src/push_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ pub trait PushService {
async fn get_messages(
&mut self,
) -> Result<Vec<EnvelopeEntity>, ServiceError> {
Ok(self.get(MESSAGE_PATH).await?)
let entity_list: EnvelopeEntityList = self.get(MESSAGE_PATH).await?;
Ok(entity_list.messages)
}
}

Expand Down
22 changes: 22 additions & 0 deletions libsignal-service/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod serde_base64 {
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<T, S>(key: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: AsRef<[u8]>,
S: Serializer,
{
serializer.serialize_str(&base64::encode(key.as_ref()))
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<Vec<u8>, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
String::deserialize(deserializer).and_then(|string| {
base64::decode(&string)
.map_err(|err| Error::custom(err.to_string()))
})
}
}

0 comments on commit 5837465

Please sign in to comment.