solana_message/versions/
sanitized.rs

1use {
2    super::VersionedMessage, crate::compiled_instruction::CompiledInstruction,
3    solana_address::Address, solana_sanitize::SanitizeError,
4};
5
6/// Wraps a sanitized `VersionedMessage` to provide a safe API
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct SanitizedVersionedMessage {
9    pub message: VersionedMessage,
10}
11
12impl TryFrom<VersionedMessage> for SanitizedVersionedMessage {
13    type Error = SanitizeError;
14    fn try_from(message: VersionedMessage) -> Result<Self, Self::Error> {
15        Self::try_new(message)
16    }
17}
18
19impl SanitizedVersionedMessage {
20    pub fn try_new(message: VersionedMessage) -> Result<Self, SanitizeError> {
21        message.sanitize()?;
22        Ok(Self { message })
23    }
24
25    /// Program instructions that will be executed in sequence and committed in
26    /// one atomic transaction if all succeed.
27    pub fn instructions(&self) -> &[CompiledInstruction] {
28        self.message.instructions()
29    }
30
31    /// Program instructions iterator which includes each instruction's program
32    /// id.
33    pub fn program_instructions_iter(
34        &self,
35    ) -> impl Iterator<Item = (&Address, &CompiledInstruction)> + Clone {
36        self.message.instructions().iter().map(move |ix| {
37            (
38                self.message
39                    .static_account_keys()
40                    .get(usize::from(ix.program_id_index))
41                    .expect("program id index is sanitized"),
42                ix,
43            )
44        })
45    }
46}