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
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

113 changes: 66 additions & 47 deletions programs/mpl-core/src/processor/burn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use solana_program::{account_info::AccountInfo, entrypoint::ProgramResult};
use crate::{
error::MplCoreError,
instruction::accounts::BurnAccounts,
plugins::{CheckResult, Plugin, ValidationResult},
state::{Asset, Compressible, CompressionProof, Key},
plugins::{CheckResult, Plugin, PluginType, ValidationResult},
state::{Asset, Authority, Compressible, CompressionProof, Key},
utils::{close_program_account, fetch_core_data, load_key, verify_proof},
};

Expand All @@ -25,62 +25,81 @@ pub(crate) fn burn<'a>(accounts: &'a [AccountInfo<'a>], args: BurnArgs) -> Progr
assert_signer(payer)?;
}

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

if ctx.accounts.authority.key != &asset.owner {
return Err(MplCoreError::InvalidAuthority.into());
}

// TODO: Check delegates in compressed case.
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)?;

asset.wrap()?;
}
Key::Asset => {
let (asset, _, plugin_registry) = fetch_core_data::<Asset>(ctx.accounts.asset_address)?;
if ctx.accounts.authority.key != &asset.owner {
return Err(MplCoreError::InvalidAuthority.into());
}
Comment on lines +40 to +42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm thinking is that we want to have it be consistent across all instructions so like we do asset.check_burn(), then asset.validate_burn().... later plugin.check_burn(), then plugin.validate_burn(). And in any other ix call the same kind of check/validate methods.

But let me have a quick chat with @blockiosaurus tomorrow about the permissions model, since I have heard some talk about changes so we might have a different way to go about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok unfortunately there's been more significant changes in main due to collection permissions, and I have some other suggestions for the team for the new way it works. I think maybe we need to pair program this tomorrow. We can discuss in standup.

asset.wrap()?;

let mut approved = false;
match Asset::check_transfer() {
CheckResult::CanApprove | CheckResult::CanReject => {
match asset.validate_burn(&ctx.accounts)? {
ValidationResult::Approved => {
approved = true;
}
ValidationResult::Rejected => {
return Err(MplCoreError::InvalidAuthority.into())
}
ValidationResult::Pass => (),
Some(
plugin_schemes
.into_iter()
.map(|plugin_schema| {
(
plugin_schema.plugin.clone(),
PluginType::from(&plugin_schema.plugin),
plugin_schema.authorities,
)
})
.collect(),
)
}
Key::Asset => {
let (asset, _, plugin_registry) =
fetch_core_data::<Asset>(ctx.accounts.asset_address)?;
match asset.validate_burn(&ctx.accounts)? {
ValidationResult::Approved => {
approved = true;
}
ValidationResult::Rejected => return Err(MplCoreError::InvalidAuthority.into()),
ValidationResult::Pass => (),
}
CheckResult::None => (),
};

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_burn(&ctx.accounts, &args, &record.authorities)?;
if result == ValidationResult::Rejected {
return Err(MplCoreError::InvalidAuthority.into());
} else if result == ValidationResult::Approved {
approved = true;
}
if let Some(plugin_registry) = plugin_registry {
let mut plugins = vec![];

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_burn(),
CheckResult::CanApprove | CheckResult::CanReject
) {
let result = plugin.validate_burn(&ctx.accounts, &args, &authorities)?;

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

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

close_program_account(ctx.accounts.asset_address, ctx.accounts.authority)
Expand Down
167 changes: 78 additions & 89 deletions programs/mpl-core/src/processor/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand All @@ -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!()
}
}
Loading