spl_token_2022/extension/cpi_guard/
mod.rs

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