I'm passing two accounts to my program: a payer account and a PDA.
The PDA should be derived from the payer address.
How do I verify it inside the program? Without Anchor.
you can generate a pda from the program id and any &[&[u8]]
using Pubkey::find_program_address
. here is an example from the spl single pool program:
fn find_pool_stake_authority_address_and_bump(
program_id: &Pubkey,
pool_address: &Pubkey,
) -> (Pubkey, u8) {
Pubkey::find_program_address(
&[POOL_STAKE_AUTHORITY_PREFIX, pool_address.as_ref()],
program_id,
)
}
prefix is a static string, used for namespacing different kinds of pdas. since the user also passes in the pda as an account, you must check that the generated pda address matches the address they give you, and return an error if it doesnt
also, assuming payer is meant to own the derived account, you should enforce in your program that it is a signer
You could add a field authority to the account data and initialize it with the payer's address.
Below is pseudo-code for illustration purposes
account.authority = payer
You could then do an initiation check on the account after which an address check will ascertain that the authority field matches payer address calling the ix.