Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions anchor/common/ssv_types/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,26 @@ impl SSVMessage {
msg_type: MsgType,
msg_id: MessageId,
data: Vec<u8>,
) -> Result<Self, SSVMessageError> {
let ssv_message = Self::from_parts(msg_type, msg_id, data)?;
ssv_message.validate()?;
Ok(ssv_message)
}

/// Bounds-checked construction without semantic validation; see [`Self::validate`].
pub fn from_parts(
msg_type: MsgType,
msg_id: MessageId,
data: Vec<u8>,
) -> Result<Self, SSVMessageError> {
let data = try_to_variable_list::<u8, SSVMessageDataLen, _, _>(data, |provided, max| {
SSVMessageError::SSVDataTooBig { provided, max }
})?;

let ssv_message = SSVMessage {
Ok(SSVMessage {
msg_type,
msg_id,
data,
};
ssv_message.validate()?;
Ok(ssv_message)
})
}

/// Validate the SSV Message
Expand Down Expand Up @@ -473,6 +481,19 @@ impl SignedSSVMessage {
operator_ids: Vec<OperatorId>,
ssv_message: SSVMessage,
full_data: Vec<u8>,
) -> Result<Self, SignedSSVMessageError> {
let signed_ssv_message =
Self::from_parts(signatures, operator_ids, ssv_message, full_data)?;
signed_ssv_message.validate()?;
Ok(signed_ssv_message)
}

/// Bounds-checked construction without semantic validation; see [`Self::validate`].
pub fn from_parts(
signatures: Vec<[u8; RSA_SIGNATURE_SIZE]>,
operator_ids: Vec<OperatorId>,
ssv_message: SSVMessage,
full_data: Vec<u8>,
) -> Result<Self, SignedSSVMessageError> {
// Convert Vec<[u8; 256]> to VariableList<VariableList<u8, U256>, U13>
// First convert each [u8; 256] to VariableList<u8, U256>
Expand All @@ -498,7 +519,7 @@ impl SignedSSVMessage {
|provided, max| SignedSSVMessageError::TooManySignatures { provided, max },
)?;

let signed_ssv_message = SignedSSVMessage {
Ok(SignedSSVMessage {
signatures,
operator_ids: try_to_variable_list::<OperatorId, U13, _, _>(
operator_ids,
Expand All @@ -509,11 +530,7 @@ impl SignedSSVMessage {
full_data,
|provided, max| SignedSSVMessageError::FullDataTooLong { provided, max },
)?,
};

signed_ssv_message.validate()?;

Ok(signed_ssv_message)
})
}

/// Returns a reference to the signatures.
Expand Down
Loading