mpl_core/hooked/
plugin.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#[cfg(feature = "anchor")]
use anchor_lang::prelude::AnchorDeserialize as CrateDeserialize;
#[cfg(not(feature = "anchor"))]
use borsh::BorshDeserialize as CrateDeserialize;
use num_traits::FromPrimitive;
use solana_program::{account_info::AccountInfo, pubkey::Pubkey};

use crate::{
    accounts::{BaseAssetV1, BaseCollectionV1, PluginHeaderV1},
    errors::MplCoreError,
    types::{
        ExternalPluginAdapter, ExternalPluginAdapterKey, ExternalPluginAdapterType, LinkedDataKey,
        Plugin, PluginAuthority, PluginType, RegistryRecord,
    },
    AddBlockerPlugin, AppDataWithData, AttributesPlugin, AutographPlugin, BaseAuthority,
    BasePlugin, BurnDelegatePlugin, DataBlob, DataSectionWithData, EditionPlugin,
    ExternalPluginAdaptersList, ExternalRegistryRecordSafe, FreezeDelegatePlugin,
    ImmutableMetadataPlugin, LifecycleHookWithData, MasterEditionPlugin,
    PermanentBurnDelegatePlugin, PermanentFreezeDelegatePlugin, PermanentTransferDelegatePlugin,
    PluginRegistryV1Safe, PluginsList, RegistryRecordSafe, RoyaltiesPlugin, SolanaAccount,
    TransferDelegatePlugin, UpdateDelegatePlugin, VerifiedCreatorsPlugin,
};

/// Fetch the plugin from the registry.
pub fn fetch_plugin<T: DataBlob + SolanaAccount, U: CrateDeserialize>(
    account: &AccountInfo,
    plugin_type: PluginType,
) -> Result<(PluginAuthority, U, usize), std::io::Error> {
    let asset = T::load(account, 0)?;

    if asset.len() == account.data_len() {
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            MplCoreError::PluginNotFound.to_string(),
        ));
    }

    let header = PluginHeaderV1::from_bytes(&(*account.data).borrow()[asset.len()..])?;
    let plugin_registry = PluginRegistryV1Safe::from_bytes(
        &(*account.data).borrow()[header.plugin_registry_offset as usize..],
    )?;

    // Find the plugin in the registry.
    let registry_record = plugin_registry
        .registry
        .iter()
        .find(|record| {
            if let Some(plugin) = PluginType::from_u8(record.plugin_type) {
                plugin == plugin_type
            } else {
                false
            }
        })
        .ok_or(std::io::Error::new(
            std::io::ErrorKind::Other,
            MplCoreError::PluginNotFound.to_string(),
        ))?;

    // Deserialize the plugin.
    let plugin =
        Plugin::deserialize(&mut &(*account.data).borrow()[(registry_record.offset as usize)..])?;

    if PluginType::from(&plugin) != plugin_type {
        return Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            MplCoreError::PluginNotFound.to_string(),
        ));
    }

    let inner = U::deserialize(
        &mut &(*account.data).borrow()[registry_record.offset.checked_add(1).ok_or(
            std::io::Error::new(
                std::io::ErrorKind::Other,
                MplCoreError::NumericalOverflow.to_string(),
            ),
        )? as usize..],
    )?;

    // Return the plugin and its authority.
    Ok((
        registry_record.authority.clone(),
        inner,
        registry_record.offset as usize,
    ))
}

/// Fetch the plugin on an asset.
pub fn fetch_asset_plugin<U: CrateDeserialize>(
    account: &AccountInfo,
    plugin_type: PluginType,
) -> Result<(PluginAuthority, U, usize), std::io::Error> {
    fetch_plugin::<BaseAssetV1, U>(account, plugin_type)
}

/// Fetch the plugin on a collection.
pub fn fetch_collection_plugin<U: CrateDeserialize>(
    account: &AccountInfo,
    plugin_type: PluginType,
) -> Result<(PluginAuthority, U, usize), std::io::Error> {
    fetch_plugin::<BaseCollectionV1, U>(account, plugin_type)
}

/// Fetch the plugin registry, dropping any unknown plugins (i.e. `PluginType`s that are too new
///  for this client to know about).
pub fn fetch_plugins(account_data: &[u8]) -> Result<Vec<RegistryRecord>, std::io::Error> {
    let asset = BaseAssetV1::from_bytes(account_data)?;

    let header = PluginHeaderV1::from_bytes(&account_data[asset.len()..])?;
    let plugin_registry = PluginRegistryV1Safe::from_bytes(
        &account_data[(header.plugin_registry_offset as usize)..],
    )?;

    let filtered_plugin_registry = plugin_registry
        .registry
        .iter()
        .filter_map(|record| {
            PluginType::from_u8(record.plugin_type).map(|plugin_type| RegistryRecord {
                plugin_type,
                authority: record.authority.clone(),
                offset: record.offset,
            })
        })
        .collect();

    Ok(filtered_plugin_registry)
}

/// Fetch the external plugin adapter from the registry.
pub fn fetch_external_plugin_adapter<T: DataBlob + SolanaAccount, U: CrateDeserialize>(
    account: &AccountInfo,
    core: Option<&T>,
    plugin_key: &ExternalPluginAdapterKey,
) -> Result<(PluginAuthority, U, usize), std::io::Error> {
    let registry_record = fetch_external_registry_record(account, core, plugin_key)?;

    let inner = U::deserialize(
        &mut &(*account.data).borrow()[registry_record.offset.checked_add(1).ok_or(
            std::io::Error::new(
                std::io::ErrorKind::Other,
                MplCoreError::NumericalOverflow.to_string(),
            ),
        )? as usize..],
    )?;

    // Return the plugin and its authority.
    Ok((
        registry_record.authority.clone(),
        inner,
        registry_record.offset as usize,
    ))
}

/// Fetch the external plugin adapter from the registry.
pub fn fetch_wrapped_external_plugin_adapter<T: DataBlob + SolanaAccount>(
    account: &AccountInfo,
    core: Option<&T>,
    plugin_key: &ExternalPluginAdapterKey,
) -> Result<(ExternalRegistryRecordSafe, ExternalPluginAdapter), std::io::Error> {
    let registry_record = fetch_external_registry_record(account, core, plugin_key)?;

    // Deserialize the plugin.
    let plugin = ExternalPluginAdapter::deserialize(
        &mut &(*account.data).borrow()[registry_record.offset as usize..],
    )?;

    // Return the plugin and its authority.
    Ok((registry_record, plugin))
}

// Helper to unwrap optional data offset and data length.
fn unwrap_data_offset_and_data_len(
    data_offset: Option<u64>,
    data_len: Option<u64>,
) -> Result<(usize, usize), std::io::Error> {
    let data_offset = data_offset.ok_or(std::io::Error::new(
        std::io::ErrorKind::Other,
        MplCoreError::InvalidPlugin.to_string(),
    ))?;

    let data_len = data_len.ok_or(std::io::Error::new(
        std::io::ErrorKind::Other,
        MplCoreError::InvalidPlugin.to_string(),
    ))?;

    Ok((data_offset as usize, data_len as usize))
}

/// Fetch the external plugin adapter data offset and length.  These can be used to
/// directly slice the account data for use of the external plugin adapter data.
pub fn fetch_external_plugin_adapter_data_info<T: DataBlob + SolanaAccount>(
    account: &AccountInfo,
    core: Option<&T>,
    plugin_key: &ExternalPluginAdapterKey,
) -> Result<(usize, usize), std::io::Error> {
    let registry_record = fetch_external_registry_record(account, core, plugin_key)?;
    let (data_offset, data_len) =
        unwrap_data_offset_and_data_len(registry_record.data_offset, registry_record.data_len)?;

    // Return the data offset and length.
    Ok((data_offset, data_len))
}

// Internal helper to fetch just the external registry record for the external plugin key.
fn fetch_external_registry_record<T: DataBlob + SolanaAccount>(
    account: &AccountInfo,
    core: Option<&T>,
    plugin_key: &ExternalPluginAdapterKey,
) -> Result<ExternalRegistryRecordSafe, std::io::Error> {
    let size = match core {
        Some(core) => core.len(),
        None => {
            let asset = T::load(account, 0)?;

            if asset.len() == account.data_len() {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::Other,
                    MplCoreError::ExternalPluginAdapterNotFound.to_string(),
                ));
            }

            asset.len()
        }
    };

    let header = PluginHeaderV1::from_bytes(&(*account.data).borrow()[size..])?;
    let plugin_registry = PluginRegistryV1Safe::from_bytes(
        &(*account.data).borrow()[header.plugin_registry_offset as usize..],
    )?;

    // Find and return the registry record.
    let result = find_external_plugin_adapter(&plugin_registry, plugin_key, account)?;
    result.1.cloned().ok_or_else(|| {
        std::io::Error::new(
            std::io::ErrorKind::Other,
            MplCoreError::ExternalPluginAdapterNotFound.to_string(),
        )
    })
}

/// List all plugins in an account, dropping any unknown plugins (i.e. `PluginType`s that are too
/// new for this client to know about). Note this also does not support external plugin adapters for now,
/// and will be updated when those are defined.
pub fn list_plugins(account_data: &[u8]) -> Result<Vec<PluginType>, std::io::Error> {
    let asset = BaseAssetV1::from_bytes(account_data)?;
    let header = PluginHeaderV1::from_bytes(&account_data[asset.len()..])?;
    let plugin_registry = PluginRegistryV1Safe::from_bytes(
        &account_data[(header.plugin_registry_offset as usize)..],
    )?;

    Ok(plugin_registry
        .registry
        .iter()
        .filter_map(|registry_record| PluginType::from_u8(registry_record.plugin_type))
        .collect())
}

// Convert a slice of `RegistryRecordSafe` into the `PluginsList` type, dropping any unknown
// plugins (i.e. `PluginType`s that are too new for this client to know about).
pub(crate) fn registry_records_to_plugin_list(
    registry_records: &[RegistryRecordSafe],
    account_data: &[u8],
) -> Result<PluginsList, std::io::Error> {
    let result = registry_records
        .iter()
        .try_fold(PluginsList::default(), |mut acc, record| {
            if PluginType::from_u8(record.plugin_type).is_some() {
                let authority: BaseAuthority = record.authority.clone().into();
                let base = BasePlugin {
                    authority,
                    offset: Some(record.offset),
                };
                let plugin = Plugin::deserialize(&mut &account_data[record.offset as usize..])?;

                match plugin {
                    Plugin::Royalties(royalties) => {
                        acc.royalties = Some(RoyaltiesPlugin { base, royalties });
                    }
                    Plugin::FreezeDelegate(freeze_delegate) => {
                        acc.freeze_delegate = Some(FreezeDelegatePlugin {
                            base,
                            freeze_delegate,
                        });
                    }
                    Plugin::BurnDelegate(burn_delegate) => {
                        acc.burn_delegate = Some(BurnDelegatePlugin {
                            base,
                            burn_delegate,
                        });
                    }
                    Plugin::TransferDelegate(transfer_delegate) => {
                        acc.transfer_delegate = Some(TransferDelegatePlugin {
                            base,
                            transfer_delegate,
                        });
                    }
                    Plugin::UpdateDelegate(update_delegate) => {
                        acc.update_delegate = Some(UpdateDelegatePlugin {
                            base,
                            update_delegate,
                        });
                    }
                    Plugin::PermanentFreezeDelegate(permanent_freeze_delegate) => {
                        acc.permanent_freeze_delegate = Some(PermanentFreezeDelegatePlugin {
                            base,
                            permanent_freeze_delegate,
                        });
                    }
                    Plugin::Attributes(attributes) => {
                        acc.attributes = Some(AttributesPlugin { base, attributes });
                    }
                    Plugin::PermanentTransferDelegate(permanent_transfer_delegate) => {
                        acc.permanent_transfer_delegate = Some(PermanentTransferDelegatePlugin {
                            base,
                            permanent_transfer_delegate,
                        })
                    }
                    Plugin::PermanentBurnDelegate(permanent_burn_delegate) => {
                        acc.permanent_burn_delegate = Some(PermanentBurnDelegatePlugin {
                            base,
                            permanent_burn_delegate,
                        })
                    }
                    Plugin::Edition(edition) => acc.edition = Some(EditionPlugin { base, edition }),
                    Plugin::MasterEdition(master_edition) => {
                        acc.master_edition = Some(MasterEditionPlugin {
                            base,
                            master_edition,
                        })
                    }
                    Plugin::AddBlocker(add_blocker) => {
                        acc.add_blocker = Some(AddBlockerPlugin { base, add_blocker })
                    }
                    Plugin::ImmutableMetadata(immutable_metadata) => {
                        acc.immutable_metadata = Some(ImmutableMetadataPlugin {
                            base,
                            immutable_metadata,
                        })
                    }
                    Plugin::VerifiedCreators(verified_creators) => {
                        acc.verified_creators = Some(VerifiedCreatorsPlugin {
                            base,
                            verified_creators,
                        })
                    }
                    Plugin::Autograph(autograph) => {
                        acc.autograph = Some(AutographPlugin { base, autograph })
                    }
                }
            }
            Ok(acc)
        });

    result
}

// Convert a slice of `AdapterRegistryRecordSafe` into the `ExternalPluginAdaptersList` type, dropping any unknown
// plugins (i.e. `ExternalPluginAdapterType`s that are too new for this client to know about).
pub(crate) fn registry_records_to_external_plugin_adapter_list(
    registry_records: &[ExternalRegistryRecordSafe],
    account_data: &[u8],
) -> Result<ExternalPluginAdaptersList, std::io::Error> {
    let result = registry_records.iter().try_fold(
        ExternalPluginAdaptersList::default(),
        |mut acc, record| {
            if ExternalPluginAdapterType::from_u8(record.plugin_type).is_some() {
                let plugin = ExternalPluginAdapter::deserialize(
                    &mut &account_data[record.offset as usize..],
                )?;

                match plugin {
                    ExternalPluginAdapter::LifecycleHook(lifecycle_hook) => {
                        let (data_offset, data_len) =
                            unwrap_data_offset_and_data_len(record.data_offset, record.data_len)?;

                        acc.lifecycle_hooks.push(LifecycleHookWithData {
                            base: lifecycle_hook,
                            data_offset,
                            data_len,
                        })
                    }
                    ExternalPluginAdapter::LinkedLifecycleHook(lifecycle_hook) => {
                        acc.linked_lifecycle_hooks.push(lifecycle_hook)
                    }
                    ExternalPluginAdapter::Oracle(oracle) => acc.oracles.push(oracle),
                    ExternalPluginAdapter::AppData(app_data) => {
                        let (data_offset, data_len) =
                            unwrap_data_offset_and_data_len(record.data_offset, record.data_len)?;

                        acc.app_data.push(AppDataWithData {
                            base: app_data,
                            data_offset,
                            data_len,
                        })
                    }
                    ExternalPluginAdapter::LinkedAppData(app_data) => {
                        acc.linked_app_data.push(app_data)
                    }
                    ExternalPluginAdapter::DataSection(data_section) => {
                        let (data_offset, data_len) =
                            unwrap_data_offset_and_data_len(record.data_offset, record.data_len)?;

                        acc.data_sections.push(DataSectionWithData {
                            base: data_section,
                            data_offset,
                            data_len,
                        })
                    }
                }
            }
            Ok(acc)
        },
    );

    result
}

pub(crate) fn find_external_plugin_adapter<'b>(
    plugin_registry: &'b PluginRegistryV1Safe,
    plugin_key: &ExternalPluginAdapterKey,
    account: &AccountInfo<'_>,
) -> Result<(Option<usize>, Option<&'b ExternalRegistryRecordSafe>), std::io::Error> {
    let mut result = (None, None);
    for (i, record) in plugin_registry.external_registry.iter().enumerate() {
        if record.plugin_type == ExternalPluginAdapterType::from(plugin_key) as u8
            && (match plugin_key {
                ExternalPluginAdapterKey::LifecycleHook(address)
                | ExternalPluginAdapterKey::Oracle(address)
                | ExternalPluginAdapterKey::LinkedLifecycleHook(address) => {
                    let pubkey_offset = record.offset.checked_add(1).ok_or(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        MplCoreError::NumericalOverflow,
                    ))?;
                    address
                        == &match Pubkey::deserialize(
                            &mut &account.data.borrow()[pubkey_offset as usize..],
                        ) {
                            Ok(address) => address,
                            Err(_) => {
                                return Err(std::io::Error::new(
                                    std::io::ErrorKind::Other,
                                    Box::<dyn std::error::Error + Send + Sync>::from(
                                        MplCoreError::DeserializationError,
                                    ),
                                ))
                            }
                        }
                }
                ExternalPluginAdapterKey::AppData(authority) => {
                    let authority_offset =
                        record.offset.checked_add(1).ok_or(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            MplCoreError::NumericalOverflow,
                        ))?;
                    authority
                        == &match PluginAuthority::deserialize(
                            &mut &account.data.borrow()[authority_offset as usize..],
                        ) {
                            Ok(authority) => authority,
                            Err(_) => {
                                return Err(std::io::Error::new(
                                    std::io::ErrorKind::Other,
                                    Box::<dyn std::error::Error + Send + Sync>::from(
                                        MplCoreError::DeserializationError,
                                    ),
                                ))
                            }
                        }
                }
                ExternalPluginAdapterKey::LinkedAppData(authority) => {
                    let authority_offset =
                        record.offset.checked_add(1).ok_or(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            MplCoreError::NumericalOverflow,
                        ))?;
                    authority
                        == &match PluginAuthority::deserialize(
                            &mut &account.data.borrow()[authority_offset as usize..],
                        ) {
                            Ok(authority) => authority,
                            Err(_) => {
                                return Err(std::io::Error::new(
                                    std::io::ErrorKind::Other,
                                    Box::<dyn std::error::Error + Send + Sync>::from(
                                        MplCoreError::DeserializationError,
                                    ),
                                ))
                            }
                        }
                }
                ExternalPluginAdapterKey::DataSection(linked_data_key) => {
                    let linked_data_key_offset =
                        record.offset.checked_add(1).ok_or(std::io::Error::new(
                            std::io::ErrorKind::Other,
                            MplCoreError::NumericalOverflow,
                        ))?;
                    linked_data_key
                        == &match LinkedDataKey::deserialize(
                            &mut &account.data.borrow()[linked_data_key_offset as usize..],
                        ) {
                            Ok(linked_data_key) => linked_data_key,
                            Err(_) => {
                                return Err(std::io::Error::new(
                                    std::io::ErrorKind::Other,
                                    Box::<dyn std::error::Error + Send + Sync>::from(
                                        MplCoreError::DeserializationError,
                                    ),
                                ))
                            }
                        }
                }
            })
        {
            result = (Some(i), Some(record));
            break;
        }
    }

    Ok(result)
}