Skip to content

Commit

Permalink
attempt commit to pending proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronvoell committed Apr 4, 2024
1 parent f1c7d2f commit 27bccea
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 67 deletions.
15 changes: 12 additions & 3 deletions mls_validation_service/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ fn validate_key_package(key_package_bytes: Vec<u8>) -> Result<ValidateKeyPackage
mod tests {
use ethers::signers::LocalWallet;
use openmls::{
extensions::{ApplicationIdExtension, Extension, Extensions},
extensions::{ApplicationIdExtension, Extension, ExtensionType, Extensions},
prelude::{
tls_codec::Serialize, Ciphersuite, Credential as OpenMlsCredential, CredentialWithKey,
CryptoConfig,
tls_codec::Serialize, Capabilities, Ciphersuite, Credential as OpenMlsCredential,
CredentialWithKey, CryptoConfig,
},
prelude_test::KeyPackage,
versions::ProtocolVersion,
Expand Down Expand Up @@ -177,7 +177,16 @@ mod tests {
let application_id =
Extension::ApplicationId(ApplicationIdExtension::new(account_address.as_bytes()));

let capabilities = Capabilities::new(
None,
Some(&[CIPHERSUITE]),
Some(&[ExtensionType::Unknown(0xff11)]),
None,
None,
);

let kp = KeyPackage::builder()
.leaf_node_capabilities(capabilities)
.leaf_node_extensions(Extensions::single(application_id))
.build(
CryptoConfig {
Expand Down
25 changes: 14 additions & 11 deletions xmtp_mls/src/groups/group_mutable_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use openmls::{extensions::{Extension, UnknownExtension}, group::MlsGroup as OpenMlsGroup};
use openmls::{
extensions::{Extension, UnknownExtension},
group::MlsGroup as OpenMlsGroup,
};
use prost::Message;
use thiserror::Error;

Expand All @@ -21,17 +24,16 @@ pub struct GroupMutableMetadata {
}

impl GroupMutableMetadata {
pub fn new(
group_name: String,
allow_list_account_addresses: Vec<String>,
) -> Self {
pub fn new(group_name: String, allow_list_account_addresses: Vec<String>) -> Self {
Self {
group_name,
allow_list_account_addresses,
}
}

pub(crate) fn from_proto(proto: GroupMutableMetadataProto) -> Result<Self, GroupMutableMetadataError> {
pub(crate) fn from_proto(
proto: GroupMutableMetadataProto,
) -> Result<Self, GroupMutableMetadataError> {
Ok(Self::new(
proto.group_name.clone(),
proto.allow_list_account_addresses.clone(),
Expand Down Expand Up @@ -75,12 +77,13 @@ impl TryFrom<GroupMutableMetadataProto> for GroupMutableMetadata {
}
}

pub fn extract_group_mutable_metadata(group: &OpenMlsGroup) -> Result<GroupMutableMetadata, GroupMutableMetadataError> {
let extensions = group
.export_group_context()
.extensions();
pub fn extract_group_mutable_metadata(
group: &OpenMlsGroup,
) -> Result<GroupMutableMetadata, GroupMutableMetadataError> {
let extensions = group.export_group_context().extensions();
for extension in extensions.iter() {
if let Extension::Unknown(0xff00, UnknownExtension(meta_data)) = extension {
// TODO: Replace with Constant
if let Extension::Unknown(0xff11, UnknownExtension(meta_data)) = extension {
return GroupMutableMetadata::try_from(meta_data);
}
}
Expand Down
21 changes: 11 additions & 10 deletions xmtp_mls/src/groups/group_permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ impl PolicySet {
)?,
MembershipPolicies::try_from(
proto
.update_group_name_policy
.ok_or(PolicyError::InvalidPolicy)?,
.update_group_name_policy
.ok_or(PolicyError::InvalidPolicy)?,
)?,
MembershipPolicies::try_from(
proto
.update_allow_list_policy
.ok_or(PolicyError::InvalidPolicy)?,
.update_allow_list_policy
.ok_or(PolicyError::InvalidPolicy)?,
)?,
))
}
Expand Down Expand Up @@ -353,10 +353,11 @@ fn default_remove_installation_policy() -> MembershipPolicies {
/// A policy where any member can add or remove any other member or set group name
pub(crate) fn policy_everyone_is_admin() -> PolicySet {
PolicySet::new(
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::deny())
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::deny(),
)
}

/// A policy where only the group creator can add or remove members
Expand Down Expand Up @@ -471,7 +472,7 @@ mod tests {
#[test]
fn test_allow_all() {
let permissions = PolicySet::new(
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
MembershipPolicies::allow(),
Expand All @@ -484,7 +485,7 @@ mod tests {
#[test]
fn test_deny() {
let permissions = PolicySet::new(
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
MembershipPolicies::deny(),
Expand Down
21 changes: 13 additions & 8 deletions xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use xmtp_proto::xmtp::mls::database::{
SendWelcomes as SendWelcomesProto,
},
remove_members_data::{Version as RemoveMembersVersion, V1 as RemoveMembersV1},
update_metadata_data::{Version as UpdateMetadataVersion, V1 as UpdateMetadataV1},
send_message_data::{Version as SendMessageVersion, V1 as SendMessageV1},
update_metadata_data::{Version as UpdateMetadataVersion, V1 as UpdateMetadataV1},
AccountAddresses, AddMembersData,
AddressesOrInstallationIds as AddressesOrInstallationIdsProtoWrapper, InstallationIds,
PostCommitAction as PostCommitActionProto, RemoveMembersData, SendMessageData, UpdateMetadataData,
PostCommitAction as PostCommitActionProto, RemoveMembersData, SendMessageData,
UpdateMetadataData,
};

use crate::{
Expand Down Expand Up @@ -231,7 +232,10 @@ pub struct UpdateMetadataIntentData {

impl UpdateMetadataIntentData {
pub fn new(group_name: String, allow_list_account_addresses: Vec<String>) -> Self {
Self { group_name, allow_list_account_addresses }
Self {
group_name,
allow_list_account_addresses,
}
}

pub(crate) fn to_bytes(&self) -> Vec<u8> {
Expand All @@ -252,13 +256,11 @@ impl UpdateMetadataIntentData {
pub(crate) fn from_bytes(data: &[u8]) -> Result<Self, IntentError> {

Check warning on line 256 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / Test

associated function `from_bytes` is never used

Check warning on line 256 in xmtp_mls/src/groups/intents.rs

View workflow job for this annotation

GitHub Actions / workspace

associated function `from_bytes` is never used

warning: associated function `from_bytes` is never used --> xmtp_mls/src/groups/intents.rs:256:19 | 233 | impl UpdateMetadataIntentData { | ----------------------------- associated function in this implementation ... 256 | pub(crate) fn from_bytes(data: &[u8]) -> Result<Self, IntentError> { | ^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default
let msg = UpdateMetadataData::decode(data)?;
let group_name = match msg.version {
Some(UpdateMetadataVersion::V1(ref v1)) => v1
.group_name.clone(),
Some(UpdateMetadataVersion::V1(ref v1)) => v1.group_name.clone(),
None => return Err(IntentError::Generic("missing payload".to_string())),
};
let allow_list_account_addresses = match msg.version {
Some(UpdateMetadataVersion::V1(v1)) => v1
.allow_list_account_addresses.clone(),
Some(UpdateMetadataVersion::V1(v1)) => v1.allow_list_account_addresses.clone(),
None => return Err(IntentError::Generic("missing payload".to_string())),
};

Expand Down Expand Up @@ -420,7 +422,10 @@ mod tests {
let wallet = generate_local_wallet();
let account_address = wallet.get_address();

let intent = UpdateMetadataIntentData::new("group name".to_string(), vec![account_address.clone()].into());
let intent = UpdateMetadataIntentData::new(
"group name".to_string(),
vec![account_address.clone()].into(),
);
let as_bytes: Vec<u8> = intent.clone().try_into().unwrap();
let restored_intent = UpdateMetadataIntentData::from_bytes(as_bytes.as_slice()).unwrap();

Expand Down
69 changes: 49 additions & 20 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ pub mod validated_commit;

use intents::SendMessageIntentData;
use openmls::{
extensions::{Extension, Extensions, Metadata, UnknownExtension},
group::{MlsGroupCreateConfig, MlsGroupJoinConfig, ProposalError},
credentials::CredentialType,
extensions::{
Extension, ExtensionType, Extensions, Metadata, RequiredCapabilitiesExtension,
UnknownExtension,
},
group::{
CommitToPendingProposalsError, MlsGroupCreateConfig, MlsGroupJoinConfig, ProposalError,
},
prelude::{
CredentialWithKey, CryptoConfig, Error as TlsCodecError, GroupId, MlsGroup as OpenMlsGroup,
StagedWelcome, Welcome as MlsWelcome, WireFormatPolicy,
Expand All @@ -23,16 +29,26 @@ use thiserror::Error;
use xmtp_cryptography::signature::is_valid_ed25519_public_key;
use xmtp_proto::{
api_client::XmtpMlsClient,
xmtp::mls::{api::v1::{
group_message::{Version as GroupMessageVersion, V1 as GroupMessageV1},
GroupMessage,
}, database::AccountAddresses, message_contents::{plaintext_envelope::{Content, V1}, GroupMutableMetadataV1, PlaintextEnvelope}},
xmtp::mls::{
api::v1::{
group_message::{Version as GroupMessageVersion, V1 as GroupMessageV1},
GroupMessage,
},
message_contents::{
plaintext_envelope::{Content, V1},
GroupMutableMetadataV1, PlaintextEnvelope,
},
},
};

use self::{group_metadata::extract_group_metadata, group_mutable_metadata::{GroupMutableMetadata, GroupMutableMetadataError}, intents::UpdateMetadataIntentData};
use self::group_mutable_metadata::extract_group_mutable_metadata;
pub use self::group_permissions::PreconfiguredPolicies;
pub use self::intents::{AddressesOrInstallationIds, IntentError};
use self::{
group_metadata::extract_group_metadata,
group_mutable_metadata::{GroupMutableMetadata, GroupMutableMetadataError},
intents::UpdateMetadataIntentData,
};
use self::{
group_metadata::{ConversationType, GroupMetadata, GroupMetadataError},
group_permissions::PolicySet,
Expand Down Expand Up @@ -122,6 +138,8 @@ pub enum GroupError {
EncodeError(#[from] prost::EncodeError),
#[error("group context extension proposal error: {0}")]
ProposalError(#[from] ProposalError<()>),
#[error("commit to pending proposal error: {0}")]
CommitToPendingProposalsError(#[from] CommitToPendingProposalsError<StorageError>),
}

impl RetryableError for GroupError {
Expand Down Expand Up @@ -355,13 +373,9 @@ where
self.sync_until_intent_resolved(conn, intent.id).await
}

pub async fn update_group_metadata(
&self,
group_name: String,
) -> Result<(), GroupError> {
pub async fn update_group_metadata(&self, group_name: String) -> Result<(), GroupError> {
// TODO: enable mutable metadata allow list
let empty_account_addresses = vec![];

let conn = &mut self.client.store.conn()?;
let intent_data: Vec<u8> =
UpdateMetadataIntentData::new(group_name, empty_account_addresses).to_bytes();
Expand Down Expand Up @@ -504,7 +518,6 @@ pub fn build_mutable_metadata_extension(
group_name: String,
allow_list_account_addresses: Vec<String>,
) -> Result<Extension, GroupError> {

let mutable_metadata: GroupMutableMetadataV1 = GroupMutableMetadataV1 {
group_name,
allow_list_account_addresses,
Expand All @@ -513,16 +526,23 @@ pub fn build_mutable_metadata_extension(
let unknown_gc_extension = UnknownExtension(mutable_metadata.encode_to_vec());

// TODO: Where should the constant hex value live?
Ok(Extension::Unknown(0xff00, unknown_gc_extension))
Ok(Extension::Unknown(0xff11, unknown_gc_extension))
}

fn build_group_config(
protected_metadata_extension: Extension,
mutable_metadata_extension: Extension
mutable_metadata_extension: Extension,
) -> Result<MlsGroupCreateConfig, GroupError> {
let mut extensions = Extensions::single(protected_metadata_extension);
extensions.add(mutable_metadata_extension)?;

let rc_extensions = &[ExtensionType::Unknown(0xff11)];
let proposals = &[];
let credentials = &[CredentialType::Basic];
let required_capabilities =
RequiredCapabilitiesExtension::new(rc_extensions, proposals, credentials);

let _ = extensions.add(Extension::RequiredCapabilities(required_capabilities))?;

Check warning on line 545 in xmtp_mls/src/groups/mod.rs

View workflow job for this annotation

GitHub Actions / workspace

this let-binding has unit value

warning: this let-binding has unit value --> xmtp_mls/src/groups/mod.rs:545:5 | 545 | let _ = extensions.add(Extension::RequiredCapabilities(required_capabilities))?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `extensions.add(Extension::RequiredCapabilities(required_capabilities))?;` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value = note: `#[warn(clippy::let_unit_value)]` on by default
Ok(MlsGroupCreateConfig::builder()
.with_group_context_extensions(extensions)?
.crypto_config(CryptoConfig::with_default_version(CIPHERSUITE))
Expand All @@ -542,7 +562,7 @@ fn build_group_join_config() -> MlsGroupJoinConfig {

#[cfg(test)]
mod tests {
use openmls::{group, prelude::Member};
use openmls::prelude::Member;
use prost::Message;
use xmtp_api_grpc::grpc_api_helper::Client as GrpcClient;
use xmtp_cryptography::utils::generate_local_wallet;
Expand Down Expand Up @@ -1002,19 +1022,28 @@ mod tests {
// TODO: Add check for updating group mutable metadata
let amal = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let bola = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let charlie = ClientBuilder::new_test_client(&generate_local_wallet()).await;

let policies = Some(PreconfiguredPolicies::GroupCreatorIsAdmin);

let amal_group: MlsGroup<_> = amal.create_group(policies)
.unwrap();
let amal_group: MlsGroup<_> = amal.create_group(policies).unwrap();

amal_group.sync().await.unwrap();

// Add bola to the group
println!("Adding Bola to the group");
amal_group
.add_members(vec![bola.account_address()])
.await
.unwrap();

let bola_group = receive_group_invite(&bola).await;
bola_group.sync().await.unwrap();

let mut group_mutable_metadata = amal_group.mutable_metadata().unwrap();
assert!(group_mutable_metadata.group_name.eq("New Group"));

// Update group name
// Update group name
println!("Updating group name");
amal_group
.update_group_metadata("New Group Name 1".to_string())
.await
Expand Down
Loading

0 comments on commit 27bccea

Please sign in to comment.