Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

token-2022: Zeroize account data on close #2764

Merged
merged 2 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 12 additions & 1 deletion token/program-2022/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ mod entrypoint;

// Export current sdk types for downstream users building with a different sdk version
pub use solana_program;
use solana_program::{entrypoint::ProgramResult, program_error::ProgramError, pubkey::Pubkey};
use solana_program::{
entrypoint::ProgramResult,
program_error::ProgramError,
program_memory::sol_memcmp,
pubkey::{Pubkey, PUBKEY_BYTES},
};

/// Convert the UI representation of a token amount (using the decimals field defined in its mint)
/// to the raw amount
Expand All @@ -38,3 +43,9 @@ pub fn check_program_account(spl_token_program_id: &Pubkey) -> ProgramResult {
}
Ok(())
}

/// Checks two pubkeys for equality in a computationally cheap way using
/// `sol_memcmp`
pub fn cmp_pubkeys(a: &Pubkey, b: &Pubkey) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we could manually implement PartialEq for Pubkey to make this optimization available to all Rust programs automatically

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. I want to wait until the memcmp costs are sorted out and be sure that it's cheaper before making it the default behavior.

sol_memcmp(a.as_ref(), b.as_ref(), PUBKEY_BYTES) == 0
}
Loading