From 58ac7c26e153bd2818c76466befe2e1eef800e49 Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 18 Oct 2023 14:38:22 +0200 Subject: [PATCH] token 2022: move update authority check to shared file --- token/program-2022/src/extension/mod.rs | 2 ++ .../src/extension/token_metadata/processor.rs | 17 +----------- .../src/extension/update_authority.rs | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 token/program-2022/src/extension/update_authority.rs diff --git a/token/program-2022/src/extension/mod.rs b/token/program-2022/src/extension/mod.rs index 3fc5b04ea43..23afe0a3694 100644 --- a/token/program-2022/src/extension/mod.rs +++ b/token/program-2022/src/extension/mod.rs @@ -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)] diff --git a/token/program-2022/src/extension/token_metadata/processor.rs b/token/program-2022/src/extension/token_metadata/processor.rs index 88f58d8a9ec..4a19bab8fb3 100644 --- a/token/program-2022/src/extension/token_metadata/processor.rs +++ b/token/program-2022/src/extension/token_metadata/processor.rs @@ -6,7 +6,7 @@ use { error::TokenError, extension::{ alloc_and_serialize, metadata_pointer::MetadataPointer, BaseStateWithExtensions, - StateWithExtensions, + StateWithExtensions, update_authority::check_update_authority, }, state::Mint, }, @@ -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::::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, diff --git a/token/program-2022/src/extension/update_authority.rs b/token/program-2022/src/extension/update_authority.rs new file mode 100644 index 00000000000..8b46e8f36c9 --- /dev/null +++ b/token/program-2022/src/extension/update_authority.rs @@ -0,0 +1,27 @@ +//! 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::::from(*expected_update_authority) + .ok_or(TokenMetadataError::ImmutableMetadata)?; + if update_authority != *update_authority_info.key { + return Err(TokenMetadataError::IncorrectUpdateAuthority.into()); + } + Ok(()) +} \ No newline at end of file