Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

burn-transfer-for-compressed-nfts #4

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add delegates checks
kstepanovdev committed Mar 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 0575b0f6812563843baacd89a360da26de4757ac
167 changes: 78 additions & 89 deletions programs/mpl-core/src/processor/transfer.rs
Original file line number Diff line number Diff line change
@@ -5,8 +5,8 @@ use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult};
use crate::{
error::MplCoreError,
instruction::accounts::TransferAccounts,
plugins::{CheckResult, Plugin, ValidationResult},
state::{Asset, Compressible, CompressionProof, HashedAsset, Key, SolanaAccount},
plugins::{CheckResult, Plugin, PluginType, ValidationResult},
state::{Asset, Authority, Compressible, CompressionProof, HashedAsset, Key, SolanaAccount},
utils::{fetch_core_data, load_key, verify_proof},
};

@@ -26,100 +26,89 @@ pub(crate) fn transfer<'a>(accounts: &'a [AccountInfo<'a>], args: TransferArgs)
assert_signer(payer)?;
}

match load_key(ctx.accounts.asset_address, 0)? {
Key::HashedAsset => {
let compression_proof = args
.compression_proof
.ok_or(MplCoreError::MissingCompressionProof)?;
let (mut asset, _) = verify_proof(ctx.accounts.asset_address, &compression_proof)?;

if ctx.accounts.authority.key != &asset.owner {
return Err(MplCoreError::InvalidAuthority.into());
let mut approved = false;
let (mut asset_outer, mut key_type) = (None, Key::HashedAsset);
let plugins: Option<Vec<(Plugin, PluginType, Vec<Authority>)>> =
match load_key(ctx.accounts.asset_address, 0)? {
Key::HashedAsset => {
let compression_proof = args
.compression_proof
.as_ref()
.ok_or(MplCoreError::MissingCompressionProof)?;
let (asset, plugin_schemes) =
verify_proof(ctx.accounts.asset_address, compression_proof)?;
if ctx.accounts.authority.key != &asset.owner {
return Err(MplCoreError::InvalidAuthority.into());
}
asset_outer = Some(asset);

Some(
plugin_schemes
.into_iter()
.map(|plugin_schema| {
(
plugin_schema.plugin.clone(),
PluginType::from(&plugin_schema.plugin),
plugin_schema.authorities,
)
})
.collect(),
)
}

// TODO: Check delegates in compressed case.

asset.owner = *ctx.accounts.new_owner.key;

asset.wrap()?;

// Make a new hashed asset with updated owner and save to account.
HashedAsset::new(asset.hash()?).save(ctx.accounts.asset_address, 0)
}
Key::Asset => {
// let mut asset = Asset::load(ctx.accounts.asset_address, 0)?;

// let mut authority_check: Result<(), ProgramError> =
// Err(MplCoreError::InvalidAuthority.into());
// if asset.get_size() != ctx.accounts.asset_address.data_len() {
// solana_program::msg!("Fetch Plugin");
// let (authorities, plugin, _) =
// fetch_plugin(ctx.accounts.asset_address, PluginType::Delegate)?;

// solana_program::msg!("Assert authority");
// authority_check = assert_authority(&asset, ctx.accounts.authority, &authorities);

// if let Plugin::Delegate(delegate) = plugin {
// if delegate.frozen {
// return Err(MplCoreError::AssetIsFrozen.into());
// }
// }
// }

// match authority_check {
// Ok(_) => Ok::<(), ProgramError>(()),
// Err(_) => {
// if ctx.accounts.authority.key != &asset.owner {
// Err(MplCoreError::InvalidAuthority.into())
// } else {
// Ok(())
// }
// }
// }?;

let (mut asset, _, plugin_registry) =
fetch_core_data::<Asset>(ctx.accounts.asset_address)?;

let mut approved = false;
match Asset::check_transfer() {
CheckResult::CanApprove | CheckResult::CanReject => {
match asset.validate_transfer(&ctx.accounts)? {
ValidationResult::Approved => {
approved = true;
}
ValidationResult::Rejected => {
return Err(MplCoreError::InvalidAuthority.into())
}
ValidationResult::Pass => (),
Key::Asset => {
let (asset, _, plugin_registry) =
fetch_core_data::<Asset>(ctx.accounts.asset_address)?;
match asset.validate_transfer(&ctx.accounts)? {
ValidationResult::Approved => {
approved = true;
}
ValidationResult::Rejected => return Err(MplCoreError::InvalidAuthority.into()),
ValidationResult::Pass => (),
}
CheckResult::None => (),
};
(asset_outer, key_type) = (Some(asset), Key::Asset);

if let Some(plugin_registry) = plugin_registry {
let mut plugins = vec![];

if let Some(plugin_registry) = plugin_registry {
for record in plugin_registry.registry {
if matches!(
record.plugin_type.check_transfer(),
CheckResult::CanApprove | CheckResult::CanReject
) {
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 {
approved = true;
}
for record in plugin_registry.registry {
let plugin = Plugin::load(ctx.accounts.asset_address, record.offset)?;
plugins.push((plugin, record.plugin_type, record.authorities))
}
Some(plugins)
} else {
None
}
}
_ => return Err(MplCoreError::IncorrectAccount.into()),
};

if plugins.is_some() {
for (plugin, plugin_type, authorities) in plugins.unwrap() {
if matches!(
plugin_type.check_transfer(),
CheckResult::CanApprove | CheckResult::CanReject
) {
let result = plugin.validate_transfer(&ctx.accounts, &args, &authorities)?;

match result {
ValidationResult::Approved => approved = true,
ValidationResult::Pass => (),
ValidationResult::Rejected => return Err(MplCoreError::InvalidAuthority.into()),
}
};

if !approved {
return Err(MplCoreError::InvalidAuthority.into());
}

asset.owner = *ctx.accounts.new_owner.key;
asset.save(ctx.accounts.asset_address, 0)
}
_ => Err(MplCoreError::IncorrectAccount.into()),
}

if !approved {
return Err(MplCoreError::InvalidAuthority.into());
}

// cannot be None
let mut asset = asset_outer.unwrap();
asset.owner = *ctx.accounts.new_owner.key;
match key_type {
Key::Asset => asset.save(ctx.accounts.asset_address, 0),
Key::HashedAsset => HashedAsset::new(asset.hash()?).save(ctx.accounts.asset_address, 0),
_ => unreachable!()
}
}