From 1ce94c7f519963ecc7d7ac82f391490e8f0b0e41 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-metadata/example/src/processor.rs | 20 ++++------------ token/program-2022/src/extension/mod.rs | 2 ++ .../src/extension/token_metadata/processor.rs | 19 ++------------- .../src/extension/update_authority.rs | 23 +++++++++++++++++++ 4 files changed, 31 insertions(+), 33 deletions(-) create mode 100644 token/program-2022/src/extension/update_authority.rs diff --git a/token-metadata/example/src/processor.rs b/token-metadata/example/src/processor.rs index 1ff45bf12e3..5e282a0086a 100644 --- a/token-metadata/example/src/processor.rs +++ b/token-metadata/example/src/processor.rs @@ -12,7 +12,10 @@ 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::{ @@ -25,21 +28,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/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..61af3ebeb75 100644 --- a/token/program-2022/src/extension/token_metadata/processor.rs +++ b/token/program-2022/src/extension/token_metadata/processor.rs @@ -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, }, @@ -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..049fe100567 --- /dev/null +++ b/token/program-2022/src/extension/update_authority.rs @@ -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::::from(*expected_update_authority) + .ok_or(TokenMetadataError::ImmutableMetadata)?; + if update_authority != *update_authority_info.key { + return Err(TokenMetadataError::IncorrectUpdateAuthority.into()); + } + Ok(()) +}