2

I'm getting This error

Is initialized!:
     Error: Signature verification failed
      at Transaction.serialize (node_modules/@solana/web3.js/src/transaction.ts:736:13)
      at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:145:22)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at Object.rpc [as initialize] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)

with this tests

describe("testContract", () => {
  // Configure the client to use the local cluster.
  anchor.setProvider(anchor.AnchorProvider.env());

  const program = anchor.workspace.TestContract as Program<TestContract>;

  it("Is initialized!", async () => {
    // Add your test here.
    const key = new PublicKey("U4NHM8DNT3kCNrRtB9ymgt1mcR6RBaHwUHWLoxM4KTF")
     const tx = await program.methods.initialize().accounts( {
      sender : key
    }).rpc();
    console.log("Your transaction signature", tx);
  });
});

And the contract is correct and was working before I added an account field to it

#[program]
pub mod test_contract {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        let hi = &ctx.accounts.sender.to_account_info().key();
        msg!("{}",hi);
        Ok(())
    }
}

#[derive(Accounts)]
pub struct Initialize<'info> {
    ///CHECK : Not dangerous
    #[account(mut, signer)]
    pub sender: AccountInfo<'info>,
}

3 Answers 3

3

I don't know much about anchor, but you appear to be passing a PublicKey where a Keypair is needed. Public keys cannot produce signatures, the corresponding private key is needed for that

3
  • Not passsing kepair anywhere, just passing sender as a account Aug 2, 2022 at 18:41
  • which is precisely what i said is the problem
    – trent.sol
    Aug 2, 2022 at 21:47
  • 1
    trent is correct-- you need a keypair to sign. in anchor test you need to add .signer([keypair]) to your initialize method. i created an example for you on solpg: beta.solpg.io/63f777d067edfe0f001069d1
    – AMilz
    Feb 23 at 14:27
2

Try using your local keypair that you've got configured with Anchor to test:

const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const wallet = provider.wallet as anchor.Wallet;

const tx = await program.methods.initialize()
    .accounts( {
        sender : wallet.publicKey
    })
    .signers([wallet.payer])
    .rpc();
0

On the front end, anchor will sign as the provider wallet automatically when you used .rpc() or provider.sendAndConfirm(). If you want other signers (in this case the PublicKey you made) you will need to add them as well. You'll need to make a Keypair instead:

    const keypair = Keypair.generate();
     const tx = await program.methods.initialize().accounts( {
      sender : keypair.publicKey
     })
    .signers([keypair])
   .rpc();

or remove the requirement for the key to be a signer on the Rust side, since there is no reason for that in your example. Sidenote: it's more common to user the Signer type when specifying a signer: pub sender: Signer<'info>,

Your Answer

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

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