交易模拟失败:错误处理指令0:未经授权签名或可写帐户的交叉程序调用

发布于 2025-02-12 11:26:08 字数 3402 浏览 1 评论 0原文

在添加新博客时,我会遇到此错误:错误:无法发送事务:交易模拟失败:错误处理指令0:带有未经授权的签名者或可写的帐户

客户端代码的

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;

交叉编程调用这是我的链码,该代码写在锚点生锈。 https://book.andor-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,
}

请帮助我是Solana的新手。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心病无药医 2025-02-19 11:26:08

我错误地使用授权作为种子
seeds = [b“ blog” .AS_REF(),pertional.key()。as_ref()]

正确种子
seeds = [b“ blog” .AS_REF(),rando_key.key()。as_ref()]

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()]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文