From 3134d477665805e1d1753fafb409649e920ee3f0 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Tue, 10 Dec 2024 15:47:32 -0800 Subject: [PATCH 1/3] feat: adding new xmtp_content_types crate --- Cargo.lock | 4 ++++ Cargo.toml | 2 ++ xmtp_content_types/Cargo.toml | 7 +++++++ xmtp_content_types/src/lib.rs | 1 + 4 files changed, 14 insertions(+) create mode 100644 xmtp_content_types/Cargo.toml create mode 100644 xmtp_content_types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 17a96844d..25fbf8f7a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7300,6 +7300,10 @@ dependencies = [ "xmtp_proto", ] +[[package]] +name = "xmtp_content_types" +version = "0.1.0" + [[package]] name = "xmtp_cryptography" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 0226f5a35..c72428bfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "bindings_ffi", "xtask", "xmtp_debug", + "xmtp_content_types", ] # Make the feature resolver explicit. @@ -102,6 +103,7 @@ xmtp_cryptography = { path = "xmtp_cryptography" } xmtp_id = { path = "xmtp_id" } xmtp_mls = { path = "xmtp_mls" } xmtp_proto = { path = "xmtp_proto" } +xmtp_content_types = { path = "xmtp_content_types" } [profile.dev] # Disabling debug info speeds up builds a bunch, diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml new file mode 100644 index 000000000..e04d4f0eb --- /dev/null +++ b/xmtp_content_types/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "xmtp_content_types" +edition = "2021" +license.workspace = true +version.workspace = true + +[dependencies] diff --git a/xmtp_content_types/src/lib.rs b/xmtp_content_types/src/lib.rs new file mode 100644 index 000000000..63dc816d7 --- /dev/null +++ b/xmtp_content_types/src/lib.rs @@ -0,0 +1 @@ +// TODO: move content types here From 7645d95cbcbd4e28da546948861fd50bc7bf824e Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Tue, 10 Dec 2024 16:13:26 -0800 Subject: [PATCH 2/3] move content types to new crate --- Cargo.lock | 9 ++++++ examples/cli/Cargo.toml | 1 + examples/cli/cli-client.rs | 2 +- examples/cli/serializable.rs | 8 ++--- xmtp_content_types/Cargo.toml | 11 +++++-- .../src}/group_updated.rs | 2 +- xmtp_content_types/src/lib.rs | 29 ++++++++++++++++++- .../src}/membership_change.rs | 2 +- xmtp_content_types/src/test_utils.rs | 15 ++++++++++ .../codecs => xmtp_content_types/src}/text.rs | 2 +- xmtp_mls/Cargo.toml | 1 + xmtp_mls/src/codecs/mod.rs | 21 -------------- xmtp_mls/src/groups/mls_sync.rs | 4 +-- xmtp_mls/src/groups/mod.rs | 2 +- xmtp_mls/src/lib.rs | 1 - 15 files changed, 72 insertions(+), 38 deletions(-) rename {xmtp_mls/src/codecs => xmtp_content_types/src}/group_updated.rs (98%) rename {xmtp_mls/src/codecs => xmtp_content_types/src}/membership_change.rs (98%) create mode 100644 xmtp_content_types/src/test_utils.rs rename {xmtp_mls/src/codecs => xmtp_content_types/src}/text.rs (97%) delete mode 100644 xmtp_mls/src/codecs/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 25fbf8f7a..96b598356 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7294,6 +7294,7 @@ dependencies = [ "valuable", "valuable-serde", "xmtp_api_grpc", + "xmtp_content_types", "xmtp_cryptography", "xmtp_id", "xmtp_mls", @@ -7303,6 +7304,13 @@ dependencies = [ [[package]] name = "xmtp_content_types" version = "0.1.0" +dependencies = [ + "prost", + "rand", + "thiserror 2.0.6", + "tonic", + "xmtp_proto", +] [[package]] name = "xmtp_cryptography" @@ -7426,6 +7434,7 @@ dependencies = [ "web-time", "xmtp_api_grpc", "xmtp_api_http", + "xmtp_content_types", "xmtp_cryptography", "xmtp_id", "xmtp_proto", diff --git a/examples/cli/Cargo.toml b/examples/cli/Cargo.toml index 9d837d9a1..830931bce 100644 --- a/examples/cli/Cargo.toml +++ b/examples/cli/Cargo.toml @@ -39,6 +39,7 @@ valuable = { version = "0.1", features = ["derive"] } valuable-serde = "0.1" xmtp_api_grpc = { path = "../../xmtp_api_grpc" } xmtp_cryptography = { path = "../../xmtp_cryptography" } +xmtp_content_types = { path = "../../xmtp_content_types" } xmtp_id = { path = "../../xmtp_id" } xmtp_mls = { path = "../../xmtp_mls" } xmtp_proto = { path = "../../xmtp_proto", features = ["proto_full"] } diff --git a/examples/cli/cli-client.rs b/examples/cli/cli-client.rs index a9513bec5..ec285b1d9 100755 --- a/examples/cli/cli-client.rs +++ b/examples/cli/cli-client.rs @@ -32,6 +32,7 @@ use tracing_subscriber::{ use valuable::Valuable; use xmtp_api_grpc::grpc_api_helper::Client as ClientV3; use xmtp_api_grpc::replication_client::ClientV4; +use xmtp_content_types::{text::TextCodec, ContentCodec}; use xmtp_cryptography::{ signature::{RecoverableSignature, SignatureError}, utils::rng, @@ -47,7 +48,6 @@ use xmtp_mls::XmtpApi; use xmtp_mls::{ builder::ClientBuilderError, client::ClientError, - codecs::{text::TextCodec, ContentCodec}, groups::{device_sync::MessageHistoryUrls, GroupMetadataOptions}, identity::IdentityStrategy, storage::{ diff --git a/examples/cli/serializable.rs b/examples/cli/serializable.rs index cbc3190a6..c6ee793ce 100644 --- a/examples/cli/serializable.rs +++ b/examples/cli/serializable.rs @@ -1,12 +1,8 @@ use prost::Message; use serde::Serialize; use valuable::Valuable; -use xmtp_mls::{ - codecs::{text::TextCodec, ContentCodec}, - groups::MlsGroup, - storage::group_message::StoredGroupMessage, - XmtpApi, -}; +use xmtp_content_types::{text::TextCodec, ContentCodec}; +use xmtp_mls::{groups::MlsGroup, storage::group_message::StoredGroupMessage, XmtpApi}; use xmtp_proto::xmtp::mls::message_contents::EncodedContent; #[derive(Serialize, Debug, Valuable)] diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml index e04d4f0eb..b9c6394b8 100644 --- a/xmtp_content_types/Cargo.toml +++ b/xmtp_content_types/Cargo.toml @@ -1,7 +1,14 @@ [package] -name = "xmtp_content_types" edition = "2021" -license.workspace = true +name = "xmtp_content_types" version.workspace = true +license.workspace = true [dependencies] +thiserror = { workspace = true } +prost = { workspace = true, features = ["prost-derive"] } +rand = { workspace = true } +tonic = { version = "0.12", features = ["transport"] } + +# XMTP/Local +xmtp_proto = { workspace = true, features = ["convert"] } diff --git a/xmtp_mls/src/codecs/group_updated.rs b/xmtp_content_types/src/group_updated.rs similarity index 98% rename from xmtp_mls/src/codecs/group_updated.rs rename to xmtp_content_types/src/group_updated.rs index 09ebea595..7aa797ee0 100644 --- a/xmtp_mls/src/codecs/group_updated.rs +++ b/xmtp_content_types/src/group_updated.rs @@ -52,7 +52,7 @@ pub(crate) mod tests { use xmtp_proto::xmtp::mls::message_contents::{group_updated::Inbox, GroupUpdated}; - use crate::utils::test::rand_string; + use crate::test_utils::rand_string; use super::*; diff --git a/xmtp_content_types/src/lib.rs b/xmtp_content_types/src/lib.rs index 63dc816d7..b882891a1 100644 --- a/xmtp_content_types/src/lib.rs +++ b/xmtp_content_types/src/lib.rs @@ -1 +1,28 @@ -// TODO: move content types here +pub mod group_updated; +pub mod membership_change; +#[cfg(test)] +mod test_utils; +pub mod text; + +use thiserror::Error; +use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent}; + +pub enum ContentType { + GroupMembershipChange, + GroupUpdated, + Text, +} + +#[derive(Debug, Error)] +pub enum CodecError { + #[error("encode error {0}")] + Encode(String), + #[error("decode error {0}")] + Decode(String), +} + +pub trait ContentCodec { + fn content_type() -> ContentTypeId; + fn encode(content: T) -> Result; + fn decode(content: EncodedContent) -> Result; +} diff --git a/xmtp_mls/src/codecs/membership_change.rs b/xmtp_content_types/src/membership_change.rs similarity index 98% rename from xmtp_mls/src/codecs/membership_change.rs rename to xmtp_content_types/src/membership_change.rs index e64f50df6..b83483806 100644 --- a/xmtp_mls/src/codecs/membership_change.rs +++ b/xmtp_content_types/src/membership_change.rs @@ -54,7 +54,7 @@ pub(crate) mod tests { use xmtp_proto::xmtp::mls::message_contents::MembershipChange; - use crate::utils::test::{rand_string, rand_vec}; + use crate::test_utils::{rand_string, rand_vec}; use super::*; diff --git a/xmtp_content_types/src/test_utils.rs b/xmtp_content_types/src/test_utils.rs new file mode 100644 index 000000000..dead0c7e6 --- /dev/null +++ b/xmtp_content_types/src/test_utils.rs @@ -0,0 +1,15 @@ +#[cfg(test)] +use rand::{ + distributions::{Alphanumeric, DistString}, + Rng, +}; + +#[cfg(test)] +pub(crate) fn rand_string() -> String { + Alphanumeric.sample_string(&mut rand::thread_rng(), 24) +} + +#[cfg(test)] +pub(crate) fn rand_vec() -> Vec { + rand::thread_rng().gen::<[u8; 24]>().to_vec() +} diff --git a/xmtp_mls/src/codecs/text.rs b/xmtp_content_types/src/text.rs similarity index 97% rename from xmtp_mls/src/codecs/text.rs rename to xmtp_content_types/src/text.rs index d016f1513..124fb7831 100644 --- a/xmtp_mls/src/codecs/text.rs +++ b/xmtp_content_types/src/text.rs @@ -58,7 +58,7 @@ pub(crate) mod tests { #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker); - use crate::codecs::{text::TextCodec, ContentCodec}; + use crate::{text::TextCodec, ContentCodec}; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] #[cfg_attr(not(target_arch = "wasm32"), test)] diff --git a/xmtp_mls/Cargo.toml b/xmtp_mls/Cargo.toml index c1f8b4c72..e9d16676d 100644 --- a/xmtp_mls/Cargo.toml +++ b/xmtp_mls/Cargo.toml @@ -73,6 +73,7 @@ web-time.workspace = true zeroize.workspace = true # XMTP/Local +xmtp_content_types = { path = "../xmtp_content_types" } xmtp_cryptography = { workspace = true } xmtp_id = { path = "../xmtp_id" } xmtp_proto = { workspace = true, features = ["convert"] } diff --git a/xmtp_mls/src/codecs/mod.rs b/xmtp_mls/src/codecs/mod.rs deleted file mode 100644 index dc44e7cf4..000000000 --- a/xmtp_mls/src/codecs/mod.rs +++ /dev/null @@ -1,21 +0,0 @@ -pub mod group_updated; -pub mod membership_change; -pub mod text; - -use thiserror::Error; - -use xmtp_proto::xmtp::mls::message_contents::{ContentTypeId, EncodedContent}; - -#[derive(Debug, Error)] -pub enum CodecError { - #[error("encode error {0}")] - Encode(String), - #[error("decode error {0}")] - Decode(String), -} - -pub trait ContentCodec { - fn content_type() -> ContentTypeId; - fn encode(content: T) -> Result; - fn decode(content: EncodedContent) -> Result; -} diff --git a/xmtp_mls/src/groups/mls_sync.rs b/xmtp_mls/src/groups/mls_sync.rs index d1131d40c..a613551f8 100644 --- a/xmtp_mls/src/groups/mls_sync.rs +++ b/xmtp_mls/src/groups/mls_sync.rs @@ -9,7 +9,6 @@ use super::{ GroupError, HmacKey, IntentError, MlsGroup, ScopedGroupClient, }; use crate::{ - codecs::{group_updated::GroupUpdatedCodec, ContentCodec}, configuration::{ GRPC_DATA_LIMIT, HMAC_SALT, MAX_GROUP_SIZE, MAX_INTENT_PUBLISH_ATTEMPTS, MAX_PAST_EPOCHS, SYNC_UPDATE_INSTALLATIONS_INTERVAL_NS, @@ -64,6 +63,7 @@ use std::{ ops::RangeInclusive, }; use thiserror::Error; +use xmtp_content_types::{group_updated::GroupUpdatedCodec, CodecError, ContentCodec}; use xmtp_id::{InboxId, InboxIdRef}; use xmtp_proto::xmtp::mls::{ api::v1::{ @@ -122,7 +122,7 @@ pub enum GroupMessageProcessingError { #[error(transparent)] Intent(#[from] IntentError), #[error(transparent)] - Codec(#[from] crate::codecs::CodecError), + Codec(#[from] CodecError), #[error("wrong credential type")] WrongCredentialType(#[from] BasicCredentialError), #[error(transparent)] diff --git a/xmtp_mls/src/groups/mod.rs b/xmtp_mls/src/groups/mod.rs index 360245fd1..c8e19e933 100644 --- a/xmtp_mls/src/groups/mod.rs +++ b/xmtp_mls/src/groups/mod.rs @@ -1609,6 +1609,7 @@ pub(crate) mod tests { use openmls::prelude::Member; use prost::Message; use std::sync::Arc; + use xmtp_content_types::{group_updated::GroupUpdatedCodec, ContentCodec}; use xmtp_cryptography::utils::generate_local_wallet; use xmtp_proto::xmtp::mls::api::v1::group_message::Version; use xmtp_proto::xmtp::mls::message_contents::EncodedContent; @@ -1616,7 +1617,6 @@ pub(crate) mod tests { use crate::{ assert_err, builder::ClientBuilder, - codecs::{group_updated::GroupUpdatedCodec, ContentCodec}, groups::{ build_dm_protected_metadata_extension, build_mutable_metadata_extension_default, build_protected_metadata_extension, diff --git a/xmtp_mls/src/lib.rs b/xmtp_mls/src/lib.rs index fb21ba10b..71d2ede3f 100644 --- a/xmtp_mls/src/lib.rs +++ b/xmtp_mls/src/lib.rs @@ -4,7 +4,6 @@ pub mod api; pub mod builder; pub mod client; -pub mod codecs; pub mod configuration; pub mod groups; mod hpke; From bae42ca506b773444fb0469d4d88f2544c738295 Mon Sep 17 00:00:00 2001 From: cameronvoell Date: Wed, 11 Dec 2024 14:30:13 -0800 Subject: [PATCH 3/3] fix wasm bindings build --- xmtp_content_types/Cargo.toml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmtp_content_types/Cargo.toml b/xmtp_content_types/Cargo.toml index b9c6394b8..4d0a2b2ee 100644 --- a/xmtp_content_types/Cargo.toml +++ b/xmtp_content_types/Cargo.toml @@ -8,7 +8,9 @@ license.workspace = true thiserror = { workspace = true } prost = { workspace = true, features = ["prost-derive"] } rand = { workspace = true } -tonic = { version = "0.12", features = ["transport"] } # XMTP/Local xmtp_proto = { workspace = true, features = ["convert"] } + +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +tonic = { version = "0.12", features = ["transport"] }