diff --git a/programs/mpl-core/src/error.rs b/programs/mpl-core/src/error.rs index 4756c95e..3d39b2a1 100644 --- a/programs/mpl-core/src/error.rs +++ b/programs/mpl-core/src/error.rs @@ -204,6 +204,10 @@ pub enum MplCoreError { /// 48 - Cannot move asset to collection with permanent delegates #[error("Cannot move asset to collection with permanent delegates")] PermanentDelegatesPreventMove, + + /// 49 - Invalid plugin header data + #[error("Invalid plugin header data")] + InvalidPluginHeader, } impl PrintProgramError for MplCoreError { diff --git a/programs/mpl-core/src/plugins/plugin_header.rs b/programs/mpl-core/src/plugins/plugin_header.rs index bf4b2520..e4c8c657 100644 --- a/programs/mpl-core/src/plugins/plugin_header.rs +++ b/programs/mpl-core/src/plugins/plugin_header.rs @@ -1,12 +1,14 @@ use crate::state::{DataBlob, Key, SolanaAccount}; use borsh::{BorshDeserialize, BorshSerialize}; use shank::ShankAccount; +use bytemuck; /// The plugin header is the first part of the plugin metadata. /// This field stores the Key /// And a pointer to the Plugin Registry stored at the end of the account. #[repr(C)] #[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount)] +#[derive(bytemuck::Pod, bytemuck::Zeroable)] pub struct PluginHeaderV1 { /// The Discriminator of the header which doubles as a Plugin metadata version. pub key: Key, // 1 diff --git a/programs/mpl-core/src/plugins/utils.rs b/programs/mpl-core/src/plugins/utils.rs index c585cadc..10c7e0f1 100644 --- a/programs/mpl-core/src/plugins/utils.rs +++ b/programs/mpl-core/src/plugins/utils.rs @@ -7,6 +7,7 @@ use solana_program::{ pubkey::Pubkey, }; use std::collections::HashSet; +use bytemuck; use crate::{ error::MplCoreError, @@ -578,8 +579,8 @@ pub fn delete_plugin<'a, T: DataBlob>( return Err(MplCoreError::PluginNotFound.into()); } - //TODO: Bytemuck this. - let mut header = PluginHeaderV1::load(account, asset.len())?; + let header = bytemuck::try_from_bytes::(&account.data[..std::mem::size_of::()]) + .map_err(|_| MplCoreError::InvalidPluginHeader)?; let mut plugin_registry = PluginRegistryV1::load(account, header.plugin_registry_offset)?; if let Some(index) = plugin_registry @@ -659,13 +660,11 @@ pub fn delete_external_plugin_adapter<'a, T: DataBlob>( return Err(MplCoreError::ExternalPluginAdapterNotFound.into()); } - //TODO: Bytemuck this. - let mut header = PluginHeaderV1::load(account, asset.len())?; + let header = bytemuck::try_from_bytes::(&account.data[..std::mem::size_of::()]) + .map_err(|_| MplCoreError::InvalidPluginHeader)?; let mut plugin_registry = PluginRegistryV1::load(account, header.plugin_registry_offset)?; - let result = find_external_plugin_adapter(&plugin_registry, plugin_key, account)?; - - if let (Some(index), _) = result { + if let Some(index) = plugin_registry.find_external_plugin_adapter_index(plugin_key) { let registry_record = plugin_registry.external_registry.remove(index); let serialized_registry_record = registry_record.try_to_vec()?; diff --git a/programs/mpl-core/src/state/mod.rs b/programs/mpl-core/src/state/mod.rs index a3a469e7..4e015922 100644 --- a/programs/mpl-core/src/state/mod.rs +++ b/programs/mpl-core/src/state/mod.rs @@ -87,6 +87,8 @@ impl DataBlob for Authority { FromPrimitive, EnumIter, )] +#[derive(bytemuck::Pod, bytemuck::Zeroable)] +#[repr(u8)] pub enum Key { /// Uninitialized or invalid account. Uninitialized,