I am getting this error while adding a new blog: Error: Failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Cross-program invocation with unauthorized signer or writable account
Client Code
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { getProgramInstance } from "../../utils";
import { SOLANA_HOST } from "../../utils/const";
import * as anchor from "@project-serum/anchor";
const utf8 = anchor.utils.bytes.utf8;
const { BN, web3 } = anchor;
const { SystemProgram } = web3;
const defaultAccounts = {
tokenProgram: TOKEN_PROGRAM_ID,
clock: anchor.web3.SYSVAR_CLOCK_PUBKEY,
systemProgram: SystemProgram.programId,
};
const useBlog = (
wallet: any,
title?: string,
content?: string,
imageUrl?: string
) => {
const connection = new anchor.web3.Connection(SOLANA_HOST);
const program = getProgramInstance(connection, wallet);
const getBlogs = async () => {
console.log("fetching");
const blogs = await program.account.blogAccount.all();
console.log(blogs);
return blogs;
};
const addBlogs = async () => {
if (!title || !content || !imageUrl) return alert("Please fill all fields");
const randomkey = anchor.web3.Keypair.generate().publicKey;
let [blog_pda] = await anchor.web3.PublicKey.findProgramAddress(
[utf8.encode("blog"), randomkey.toBuffer()],
program.programId
);
const tx = await program.rpc.storeBlog(title, content, imageUrl, {
accounts: {
blog: blog_pda,
randomKey: randomkey,
authority: wallet.publicKey,
...defaultAccounts,
},
});
console.log(tx);
return true;
};
return { getBlogs, addBlogs };
};
export default useBlog;
This is my On-Chain Code which is written in anchor rust. https://book.anchor-lang.com/
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Token};
use anchor_lang::solana_program::entrypoint::ProgramResult;
use std::mem::size_of;
declare_id!("1111111111111111111111111111111111111111111");
const TITLE_LENGHT: usize = 255;
const CONTENT_LENGTH: usize = 500;
const IMAGE_LENGTH: usize = 255;
#[program]
mod d_blog {
use super::*;
pub fn store_blog(
ctx: Context<StoreBlog>,
title: String,
content: String,
image_url: String,
) -> ProgramResult {
let blog = &mut ctx.accounts.blog;
blog.authority = ctx.accounts.authority.key();
blog.title = title;
blog.content = content;
blog.image_url = image_url;
Ok(())
}
}
// store blog context
#[derive(Accounts)]
pub struct StoreBlog<'info> {
#[account(
init, mut, seeds = [b"blog".as_ref(), authority.key().as_ref()],
bump, payer = authority, space = size_of::<BlogAccount>() + TITLE_LENGHT + CONTENT_LENGTH + IMAGE_LENGTH + 8,
)]
pub blog: Account<'info, BlogAccount>,
#[account(mut)]
pub random_key: AccountInfo<'info>,
#[account(mut)]
pub authority: Signer<'info>,
pub system_program: UncheckedAccount<'info>,
#[account(constraint = token_program.key == &token::ID)]
pub token_program: Program<'info, Token>,
pub clock: Sysvar<'info, Clock>,
}
#[account]
pub struct BlogAccount {
pub authority: Pubkey,
pub title: String,
pub content: String,
pub image_url: String,
}
Please help I am new at Solana. Thanks
By mistake i use authority for seeds seeds = [b"blog".as_ref(), authority.key().as_ref()]
Correct seed seeds = [b"blog".as_ref(), random_key.key().as_ref()]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.