solana_program/message/versions/
sanitized.rs

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