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;