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 4-initialization program #42

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
12 changes: 8 additions & 4 deletions programs/4-initialization/insecure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,34 @@ pub mod initialization_insecure {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
// Deserialize the user account data
let mut user = User::try_from_slice(&ctx.accounts.user.data.borrow()).unwrap();

// Set the authority of the user
user.authority = ctx.accounts.authority.key();

// Re-serialize the user data back into the account
let mut storage = ctx.accounts.user.try_borrow_mut_data()?;
user.serialize(storage.deref_mut()).unwrap();
Ok(())
}
}

/*
- reinitialize
- create and dont initialize
- passing previously initialzed accounts from other programs
(e.g. token program => need to check delegate and authority)
- reinitialize: The program can reinitialize the account, which may be a security risk.
- create and don't initialize: The account could be created without being initialized.
- passing previously initialized accounts from other programs (e.g., token program => need to check delegate and authority).
*/

#[derive(Accounts)]
pub struct Initialize<'info> {
// Unchecked user account, vulnerable to attacks like re-initialization
user: AccountInfo<'info>,
authority: Signer<'info>,
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct User {
// Authority key of the user account
authority: Pubkey,
}
8 changes: 7 additions & 1 deletion programs/4-initialization/recommended/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ pub mod reinitialization_4 {
use super::*;

pub fn init(_ctx: Context<Init>) -> ProgramResult {
// Placeholder message for program initialization
msg!("GM");
Ok(())
}
}


#[derive(Accounts)]
pub struct Init<'info> {
// Initializes a new user account, allocating 8 bytes for the discriminator and 32 bytes for the Pubkey
#[account(init, payer = authority, space = 8+32)]
user: Account<'info, User>,

// Authority is the payer of the transaction and must sign
#[account(mut)]
authority: Signer<'info>,

// System program for allocating the account
system_program: Program<'info, System>,
}

#[account]
pub struct User {
// Authority key of the user account
authority: Pubkey,
}
9 changes: 9 additions & 0 deletions programs/4-initialization/secure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@ pub mod reinitialization_secure_recommended {
use super::*;

pub fn initialize(ctx: Context<Initialize>) -> ProgramResult {
// Deserialize the user account data
let mut user = User::try_from_slice(&ctx.accounts.user.data.borrow()).unwrap();

// Check if the account has been initialized by verifying the discriminator
if !user.discriminator {
return Err(ProgramError::InvalidAccountData);
}

// Set the authority and mark the account as initialized by setting discriminator to true
user.authority = ctx.accounts.authority.key();
user.discriminator = true;

// Re-serialize the user data back into the account
let mut storage = ctx.accounts.user.try_borrow_mut_data()?;
user.serialize(storage.deref_mut()).unwrap();

// Log a message indicating successful initialization
msg!("GM");
Ok(())
}
}

#[derive(Accounts)]
pub struct Initialize<'info> {
// User account with manual data checks for security
user: AccountInfo<'info>,
authority: Signer<'info>,
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct User {
// Discriminator to ensure account has been initialized correctly
discriminator: bool,
// Authority key of the user account
authority: Pubkey,
}