feat: Sysvar address check example added (#15)

This commit is contained in:
Anoushk Kharangate 2022-07-14 21:55:41 +05:30 committed by GitHub
parent 7dc2f407f1
commit 302b6ac59d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 595 additions and 189 deletions

659
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,4 +10,5 @@ members = [
"programs/7-bump-seed-canonicalization/*",
"programs/8-pda-sharing/*",
"programs/9-closing-accounts/*",
"programs/10-sysvar-address-checking/*"
]

View File

@ -0,0 +1,22 @@
[package]
name = "insecure"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "insecure"
[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []
[profile.release]
overflow-checks = true
[dependencies]
anchor-lang = "0.25.0"

View File

@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -0,0 +1,18 @@
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod insecure {
use super::*;
pub fn check_sysvar_address(ctx: Context<CheckSysvarAddress>) -> Result<()> {
msg!("Rent Key -> {}", ctx.accounts.rent.key().to_string());
Ok(())
}
}
#[derive(Accounts)]
pub struct CheckSysvarAddress<'info> {
rent: AccountInfo<'info>,
}

View File

@ -0,0 +1,22 @@
[package]
name = "recommended"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "secure"
[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []
[profile.release]
overflow-checks = true
[dependencies]
anchor-lang = "0.25.0"

View File

@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -0,0 +1,18 @@
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod recommended {
use super::*;
pub fn check_sysvar_address(ctx: Context<CheckSysvarAddress>) -> Result<()> {
msg!("Rent Key -> {}", ctx.accounts.rent.key().to_string());
Ok(())
}
}
#[derive(Accounts)]
pub struct CheckSysvarAddress<'info> {
rent: Sysvar<'info, Rent>,
}

View File

@ -0,0 +1,19 @@
[package]
name = "secure"
version = "0.1.0"
description = "Created with Anchor"
edition = "2021"
[lib]
crate-type = ["cdylib", "lib"]
name = "secure"
[features]
no-entrypoint = []
no-idl = []
no-log-ix-name = []
cpi = ["no-entrypoint"]
default = []
[dependencies]
anchor-lang = "0.25.0"

View File

@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@ -0,0 +1,19 @@
use anchor_lang::prelude::*;
use anchor_lang::solana_program::sysvar;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod secure {
use super::*;
pub fn check_sysvar_address(ctx: Context<CheckSysvarAddress>) -> Result<()> {
require_eq!(ctx.accounts.rent.key(), sysvar::rent::ID);
msg!("Rent Key -> {}", ctx.accounts.rent.key().to_string());
Ok(())
}
}
#[derive(Accounts)]
pub struct CheckSysvarAddress<'info> {
rent: AccountInfo<'info>,
}