safe_token_2022/extension/cpi_guard/
mod.rs

1use {
2    crate::{
3        extension::{BaseStateWithExtensions, Extension, ExtensionType, StateWithExtensionsMut},
4        pod::PodBool,
5        state::Account,
6    },
7    bytemuck::{Pod, Zeroable},
8    solana_program::instruction::{get_stack_height, TRANSACTION_LEVEL_STACK_HEIGHT},
9};
10
11/// CPI Guard extension instructions
12pub mod instruction;
13
14/// CPI Guard extension processor
15pub mod processor;
16
17/// CPI Guard extension for Accounts
18#[repr(C)]
19#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
20pub struct CpiGuard {
21    /// Lock privileged token operations from happening via CPI
22    pub lock_cpi: PodBool,
23}
24impl Extension for CpiGuard {
25    const TYPE: ExtensionType = ExtensionType::CpiGuard;
26}
27
28/// Determine if CPI Guard is enabled for this account
29pub fn cpi_guard_enabled(account_state: &StateWithExtensionsMut<Account>) -> bool {
30    if let Ok(extension) = account_state.get_extension::<CpiGuard>() {
31        return extension.lock_cpi.into();
32    }
33    false
34}
35
36/// Determine if we are in CPI
37pub fn in_cpi() -> bool {
38    get_stack_height() > TRANSACTION_LEVEL_STACK_HEIGHT
39}