Skip to content

Commit

Permalink
fix: do a serde encoding override to base64 encode bytes fields in Me…
Browse files Browse the repository at this point in the history
…ssageApi
  • Loading branch information
Michael Xu committed Apr 15, 2023
1 parent 0956338 commit 263a4d2
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/xmtp-networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tonic-reflection = "0.6.0"
tokio-rustls = "0.24.0"
serde = { version = "1.0.160", features = ["derive"] }
serde_json = "1.0"
base64 = "0.21.0"

[build-dependencies]
tonic-build = "^0.9"
8 changes: 8 additions & 0 deletions crates/xmtp-networking/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.build_client(true)
.type_attribute(".", "#[derive(serde::Deserialize, serde::Serialize)]")
.file_descriptor_set_path(out_dir.join("store_descriptor.bin"))
.field_attribute(
"xmtp.message_api.v1.Envelope.message",
"#[serde(serialize_with = \"crate::serialize_utils::as_base64\")]",
)
.field_attribute(
"xmtp.message_api.v1.IndexCursor.digest",
"#[serde(serialize_with = \"crate::serialize_utils::as_base64\")]",
)
.out_dir("./src")
.compile(
&[
Expand Down
4 changes: 2 additions & 2 deletions crates/xmtp-networking/src/grpc_api_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ pub fn test_envelope() -> String {
message: vec![65],
..Default::default()
};
let json = serde_json::to_string(&envelope).unwrap();
json

serde_json::to_string(&envelope).unwrap()
}
5 changes: 4 additions & 1 deletion crates/xmtp-networking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pub mod grpc_api_helper;
pub mod proto_helper;
// Custom patching of protobuf serialization for bytes -> base64
// https://github.com/tokio-rs/prost/issues/75#issuecomment-1383233271
pub mod serialize_utils;

#[cfg(test)]
mod tests {
Expand All @@ -12,7 +15,7 @@ mod tests {
let serialized = test_envelope();
assert_eq!(
serialized,
"{\"content_topic\":\"\",\"timestamp_ns\":0,\"message\":[QQ==]}"
"{\"content_topic\":\"\",\"timestamp_ns\":0,\"message\":\"QQ==\"}"
);
}

Expand Down
12 changes: 12 additions & 0 deletions crates/xmtp-networking/src/serialize_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use base64::Engine;
use serde::Serializer;

pub fn as_base64<S>(
data: &[u8],
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where
S: Serializer,
{
serializer.serialize_str(&base64::engine::general_purpose::STANDARD.encode(data))
}
2 changes: 2 additions & 0 deletions crates/xmtp-networking/src/xmtp.message_api.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub struct AuthData {
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct IndexCursor {
#[prost(bytes = "vec", tag = "1")]
#[serde(serialize_with = "crate::serialize_utils::as_base64")]
pub digest: ::prost::alloc::vec::Vec<u8>,
#[prost(uint64, tag = "2")]
pub sender_time_ns: u64,
Expand Down Expand Up @@ -98,6 +99,7 @@ pub struct Envelope {
#[prost(uint64, tag = "2")]
pub timestamp_ns: u64,
#[prost(bytes = "vec", tag = "3")]
#[serde(serialize_with = "crate::serialize_utils::as_base64")]
pub message: ::prost::alloc::vec::Vec<u8>,
}
/// Publish
Expand Down

0 comments on commit 263a4d2

Please sign in to comment.