PDAs as signers
Suggest an editA 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.
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:
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)?;Three rules cover most failures of this code:
- The bump goes last and comes from the account. When
find_program_addressoriginally 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-runningfind_program_addresswould also work but costs significant compute units. - Bind each
Pubkeyto a local variable first. An inlinectx.accounts.mint.key().as_ref()borrows from aPubkeytemporary 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.
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.