Skip to content

​What Is CPI Guard?

The Token Extension Program’s CpiGuard account extension prevents another onchain program from using a token account for certain token instructions through a cross-program invocation (CPI). When CPI guard is enabled, the Token Extension Program rejects these instructions when another onchain program invokes them through CPI:
  • Transfer, TransferChecked, and TransferCheckedWithFee when the token account owner is used as the instruction authority
  • Burn and BurnChecked when the token account owner is used as the instruction authority
  • Confidential Transfer and TransferWithFee, and confidential Burn, when the token account owner is used as the instruction authority
  • UnwrapLamports (Token-2022 v11 and later)
  • Approve and ApproveChecked
  • CloseAccount when lamports would be sent somewhere other than the token account owner
  • SetAuthority when adding or replacing the close authority
When CPI guard is enabled, these cases are still allowed:
  • Transfer, TransferChecked, TransferCheckedWithFee, Burn, and BurnChecked through CPI when a delegate or permanent delegate signs instead of the token account owner
  • CloseAccount through CPI when lamports are sent to the token account owner
  • SetAuthority to remove an existing close authority
  • Revoke
When the token account owner is used as the instruction authority, Token Extension Program instructions can still be processed if the instruction is added directly to the transaction instead of being invoked through CPI. Changing the token account owner with SetAuthority stays blocked while CPI guard is enabled, even outside CPI. EnableCpiGuard and DisableCpiGuard also cannot be invoked through CPI.

​Before you start

The examples on this page run against a local validator at http://localhost:8899 (WebSocket ws://localhost:8900) and fund their accounts with airdrops from it. Start one in a separate terminal with Surfpool (surfpool start) or solana-test-validator. Each code section below lists the packages or crates it needs. The TypeScript examples use top-level await, so run them as ES modules, for example with npx tsx index.ts in a project whose package.json sets "type": "module".

​How to Enable CPI Guard

To enable CPI guard:
  1. Create and initialize a mint.
  2. Calculate the token account size and rent needed for a token account with CpiGuard.
  3. Create and initialize a token account for the mint.
  4. Invoke EnableCpiGuard on the token account.
  5. Invoke DisableCpiGuard on the token account to allow those instructions through CPI again.
1

Create and initialize the mint

Create and initialize the mint that the token account will use.
Example
2

Calculate token account size

Calculate the token account size for the base token account plus the CpiGuard extension. This is the size used in CreateAccount.
Example
3

Calculate rent

Calculate rent using the token account size needed for the CpiGuard extension.
Example
4

Create and initialize the token account

Create the token account with the calculated space and lamports, then initialize it for the mint.
Example
5

Enable CPI guard

Enable CpiGuard on the token account.
Example
6

Disable CPI guard

Disable CpiGuard on the token account.
Example

​Source Reference

ItemDescriptionSource
CpiGuardAccount extension that stores whether CPI guard is currently enabled for the token account.Source
CpiGuardInstruction::EnableInstruction that enables CPI guard on a token account.Source
CpiGuardInstruction::DisableInstruction that disables CPI guard on a token account.Source
process_toggle_cpi_guardProcessor logic used by Enable and Disable that validates account ownership, rejects CPI changes to the guard itself, and initializes CpiGuard if it is not already present before setting lock_cpi.Source

​Kit

These examples use the generated Token-2022 instructions directly.
Terminal
Instructions

​Web3.js v3

These examples use the class-based @solana/web3.js v3 API with @solana-program instruction builders.
Terminal
Instructions

​Rust

Add these dependencies to Cargo.toml (tested with Rust 1.89, 1.98, and 1.99):
Cargo.toml
Rust

​Web3.js (legacy)

These examples use legacy @solana/web3.js with @solana/spl-token.
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.
Terminal
Instructions