coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.55k stars 1.31k forks source link

Order of arguments causes the PDA creation to fail with signer issue #1944

Open yourarj opened 2 years ago

yourarj commented 2 years ago

Creating PDA with one of seed as method argument. When the order of the arguments provided is reversed creation works fine but the other way around don't work.

#[derive(Accounts)]
#[instruction(seed_number: u64)]
pub struct Initialize<'info> {
    #[account(
        init,
        payer=creator,
        space=1000,
        seeds = [
            b"data".as_ref(),
            creator.key.as_ref(),
            seed_number.to_le_bytes().as_ref()
        ],
        bump
    )]
    pub data_holder: Account<'info, DataHolder>,
    #[account(mut)]
    pub creator: Signer<'info>,
    pub system_program: Program<'info, System>,
}

and the method signature is

    pub fn initialize(
        ctx: Context<Initialize>,
        data_owner: Pubkey,
        seed_number: u64,
    ) -> Result<()> {
        ctx.accounts.data_holder.seed_number = seed_number;
        ctx.accounts.data_holder.data_owner = data_owner;
        Ok(())
    }

causes

Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account

but when parameter order is swapped of data_owner and seed_number. everything works fine.

reproduced with program here in repo https://github.com/yourarj/anchor-pda-account-creation-issue

0xdeepmehta commented 2 years ago

Order of method args and instruction macro args should be the same.

for an eg. #[instruction(arg1: u64)]


    pub fn initialize(
        ctx: Context<Initialize>,
        arg1: u64,
        arg2: Pubkey,
    ) -> Result<()> {.....}
yourarj commented 2 years ago

the issue is yes it is. @0xdeepmehta please have a look at the referenced repo where I've reproduced the issue.

0xdeepmehta commented 2 years ago

This is feature, not a bug 🐛.

yourarj commented 2 years ago

I agree with point but the is reproducible when order is maintained.

Can you also please point me to the relevant docs also?