Search code examples
solanasolana-web3jsanchor-solana

Can I set solana account field to private?


I have this following struct defined in my Solana Anchor program:

#[account]
pub struct TestAccount1 {
    pub data: u64,
    authority: Pubkey,
}

The authority field is private by default since I didn't specify pub, and I expect that only the program can access this field.

However, I can still fetch this info directly from getAccountInfo RPC method, and successfully decode it.

So just wondering are there any differences between using public and private fields in Solana account? or they actually behaves the same?

Thanks


Solution

  • All data on the blockchain is public, so using private variables makes no difference.

    Either way, only your program can make changes to its accounts, so it's completely safe to make all fields public.

    In fact, public fields helps integrations with other applications because they can deserialize your accounts more easily, so it's recommended to make all account fields public in your struct.