web3_utils/
check.rs

1use solana_program::{
2    account_info::AccountInfo,
3    entrypoint::ProgramResult,
4    msg,
5    program_error::ProgramError,
6    pubkey::Pubkey,
7    sysvar::{rent::Rent, Sysvar},
8};
9
10// Safety verification functions
11pub fn check_account_key(account: &AccountInfo, key: &Pubkey) -> ProgramResult {
12    if account.key != key {
13        msg!("Wrong account key: {} should be {}", account.key, key);
14        return Err(ProgramError::InvalidArgument);
15    }
16    Ok(())
17}
18
19pub fn check_account_owner(account: &AccountInfo, owner: &Pubkey) -> ProgramResult {
20    if account.owner != owner {
21        msg!("Wrong account owner: {} should be {}", account.owner, owner);
22        return Err(ProgramError::InvalidArgument);
23    }
24    Ok(())
25}
26
27pub fn check_signer(account: &AccountInfo) -> ProgramResult {
28    if !(account.is_signer) {
29        msg!("Missing signature for: {}", account.key);
30        return Err(ProgramError::MissingRequiredSignature);
31    }
32    Ok(())
33}
34
35pub fn check_account_derivation(
36    account: &AccountInfo,
37    seeds: &[&[u8]],
38    program_id: &Pubkey,
39) -> Result<u8, ProgramError> {
40    let (key, nonce) = Pubkey::find_program_address(seeds, program_id);
41    check_account_key(account, &key)?;
42    Ok(nonce)
43}
44
45pub fn check_rent_exempt(account: &AccountInfo) -> ProgramResult {
46    let rent = Rent::get()?;
47    if !rent.is_exempt(account.lamports(), account.data_len()) {
48        return Err(ProgramError::AccountNotRentExempt);
49    }
50    Ok(())
51}
52