alessandro buffoli
alessandro buffoli

Reputation: 701

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.

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.

Also, right now, if the creator of the dataAccount wants to update the data can update it outside of my smart contract?

Thanks

Upvotes: 0

Views: 443

Answers (1)

Anoushk
Anoushk

Reputation: 659

The account cant be updated outside of your contract since its a PDA(program derived account) and only your program can modify it through the instructions youve written, If you want to make it read only you have to write the program in such a way that it doesnt change the data of the account(just dont make a function that does that)

Upvotes: 2

Related Questions