Generating PDAs with Anchor

Program-Derived Addresses (PDAs)

Chapter 2: Generating PDAs with Anchor

Anchor simplifies PDA creation with attributes like seeds and bump.

  • Key Points:

    • Use #[account(seeds = [...], bump)] to define PDAs.

    • The bump ensures the address is off-curve.

    • Anchor validates PDAs automatically.

  • Code Example:

#[derive(Accounts)]
pub struct CreatePDA<'info> {
    #[account(
        init,
        payer = signer,
        space = 8 + 32,
        seeds = [b"vault", signer.key().as_ref()],
        bump
    )]
    pub vault: Account<'info, Vault>,
    pub signer: Signer<'info>,
    pub system_program: Program<'info, System>,
}
  • Interactivity: Experiment with different seeds and observe how the PDA changes.

Last updated