Sign in

PDAs as signers

Suggest an edit

A program cannot keep a private key, so it cannot sign the way a wallet does. It signs for a PDA by passing the PDA's seeds to invoke_signed. The runtime re-derives the address with the calling program's ID and, if it matches an account in the call, treats that account as a signer.

The runtime accepts a re-derived address in place of a signature

A program that holds funds has to authorize moving them, and the Token Program moves tokens only when the owner of the source token account has signed. A normal account signs by producing a cryptographic signature with its private key. The runtime verifies the signature, sees that it matches the account's public key, and marks the account as a signer for the transaction.

A program has no private key to do this with, because its bytecode is public and a key stored in it would be public too. So the accounts it controls sit at PDAs, as the PDA lesson showed. A PDA's address is a hash that lands off the ed25519 curve, which means no private key exists that would produce that address.

What the runtime accepts instead is a re-derivation. When a program calls invoke_signed, it passes a list of seed sets along with the inner call. The runtime takes each seed set, appends the calling program's ID, runs the PDA derivation, and gets an address back. If that address matches an account in the call, the runtime marks it as a signer in the inner frame, as if a real signature had been provided.

The calling program's ID is mixed into the derivation, so a different program calling with the same seeds would derive a different PDA. The seeds aren't a secret. The program ID is what makes the seeds work for one specific program and nobody else.

Four steps of invoke_signed: seeds, PDA re-derivation, match, signer checkFour connected boxes show what invoke_signed does: your program calls invoke_signed with seeds, the runtime re-derives the PDA from the seeds and program ID, the derived PDA is matched against the accounts passed in, and the called program then sees the PDA as a signer. A caption below states that the seeds are the signature, and only your program can produce them under its program ID. What invoke_signed actually does 1. Your program calls invoke_signed with seeds invoke_signed(&ix, &accounts, signer_seeds) signer_seeds = &[&[b"vault", mint.as_ref(), &[bump]]] the seeds plus the bump describe one PDA you're signing as 2. Runtime re-derives the PDA pda = sha256(seeds || your_program_id || "ProgramDerivedAddress") your_program_id is whatever program is currently executing. a different program calling with the same seeds would derive a different PDA. this is why only your program can sign for PDAs derived under your program's ID. 3. Match against accounts in the call if the derived PDA matches an account passed in &accounts: → mark it as signer for the inner call if not: CPI fails with "unauthorized signer or writable account" 4. Called program runs and sees the PDA as a signer the Token Program's "is authority a signer?" check passes the inner program has no idea this signer is a PDA to it, the PDA looks like any other authorized account The seeds are the signature. Only your program can produce them under your program's ID.

An Ethereum contract needs no such step. When it calls a token contract, the token sees the contract's own address as msg.sender and moves the balance recorded under that address. The Token Program checks signer flags instead, so a Solana program needs an address it can sign for.

In Anchor the seeds are one more argument to the CPI context

The raw invoke_signed syscall is workable but verbose. In practice you use Anchor's CpiContext::new_with_signer, which takes the signer seeds as a third argument and otherwise looks like the plain CpiContext::new used for non-PDA CPIs. The &[&[&[u8]]] type of signer_seeds is that list of seed sets, with one entry per PDA you're signing for.

The full pattern for signing as a Vault PDA, whose seeds are b"vault" and the mint's address, fits into six small pieces:

rust
let bump = ctx.accounts.vault.bump;
let mint_key = ctx.accounts.mint.key();
let seeds: &[&[u8]] = &[b"vault", mint_key.as_ref(), &[bump]];
let signer_seeds: &[&[&[u8]]] = &[seeds];

let cpi_ctx = CpiContext::new_with_signer(
    ctx.accounts.token_program.to_account_info(),
    Transfer {
        from: ctx.accounts.vault_token.to_account_info(),
        to: ctx.accounts.user_token.to_account_info(),
        authority: ctx.accounts.vault.to_account_info(),
    },
    signer_seeds,
);
token::transfer(cpi_ctx, amount)?;
Signing as a PDA in Anchor, line by lineA six-step code block shows a Solana handler reading the vault's stored bump, building seeds with the bump last, wrapping them in signer_seeds, and using CpiContext::new_with_signer to call token::transfer. Side callouts flag why each step matters: bump stored on state at init, Rust needing a binding for temporaries, always putting bump last, and the double-reference signer form that is the one change versus a plain CPI. Signing as a PDA in Anchor, line by line inside your handler // 1. read the bump stored on the account let bump = ctx.accounts.vault.bump; // 2. bind values to locals (lifetimes) let mint_key = ctx.accounts.mint.key(); // 3. build the seeds, bump LAST let seeds: &[&[u8]] = &[ b"vault", mint_key.as_ref(), &[bump], ]; // 4. wrap in outer slice (one set per PDA) let signer_seeds: &[&[&[u8]]] = &[seeds]; // 5. build CpiContext with signer let cpi_ctx = CpiContext::new_with_signer( ctx.accounts.token_program.to_account_info(), Transfer { from, to, authority }, signer_seeds, ); // 6. make the CPI token::transfer(cpi_ctx, amount)?; stored on your state at init Rust needs a binding temporaries die too early bump LAST, always forget this and you derive the wrong PDA double reference awkward but supports many PDAs signer form the one change vs plain CPI The first time you write this it feels heavy. By the third time it's reflex. Every Solana program that holds tokens has some version of these six lines.

Three rules cover most failures of this code:

  • The bump goes last and comes from the account. When find_program_address originally derived the canonical PDA, the algorithm appended the bump byte after the rest of the seeds and ran the hash. To re-derive that same address, you must replay the same input in the same order. Move the bump to the front, leave it out, or pass a wrong value, and the seeds no longer derive the vault's address. They derive some other address or, if the hash lands on the curve, no address at all. Read the bump from the account where it was stored at init. Re-running find_program_address would also work but costs significant compute units.
  • Bind each Pubkey to a local variable first. An inline ctx.accounts.mint.key().as_ref() borrows from a Pubkey temporary that is dropped before the seeds are used, and the compiler reports "temporary value dropped while borrowed."
  • Use the seeds of the PDA you are acting for. If your program manages many PDAs of the same kind, such as per-user vaults, the seeds in your CPI must match the specific PDA. Mixing seeds for one user's vault into a transaction acting on another user's vault is a bug that the compiler can't catch, so test these paths.

When PDA signing fails because the seeds derive some other address, the error is Cross-program invocation with unauthorized signer or writable account. The runtime is telling you that an account in the inner call needed to be a signer but wasn't, or needed to be writable but wasn't. For PDA signing failures, signer is almost always the missing flag. If the seeds derive no valid address, which happens for about half of wrong seed sets, the call stops earlier with Could not create program address with signer seeds. Either way, review the seeds and the bump, and check that the PDA address you are trying to sign for is in your accounts list.

The Token Program sees an ordinary signed transfer

The canonical use case for PDA signing is a vault that holds tokens on behalf of a program. A Vault PDA is derived from some seeds, and a token account is created with the Vault PDA as its owner. Say the vault's token account holds 1,000 USDC, a user's token account holds 0, and your handler pays the user 300 USDC.

Vault PDA transfer flow: signing via invoke_signed and balance changesA Vault PDA owns a Vault Token Account holding 1,000 USDC, while the User Token Account starts at 0 USDC. To transfer 300 USDC, the program calls invoke_signed with the vault's seeds so the Token Program accepts the PDA as signer, leaving the vault with 700 USDC and the user with 300 USDC. The classic pattern: a Vault PDA that holds tokens before the transfer Vault PDA address: 9Hx2...mK3p seeds: [b"vault", mint, [bump]] owns: → Vault Token Account Vault Token Account mint: USDC owner: Vault PDA balance: 1,000 USDC User Token Account mint: USDC owner: User balance: 0 USDC to move tokens out of the Vault ATA, the Token Program needs Vault PDA to sign your handler runs transfer(300) 1. build CpiContext::new_with_signer with vault seeds 2. Token Program is invoked, sees Vault PDA in the signer set 3. checks: authority == vault_ata.owner → ✓ 4. checks: authority is signer → ✓ (runtime accepted the seeds) 5. mutates balances: vault -300, user +300 after the transfer Vault Token Account balance: 700 USDC (-300) User Token Account balance: 300 USDC (+300) No human signed for the Vault. The seeds passed to invoke_signed were its signature.

The handler passes the Vault PDA as the authority field in the Transfer instruction and the vault's seeds as signer seeds. The Token Program reads the authority field, looks at the source token account's owner field, confirms they match, and checks the runtime signer set to confirm the authority signed. Both checks pass, so it takes 300 from the vault's token account and adds 300 to the user's, leaving 700 and 300. From the Token Program's perspective, this looks identical to a transfer authorized by a normal wallet. The Token Program has no concept of PDAs.

The Vault PDA itself doesn't hold the tokens. They live in the token account, a separate account that records the Vault PDA as its owner, in the same way it would record a user's wallet. The next lesson covers token accounts.

The only thing your program needed to provide that a wallet-signed transfer would not provide is the seeds. Everything else is identical, including the instruction shape, the accounts list, and the amount.

Whoever can make your program sign controls the PDA

Every path in your program that reaches invoke_signed with the vault's seeds can move the vault's tokens, so the checks that run before that call are all that stands between a caller and the funds. The holder of the program's upgrade authority has the same power: they can deploy new code to the same program ID, and that code derives the same PDAs (program upgrades).

For each PDA that owns funds, list the paths in your program that end in a signature by that PDA, and say what each path checks before it signs.