简体   繁体   English

将 Solana 数据帐户设为只读

[英]Make a Solana Data Account read only

I'm writing a program in Solana, I have to save data on the chain but make the data read only.我正在 Solana 中编写程序,我必须将数据保存在链上,但使数据只读。

Right now I'm using anchor and I've wrote this:现在我正在使用锚,我写了这个:

    pub fn save_data(ctx: Context<SaveData>, content: String, actor: String) -> Result<()> {
        let service_account = &mut ctx.accounts.service_account;
        let data_account = &mut ctx.accounts.data_account;
        let company_account = &mut ctx.accounts.company_account;
        let authority = &mut ctx.accounts.authority;

        data_account.content = content;
        data_account.actor = actor;
        data_account.company = company_account.key();
        data_account.authority = authority.key();
        data_account.pre_data_key = service_account.current_data_key;

        service_account.current_data_key = data_account.key();

        Ok(())
    }

#[derive(Accounts)]
pub struct SaveData<'info> {
    #[account(init, payer = authority, space = 8 + 50 + 32 + 32 + 32 + 32 )]
    pub data_account: Account<'info, DataState>,

    #[account(mut, has_one = authority)]
    pub company_account: Account<'info, CompanyState>,

    #[account(mut)]
    pub service_account: Account<'info, ServiceState>,

    #[account(mut)]
    pub authority: Signer<'info>,
    pub system_program: Program<'info, System>,
}

#[account]
pub struct DataState {
    content: String,
    actor: String,
    company: Pubkey,
    pub pre_data_key: Pubkey,
    pub authority: Pubkey,
}

I didn't implement any update or delete method but I would like to know if it would be possible to make the data content just read only.我没有实现任何更新或删除方法,但我想知道是否可以使数据content只读。

Also, right now, if the creator of the dataAccount wants to update the data can update it outside of my smart contract?另外,现在,如果 dataAccount 的创建者想要更新数据,可以在我的智能合约之外更新它吗?

Thanks谢谢

该帐户不能在您的合同之外更新,因为它是一个 PDA(程序派生帐户),并且只有您的程序可以通过您编写的指令对其进行修改,如果您想让它只读,您必须以这样的方式编写程序它不会改变帐户的数据(只是不做一个这样做的功能)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM