Skip to content

Commit

Permalink
token 2022: move update authority check to shared file
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalojoec committed Oct 19, 2023
1 parent acabef3 commit f103552
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 33 deletions.
17 changes: 1 addition & 16 deletions token-metadata/example/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
pubkey::Pubkey,
},
spl_pod::optional_keys::OptionalNonZeroPubkey,
spl_token_2022::{extension::StateWithExtensions, state::Mint},
spl_token_2022::{extension::{update_authority::check_update_authority, StateWithExtensions}, state::Mint},
spl_token_metadata_interface::{
error::TokenMetadataError,
instruction::{
Expand All @@ -25,21 +25,6 @@ use {
},
};

fn check_update_authority(
update_authority_info: &AccountInfo,
expected_update_authority: &OptionalNonZeroPubkey,
) -> Result<(), ProgramError> {
if !update_authority_info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
let update_authority = Option::<Pubkey>::from(*expected_update_authority)
.ok_or(TokenMetadataError::ImmutableMetadata)?;
if update_authority != *update_authority_info.key {
return Err(TokenMetadataError::IncorrectUpdateAuthority.into());
}
Ok(())
}

/// Processes a [Initialize](enum.TokenMetadataInstruction.html) instruction.
pub fn process_initialize(
_program_id: &Pubkey,
Expand Down
2 changes: 2 additions & 0 deletions token/program-2022/src/extension/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub mod token_metadata;
pub mod transfer_fee;
/// Transfer Hook extension
pub mod transfer_hook;
/// Update authority util
pub mod update_authority;

/// Length in TLV structure
#[derive(Clone, Copy, Debug, Default, PartialEq, Pod, Zeroable)]
Expand Down
19 changes: 2 additions & 17 deletions token/program-2022/src/extension/token_metadata/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use {
check_program_account,
error::TokenError,
extension::{
alloc_and_serialize, metadata_pointer::MetadataPointer, BaseStateWithExtensions,
StateWithExtensions,
alloc_and_serialize, metadata_pointer::MetadataPointer,
update_authority::check_update_authority, BaseStateWithExtensions, StateWithExtensions,
},
state::Mint,
},
Expand All @@ -29,21 +29,6 @@ use {
},
};

fn check_update_authority(
update_authority_info: &AccountInfo,
expected_update_authority: &OptionalNonZeroPubkey,
) -> Result<(), ProgramError> {
if !update_authority_info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
let update_authority = Option::<Pubkey>::from(*expected_update_authority)
.ok_or(TokenMetadataError::ImmutableMetadata)?;
if update_authority != *update_authority_info.key {
return Err(TokenMetadataError::IncorrectUpdateAuthority.into());
}
Ok(())
}

/// Processes a [Initialize](enum.TokenMetadataInstruction.html) instruction.
pub fn process_initialize(
_program_id: &Pubkey,
Expand Down
23 changes: 23 additions & 0 deletions token/program-2022/src/extension/update_authority.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//! Utility function for checking an update authority

use {
solana_program::{account_info::AccountInfo, program_error::ProgramError, pubkey::Pubkey},
spl_pod::optional_keys::OptionalNonZeroPubkey,
spl_token_metadata_interface::error::TokenMetadataError,
};

/// Checks that the update authority is correct
pub fn check_update_authority(
update_authority_info: &AccountInfo,
expected_update_authority: &OptionalNonZeroPubkey,
) -> Result<(), ProgramError> {
if !update_authority_info.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
let update_authority = Option::<Pubkey>::from(*expected_update_authority)
.ok_or(TokenMetadataError::ImmutableMetadata)?;
if update_authority != *update_authority_info.key {
return Err(TokenMetadataError::IncorrectUpdateAuthority.into());
}
Ok(())
}

0 comments on commit f103552

Please sign in to comment.