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 insecure, recommended, and secure examples #41

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
16 changes: 12 additions & 4 deletions programs/3-type-cosplay/insecure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};

// Declare the program ID
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

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

pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {
// Attempt to deserialize the user account data
let user = User::try_from_slice(&ctx.accounts.user.data.borrow()).unwrap();

// Check if the owner of the account is the program itself
if ctx.accounts.user.owner != ctx.program_id {
return Err(ProgramError::IllegalOwner);
}

// Check if the authority matches the one stored in the user account
if user.authority != ctx.accounts.authority.key() {
return Err(ProgramError::InvalidAccountData);
}

// Log a message for debugging purposes
msg!("GM {}", user.authority);
Ok(())
}
}

#[derive(Accounts)]
pub struct UpdateUser<'info> {
user: AccountInfo<'info>,
authority: Signer<'info>,
user: AccountInfo<'info>, // User account (AccountInfo used instead of Account<User>)
authority: Signer<'info>, // The signer who is expected to be the authority
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct User {
authority: Pubkey,
authority: Pubkey, // Public key of the authority who can update the user
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct Metadata {
account: Pubkey,
account: Pubkey, // Metadata account holding a public key
}
12 changes: 7 additions & 5 deletions programs/3-type-cosplay/recommended/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};

// Declare the program ID
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

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

pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {
// Log a message for debugging purposes
msg!("GM {}", ctx.accounts.user.authority);
Ok(())
}
}

#[derive(Accounts)]
pub struct UpdateUser<'info> {
#[account(has_one = authority)]
user: Account<'info, User>,
authority: Signer<'info>,
#[account(has_one = authority)] // Ensures that the user account's authority matches the provided signer
user: Account<'info, User>, // User account with a strict type check
authority: Signer<'info>, // The signer who is expected to be the authority
}

#[account]
pub struct User {
authority: Pubkey,
authority: Pubkey, // Public key of the authority who can update the user
}

#[account]
pub struct Metadata {
account: Pubkey,
account: Pubkey, // Metadata account holding a public key
}
26 changes: 18 additions & 8 deletions programs/3-type-cosplay/secure/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,58 @@
use anchor_lang::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};

// Declare the program ID
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

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

pub fn update_user(ctx: Context<UpdateUser>) -> ProgramResult {
// Attempt to deserialize the user account data
let user = User::try_from_slice(&ctx.accounts.user.data.borrow()).unwrap();

// Check if the owner of the account is the program itself
if ctx.accounts.user.owner != ctx.program_id {
return Err(ProgramError::IllegalOwner);
}

// Check if the authority matches the one stored in the user account
if user.authority != ctx.accounts.authority.key() {
return Err(ProgramError::InvalidAccountData);
}

// Check if the account has the correct discriminant to prevent type confusion
if user.discriminant != AccountDiscriminant::User {
return Err(ProgramError::InvalidAccountData);
}

// Log a message for debugging purposes
msg!("GM {}", user.authority);
Ok(())
}
}

#[derive(Accounts)]
pub struct UpdateUser<'info> {
user: AccountInfo<'info>,
authority: Signer<'info>,
user: AccountInfo<'info>, // User account (AccountInfo used instead of Account<User>)
authority: Signer<'info>, // The signer who is expected to be the authority
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct User {
discriminant: AccountDiscriminant,
authority: Pubkey,
discriminant: AccountDiscriminant, // Discriminant to distinguish between different account types
authority: Pubkey, // Public key of the authority who can update the user
}

#[derive(BorshSerialize, BorshDeserialize)]
pub struct Metadata {
discriminant: AccountDiscriminant,
account: Pubkey,
discriminant: AccountDiscriminant, // Discriminant to distinguish between different account types
account: Pubkey, // Metadata account holding a public key
}

#[derive(BorshSerialize, BorshDeserialize, PartialEq)]
pub enum AccountDiscriminant {
User,
Metadata,
User, // Discriminant value for User accounts
Metadata, // Discriminant value for Metadata accounts
}