generated from metaplex-foundation/solana-project-template
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
142 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,93 @@ | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use mpl_utils::assert_signer; | ||
use mpl_utils::resize_or_reallocate_account_raw; | ||
use solana_program::{ | ||
account_info::AccountInfo, entrypoint::ProgramResult, program::invoke, | ||
program_memory::sol_memcpy, rent::Rent, system_instruction, system_program, sysvar::Sysvar, | ||
account_info::AccountInfo, entrypoint::ProgramResult, program_memory::sol_memcpy, | ||
}; | ||
|
||
use crate::{ | ||
error::MplAssetError, | ||
instruction::accounts::CreateAccounts, | ||
state::{Asset, Compressible, DataState, HashedAsset, Key}, | ||
instruction::accounts::CompressAccounts, | ||
plugins::{fetch_plugins, Plugin}, | ||
state::{ | ||
Asset, AuthorityVec, Compressible, DataBlob, HashedAsset, HashedAssetSchema, Key, | ||
PluginHash, SolanaAccount, | ||
}, | ||
utils::load_key, | ||
}; | ||
|
||
#[repr(C)] | ||
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Eq, Debug, Clone)] | ||
pub struct CompressArgs {} | ||
|
||
pub(crate) fn compress<'a>(accounts: &'a [AccountInfo<'a>], args: CompressArgs) -> ProgramResult { | ||
pub(crate) fn compress<'a>(accounts: &'a [AccountInfo<'a>], _args: CompressArgs) -> ProgramResult { | ||
// Accounts. | ||
let ctx = CompressAccounts::context(accounts)?; | ||
|
||
// Guards. | ||
assert_signer(ctx.accounts.owner)?; | ||
let payer = if let Some(payer) = ctx.accounts.payer { | ||
assert_signer(payer)?; | ||
payer | ||
} else { | ||
ctx.accounts.owner | ||
}; | ||
|
||
match load_key(ctx.accounts.asset_address, 0)? { | ||
Key::Asset => { | ||
let asset = Asset::load(ctx.accounts.asset_address, 0)?; | ||
|
||
if ctx.accounts.owner.key != &asset.owner { | ||
return Err(MplAssetError::InvalidAuthority.into()); | ||
} | ||
|
||
// TODO: Delegated compress/decompress authority. | ||
|
||
if asset.get_size() != ctx.accounts.asset_address.data_len() { | ||
let registry_records = fetch_plugins(ctx.accounts.asset_address)?; | ||
|
||
let mut plugin_hashes = Vec::with_capacity(registry_records.len()); | ||
for record in registry_records { | ||
let authorities: AuthorityVec = record.data.authorities; | ||
let plugin_authorities_hash = authorities.hash()?; | ||
|
||
let plugin = Plugin::deserialize( | ||
&mut &(*ctx.accounts.asset_address.data).borrow()[record.data.offset..], | ||
)?; | ||
let plugin_hash = plugin.hash()?; | ||
|
||
plugin_hashes.push(PluginHash { | ||
plugin_authorities_hash, | ||
plugin_hash, | ||
}); | ||
} | ||
|
||
let asset_hash = asset.hash()?; | ||
let hashed_asset_schema = HashedAssetSchema { | ||
asset_hash, | ||
plugin_hashes, | ||
}; | ||
|
||
let hashed_asset = HashedAsset::new(hashed_asset_schema.hash()?); | ||
let serialized_data = hashed_asset.try_to_vec()?; | ||
|
||
resize_or_reallocate_account_raw( | ||
ctx.accounts.asset_address, | ||
payer, | ||
ctx.accounts.system_program, | ||
serialized_data.len(), | ||
)?; | ||
|
||
sol_memcpy( | ||
&mut ctx.accounts.asset_address.try_borrow_mut_data()?, | ||
&serialized_data, | ||
serialized_data.len(), | ||
); | ||
} | ||
} | ||
Key::HashedAsset => return Err(MplAssetError::AlreadyCompressed.into()), | ||
_ => return Err(MplAssetError::IncorrectAccount.into()), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use shank::ShankAccount; | ||
|
||
use crate::state::Compressible; | ||
|
||
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount, PartialEq, Eq)] | ||
pub struct PluginHash { | ||
pub plugin_authorities_hash: [u8; 32], | ||
pub plugin_hash: [u8; 32], | ||
} | ||
|
||
#[derive(Clone, BorshSerialize, BorshDeserialize, Debug, ShankAccount, PartialEq, Eq)] | ||
pub struct HashedAssetSchema { | ||
pub asset_hash: [u8; 32], | ||
pub plugin_hashes: Vec<PluginHash>, | ||
} | ||
|
||
impl Compressible for HashedAssetSchema {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters