SummaryCreate PDA accounts via
invoke_signed with the PDA’s seeds. Only the
owning program can sign for a PDA. Anchor’s init constraint automates
PDA account creation.PDA Signing via invoke_signed
When a program needs to sign on behalf of a PDA during a CPI, it uses
invoke_signed with the PDA’s seeds. The runtime verifies the seeds derive
the expected PDA using the calling program’s ID, ensuring only the owning
program can sign. For the full verification flow, see
PDA Signing.
Security: Control what a PDA signature authorizesThe runtime checks that your program owns the PDA. It doesn’t check what
the signature is used for. Inside
invoke_signed, the callee can do
anything that PDA’s signature allows, so:- Build signer seeds from validated accounts and stored state, never from raw instruction data. Caller-supplied seeds let the caller pick which of your PDAs signs.
- Verify the program ID you invoke before signing (see CPI).
- Re-derive the PDA and compare it with the account passed in, so the signature covers the account you intend.
Create a PDA Account
Deriving a PDA and creating an account at a PDA are separate operations. You must explicitly create the account after deriving the address. To create an account at a PDA, the deriving program invokes the System Program’screate_account instruction via invoke_signed, passing
the PDA’s seeds so the runtime can verify the program’s authority over that
address.
The example below uses the Anchor framework
to create a new account with a program-derived address. The program includes a
single initialize instruction to create the new account,
which will store the user address and
bump seed used to derive the PDA.
To run this example locally with the Anchor CLI:
-
Create a project with a single-file program and a Mocha test:
Terminal
-
Replace
programs/pda-account/src/lib.rswith the program below andtests/pda-account.tswith the test. -
Point the program’s
declare_id!at your project’s program ID:Without this step,Terminalanchor buildonly warnsProgram ID mismatch detected, andanchor testfails with an error such asDeclaredProgramIdMismatch(Anchor 1.1.2) orThis program may not be used for executing instructions. -
Build the program and run the test:
Terminal
Program
init constraint tells Anchor to
invoke the System Program
to create a new account using the PDA as the address. The seeds
used to create the PDA are:
- The fixed string: “data”
- The address of the user account provided in the instruction
- The canonical bump seed
find_program_address to derive the PDA and find the bump.
pda_account
initialize instruction to create a new account with a
program-derived address. The file contains code to
derive the PDA.
The example also shows how to fetch the new account that will
be created.
Test
If you invoke the
initialize instruction again with the same user address
seed, the transaction will fail. This happens because an account already
exists at the derived address.Security: Reinitialization with init_if_neededAnchor’s
init fails when the account exists, which is the behavior
above. init_if_needed succeeds either way and runs your handler on an
existing account. If the handler writes initial state unconditionally, a
caller can reset a live account, for example overwriting its stored
user or zeroing a balance. Use init where you can; with
init_if_needed, check whether the account is already initialized before
writing to it.