Suppose I use the function close, which closes an arbitrary account,
pub fn close(ctx: Context<Close>) -> ProgramResult {
let dest_starting_lamports = ctx.accounts.destination.lamports();
let account = ctx.accounts.account.to_account_info();
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();
**account.lamports.borrow_mut() = 0;
let mut data = account.try_borrow_mut_data()?;
for byte in data.deref_mut().iter_mut() {
*byte = 0;
}
let dst: &mut [u8] = &mut data;
let mut cursor = Cursor::new(dst);
cursor.write_all(&CLOSED_ACCOUNT_DISCRIMINATOR).unwrap();
Ok(())
}
}
Where Close is a struct of addresses containing the address to close and the rent destination. What prevents me from just inputing any account, wouldn't the program be able to close it?
My guess is that any account that is passed on to the function must be owned by this program in order for it to close it is that so? However I fail to see how the code thats this into account
Finally, if this is the case how can a user give a program the right to burn this account the user owns?