I'm kinda confused on how ownership and signing works in Solana/Anchor, like if I create a user like this:
#[derive(Accounts)]
pub struct InitializeUser<'info> {
#[account(
init,
payer = signer,
space = 8 + UserAccount::INIT_SPACE,
seeds = [b"USER", signer.key().as_ref()],
bump
)]
pub user: Account<'info, UserAccount>,
#[account(mut)]
pub signer: Signer<'info>,
pub system_program: Program<'info, System>,
}
and the call to create it from the client:
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace.myProgram as anchor.Program<MyProgram>;
const userKp = anchor.web3.Keypair.generate();
const userPubkey = userKp.publicKey;
const _tx = await program.methods
.initializeUser(some_data)
.accounts({
signer: userPubkey,
})
.signers([userKp])
.rpc();
At this point I'm specifying that the signer of the transaction is some wallet, but not the program itself, so who is the owner (or can there be multiple)? And who can change the data of the UserAccount
just created, is it just the owner that can do it?
Like the following Account restricts that the signer of the transaction will be able to change only the PDA derived from its address because it's how we are calculating it:
#[derive(Accounts)]
pub struct UpdateUser<'info> {
#[account(
mut,
seeds = [b"USER", signer.key().as_ref()],
bump
)]
pub user: Account<'info, UserAccount>,
#[account(mut)]
pub signer: Signer<'info>
}
How can I change this so that the wallet that created the account or the program that the account was derived from can change its data? For example, in this case if I wanted to modify another user's account I'd have to issue a request for their signature, but I'd like a way to select who can change the data, without needing to require permission.