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 comments to 7-bump-seed-canonicalization programs (insecure, recommended, secure) #45

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
8 changes: 6 additions & 2 deletions programs/7-bump-seed-canonicalization/insecure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ pub mod bump_seed_canonicalization_insecure {
use super::*;

pub fn set_value(ctx: Context<BumpSeed>, key: u64, new_value: u64, bump: u8) -> ProgramResult {
// Derive the PDA using the provided bump seed
let address =
Pubkey::create_program_address(&[key.to_le_bytes().as_ref(), &[bump]], ctx.program_id)?;

// Ensure the derived address matches the account key
if address != ctx.accounts.data.key() {
return Err(ProgramError::InvalidArgument);
}

// Update the value stored in the account
ctx.accounts.data.value = new_value;

Ok(())
Expand All @@ -21,10 +25,10 @@ pub mod bump_seed_canonicalization_insecure {

#[derive(Accounts)]
pub struct BumpSeed<'info> {
data: Account<'info, Data>,
data: Account<'info, Data>, // The account where the data will be stored
}

#[account]
pub struct Data {
value: u64,
value: u64, // The value stored in the account
}
16 changes: 4 additions & 12 deletions programs/7-bump-seed-canonicalization/recommended/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod bump_seed_canonicalization_recommended {
use super::*;

pub fn set_value(ctx: Context<BumpSeed>, key: u64, new_value: u64) -> ProgramResult {
// Simply update the value stored in the account, using PDA in a safe manner
ctx.accounts.data.value = new_value;
Ok(())
}
Expand All @@ -15,21 +16,12 @@ pub mod bump_seed_canonicalization_recommended {
#[derive(Accounts)]
#[instruction(key: u64)]
pub struct BumpSeed<'info> {
// Note a subtle pattern that is not displayed here.
//
// Usually, the usage of PDAs is broken into two parts:
//
// 1) allocation via `#[account(init, seeds = [...], bump)]`
// 2) using the account via `#[account(init, seeds = [...], bump = data.bump)]
//
// When using a PDA, it's usually recommend to store the bump seed in the
// account data, so that you can use it as demonstrated in 2), which will
// provide a more efficient check.
// Use the key to generate the PDA and store the bump seed in the account for future use.
#[account(seeds = [key.to_le_bytes().as_ref()], bump)]
data: Account<'info, Data>,
data: Account<'info, Data>, // The PDA account to be validated and used
}

#[account]
pub struct Data {
value: u64,
value: u64, // The value stored in the account
}
8 changes: 6 additions & 2 deletions programs/7-bump-seed-canonicalization/secure/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ pub mod bump_seed_canonicalization_secure {
new_value: u64,
bump: u8,
) -> ProgramResult {
// Safely derive the PDA and the correct bump seed using find_program_address
let (address, expected_bump) =
Pubkey::find_program_address(&[key.to_le_bytes().as_ref()], ctx.program_id);

// Ensure the derived address matches the account key
if address != ctx.accounts.data.key() {
return Err(ProgramError::InvalidArgument);
}
// Ensure the expected bump matches the provided bump
if expected_bump != bump {
return Err(ProgramError::InvalidArgument);
}

// Update the value stored in the account
ctx.accounts.data.value = new_value;
Ok(())
}
}

#[derive(Accounts)]
pub struct BumpSeed<'info> {
data: Account<'info, Data>,
data: Account<'info, Data>, // The PDA account to be validated and used
}

#[account]
pub struct Data {
value: u64,
value: u64, // The value stored in the account
}