hamzabellouch/agent-skills/anchor-solana-smart-contracts-rust
Claude Code and agent skill by hamzabellouch/agent-skills
- community
Checking that the SKILL.md file is still in the repository.
Production-grade Solana smart contract (program) development using Rust and the Anchor framework. Covers security vulnerability mitigation, account validation, PDA derivation, CPI safety, and TypeScript testing.
Install
npx vskill@latest install hamzabellouch/agent-skills/anchor-solana-smart-contracts-rustSKILL.md
The same SKILL.md was published earlier as hamzabellouch/agent-skills/anchor-solana-smart-contracts-rust.
Anchor Solana Smart Contracts & Rust Security Guide
This skill guide provides production-grade architecture, vulnerability defenses, security patterns, anti-patterns, and testing strategies for developing Solana programs using Rust and the Anchor Framework.
1. Core Principles of Solana & Anchor Security
Unlike EVM contracts where code and state reside together in a single contract account, Solana separates code (Programs) from state (Accounts).
- Explicit Account Validation: Programs must validate every account passed into an instruction (Owner, Signer, Writable, PDA Seeds/Bump, Expected Key).
- PDA (Program Derived Address) Hardening: Never trust a client-provided bump without validating it against
Pubkey::find_program_addressor storing/canonicalizing the canonical bump. - Strict Type Safety: Enforce explicit discriminator checks to prevent account type spoofing ("Type Cosplaying").
- Checked Arithmetic: Solana execution halts on panics; always use
checked_add,checked_sub,checked_mul, or Anchor's safe math helpers.
2. Critical Solana Vulnerabilities & Security Defenses
2.1 Account Reloading & Owner Validation (Type Cosplaying)
Vulnerability
If an account struct does not validate that an account is owned by the expected program or derived with the expected Anchor account discriminator, an attacker can create a fake account with identical memory layout owned by a malicious program.
Defense
Anchor handles account discriminators automatically when using #[account] and Account<'info, T>. Never use UncheckedAccount unless strictly necessary and manually validated.
2.2 Reinitialization & Closed Account Revival
Vulnerability
If an account is closed (lamports drained to 0) in Solana, an attacker can re-fund the account in the same transaction or block and reinitialize it if the program doesn't set a closed discriminator or verify state flags.
Defense
Anchor handles closing accounts safely via #[account(close = receiver)]. This zeroes the account memory and writes a special CLOSED_ACCOUNT_DISCRIMINATOR (8 zero bytes) to prevent reuse within the same transaction.
2.3 PDA Seed Collision & Canonical Bump Defense
Vulnerability
Using non-unique seeds (e.g., overlapping strings or uncontrolled user inputs) can allow two distinct logic paths to resolve to the exact same Program Derived Address (PDA).
Defense Strategy
- Prefix seeds with constant byte literals (
b"user_profile"). - Include all necessary domain identifiers (e.g.,
user.key(),mint.key()). - Always validate and store the canonical bump returned by Anchor's
find_program_address.
2.4 Arbitrary CPI & Program ID Checks
Vulnerability
Passing an unvalidated AccountInfo as a program ID when making a Cross-Program Invocation (CPI) allows an attacker to pass a malicious program that mimics standard interfaces (e.g., fake Token Program).
Defense
Use Anchor's strongly typed Program<'info, System> or Program<'info, Token> instead of raw AccountInfo.
3. Production Anchor Testing with TypeScript / Mocha
Testing Anchor programs involves using @coral-xyz/anchor and @solana/web3.js with local validator test runners (anchor test).
4. Solana Security Checklist
- [ ] Signer Check: Are all accounts modifying state or transferring funds marked with
Signer<'info>? - [ ] Owner Check: Are all custom accounts defined using
Account<'info, T>to enforce Anchor discriminator and program ownership checks? - [ ] Bump Seed Validation: Is canonical bump passed/validated during PDA account resolution?
- [ ] CPI Verification: Are external program calls strictly typed using
Program<'info, TargetProgram>? - [ ] Arithmetic Checks: Are all numerical operations guarded by
checked_add,checked_sub,checked_mul, etc.? - [ ] Rent Exemption: Are newly initialized accounts guaranteed to be rent-exempt?
- [ ] Account Closing: Is
close = destinationused to drain lamports and clear discriminators safely when closing accounts?
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewNot runA model review is scheduled for the next rescan.
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingProgramming/Web3_and_Blockchain/anchor-solana-smart-contracts-rust/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Grok Build (Cursor)
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/hamzabellouch/agent-skills/anchor-solana-smart-contracts-rust)