Including the newAccount
Keypair as signer in the initialize
instruction using the anchor format below results in a WalletSendTransactionError: Signature verification failed
after approving from connected wallet.
const newAccount = Keypair.generate()
const transaction = await program.methods
.initialize()
.accounts({
counter: newAccount.publicKey,
user: wallet.publicKey,
systemAccount: SystemProgram.programId,
})
.signers([newAccount]) <---- including signature here not working
.transaction()
sendTransaction(transaction, connection)
It works fine if I include newAccount
as signer in SendTransaction
:
const transaction = await program.methods
.initialize()
.accounts({
counter: newAccount.publicKey,
user: wallet.publicKey,
systemAccount: SystemProgram.programId,
})
.transaction()
sendTransaction(transaction, connection, { signers: [newAccount] }) <--- works like this
Could someone explain why the first approach does not work? I thought .signers[( )]
included the signature in the transaction.
Using .rpc
also works with no errors:
const transaction = await program.methods
.initialize()
.accounts({
counter: newAccount.publicKey,
user: wallet.publicKey,
systemAccount: anchor.web3.SystemProgram.programId,
})
.signers([newAccount]) <---- works with .rpc()
.rpc()
Below is the initialize
instruction:
#[program]
pub mod counter {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
let counter = &mut ctx.accounts.counter;
counter.count = 0;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub counter: Account<'info, Counter>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct Counter {
pub count: u64,
}
initialize
instruction?transaction.signatures.map(s => s.publicKey.toBase58())
in the first case? Also just to sanity check, does the first example work if you do.rpc()
at the end instead of.transaction()
?initialize
instruction. Using.signers([ ])
works correctly with.rpc
. Although didn't get any signatures when I tried to console.logtransaction.signatures.map(s => s.publicKey.toBase58())
using.transaction
with.signers([newAccount])
. Logged[]