Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added necessary comments to insecure, recommended, and secure closing account programs #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions programs/9-closing-accounts/insecure-still-still/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
use anchor_lang::prelude::*;
use std::io::Write;
use std::ops::DerefMut;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

#[program]
pub mod closing_accounts_insecure_still_still {
pub mod closing_accounts_insecure_still {
use super::*;

pub fn close(ctx: Context<Close>) -> ProgramResult {
let account = ctx.accounts.account.to_account_info();

let dest_starting_lamports = ctx.accounts.destination.lamports();

// Transfer all lamports from the account being closed to the destination.
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();

// Set the lamports of the account being closed to zero.
**account.lamports.borrow_mut() = 0;

// Zero out the account's data.
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 = std::io::Cursor::new(dst);
cursor
.write_all(&anchor_lang::__private::CLOSED_ACCOUNT_DISCRIMINATOR)
.unwrap();

Ok(())
}
}

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(zero)] // Ensure the account has zeroed data before use.
account: Account<'info, Data>,
authority: Signer<'info>,
}

#[derive(Accounts)]
pub struct Close<'info> {
account: Account<'info, Data>,
Expand Down
6 changes: 5 additions & 1 deletion programs/9-closing-accounts/insecure-still/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ pub mod closing_accounts_insecure_still {

let dest_starting_lamports = ctx.accounts.destination.lamports();

// Transfer all lamports from the account being closed to the destination.
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();

// Set the lamports of the account being closed to zero.
**account.lamports.borrow_mut() = 0;

// Zero out the account's data.
let mut data = account.try_borrow_mut_data()?;
for byte in data.deref_mut().iter_mut() {
*byte = 0;
Expand All @@ -28,7 +32,7 @@ pub mod closing_accounts_insecure_still {

#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(zero)]
#[account(zero)] // Ensure the account has zeroed data before use.
account: Account<'info, Data>,
authority: Signer<'info>,
}
Expand Down
6 changes: 5 additions & 1 deletion programs/9-closing-accounts/insecure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ pub mod closing_accounts_insecure {
use super::*;

pub fn close(ctx: Context<Close>) -> ProgramResult {
// Get the current balance of the destination account
let dest_starting_lamports = ctx.accounts.destination.lamports();

// Transfer all lamports from the account being closed to the destination account
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(ctx.accounts.account.to_account_info().lamports())
.unwrap();

// Set the balance of the closed account to zero
**ctx.accounts.account.to_account_info().lamports.borrow_mut() = 0;

Ok(())
Expand All @@ -21,7 +25,7 @@ pub mod closing_accounts_insecure {
#[derive(Accounts)]
pub struct Close<'info> {
account: Account<'info, Data>,
destination: AccountInfo<'info>,
destination: AccountInfo<'info>, // The account to receive the lamports
}

#[account]
Expand Down
3 changes: 2 additions & 1 deletion programs/9-closing-accounts/recommended/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ pub mod closing_accounts_recommended {

#[derive(Accounts)]
pub struct Close<'info> {
// Automatically transfer lamports from the account to the destination and close the account
#[account(mut, close = destination)]
account: Account<'info, Data>,
#[account(mut)]
destination: AccountInfo<'info>,
destination: AccountInfo<'info>, // Receiver of the remaining lamports
}

#[account]
Expand Down
13 changes: 9 additions & 4 deletions programs/9-closing-accounts/secure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ pub mod closing_accounts_secure {
let dest_starting_lamports = ctx.accounts.destination.lamports();

let account = ctx.accounts.account.to_account_info();
// Transfer lamports from the account being closed to the destination account
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();
**account.lamports.borrow_mut() = 0;

// Clear the account data
let mut data = account.try_borrow_mut_data()?;
for byte in data.deref_mut().iter_mut() {
*byte = 0;
}

// Write CLOSED_ACCOUNT_DISCRIMINATOR to mark the account as closed
let dst: &mut [u8] = &mut data;
let mut cursor = Cursor::new(dst);
cursor.write_all(&CLOSED_ACCOUNT_DISCRIMINATOR).unwrap();
Expand All @@ -36,6 +39,7 @@ pub mod closing_accounts_secure {
let data = account.try_borrow_data()?;
assert!(data.len() > 8);

// Check if the account is already closed by inspecting the discriminator
let mut discriminator = [0u8; 8];
discriminator.copy_from_slice(&data[0..8]);
if discriminator != CLOSED_ACCOUNT_DISCRIMINATOR {
Expand All @@ -44,6 +48,7 @@ pub mod closing_accounts_secure {

let dest_starting_lamports = ctx.accounts.destination.lamports();

// Transfer remaining lamports from the account to the destination
**ctx.accounts.destination.lamports.borrow_mut() = dest_starting_lamports
.checked_add(account.lamports())
.unwrap();
Expand All @@ -55,14 +60,14 @@ pub mod closing_accounts_secure {

#[derive(Accounts)]
pub struct Close<'info> {
account: Account<'info, Data>,
destination: AccountInfo<'info>,
account: Account<'info, Data>, // The account to be closed
destination: AccountInfo<'info>, // The account that receives the lamports
}

#[derive(Accounts)]
pub struct ForceDefund<'info> {
account: AccountInfo<'info>,
destination: AccountInfo<'info>,
account: AccountInfo<'info>, // The already-closed account
destination: AccountInfo<'info>, // The account receiving any remaining lamports
}

#[account]
Expand Down