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

Add comments to account_data_matching programs #39

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
4 changes: 4 additions & 0 deletions programs/1-account-data-matching/insecure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ pub mod account_data_matching_insecure {
use super::*;

pub fn log_message(ctx: Context<LogMessage>) -> ProgramResult {
// Directly unpacking token account data without validating ownership
let token = SplTokenAccount::unpack(&ctx.accounts.token.data.borrow())?;
// Logging the account balance
msg!("Your account balance is: {}", token.amount);
Ok(())
}
}

#[derive(Accounts)]
pub struct LogMessage<'info> {
// This account is assumed to be a valid token account without verification
token: AccountInfo<'info>,
// This signer is not validated against the token account owner
authority: Signer<'info>,
}
3 changes: 3 additions & 0 deletions programs/1-account-data-matching/recommended/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ pub mod account_data_matching_recommended {
use super::*;

pub fn log_message(ctx: Context<LogMessage>) -> ProgramResult {
// Logging the account balance directly from the validated token account
msg!("Your account balance is: {}", ctx.accounts.token.amount);
Ok(())
}
}

#[derive(Accounts)]
pub struct LogMessage<'info> {
// Validating that the authority is the owner of the token account
#[account(constraint = authority.key == &token.owner)]
token: Account<'info, TokenAccount>,
// Authority must sign the transaction and own the token account
authority: Signer<'info>,
}
7 changes: 6 additions & 1 deletion programs/1-account-data-matching/secure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ pub mod account_data_matching_secure {
use super::*;

pub fn log_message(ctx: Context<LogMessage>) -> ProgramResult {
// Unpacking token account data
let token = SplTokenAccount::unpack(&ctx.accounts.token.data.borrow())?;
// Explicitly checking that the authority is the owner of the token account
if ctx.accounts.authority.key != &token.owner {
return Err(ProgramError::InvalidAccountData);
}
msg!("Your acocunt balance is: {}", token.amount);
// Logging the account balance if ownership is verified
msg!("Your account balance is: {}", token.amount);
Ok(())
}
}

#[derive(Accounts)]
pub struct LogMessage<'info> {
// Token account data needs to be carefully handled and ownership verified
token: AccountInfo<'info>,
// This signer is required to be the owner of the token account
authority: Signer<'info>,
}