1

Suppose I have a PDA that is supposed to be initialized by calling a CPI in my program and I want to validate that the account is valid, is there a way I could add a check to make sure that the program address passed is similar to the PDA which would be derived from the given seeds


#[derive(Accounts)]
pub struct 
CreateBid<'info> {
    #[account(
        mut
    )]
    pub signer: Signer<'info>,

    /// CHECK: initialized by another program through the CPI
    #[account(
        mut,

    )]
    pub pda_to_initialize_through_cpi: AccountInfo<'info>,
}

Is there a way I could add the find_program_address method using the address constraint to make sure that it is the address I expected?

 /// CHECK: initialized by another program through the CPI
    #[account(
        mut,
        address = find_program_address(seeds_as_slice).0
    )]
    pub pda_to_initialize_through_cpi: AccountInfo<'info>,

1 Answer 1

3

You can use the seeds and bump constraints, and then also specify the program that the pda should be derived from.

For example:

    #[account(
        seeds = [b"some_seed"],
        bump,
        seeds::program = other_program.key()
    )]
    pub pda: AccountInfo<'info>,
    pub other_program: Program<'info, OtherProgram>

Here's a specific example for the metaplex metadata account:

    #[account(
        mut,
        seeds = [
            b"metadata",
            metadata_program.key().key().as_ref(),
            token_mint.key().as_ref(),
        ],
        bump,
        seeds::program = metadata_program.key()
    )]
    pub metadata_account: AccountInfo<'info>,
    pub metadata_program: Program<'info, Metadata>,
2
  • How can I get the bump?
    – Jimii
    Commented Dec 5, 2023 at 12:43
  • When you just use the bump constraint, anchor will automatically derive the pda and return the bump. If the bump is stored on another account, then you can use bump = <some_bump> as the constraint and save some compute units on the transaction
    – john
    Commented Dec 5, 2023 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.