|
| 1 | +use crate::{ |
| 2 | + error::DoubleZeroError, |
| 3 | + pda::get_index_pda, |
| 4 | + seeds::{SEED_INDEX, SEED_PREFIX}, |
| 5 | + serializer::try_acc_create, |
| 6 | + state::{accounttype::AccountType, globalstate::GlobalState, index::Index}, |
| 7 | +}; |
| 8 | +use borsh::BorshSerialize; |
| 9 | +use borsh_incremental::BorshDeserializeIncremental; |
| 10 | +use doublezero_program_common::validate_account_code; |
| 11 | +use solana_program::{ |
| 12 | + account_info::{next_account_info, AccountInfo}, |
| 13 | + entrypoint::ProgramResult, |
| 14 | + program_error::ProgramError, |
| 15 | + pubkey::Pubkey, |
| 16 | +}; |
| 17 | +use std::fmt; |
| 18 | + |
| 19 | +#[cfg(test)] |
| 20 | +use solana_program::msg; |
| 21 | + |
| 22 | +#[derive(BorshSerialize, BorshDeserializeIncremental, PartialEq, Clone, Default)] |
| 23 | +pub struct IndexCreateArgs { |
| 24 | + pub entity_seed: String, |
| 25 | + pub code: String, |
| 26 | +} |
| 27 | + |
| 28 | +impl fmt::Debug for IndexCreateArgs { |
| 29 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 30 | + write!(f, "entity_seed: {}, code: {}", self.entity_seed, self.code) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +pub fn process_create_index( |
| 35 | + program_id: &Pubkey, |
| 36 | + accounts: &[AccountInfo], |
| 37 | + value: &IndexCreateArgs, |
| 38 | +) -> ProgramResult { |
| 39 | + let accounts_iter = &mut accounts.iter(); |
| 40 | + |
| 41 | + let index_account = next_account_info(accounts_iter)?; |
| 42 | + let entity_account = next_account_info(accounts_iter)?; |
| 43 | + let globalstate_account = next_account_info(accounts_iter)?; |
| 44 | + let payer_account = next_account_info(accounts_iter)?; |
| 45 | + let system_program = next_account_info(accounts_iter)?; |
| 46 | + |
| 47 | + #[cfg(test)] |
| 48 | + msg!("process_create_index({:?})", value); |
| 49 | + |
| 50 | + assert!(payer_account.is_signer, "Payer must be a signer"); |
| 51 | + |
| 52 | + // Validate accounts |
| 53 | + assert_eq!( |
| 54 | + globalstate_account.owner, program_id, |
| 55 | + "Invalid GlobalState Account Owner" |
| 56 | + ); |
| 57 | + assert_eq!( |
| 58 | + entity_account.owner, program_id, |
| 59 | + "Invalid Entity Account Owner" |
| 60 | + ); |
| 61 | + assert_eq!( |
| 62 | + *system_program.unsigned_key(), |
| 63 | + solana_system_interface::program::ID, |
| 64 | + "Invalid System Program Account Owner" |
| 65 | + ); |
| 66 | + assert!(index_account.is_writable, "Index Account is not writable"); |
| 67 | + |
| 68 | + // Check foundation allowlist |
| 69 | + let globalstate = GlobalState::try_from(globalstate_account)?; |
| 70 | + if !globalstate.foundation_allowlist.contains(payer_account.key) { |
| 71 | + return Err(DoubleZeroError::NotAllowed.into()); |
| 72 | + } |
| 73 | + |
| 74 | + // Validate and normalize code |
| 75 | + let code = |
| 76 | + validate_account_code(&value.code).map_err(|_| DoubleZeroError::InvalidAccountCode)?; |
| 77 | + let lowercase_code = code.to_ascii_lowercase(); |
| 78 | + |
| 79 | + // Derive and verify the Index PDA |
| 80 | + let (expected_pda, bump_seed) = get_index_pda(program_id, value.entity_seed.as_bytes(), &code); |
| 81 | + assert_eq!(index_account.key, &expected_pda, "Invalid Index Pubkey"); |
| 82 | + |
| 83 | + // Uniqueness: account must not already exist |
| 84 | + if !index_account.data_is_empty() { |
| 85 | + return Err(ProgramError::AccountAlreadyInitialized); |
| 86 | + } |
| 87 | + |
| 88 | + // Verify the entity account is a valid, non-Index program account |
| 89 | + assert!(!entity_account.data_is_empty(), "Entity Account is empty"); |
| 90 | + let entity_type = AccountType::from(entity_account.try_borrow_data()?[0]); |
| 91 | + assert!( |
| 92 | + entity_type != AccountType::None && entity_type != AccountType::Index, |
| 93 | + "Entity Account has invalid type for indexing: {entity_type}" |
| 94 | + ); |
| 95 | + |
| 96 | + let index = Index { |
| 97 | + account_type: AccountType::Index, |
| 98 | + pk: *entity_account.key, |
| 99 | + bump_seed, |
| 100 | + }; |
| 101 | + |
| 102 | + try_acc_create( |
| 103 | + &index, |
| 104 | + index_account, |
| 105 | + payer_account, |
| 106 | + system_program, |
| 107 | + program_id, |
| 108 | + &[ |
| 109 | + SEED_PREFIX, |
| 110 | + SEED_INDEX, |
| 111 | + value.entity_seed.as_bytes(), |
| 112 | + lowercase_code.as_bytes(), |
| 113 | + &[bump_seed], |
| 114 | + ], |
| 115 | + )?; |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
0 commit comments