Back
+20 XP
4/12
Challenge: Simulate Anchor Account Validation in Rust
Anchor's #[account] constraints validate accounts automatically. Here you'll implement the same checks manually — this is exactly what Anchor generates under the hood.
Your Task
Implement validate_account(account_owner, expected_owner, is_signer, require_signer, is_writable, require_writable) that performs Anchor-style validation:
- Check if
account_owner == expected_owner→ if not, returnErr("IncorrectProgramId") - Check if signer is required but missing → return
Err("MissingRequiredSignature") - Check if writable is required but missing → return
Err("AccountNotWritable") - If all pass, return
Ok("Valid")
This mirrors these Anchor constraints:
#[account(owner = expected_owner)]→ ownership checkSigner<'info>→ signer check#[account(mut)]→ writable check
Check order matters — validate in the order listed above (owner → signer → writable).
Test Cases
All checks pass should return Ok
Input:
"programA", "programA", true, true, true, trueExpected: __result__.is_ok()Wrong owner should return IncorrectProgramId
Input:
"programA", "programB", true, true, true, trueExpected: __result__ == Err("IncorrectProgramId".to_string())Missing signer should return MissingRequiredSignature
Input:
"programA", "programA", false, true, true, trueExpected: __result__ == Err("MissingRequiredSignature".to_string())