Program-derived address design
Program-derived addresses (PDAs) give a Solana program stable addresses for state and program-controlled funds. The basic examples derive each address from the program ID and a small seed set, carry the selected bump when needed, and use the result as an account and authority contract. The current page-visit example shows the same design in both Anchor and native program forms. See Solana programming basics for the wider account model, account lifecycles for creation and closure, and instruction and CPI patterns for signer use across program calls.
Deterministic address derivation
A PDA is an address that a program can reproduce from its program ID and ordered seed bytes. The same inputs always identify the same address, so a client can find program state before it has received an address from the program. The program ID is part of this identity, which keeps the same seed set separate across programs.
Seed contracts
Seeds are the naming contract for a PDA. The page-visit examples use the readable prefix page_visits, then add the payer or user public key that defines the record's scope. The favorites example follows the same pattern with favorites and the user's key. The seed order and byte form are part of the address, not a display choice.
Good seed sets make the state relationship clear:
- Use a fixed prefix to separate one kind of account from another.
- Add the key that gives the account its scope, such as a user or parent account.
- Keep the order and encoding stable across clients and program instructions.
References: basics/…/page_visits.rs• L3-12 basics/…/create.rs• L4-20 basics/…/page_visits.rs• L7-18 basics/…/lib.rs• L50-66
Bump values
The derivation process searches for a one-byte bump that produces a valid PDA for the chosen seed set. The bump is not a second identity for the account. It is the value that completes the seed contract and lets the program reproduce the same address during a later signed action.
The page-visit examples store the bump with the account state. Anchor obtains it during account creation and stores it with the initial record. The native create path receives the record, including its bump, and uses that value for the signed account-creation call. A client derives the address and bump together when the instruction format requires it.
References: basics/…/page_visits.rs• L5-15 basics/…/create.rs• L9-25 basics/…/page_visits.rs• L7-18 basics/…/create.rs• L18-31
PDA authority model
A PDA has no private key. The program that owns the seed contract can ask the runtime to accept the PDA as a signer by supplying the same seeds and bump during a signed invocation. This separates a transaction user's authority from a program's authority.
User and program authorities
The user signs the transaction when the user must approve payment or a state change. The PDA signs only when the program must act for program-controlled funds or another PDA-owned role. An address alone does not prove either form of authority.
The rent-vault example shows this split. A caller supplies a new account, then the program uses the vault's seeds and bump to move lamports through a System Program account-creation call. The vault can therefore pay for an account without a private key. This basic example does not check caller authority, so any caller can request spending from the vault; it demonstrates PDA signing rather than a complete access policy.
This diagram shows the two authority paths in one account flow.
sequenceDiagram
participant User
participant Program
participant Vault as Rent-vault PDA
participant System as System Program
User->>Program: Approve instruction and provide payer
Program->>Vault: Validate rent-vault seeds
Program->>System: Create account with PDA signer seeds
Vault-->>System: Authorize the lamport source
References: basics/…/init_rent_vault.rs• L4-30 basics/…/create_new_account.rs• L4-40 basics/…/init_rent_vault.rs• L18-42
Signed PDA calls
PDA signing is valid only for the exact seed set and program ID that produced the address. The program passes those seeds to the CPI boundary when it asks another program to create an account or move funds. Anchor expresses this through a signer context, while native and low-level examples build the signer seed set directly.
The called program still applies its own account and authority rules. PDA seeds provide the program-level signature proof; they do not replace checks for the target account, owner, payer, or requested operation.
The native page-visit example uses the PDA signer path to let the payer-funded transaction create an account owned by the program. The PDA is the address being created, not a user wallet that signs the transaction.
References: basics/…/create_new_account.rs• L20-40 basics/…/create.rs• L11-31 basics/…/create.rs• L9-20
Address-driven state
Address-driven state uses a PDA as the canonical location for a relationship or record. The instruction does not need a separate account registry: it derives the expected address from the relationship and reads or updates the account at that address.
User-scoped records
The page-visit example creates one PageVisits account for each user from page_visits and the user's public key. The record starts at zero and later increments use the same derived address. The Anchor example validates that relationship through its account seeds and the stored bump. The native example creates the account with the same seed set and then updates the supplied account. The favorites example applies the same pattern to one favorites record per user.
The client and program meet at the PDA contract:
- The client derives the address from the program ID and seed values.
- The create instruction initializes that address and records the initial state.
- A later instruction supplies the same address and changes only its stored value.
- A read derives or receives the address and decodes the record found there.
References: basics/…/create.rs• L4-26 basics/…/increment.rs• L4-21 basics/…/test_program_derived_addresses.rs• L38-69 basics/…/create.rs• L11-31 basics/…/increment.rs• L9-16 basics/…/test.ts• L30-69
Account validation
Every instruction should connect the supplied account to the expected seed relationship. Anchor places the seed contract in account constraints, so the page-visit increment path checks the user-derived address and the stored bump before it changes state. A native program must provide the equivalent address check when it accepts an account from a caller; the current native increment example focuses on decoding and updating the supplied record after creation.
The three basic program styles express the same design in different ways. Anchor places the seed contract in account constraints. Native code can derive the expected PDA and compare it with the supplied account. Pinocchio can recreate the address from the supplied bump and reject an invalid seed result. The important contract is the address relationship rather than the framework syntax.
References: basics/…/increment.rs• L4-15 basics/…/lib.rs• L50-66 basics/…/create_pda.rs• L14-26 basics/…/get_pda.rs• L11-25 basics/…/increment.rs• L9-16
PDA design patterns
The basic examples use two related PDA patterns. One stores application state at an address derived from a user. The other uses a program-controlled PDA as a stable source of lamports for later account work.
| Pattern | Address inputs | Main purpose |
|---|---|---|
| User-scoped state | Fixed prefix plus user public key | Find one record for each user without a registry. |
| Program-controlled vault | Fixed prefix plus program ID | Hold funds at a known address and authorize program actions. |
These patterns can be combined. A larger state model can derive child accounts from a parent PDA and additional identifiers. Each added seed must express a real uniqueness rule and remain stable for the life of the account.
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the seed prefix, seed order, byte encoding, and program ID identical in clients, account constraints, address checks, and signer seed sets.
- Keep bump transport and storage aligned with the instruction data and account state. A missing or changed bump can make an existing PDA unusable for later signed calls.
- Review every account that uses a changed seed contract. A new seed can change the address and leave old state outside the program's normal lookup path.
- Preserve the distinction between the transaction payer's signature and a PDA's program signature when a CPI account or funding path changes.
- Keep the relationship key used for derivation tied to the authority check. A user-scoped PDA must not be accepted for a different user.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Client and program use different seed order or encoding | The client supplies an address that the program cannot validate or find | Define one seed contract and use it at every derivation boundary |
| A create or CPI call uses the wrong bump | PDA creation or runtime signer verification fails | Derive the bump with the address and pass the matching value to the program action |
| An instruction accepts a PDA without checking its user or parent relationship | The caller may read or change unrelated state | Derive or constrain the expected PDA from the instruction's relationship key |
| A program treats a PDA as a normal transaction signer | The action lacks a valid signature or cannot authorize a CPI | Use the PDA seed set with a signed invocation when the program must act for the PDA |
| A vault-funding instruction omits a caller authority check | Any caller may spend lamports held by the vault | Require an authorized signer or stored authority before using the vault as a funding source |
| A seed contract changes after accounts are deployed | Existing accounts are no longer found at the expected addresses | Treat seed prefixes, order, and scope keys as stable account schema |
Key files
| File | Why to read it |
|---|---|
| basics/…/page_visits.rs | Defines the user-scoped state shape, seed prefix, and stored bump. |
| basics/…/create.rs | Shows the PDA creation contract and initial state setup. |
| basics/…/increment.rs | Shows later state access through the same user-derived PDA. |
| basics/…/test_program_derived_addresses.rs | Shows client-side address derivation and the create-then-increment state flow in a local program test. |
| basics/…/create.rs | Shows native account creation with the page-visit seed set and PDA signer seeds. |
| basics/…/increment.rs | Shows native state updates after the PDA account exists. |
| basics/…/page_visits.rs | Defines the native record, seed prefix, stored bump, and increment behavior. |
| basics/…/lib.rs | Shows a user-scoped PDA that stores an application record and can be initialized on first use. |
| basics/…/create_pda.rs | Shows explicit PDA comparison before account creation. |
| basics/…/get_pda.rs | Shows PDA validation before reading address-driven state. |
| basics/…/create_new_account.rs | Shows a PDA authorizing a System Program call to fund new account creation and notes the need for caller authorization. |
| basics/…/init_rent_vault.rs | Shows the stable rent-vault address and its bump during setup. |