Skip to content

Commit

Permalink
Remove use of .map_err where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieobject committed Mar 26, 2024
1 parent 7b721cb commit 8eb87e0
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 71 deletions.
14 changes: 6 additions & 8 deletions xmtp_mls/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use openmls:: {
framing::{MlsMessageBodyIn, MlsMessageIn},
group:: GroupEpoch,
messages::Welcome,
prelude::tls_codec::Serialize,
prelude::tls_codec::{Deserialize, Serialize, Error as TlsCodecError},
};

use openmls_traits::OpenMlsProvider;
use openmls::prelude::tls_codec::{Deserialize, Error as TlsSerializationError};
use prost::EncodeError;
use thiserror::Error;

Expand Down Expand Up @@ -64,8 +63,8 @@ pub enum ClientError {
QueryError(#[from] xmtp_proto::api_client::Error),
#[error("identity error: {0}")]
Identity(#[from] crate::identity::IdentityError),
#[error("serialization error: {0}")]
Serialization(#[from] TlsSerializationError),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("key package verification: {0}")]
KeyPackageVerification(#[from] KeyPackageVerificationError),
#[error("syncing errors: {0:?}")]
Expand Down Expand Up @@ -107,8 +106,8 @@ pub enum MessageProcessingError {
Intent(#[from] IntentError),
#[error("storage error: {0}")]
Storage(#[from] crate::storage::StorageError),
#[error("tls deserialization: {0}")]
TlsDeserialization(#[from] openmls::prelude::Error),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("unsupported message type: {0:?}")]
UnsupportedMessageType(Discriminant<MlsMessageBodyIn>),
#[error("commit validation")]
Expand Down Expand Up @@ -270,8 +269,7 @@ where
let kp = self
.identity
.new_key_package(&self.mls_provider(&connection))?;
let kp_bytes = kp.tls_serialize_detached()
.map_err(|e| ClientError::Serialization(e))?;
let kp_bytes = kp.tls_serialize_detached()?;
self.api_client.upload_key_package(kp_bytes).await?;

Ok(())
Expand Down
6 changes: 2 additions & 4 deletions xmtp_mls/src/groups/intents.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use openmls::prelude::{MlsMessageOut, tls_codec::Error as TlsSerializationError,};
use openmls::prelude::{MlsMessageOut, tls_codec::{Error as TlsSerializationError, Serialize}};
use prost::{DecodeError, Message};
use thiserror::Error;
use openmls::prelude::tls_codec::Serialize;

use xmtp_proto::xmtp::mls::database::{
add_members_data::{Version as AddMembersVersion, V1 as AddMembersV1},
Expand Down Expand Up @@ -320,8 +319,7 @@ impl PostCommitAction {
welcome: MlsMessageOut,
installations: Vec<Installation>,
) -> Result<Self, IntentError> {
let welcome_bytes = welcome.tls_serialize_detached()
.map_err(|e| IntentError::TlsSerialization(e))?;
let welcome_bytes = welcome.tls_serialize_detached()?;

Ok(Self::SendWelcomes(SendWelcomesAction::new(
installations,
Expand Down
6 changes: 3 additions & 3 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use openmls::{
extensions::{Extension, Extensions, Metadata},
group::{MlsGroupCreateConfig, MlsGroupJoinConfig, StagedWelcome},
prelude::{
CredentialWithKey, CryptoConfig, GroupId, MlsGroup as OpenMlsGroup, Welcome as MlsWelcome,
CredentialWithKey, CryptoConfig, Error as TlsCodecError, GroupId, MlsGroup as OpenMlsGroup, Welcome as MlsWelcome,
WireFormatPolicy,
},
};
Expand All @@ -67,8 +67,8 @@ pub enum GroupError {
Intent(#[from] IntentError),
#[error("create message: {0}")]
CreateMessage(#[from] openmls::prelude::CreateMessageError),
#[error("tls serialization: {0}")]
TlsSerialization(#[from] openmls::prelude::Error),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("add members: {0}")]
AddMembers(#[from] openmls::prelude::AddMembersError<StorageError>),
#[error("remove members: {0}")]
Expand Down
31 changes: 9 additions & 22 deletions xmtp_mls/src/groups/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ use std::{collections::HashMap, mem::discriminant};

use log::debug;
use openmls::{
credentials::{
BasicCredential,
errors::BasicCredentialError,
},
credentials::BasicCredential,
framing::ProtocolMessage,
group::MergePendingCommitError,
prelude::{
LeafNodeIndex, MlsGroup as OpenMlsGroup, MlsMessageIn, MlsMessageBodyIn, PrivateMessageIn,
ProcessedMessage, ProcessedMessageContent, Sender,
tls_codec::{Deserialize,Serialize}
},
prelude_test::KeyPackage,
};
use openmls_traits::OpenMlsProvider;
use prost::Message;
use openmls::prelude::tls_codec::{Deserialize, Serialize};

use xmtp_proto::{
api_client::XmtpMlsClient,
Expand Down Expand Up @@ -304,8 +301,7 @@ where
envelope: &GroupMessageV1,
allow_epoch_increment: bool,
) -> Result<(), MessageProcessingError> {
let mls_message_in = MlsMessageIn::tls_deserialize_exact(&envelope.data)
.map_err(|e| MessageProcessingError::TlsDeserialization(e))?;
let mls_message_in = MlsMessageIn::tls_deserialize_exact(&envelope.data)?;

let message = match mls_message_in.extract() {
MlsMessageBodyIn::PrivateMessage(message) => Ok(message),
Expand Down Expand Up @@ -522,8 +518,7 @@ where
intent_data.message.as_slice(),
)?;

let msg_bytes = msg.tls_serialize_detached()
.map_err(|e| GroupError::TlsSerialization(e))?;
let msg_bytes = msg.tls_serialize_detached()?;
Ok((msg_bytes, None))
}
IntentKind::AddMembers => {
Expand All @@ -548,8 +543,7 @@ where
ValidatedCommit::from_staged_commit(staged_commit, openmls_group)?;
}

let commit_bytes = commit.tls_serialize_detached()
.map_err(|e| GroupError::TlsSerialization(e))?;
let commit_bytes = commit.tls_serialize_detached()?;

let installations = key_packages
.iter()
Expand Down Expand Up @@ -602,18 +596,15 @@ where
ValidatedCommit::from_staged_commit(staged_commit, openmls_group)?;
}

let commit_bytes = commit.tls_serialize_detached()
.map_err(|e| GroupError::TlsSerialization(e))?;
let commit_bytes = commit.tls_serialize_detached()?;

Ok((commit_bytes, None))
}
IntentKind::KeyUpdate => {
let (commit, _, _) =
openmls_group.self_update(provider, &self.client.identity.installation_keys)?;

Ok((commit.tls_serialize_detached()
.map_err(|e| GroupError::TlsSerialization(e))?,
None))
Ok((commit.tls_serialize_detached()?,None))
}
}
}
Expand Down Expand Up @@ -784,9 +775,7 @@ fn validate_message_sender(
if let Some(member) = openmls_group.member_at(*leaf_node_index) {
if member.credential.eq(decrypted_message.credential()) {
let basic_credential =
BasicCredential::try_from(&member.credential)
.map_err(|_| BasicCredentialError::WrongCredentialType)?;

BasicCredential::try_from(&member.credential)?;
sender_account_address = Identity::get_validated_account_address(
basic_credential.identity(),
&member.signature_key,
Expand All @@ -799,9 +788,7 @@ fn validate_message_sender(

if sender_account_address.is_none() {
let basic_credential =
BasicCredential::try_from(decrypted_message.credential())
.map_err(|_| BasicCredentialError::WrongCredentialType)?;

BasicCredential::try_from(decrypted_message.credential())?;
return Err(MessageProcessingError::InvalidSender {
message_time_ns: message_created_ns,
credential: basic_credential.identity().to_vec(),
Expand Down
22 changes: 9 additions & 13 deletions xmtp_mls/src/groups/validated_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
members::aggregate_member_list,
};
use crate::{
identity::Identity,
identity::{Identity, IdentityError},
types::Address,
verified_key_package::{KeyPackageVerificationError, VerifiedKeyPackage},
};
Expand Down Expand Up @@ -45,10 +45,12 @@ pub enum CommitValidationError {
ListMembers(String),
#[error("Failed to parse group metadata: {0}")]
GroupMetadata(#[from] GroupMetadataError),
#[error("Failed to validate identity: {0}")]
IdentityValidation(#[from] IdentityError),
#[error("invalid application id")]
InvalidApplicationId,
#[error("wrong credential type")]
WrongCredentialType(#[from] BasicCredentialError),
#[error("Credential error")]
CredentialError(#[from] BasicCredentialError),
}

// A participant in a commit. Could be the actor or the subject of a proposal
Expand Down Expand Up @@ -168,12 +170,9 @@ fn extract_actor(
let signature_key = leaf_node.signature_key.as_slice();

let basic_credential =
BasicCredential::try_from(&leaf_node.credential)
.map_err(|_| BasicCredentialError::WrongCredentialType)?;

BasicCredential::try_from(&leaf_node.credential)?;
let account_address =
Identity::get_validated_account_address(basic_credential.identity(), signature_key)
.map_err(|_| CommitValidationError::InvalidActorCredential)?;
Identity::get_validated_account_address(basic_credential.identity(), signature_key)?;

let is_creator = account_address.eq(&group_metadata.creator_account_address);

Expand Down Expand Up @@ -224,12 +223,9 @@ fn extract_identity_from_remove(
let signature_key = member.signature_key.as_slice();

let basic_credential =
BasicCredential::try_from(&member.credential)
.map_err(|_| BasicCredentialError::WrongCredentialType)?;

BasicCredential::try_from(&member.credential)?;
let account_address =
Identity::get_validated_account_address(basic_credential.identity(), signature_key)
.map_err(|_| CommitValidationError::InvalidSubjectCredential)?;
Identity::get_validated_account_address(basic_credential.identity(), signature_key)?;
let is_creator = account_address.eq(&group_metadata.creator_account_address);

Ok(CommitParticipant {
Expand Down
12 changes: 5 additions & 7 deletions xmtp_mls/src/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use openmls_traits::types::HpkeCiphertext;
use openmls_traits::OpenMlsProvider;
use openmls_traits::{key_store::OpenMlsKeyStore, types::HpkePrivateKey};
use thiserror::Error;
use openmls::prelude::tls_codec::{Deserialize, Serialize};
use openmls::prelude::tls_codec::{Deserialize, Error as TlsCodecError, Serialize};

use crate::{
configuration::{CIPHERSUITE, WELCOME_HPKE_LABEL},
Expand All @@ -17,8 +17,8 @@ use crate::{
pub enum HpkeError {
#[error("OpenMLS HPKE error: {0}")]
Hpke(#[from] OpenmlsHpkeError),
#[error("tls error: {0}")]
TlsError(#[from] openmls::prelude::Error),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("Key not found")]
KeyNotFound,
}
Expand All @@ -34,8 +34,7 @@ pub fn encrypt_welcome(welcome_payload: &[u8], hpke_key: &[u8]) -> Result<Vec<u8
&crypto,
)?;

let serialized_ciphertext = ciphertext.tls_serialize_detached()
.map_err(|e| HpkeError::TlsError(e))?;
let serialized_ciphertext = ciphertext.tls_serialize_detached()?;

Ok(serialized_ciphertext)
}
Expand All @@ -50,8 +49,7 @@ pub fn decrypt_welcome(
.read::<HpkePrivateKey>(hpke_public_key)
.ok_or(HpkeError::KeyNotFound)?;

let ciphertext = HpkeCiphertext::tls_deserialize_exact(ciphertext)
.map_err(|e| HpkeError::TlsError(e))?;
let ciphertext = HpkeCiphertext::tls_deserialize_exact(ciphertext)?;

Ok(decrypt_with_label(
private_key.to_vec().as_slice(),
Expand Down
12 changes: 5 additions & 7 deletions xmtp_mls/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use openmls::{
prelude::{
Capabilities, Credential as OpenMlsCredential, CredentialType, CredentialWithKey,
CryptoConfig, Extension, ExtensionType, Extensions, KeyPackage, KeyPackageNewError,
Lifetime, tls_codec::Serialize,
Lifetime, tls_codec::{Error as TlsCodecError, Serialize}
},
versions::ProtocolVersion,
};
Expand Down Expand Up @@ -51,8 +51,8 @@ pub enum IdentityError {
UninitializedIdentity,
#[error("wallet signature required - please sign the text produced by text_to_sign()")]
WalletSignatureRequired,
#[error("tls serialization: {0}")]
TlsSerialization(#[from] openmls::prelude::Error),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("api error: {0}")]
ApiError(#[from] xmtp_proto::api_client::Error),
#[error("OpenMLS credential error: {0}")]
Expand Down Expand Up @@ -93,8 +93,7 @@ impl Identity {
legacy_signed_private_key: Vec<u8>,
) -> Result<Self, IdentityError> {
info!("Creating identity from legacy key");
let signature_keys = SignatureKeyPair::new(CIPHERSUITE.signature_algorithm())
.map_err(|e| IdentityError::KeyGenerationError(e))?;
let signature_keys = SignatureKeyPair::new(CIPHERSUITE.signature_algorithm())?;
let credential =
Credential::create_from_legacy(&signature_keys, legacy_signed_private_key)?;
let credential_proto: CredentialProto = credential.into();
Expand Down Expand Up @@ -143,8 +142,7 @@ impl Identity {

// Register the installation with the server
let kp = self.new_key_package(provider)?;
let kp_bytes = kp.tls_serialize_detached()
.map_err(|e| IdentityError::TlsSerialization(e))?;
let kp_bytes = kp.tls_serialize_detached()?;
api_client.register_installation(kp_bytes).await?;

// Only persist the installation keys if the registration was successful
Expand Down
11 changes: 4 additions & 7 deletions xmtp_mls/src/verified_key_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use openmls::{
},
prelude::{
tls_codec::{
Deserialize, Error as TlsSerializationError
Deserialize, Error as TlsCodecError
},
KeyPackage, KeyPackageIn, KeyPackageVerifyError
}
Expand All @@ -19,8 +19,8 @@ use crate::{

#[derive(Debug, Error)]
pub enum KeyPackageVerificationError {
#[error("serialization error: {0}")]
Serialization(#[from] TlsSerializationError),
#[error("TLS Codec error: {0}")]
TlsError(#[from] TlsCodecError),
#[error("mls validation: {0}")]
MlsValidation(#[from] KeyPackageVerifyError),
#[error("identity: {0}")]
Expand Down Expand Up @@ -56,11 +56,8 @@ impl VerifiedKeyPackage {
// Validates starting with a KeyPackage (which is already validated by OpenMLS)
pub fn from_key_package(kp: KeyPackage) -> Result<Self, KeyPackageVerificationError> {
let leaf_node = kp.leaf_node();

let basic_credential =
BasicCredential::try_from(leaf_node.credential())
.map_err(|_| BasicCredentialError::WrongCredentialType)?;

BasicCredential::try_from(leaf_node.credential())?;
let pub_key_bytes = leaf_node.signature_key().as_slice();
let account_address = identity_to_account_address(basic_credential.identity(), pub_key_bytes)?;
let application_id = extract_application_id(&kp)?;
Expand Down

0 comments on commit 8eb87e0

Please sign in to comment.