Rust CPI Crate
Cross-program invocation for Anchor programs. Verify intents directly from your on-chain program.
cargo add intentguard-cpi
When to Use CPI
Use CPI when your on-chain program needs to verify an intent as part of its own logic. For example, a DEX program that requires IntentGuard verification before executing a swap.
TypeScript-only? If you're building a frontend dApp, use the TypeScript SDK instead. CPI is for on-chain Rust programs.
Quick Start
use intentguard_cpi::{verify_intent_cpi, VerifyAccounts};
pub fn handle_protected_swap(ctx: Context<ProtectedSwap>, intent_hash: [u8; 32]) -> Result<()> {
// Verify the user's intent before executing
verify_intent_cpi(
VerifyAccounts {
intent_commit: ctx.accounts.intent_commit.to_account_info(),
config: ctx.accounts.guard_config.to_account_info(),
user: ctx.accounts.user.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
intent_guard_program: ctx.accounts.intent_guard.to_account_info(),
},
intent_hash,
)?;
// Intent verified — safe to proceed with swap
execute_swap(ctx)
}
CPI Functions
verify_intent_cpi(accounts, intent_hash)
Verifies an intent via CPI. Closes the IntentCommit PDA and returns rent to the user.
| Param | Type | Description |
|---|---|---|
accounts | VerifyAccounts | Required accounts for verification |
intent_hash | [u8; 32] | Expected intent hash |
commit_intent_cpi(accounts, app_id, intent_hash, ttl)
Commits an intent via CPI. Used when your program needs to commit on behalf of the user.
revoke_intent_cpi(accounts, app_id)
Revokes a pending intent via CPI.
update_fee_cpi(accounts, new_fee)
Updates the verify fee (admin only).
withdraw_fees_cpi(accounts, amount)
Withdraws accumulated fees (admin only).
Account Structs
pub struct VerifyAccounts<'a> {
pub intent_commit: AccountInfo<'a>, // mut
pub config: AccountInfo<'a>, // mut
pub user: AccountInfo<'a>, // signer, mut
pub system_program: AccountInfo<'a>,
pub intent_guard_program: AccountInfo<'a>,
}
pub struct AdminAccounts<'a> {
pub config: AccountInfo<'a>, // mut
pub admin: AccountInfo<'a>, // signer
pub intent_guard_program: AccountInfo<'a>,
}
PDA Helpers
use intentguard_cpi::{find_intent_commit_pda, find_config_pda};
let (intent_pda, bump) = find_intent_commit_pda(&user_key, &app_id, &program_id);
let (config_pda, bump) = find_config_pda(&program_id);
Verify Macro
For simple inline verification, use the intent_guard_verify! macro:
use intentguard_cpi::intent_guard_verify!;
// One-liner verification
intent_guard_verify!(ctx, intent_hash);
Discriminators
| Instruction | Discriminator (8 bytes) |
|---|---|
commit_intent | [175, 152, 13, 10, 40, 234, 201, 8] |
verify_intent | [240, 198, 213, 223, 94, 7, 247, 247] |
revoke_intent | [42, 248, 79, 132, 107, 96, 193, 153] |
update_fee | [232, 253, 195, 247, 148, 212, 73, 222] |
withdraw_fees | [198, 212, 171, 109, 144, 215, 174, 89] |