Overview
Theanchor keys command provides subcommands for managing program keypairs, including listing all program keys and syncing declare_id! with actual keypair values.
Subcommands
list- List all program keys in the workspacesync- Sync programdeclare_id!with actual keypair pubkeys
anchor keys list
List all program keypairs and their public keys in the workspace.Syntax
Description
This command scans the workspace for program keypairs (typically intarget/deploy/) and displays:
- Program name
- Program ID (public key)
- Keypair file path
Example
target/deploy/.
Use Cases
- Verify program IDs before deployment
- Check which programs exist in the workspace
- Copy program IDs for configuration files
- Verify keypair files are present
anchor keys sync
Sync programdeclare_id! pubkeys with the actual pubkeys from program keypair files.
Syntax
Options
Only sync the specified program instead of all programs
Description
This command:- Reads the program ID from the keypair file in
target/deploy/ - Updates the
declare_id!macro in the program’slib.rsto match - Ensures consistency between keypairs and source code
- You’ve generated a new program keypair
- You’ve copied a program from another workspace
- The
declare_id!is out of sync with the keypair
Examples
Sync All Programs
Sync Specific Program
Before and After
Before sync (programs/my_program/src/lib.rs):
Program Keypairs
Location
Program keypairs are stored in:Format
Keypair files are JSON arrays containing the secret key bytes:Generation
Program keypairs are automatically generated during:anchor init- Creates keypair for initial programanchor new- Creates keypair for new program- First build - Creates keypair if missing
Common Workflows
Creating a New Program
Using Existing Keypair
Updating Program Configuration
After syncing, updateAnchor.toml:
Build Integration
By default,anchor build checks for program ID mismatches:
Notes
The
declare_id! macro is used at runtime to verify the program is executing under the correct program ID. Keeping it in sync with the keypair is essential.For production deployments, store program keypairs securely and never commit them to version control. Add
target/ to .gitignore.See Also
- anchor build - Building programs (includes key verification)
- anchor deploy - Deploying programs
- solana-keygen - Solana CLI keypair generation