Writes data to an extension. ### Accounts: 0. `[writable, signer]` asset 1. `[writable, signer]` payer 2. `[optional]` system_program
(
program_id: &Pubkey,
ctx: Context<Write>,
data: DataInput,
)
| 26 | /// 1. `[writable, signer]` payer |
| 27 | /// 2. `[optional]` system_program |
| 28 | pub(crate) fn process_write( |
| 29 | program_id: &Pubkey, |
| 30 | ctx: Context<Write>, |
| 31 | data: DataInput, |
| 32 | ) -> ProgramResult { |
| 33 | // account validation |
| 34 | |
| 35 | require!( |
| 36 | ctx.accounts.system_program.key() == &system_program::ID, |
| 37 | ProgramError::IncorrectProgramId, |
| 38 | "system_program" |
| 39 | ); |
| 40 | |
| 41 | require!( |
| 42 | ctx.accounts.payer.is_signer(), |
| 43 | ProgramError::MissingRequiredSignature, |
| 44 | "payer" |
| 45 | ); |
| 46 | |
| 47 | require!( |
| 48 | ctx.accounts.asset.is_signer(), |
| 49 | ProgramError::MissingRequiredSignature, |
| 50 | "asset" |
| 51 | ); |
| 52 | |
| 53 | require!( |
| 54 | ctx.accounts.asset.owner() == program_id, |
| 55 | ProgramError::IllegalOwner, |
| 56 | "asset" |
| 57 | ); |
| 58 | |
| 59 | let asset_data = ctx.accounts.asset.try_borrow_data()?; |
| 60 | |
| 61 | require!( |
| 62 | asset_data.len() >= Asset::LEN, |
| 63 | AssetError::InvalidAccountLength, |
| 64 | "asset" |
| 65 | ); |
| 66 | |
| 67 | // make sure that the asset is not already initialized |
| 68 | require!( |
| 69 | asset_data[0] == Discriminator::Uninitialized.into(), |
| 70 | AssetError::AlreadyInitialized, |
| 71 | "asset" |
| 72 | ); |
| 73 | |
| 74 | // determine the account offset |
| 75 | |
| 76 | let (extension, offset) = |
| 77 | Asset::last_extension(&asset_data).ok_or(AssetError::ExtensionNotFound)?; |
| 78 | let current = extension.extension_type(); |
| 79 | let expected = offset.saturating_add(extension.length() as usize); |
| 80 | let boundary = extension.boundary(); |
| 81 | // the length of the account cannot be larger than the current extension length, |
| 82 | // otherwise we would be writting data to an extension that does not exist |
| 83 | if expected < asset_data.len() { |
| 84 | return err!(AssetError::InvalidAccountLength); |
| 85 | } |
no test coverage detected