1

I'm trying to code a staking program for tokens. My staking works fine, but when I try to call unstake, I get error : "Cross-program invocation with unauthorized signer or writable account."

My program mints tokens. The admin has mint authorization for the token. What should I do so the program has mint authorization ? I can't transfer authorization to a program ID.

Thanks

2 Answers 2

2

You can't transfer the authorization to a program ID, but you can transfer it to a PDA. Find a PDA with appropriate seeds for your situation. Include the seeds and bump in the signer_seeds array of an invoke_signed instruction within your program.

It would be something like this.

let (key, bump) = Pubkey::find_program_address(
    &[b"mint-authority"], &program_id,
);
invoke_signed(
    ...
    &[&[b"mint-authority", &[bump]]],
)?;

Using PDAs like this is how a program can sign CPI instructions.

1

If I get it correctly, you're trying to unstake tokens from an account right? In that case, the mint authority has nothing to do with it..

If you're staking the tokens in a PDA, you need to create a CPI context with signer so that your program can sign on behalf of that PDA and transfer the tokens out of the "vault". The following example assumes you're using anchor

 let cpi_ctx = CpiContext::new_with_signer(cpi_program, cpi_accounts, signer_seeds);

"cpi_program" refers to the the target program of your CPI

"cpi_accounts" refers to the accounts required by the context of your target program

"signer_seeds" refers to the seeds / bump used to derive your PDA

If you're not using a PDA to store the staked tokens, the keypair auth of that account needs to sign the transaction

Still answering your minth authorization question, and as Fuzzy Yeti said, you can create a PDA and assign that as mint authority of your mint account. By doing that, your program will be able to sign on behalf of that mint authority and mint tokens

2
  • Indeed, you're right. Could you elaborate on the signer_seeds please, how to define it from my PDA? Thanks and happy new year. Commented Jan 1 at 3:36
  • Something like this: let seeds = &[&b"mint_authority"[..], &[bump]]; let signer_seeds = &[&seeds[..]];. Happy new year!
    – ASCorreia
    Commented Jan 1 at 15:01

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.