-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into contacts-sync-fix-attempt
- Loading branch information
Showing
4 changed files
with
63 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,45 @@ | ||
pub mod serde_profile_key { | ||
pub(crate) mod serde_profile_key { | ||
|
||
use base64::{engine::general_purpose, Engine}; | ||
use libsignal_service::prelude::ProfileKey; | ||
use serde::{Deserialize, Deserializer, Serializer}; | ||
|
||
pub fn serialize<S>(profile_key: &ProfileKey, serializer: S) -> Result<S::Ok, S::Error> | ||
pub(crate) fn serialize<S>(profile_key: &ProfileKey, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&base64::encode(profile_key.bytes)) | ||
serializer.serialize_str(&general_purpose::STANDARD.encode(profile_key.bytes)) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<ProfileKey, D::Error> | ||
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<ProfileKey, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
let bytes: [u8; 32] = base64::decode(String::deserialize(deserializer)?) | ||
let bytes: [u8; 32] = general_purpose::STANDARD | ||
.decode(String::deserialize(deserializer)?) | ||
.map_err(serde::de::Error::custom)? | ||
.try_into() | ||
.map_err(|e: Vec<u8>| serde::de::Error::invalid_length(e.len(), &"32 bytes"))?; | ||
Ok(ProfileKey::create(bytes)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_serialize_deserialize() { | ||
let profile_key = ProfileKey { | ||
bytes: *b"kaijpqxdvaiaeaulmsrozckjkgbpjowc", | ||
}; | ||
let mut serializer = serde_json::Serializer::new(Vec::new()); | ||
serialize(&profile_key, &mut serializer).unwrap(); | ||
let json = String::from_utf8(serializer.into_inner()).unwrap(); | ||
assert_eq!(json, "\"a2FpanBxeGR2YWlhZWF1bG1zcm96Y2tqa2dicGpvd2M=\""); | ||
|
||
let mut deserializer = serde_json::Deserializer::from_slice(json.as_bytes()); | ||
let profile_key2: ProfileKey = deserialize(&mut deserializer).unwrap(); | ||
assert_eq!(profile_key.bytes, profile_key2.bytes); | ||
} | ||
} | ||
} |