Program derived addresses on Solana

What a PDA is, how find_program_address and the bump work, how a program signs for an address with no private key, and the seed mistakes to avoid.

On this page
  1. The problem
  2. How the address is derived
  3. Signing without a private key
  4. The same thing in Anchor
  5. Seeds are a schema
  6. Notes

The problem

A Solana program can't search for accounts at run time. The client passes in every account up front (see the account model). So if a program keeps one profile account per user, the client needs the address without asking the program.

A random keypair per profile doesn't work: you'd need another account to find the first one. What you want is a function both sides can compute, with no lookup and no secret:

address = f(program, "profile", user)

That is a program derived address (PDA).

How the address is derived

A PDA is the SHA-256 hash of:

  • the seeds you choose, up to 16, each up to 32 bytes
  • a one-byte bump
  • the program ID
  • the fixed string "ProgramDerivedAddress"

The 32-byte result might be a valid point on the ed25519 curve. If it is, a private key for it could exist and someone could sign for it outside the program. So a PDA must be off the curve, and a bump that lands on it is rejected.

find_program_address tries bumps from 255 downwards and returns the first one that works: the canonical bump. Roughly half of all hashes are off the curve, so it usually takes one or two tries.

let (profile, bump) = Pubkey::find_program_address(
    &[b"profile", user.as_ref()],
    &program_id,
);

The client gets the same address from the same inputs:

const [profile, bump] = PublicKey.findProgramAddressSync(
  [Buffer.from("profile"), user.toBuffer()],
  programId,
);

Signing without a private key

Nobody holds a PDA's private key. Instead, the program that derived it can sign for it in a cross-program invocation. It calls invoke_signed with the seeds and bump; the runtime re-derives the address, and if it matches, treats that account as a signer for the inner call.

This is how a program holds assets. A vault PDA can own tokens, and only the program can move them, because only the program can produce that signature:

let seeds: &[&[u8]] = &[b"vault", user.as_ref(), &[vault_bump]];

invoke_signed(
    &system_instruction::transfer(&vault, &user, amount),
    &[vault_info.clone(), user_info.clone(), system_program.clone()],
    &[seeds],
)?;

The same thing in Anchor

Anchor expresses PDAs as account constraints. seeds and bump make it derive the address and reject any account that doesn't match. With init it also creates the account, and the PDA signs for its own creation:

#[derive(Accounts)]
pub struct CreateProfile<'info> {
    #[account(
        init,
        payer = user,
        space = 8 + Profile::INIT_SPACE,
        seeds = [b"profile", user.key().as_ref()],
        bump,
    )]
    pub profile: Account<'info, Profile>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
#[derive(InitSpace)]
pub struct Profile {
    pub authority: Pubkey,
    pub bump: u8,
    #[max_len(32)]
    pub name: String,
}

pub fn create_profile(ctx: Context<CreateProfile>, name: String) -> Result<()> {
    let profile = &mut ctx.accounts.profile;
    profile.authority = ctx.accounts.user.key();
    profile.bump = ctx.bumps.profile; // the canonical bump Anchor found
    profile.name = name;
    Ok(())
}

Later instructions use the stored bump with bump = profile.bump. Anchor then verifies the address with one hash instead of searching down from 255. Each failed try costs compute, and storing the bump costs one byte.

Seeds are a schema

Seeds decide what is unique, so choosing them is a data-model decision:

Account Seeds Meaning
Global config ["config"] One per program
User profile ["profile", user] One per user
Escrow ["escrow", maker, seed_u64] Many per user, client picks
Market position ["position", market, user] One per user per market

Four mistakes to avoid:

  • Accepting any bump. If the program takes whatever bump the caller sends, the same seeds give several valid addresses, and a user can create duplicate "unique" accounts. Use the canonical bump or the one stored at creation.
  • Seeds that collide. Seeds are concatenated before hashing, so ["a", "bc"] and ["ab", "c"] give the same address. Use fixed-length seeds (public keys, little-endian integers) or a fixed prefix per account type.
  • Leaving the authority out. If a vault's seeds don't include its owner, anyone who can call the instruction can derive and use the same vault.
  • User strings as seeds. A seed is capped at 32 bytes and variable-length input is easy to get wrong. Hash it first if you need it.

Notes

  • A PDA can hold SOL like any address. The program moves it out with invoke_signed, or by debiting directly if it owns the account.
  • A PDA is only an address until something creates an account there. Many are used only as signers, for example as the authority over token accounts.