diff --git a/clients/js/src/generated/types/externalPluginRecord.ts b/clients/js/src/generated/types/externalPluginRecord.ts index acb1f9b3..cf4101c5 100644 --- a/clients/js/src/generated/types/externalPluginRecord.ts +++ b/clients/js/src/generated/types/externalPluginRecord.ts @@ -6,21 +6,14 @@ * @see https://github.com/metaplex-foundation/kinobi */ -import { Serializer, struct } from '@metaplex-foundation/umi/serializers'; -import { - Authority, - AuthorityArgs, - RegistryData, - RegistryDataArgs, - getAuthoritySerializer, - getRegistryDataSerializer, -} from '.'; +import { Serializer, struct, u64 } from '@metaplex-foundation/umi/serializers'; +import { Authority, AuthorityArgs, getAuthoritySerializer } from '.'; -export type ExternalPluginRecord = { authority: Authority; data: RegistryData }; +export type ExternalPluginRecord = { authority: Authority; offset: bigint }; export type ExternalPluginRecordArgs = { authority: AuthorityArgs; - data: RegistryDataArgs; + offset: number | bigint; }; export function getExternalPluginRecordSerializer(): Serializer< @@ -30,7 +23,7 @@ export function getExternalPluginRecordSerializer(): Serializer< return struct( [ ['authority', getAuthoritySerializer()], - ['data', getRegistryDataSerializer()], + ['offset', u64()], ], { description: 'ExternalPluginRecord' } ) as Serializer; diff --git a/clients/js/src/generated/types/index.ts b/clients/js/src/generated/types/index.ts index 8d677f3b..1d71a172 100644 --- a/clients/js/src/generated/types/index.ts +++ b/clients/js/src/generated/types/index.ts @@ -20,7 +20,6 @@ export * from './key'; export * from './plugin'; export * from './pluginHash'; export * from './pluginType'; -export * from './registryData'; export * from './registryRecord'; export * from './royalties'; export * from './ruleSet'; diff --git a/clients/js/src/generated/types/registryData.ts b/clients/js/src/generated/types/registryData.ts deleted file mode 100644 index 055b92cb..00000000 --- a/clients/js/src/generated/types/registryData.ts +++ /dev/null @@ -1,35 +0,0 @@ -/** - * This code was AUTOGENERATED using the kinobi library. - * Please DO NOT EDIT THIS FILE, instead use visitors - * to add features, then rerun kinobi to update it. - * - * @see https://github.com/metaplex-foundation/kinobi - */ - -import { - Serializer, - array, - struct, - u64, -} from '@metaplex-foundation/umi/serializers'; -import { Authority, AuthorityArgs, getAuthoritySerializer } from '.'; - -export type RegistryData = { offset: bigint; authorities: Array }; - -export type RegistryDataArgs = { - offset: number | bigint; - authorities: Array; -}; - -export function getRegistryDataSerializer(): Serializer< - RegistryDataArgs, - RegistryData -> { - return struct( - [ - ['offset', u64()], - ['authorities', array(getAuthoritySerializer())], - ], - { description: 'RegistryData' } - ) as Serializer; -} diff --git a/clients/js/src/generated/types/registryRecord.ts b/clients/js/src/generated/types/registryRecord.ts index 5c7ea988..fbeafafa 100644 --- a/clients/js/src/generated/types/registryRecord.ts +++ b/clients/js/src/generated/types/registryRecord.ts @@ -6,21 +6,31 @@ * @see https://github.com/metaplex-foundation/kinobi */ -import { Serializer, struct } from '@metaplex-foundation/umi/serializers'; import { + Serializer, + array, + struct, + u64, +} from '@metaplex-foundation/umi/serializers'; +import { + Authority, + AuthorityArgs, PluginType, PluginTypeArgs, - RegistryData, - RegistryDataArgs, + getAuthoritySerializer, getPluginTypeSerializer, - getRegistryDataSerializer, } from '.'; -export type RegistryRecord = { pluginType: PluginType; data: RegistryData }; +export type RegistryRecord = { + pluginType: PluginType; + authorities: Array; + offset: bigint; +}; export type RegistryRecordArgs = { pluginType: PluginTypeArgs; - data: RegistryDataArgs; + authorities: Array; + offset: number | bigint; }; export function getRegistryRecordSerializer(): Serializer< @@ -30,7 +40,8 @@ export function getRegistryRecordSerializer(): Serializer< return struct( [ ['pluginType', getPluginTypeSerializer()], - ['data', getRegistryDataSerializer()], + ['authorities', array(getAuthoritySerializer())], + ['offset', u64()], ], { description: 'RegistryRecord' } ) as Serializer; diff --git a/clients/js/src/hooked/fetchAssetWithPlugins.ts b/clients/js/src/hooked/fetchAssetWithPlugins.ts index 7b4a68ed..3c1b22ce 100644 --- a/clients/js/src/hooked/fetchAssetWithPlugins.ts +++ b/clients/js/src/hooked/fetchAssetWithPlugins.ts @@ -61,9 +61,9 @@ export async function fetchAssetWithPlugins( plugins = pluginRegistry.registry.map((record) => ({ plugin: getPluginSerializer().deserialize( maybeAccount.data, - Number(record.data.offset) + Number(record.offset) )[0], - authorities: record.data.authorities, + authorities: record.authorities, })); } diff --git a/clients/js/test/addAuthority.test.ts b/clients/js/test/addAuthority.test.ts index 866561a9..16fc03c9 100644 --- a/clients/js/test/addAuthority.test.ts +++ b/clients/js/test/addAuthority.test.ts @@ -76,13 +76,11 @@ test('it can add an authority to a plugin', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [ - { __kind: 'Owner' }, - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - ], - }, + offset: BigInt(117), + authorities: [ + { __kind: 'Owner' }, + { __kind: 'Pubkey', address: delegateAddress.publicKey }, + ], }, ], }, diff --git a/clients/js/test/addPlugin.test.ts b/clients/js/test/addPlugin.test.ts index f26ef17a..d458044f 100644 --- a/clients/js/test/addPlugin.test.ts +++ b/clients/js/test/addPlugin.test.ts @@ -62,10 +62,8 @@ test('it can add a plugin to an asset', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(117), + authorities: [{ __kind: 'Owner' }], }, ], }, diff --git a/clients/js/test/create.test.ts b/clients/js/test/create.test.ts index eacbc0e8..125a5a89 100644 --- a/clients/js/test/create.test.ts +++ b/clients/js/test/create.test.ts @@ -24,7 +24,7 @@ test('it can create a new asset in account state', async (t) => { assetAddress, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [] + plugins: [], }).sendAndConfirm(umi); // Then an account was created with the correct data. @@ -51,7 +51,7 @@ test('it can create a new asset in ledger state', async (t) => { name: 'Test Bread', uri: 'https://example.com/bread', logWrapper: publicKey('noopb9bkMVfRPU8AsbpTUg8AQkHtKwMYZiFUjNRtMmV'), - plugins: [] + plugins: [], }).sendAndConfirm(umi); // Then an account was created with the correct data. @@ -88,7 +88,7 @@ test('it can create a new asset with plugins', async (t) => { assetAddress, name: 'Test Bread', uri: 'https://example.com/bread', - plugins: [{ __kind: 'Freeze', fields: [{ frozen: false }] }] + plugins: [{ __kind: 'Freeze', fields: [{ frozen: false }] }], }).sendAndConfirm(umi); // Then an account was created with the correct data. @@ -109,10 +109,8 @@ test('it can create a new asset with plugins', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(117), + authorities: [{ __kind: 'Owner' }], }, ], }, @@ -126,4 +124,4 @@ test('it can create a new asset with plugins', async (t) => { }, ], }); -}); \ No newline at end of file +}); diff --git a/clients/js/test/delegate.test.ts b/clients/js/test/delegate.test.ts index 26338540..3a4ac761 100644 --- a/clients/js/test/delegate.test.ts +++ b/clients/js/test/delegate.test.ts @@ -62,13 +62,11 @@ test('it can delegate a new authority', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [ - { __kind: 'Owner' }, - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - ], - }, + offset: BigInt(117), + authorities: [ + { __kind: 'Owner' }, + { __kind: 'Pubkey', address: delegateAddress.publicKey }, + ], }, ], }, @@ -144,13 +142,11 @@ test('a delegate can freeze the token', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [ - { __kind: 'Owner' }, - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - ], - }, + offset: BigInt(117), + authorities: [ + { __kind: 'Owner' }, + { __kind: 'Pubkey', address: delegateAddress.publicKey }, + ], }, ], }, diff --git a/clients/js/test/delegateTransfer.test.ts b/clients/js/test/delegateTransfer.test.ts index 4de8f1eb..38bfe501 100644 --- a/clients/js/test/delegateTransfer.test.ts +++ b/clients/js/test/delegateTransfer.test.ts @@ -69,13 +69,11 @@ test('a delegate can transfer the asset', async (t) => { registry: [ { pluginType: PluginType.Transfer, - data: { - offset: BigInt(117), - authorities: [ - { __kind: 'Owner' }, - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - ], - }, + offset: BigInt(117), + authorities: [ + { __kind: 'Owner' }, + { __kind: 'Pubkey', address: delegateAddress.publicKey }, + ], }, ], }, diff --git a/clients/js/test/removeAuthority.test.ts b/clients/js/test/removeAuthority.test.ts index 6bc130d7..87d1f47f 100644 --- a/clients/js/test/removeAuthority.test.ts +++ b/clients/js/test/removeAuthority.test.ts @@ -77,13 +77,11 @@ test('it can remove an authority from a plugin', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [ - { __kind: 'Owner' }, - { __kind: 'Pubkey', address: delegateAddress.publicKey }, - ], - }, + offset: BigInt(117), + authorities: [ + { __kind: 'Owner' }, + { __kind: 'Pubkey', address: delegateAddress.publicKey }, + ], }, ], }, @@ -127,10 +125,8 @@ test('it can remove an authority from a plugin', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(117), + authorities: [{ __kind: 'Owner' }], }, ], }, @@ -204,10 +200,8 @@ test('it can remove the default authority from a plugin to make it immutable', a registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [{ __kind: 'None' }], - }, + offset: BigInt(117), + authorities: [{ __kind: 'None' }], }, ], }, diff --git a/clients/js/test/removePlugin.test.ts b/clients/js/test/removePlugin.test.ts index 276f6c79..16da4e39 100644 --- a/clients/js/test/removePlugin.test.ts +++ b/clients/js/test/removePlugin.test.ts @@ -64,10 +64,8 @@ test('it can remove a plugin from an asset', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(117), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(117), + authorities: [{ __kind: 'Owner' }], }, ], }, diff --git a/clients/js/test/update.test.ts b/clients/js/test/update.test.ts index 01e70cd4..831f563f 100644 --- a/clients/js/test/update.test.ts +++ b/clients/js/test/update.test.ts @@ -120,10 +120,8 @@ test('it can update an asset with plugins to be larger', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(120), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(120), + authorities: [{ __kind: 'Owner' }], }, ], }, @@ -184,10 +182,8 @@ test('it can update an asset with plugins to be smaller', async (t) => { registry: [ { pluginType: 2, - data: { - offset: BigInt(82), - authorities: [{ __kind: 'Owner' }], - }, + offset: BigInt(82), + authorities: [{ __kind: 'Owner' }], }, ], }, diff --git a/clients/rust/src/generated/types/external_plugin_record.rs b/clients/rust/src/generated/types/external_plugin_record.rs index cfad2374..c1734804 100644 --- a/clients/rust/src/generated/types/external_plugin_record.rs +++ b/clients/rust/src/generated/types/external_plugin_record.rs @@ -6,7 +6,6 @@ //! use crate::generated::types::Authority; -use crate::generated::types::RegistryData; use borsh::BorshDeserialize; use borsh::BorshSerialize; @@ -14,5 +13,5 @@ use borsh::BorshSerialize; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct ExternalPluginRecord { pub authority: Authority, - pub data: RegistryData, + pub offset: u64, } diff --git a/clients/rust/src/generated/types/mod.rs b/clients/rust/src/generated/types/mod.rs index 0d65c46f..13e80187 100644 --- a/clients/rust/src/generated/types/mod.rs +++ b/clients/rust/src/generated/types/mod.rs @@ -19,7 +19,6 @@ pub(crate) mod key; pub(crate) mod plugin; pub(crate) mod plugin_hash; pub(crate) mod plugin_type; -pub(crate) mod registry_data; pub(crate) mod registry_record; pub(crate) mod royalties; pub(crate) mod rule_set; @@ -39,7 +38,6 @@ pub use self::key::*; pub use self::plugin::*; pub use self::plugin_hash::*; pub use self::plugin_type::*; -pub use self::registry_data::*; pub use self::registry_record::*; pub use self::royalties::*; pub use self::rule_set::*; diff --git a/clients/rust/src/generated/types/registry_data.rs b/clients/rust/src/generated/types/registry_data.rs deleted file mode 100644 index 56b2f87a..00000000 --- a/clients/rust/src/generated/types/registry_data.rs +++ /dev/null @@ -1,17 +0,0 @@ -//! This code was AUTOGENERATED using the kinobi library. -//! Please DO NOT EDIT THIS FILE, instead use visitors -//! to add features, then rerun kinobi to update it. -//! -//! [https://github.com/metaplex-foundation/kinobi] -//! - -use crate::generated::types::Authority; -use borsh::BorshDeserialize; -use borsh::BorshSerialize; - -#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub struct RegistryData { - pub offset: u64, - pub authorities: Vec, -} diff --git a/clients/rust/src/generated/types/registry_record.rs b/clients/rust/src/generated/types/registry_record.rs index 1f90655e..5aff06b9 100644 --- a/clients/rust/src/generated/types/registry_record.rs +++ b/clients/rust/src/generated/types/registry_record.rs @@ -5,8 +5,8 @@ //! [https://github.com/metaplex-foundation/kinobi] //! +use crate::generated::types::Authority; use crate::generated::types::PluginType; -use crate::generated::types::RegistryData; use borsh::BorshDeserialize; use borsh::BorshSerialize; @@ -14,5 +14,6 @@ use borsh::BorshSerialize; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct RegistryRecord { pub plugin_type: PluginType, - pub data: RegistryData, + pub authorities: Vec, + pub offset: u64, } diff --git a/idls/mpl_core_program.json b/idls/mpl_core_program.json index 9e404cc1..27d106fc 100644 --- a/idls/mpl_core_program.json +++ b/idls/mpl_core_program.json @@ -874,13 +874,15 @@ } }, { - "name": "RegistryData", + "name": "RegistryRecord", "type": { "kind": "struct", "fields": [ { - "name": "offset", - "type": "u64" + "name": "pluginType", + "type": { + "defined": "PluginType" + } }, { "name": "authorities", @@ -889,26 +891,10 @@ "defined": "Authority" } } - } - ] - } - }, - { - "name": "RegistryRecord", - "type": { - "kind": "struct", - "fields": [ - { - "name": "pluginType", - "type": { - "defined": "PluginType" - } }, { - "name": "data", - "type": { - "defined": "RegistryData" - } + "name": "offset", + "type": "u64" } ] } @@ -925,10 +911,8 @@ } }, { - "name": "data", - "type": { - "defined": "RegistryData" - } + "name": "offset", + "type": "u64" } ] } diff --git a/programs/mpl-core/src/plugins/plugin_registry.rs b/programs/mpl-core/src/plugins/plugin_registry.rs index 1cd8e845..480ffd3e 100644 --- a/programs/mpl-core/src/plugins/plugin_registry.rs +++ b/programs/mpl-core/src/plugins/plugin_registry.rs @@ -33,24 +33,16 @@ impl SolanaAccount for PluginRegistry { } } -/// A simple type to store the core data of a plugin. -#[repr(C)] -#[derive(Clone, BorshSerialize, BorshDeserialize, Debug)] -pub struct RegistryData { - /// The offset to the plugin in the account. - pub offset: usize, - /// The authorities who have permission to utilize a plugin. - pub authorities: Vec, -} - /// A simple type to store the mapping of Plugin type to Plugin data. #[repr(C)] #[derive(Clone, BorshSerialize, BorshDeserialize, Debug)] pub struct RegistryRecord { /// The type of plugin. pub plugin_type: PluginType, - /// The data of the plugin. - pub data: RegistryData, + /// The authorities who have permission to utilize a plugin. + pub authorities: Vec, + /// The offset to the plugin in the account. + pub offset: usize, } /// A simple type to store the mapping of external Plugin authority to Plugin data. @@ -59,6 +51,6 @@ pub struct RegistryRecord { pub struct ExternalPluginRecord { /// The authority of the external plugin. pub authority: Authority, - /// The data of the plugin. - pub data: RegistryData, + /// The offset to the plugin in the account. + pub offset: usize, } diff --git a/programs/mpl-core/src/plugins/utils.rs b/programs/mpl-core/src/plugins/utils.rs index 7809d369..e2ebb0e2 100644 --- a/programs/mpl-core/src/plugins/utils.rs +++ b/programs/mpl-core/src/plugins/utils.rs @@ -11,7 +11,7 @@ use crate::{ utils::{assert_authority, resolve_authority_to_default}, }; -use super::{Plugin, PluginHeader, PluginRegistry, PluginType, RegistryData, RegistryRecord}; +use super::{Plugin, PluginHeader, PluginRegistry, PluginType, RegistryRecord}; /// Create plugin header and registry if it doesn't exist pub fn create_meta_idempotent<'a>( @@ -75,27 +75,20 @@ pub fn fetch_plugin( PluginRegistry::load(account, header.plugin_registry_offset)?; // Find the plugin in the registry. - let plugin_data = registry + let registry_record = registry .iter() - .find( - |RegistryRecord { - plugin_type: plugin_type_iter, - data: _, - }| *plugin_type_iter == plugin_type, - ) - .map( - |RegistryRecord { - plugin_type: _, - data, - }| data, - ) + .find(|record| record.plugin_type == plugin_type) .ok_or(MplCoreError::PluginNotFound)?; // Deserialize the plugin. - let plugin = Plugin::deserialize(&mut &(*account.data).borrow()[plugin_data.offset..])?; + let plugin = Plugin::deserialize(&mut &(*account.data).borrow()[registry_record.offset..])?; // Return the plugin and its authorities. - Ok((plugin_data.authorities.clone(), plugin, plugin_data.offset)) + Ok(( + registry_record.authorities.clone(), + plugin, + registry_record.offset, + )) } /// Fetch the plugin registry. @@ -143,43 +136,43 @@ pub fn initialize_plugin<'a>( let plugin_type = plugin.into(); let plugin_data = plugin.try_to_vec()?; let plugin_size = plugin_data.len(); - // let authority_bytes = authority.try_to_vec()?; - - if let Some(RegistryRecord { - plugin_type: _, - data: _, - }) = plugin_registry.registry.iter_mut().find( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == &plugin_type, - ) { + + if plugin_registry + .registry + .iter_mut() + .any(|record| record.plugin_type == plugin_type) + { return Err(MplCoreError::PluginAlreadyExists.into()); } let old_registry_offset = header.plugin_registry_offset; - let registry_data = RegistryData { + + let new_registry_record = RegistryRecord { + plugin_type, offset: old_registry_offset, authorities: vec![authority.clone()], }; + let size_increase = plugin_size .checked_add(Key::get_initial_size()) .ok_or(MplCoreError::NumericalOverflow)? - .checked_add(registry_data.clone().try_to_vec()?.len()) + .checked_add(new_registry_record.try_to_vec()?.len()) .ok_or(MplCoreError::NumericalOverflow)?; + let new_registry_offset = header .plugin_registry_offset .checked_add(plugin_size) .ok_or(MplCoreError::NumericalOverflow)?; + header.plugin_registry_offset = new_registry_offset; - plugin_registry.registry.push(RegistryRecord { - plugin_type, - data: registry_data.clone(), - }); + + plugin_registry.registry.push(new_registry_record); + let new_size = account .data_len() .checked_add(size_increase) .ok_or(MplCoreError::NumericalOverflow)?; + resize_or_reallocate_account_raw(account, payer, system_program, new_size)?; header.save(account, asset.get_size())?; plugin.save(account, old_registry_offset)?; @@ -202,24 +195,23 @@ pub fn delete_plugin<'a>( let mut header = PluginHeader::load(account, asset.get_size())?; let mut plugin_registry = PluginRegistry::load(account, header.plugin_registry_offset)?; - if let Some(index) = plugin_registry.registry.iter_mut().position( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == plugin_type, - ) { + if let Some(index) = plugin_registry + .registry + .iter_mut() + .position(|record| record.plugin_type == *plugin_type) + { let registry_record = plugin_registry.registry.remove(index); let serialized_registry_record = registry_record.try_to_vec()?; // Only allow the default authority to delete the plugin. - let authorities = registry_record.data.authorities; + let authorities = registry_record.authorities; let resolved_authority = resolve_authority_to_default(asset, authority); if resolved_authority != authorities[0] { return Err(MplCoreError::InvalidAuthority.into()); } - let plugin_offset = registry_record.data.offset; + let plugin_offset = registry_record.offset; let plugin = Plugin::load(account, plugin_offset)?; let serialized_plugin = plugin.try_to_vec()?; @@ -279,44 +271,25 @@ pub fn add_authority_to_plugin<'a>( payer: &AccountInfo<'a>, system_program: &AccountInfo<'a>, ) -> ProgramResult { - let authorities = &plugin_registry + let registry_record = &mut plugin_registry .registry .iter_mut() - .find( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == plugin_type, - ) - .ok_or(MplCoreError::PluginNotFound)? - .data - .authorities; - - assert_authority(asset, authority, authorities)?; - - if let Some(RegistryRecord { - plugin_type: _, - data: registry_data, - }) = plugin_registry.registry.iter_mut().find( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == plugin_type, - ) { - registry_data.authorities.push(new_authority.clone()); - - let authority_bytes = new_authority.try_to_vec()?; + .find(|record| record.plugin_type == *plugin_type) + .ok_or(MplCoreError::PluginNotFound)?; - let new_size = account - .data_len() - .checked_add(authority_bytes.len()) - .ok_or(MplCoreError::NumericalOverflow)?; - resize_or_reallocate_account_raw(account, payer, system_program, new_size)?; + assert_authority(asset, authority, ®istry_record.authorities)?; - plugin_registry.save(account, plugin_header.plugin_registry_offset)?; - } else { - return Err(MplCoreError::PluginNotFound.into()); - } + registry_record.authorities.push(new_authority.clone()); + + let authority_bytes = new_authority.try_to_vec()?; + + let new_size = account + .data_len() + .checked_add(authority_bytes.len()) + .ok_or(MplCoreError::NumericalOverflow)?; + resize_or_reallocate_account_raw(account, payer, system_program, new_size)?; + + plugin_registry.save(account, plugin_header.plugin_registry_offset)?; Ok(()) } @@ -334,58 +307,39 @@ pub fn remove_authority_from_plugin<'a>( payer: &AccountInfo<'a>, system_program: &AccountInfo<'a>, ) -> ProgramResult { - let authorities = &plugin_registry + let registry_record = &mut plugin_registry .registry .iter_mut() - .find( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == plugin_type, - ) - .ok_or(MplCoreError::PluginNotFound)? - .data - .authorities; + .find(|record| record.plugin_type == *plugin_type) + .ok_or(MplCoreError::PluginNotFound)?; let resolved_authority = resolve_authority_to_default(asset, authority); - if resolved_authority != authorities[0] { + if resolved_authority != registry_record.authorities[0] { return Err(MplCoreError::InvalidAuthority.into()); } - if let Some(RegistryRecord { - plugin_type: _, - data: registry_data, - }) = plugin_registry.registry.iter_mut().find( - |RegistryRecord { - plugin_type: type_iter, - data: _, - }| type_iter == plugin_type, - ) { - let index = registry_data - .authorities - .iter() - .position(|auth| auth == authority_to_remove) - .ok_or(MplCoreError::InvalidAuthority)?; - - // Here we replace the default authority with None to indicate it's been removed. - if index == 0 { - registry_data.authorities[0] = Authority::None; - plugin_registry.save(account, plugin_header.plugin_registry_offset)?; - } else { - registry_data.authorities.swap_remove(index); - - let authority_bytes = authority_to_remove.try_to_vec()?; - - let new_size = account - .data_len() - .checked_sub(authority_bytes.len()) - .ok_or(MplCoreError::NumericalOverflow)?; - resize_or_reallocate_account_raw(account, payer, system_program, new_size)?; - - plugin_registry.save(account, plugin_header.plugin_registry_offset)?; - } + let index = registry_record + .authorities + .iter() + .position(|auth| auth == authority_to_remove) + .ok_or(MplCoreError::InvalidAuthority)?; + + // Here we replace the default authority with None to indicate it's been removed. + if index == 0 { + registry_record.authorities[0] = Authority::None; + plugin_registry.save(account, plugin_header.plugin_registry_offset)?; } else { - return Err(MplCoreError::PluginNotFound.into()); + registry_record.authorities.swap_remove(index); + + let authority_bytes = authority_to_remove.try_to_vec()?; + + let new_size = account + .data_len() + .checked_sub(authority_bytes.len()) + .ok_or(MplCoreError::NumericalOverflow)?; + resize_or_reallocate_account_raw(account, payer, system_program, new_size)?; + + plugin_registry.save(account, plugin_header.plugin_registry_offset)?; } Ok(()) diff --git a/programs/mpl-core/src/processor/burn.rs b/programs/mpl-core/src/processor/burn.rs index 644514bc..a56caef5 100644 --- a/programs/mpl-core/src/processor/burn.rs +++ b/programs/mpl-core/src/processor/burn.rs @@ -65,8 +65,8 @@ pub(crate) fn burn<'a>(accounts: &'a [AccountInfo<'a>], args: BurnArgs) -> Progr record.plugin_type.check_transfer(), CheckResult::CanApprove | CheckResult::CanReject ) { - let result = Plugin::load(ctx.accounts.asset_address, record.data.offset)? - .validate_burn(&ctx.accounts, &args, &record.data.authorities)?; + let result = Plugin::load(ctx.accounts.asset_address, record.offset)? + .validate_burn(&ctx.accounts, &args, &record.authorities)?; if result == ValidationResult::Rejected { return Err(MplCoreError::InvalidAuthority.into()); } else if result == ValidationResult::Approved { diff --git a/programs/mpl-core/src/processor/compress.rs b/programs/mpl-core/src/processor/compress.rs index 1ea581d8..9109d6d8 100644 --- a/programs/mpl-core/src/processor/compress.rs +++ b/programs/mpl-core/src/processor/compress.rs @@ -48,11 +48,11 @@ pub(crate) fn compress<'a>(accounts: &'a [AccountInfo<'a>], _args: CompressArgs) let registry_records = fetch_plugins(ctx.accounts.asset_address)?; for record in registry_records { - let authorities: AuthorityVec = record.data.authorities; + let authorities: AuthorityVec = record.authorities; let plugin_authorities_hash = authorities.hash()?; let plugin = Plugin::deserialize( - &mut &(*ctx.accounts.asset_address.data).borrow()[record.data.offset..], + &mut &(*ctx.accounts.asset_address.data).borrow()[record.offset..], )?; let plugin_hash = plugin.hash()?; diff --git a/programs/mpl-core/src/processor/transfer.rs b/programs/mpl-core/src/processor/transfer.rs index 44c4bf53..cc2a9fd2 100644 --- a/programs/mpl-core/src/processor/transfer.rs +++ b/programs/mpl-core/src/processor/transfer.rs @@ -101,8 +101,8 @@ pub(crate) fn transfer<'a>(accounts: &'a [AccountInfo<'a>], args: TransferArgs) record.plugin_type.check_transfer(), CheckResult::CanApprove | CheckResult::CanReject ) { - let result = Plugin::load(ctx.accounts.asset_address, record.data.offset)? - .validate_transfer(&ctx.accounts, &args, &record.data.authorities)?; + let result = Plugin::load(ctx.accounts.asset_address, record.offset)? + .validate_transfer(&ctx.accounts, &args, &record.authorities)?; if result == ValidationResult::Rejected { return Err(MplCoreError::InvalidAuthority.into()); } else if result == ValidationResult::Approved { diff --git a/programs/mpl-core/src/processor/update.rs b/programs/mpl-core/src/processor/update.rs index d124275b..a68f6f9f 100644 --- a/programs/mpl-core/src/processor/update.rs +++ b/programs/mpl-core/src/processor/update.rs @@ -7,7 +7,7 @@ use solana_program::{ use crate::{ error::MplCoreError, instruction::accounts::UpdateAccounts, - plugins::{CheckResult, Plugin, RegistryData, RegistryRecord, ValidationResult}, + plugins::{CheckResult, Plugin, RegistryRecord, ValidationResult}, state::{Asset, DataBlob, SolanaAccount}, utils::fetch_core_data, }; @@ -53,8 +53,8 @@ pub(crate) fn update<'a>(accounts: &'a [AccountInfo<'a>], args: UpdateArgs) -> P record.plugin_type.check_transfer(), CheckResult::CanApprove | CheckResult::CanReject ) { - let result = Plugin::load(ctx.accounts.asset_address, record.data.offset)? - .validate_update(&ctx.accounts, &args, &record.data.authorities)?; + let result = Plugin::load(ctx.accounts.asset_address, record.offset)? + .validate_update(&ctx.accounts, &args, &record.authorities)?; if result == ValidationResult::Rejected { return Err(MplCoreError::InvalidAuthority.into()); } else if result == ValidationResult::Approved { @@ -128,15 +128,13 @@ pub(crate) fn update<'a>(accounts: &'a [AccountInfo<'a>], args: UpdateArgs) -> P .registry .iter_mut() .map(|record| { - let new_offset = (record.data.offset as isize) + let new_offset = (record.offset as isize) .checked_add(size_diff) .ok_or(MplCoreError::NumericalOverflow)?; Ok(RegistryRecord { plugin_type: record.plugin_type, - data: RegistryData { - offset: new_offset as usize, - authorities: record.data.authorities.clone(), - }, + offset: new_offset as usize, + authorities: record.authorities.clone(), }) }) .collect::, MplCoreError>>()?; diff --git a/programs/mpl-core/src/processor/update_plugin.rs b/programs/mpl-core/src/processor/update_plugin.rs index 2c2b8d46..27905861 100644 --- a/programs/mpl-core/src/processor/update_plugin.rs +++ b/programs/mpl-core/src/processor/update_plugin.rs @@ -5,7 +5,7 @@ use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult}; use crate::{ error::MplCoreError, instruction::accounts::UpdatePluginAccounts, - plugins::{Plugin, PluginType, RegistryRecord, ValidationResult}, + plugins::{Plugin, PluginType, ValidationResult}, utils::fetch_core_data, }; @@ -32,24 +32,20 @@ pub(crate) fn update_plugin<'a>( let plugin_registry = plugin_registry.ok_or(MplCoreError::PluginsNotInitialized)?; let plugin_type: PluginType = (&args.plugin).into(); - if let Some(RegistryRecord { - plugin_type: _, - data, - }) = plugin_registry + let registry_record = plugin_registry .registry .iter() .find(|record| record.plugin_type == plugin_type) - { - let result = Plugin::load(ctx.accounts.asset_address, data.offset)? - .validate_update_plugin(&asset, &ctx.accounts, &args, &data.authorities)?; - if result == ValidationResult::Rejected { - return Err(MplCoreError::InvalidAuthority.into()); - } else if result == ValidationResult::Approved { - //TODO: Handle plugins that are dynamically sized. - args.plugin.save(ctx.accounts.asset_address, data.offset)?; - } - } else { - return Err(MplCoreError::PluginNotFound.into()); + .ok_or(MplCoreError::PluginNotFound)?; + + let result = Plugin::load(ctx.accounts.asset_address, registry_record.offset)? + .validate_update_plugin(&asset, &ctx.accounts, &args, ®istry_record.authorities)?; + if result == ValidationResult::Rejected { + return Err(MplCoreError::InvalidAuthority.into()); + } else if result == ValidationResult::Approved { + //TODO: Handle plugins that are dynamically sized. + args.plugin + .save(ctx.accounts.asset_address, registry_record.offset)?; } Ok(())