spherenet-program-whitelist-interface 0.3.0

Interface of SphereNet's Program Whitelist Program
Documentation
//! Onchain functionality for the `RequestWhitelistEntry` instruction

use {
    crate::{
        instructions::ProgramWhitelistInstruction,
        onchain::{write_bytes, UNINIT_BYTE},
        program::ID,
    },
    core::{mem::MaybeUninit, slice::from_raw_parts},
    pinocchio::{
        account_info::AccountInfo,
        instruction::{AccountMeta, Instruction, Signer},
        program::invoke_signed,
        ProgramResult,
    },
};

/// Requests a new entry on the program whitelist. Permissionless: the
/// `deploy_authority` being requested signs to prove control of the key.
/// Creates a `Pending` entry the `whitelist_authority` must later approve.
///
/// ### Accounts:
///   0. `[SIGNER, WRITE]` Payer (funds the new entry account)
///   1. `[SIGNER]` The deploy authority being requested (proves key control)
///   2. `[]` The whitelist account (used for validation/derivation)
///   3. `[WRITE]` The whitelist entry account to be created (Pending)
///   4. `[]` System program
pub struct RequestWhitelistEntry<'a> {
    /// Payer account (funds the new entry account)
    pub payer: &'a AccountInfo,
    /// The deploy authority being requested (signs to prove control)
    pub deploy_authority: &'a AccountInfo,
    /// The whitelist account (used for validation/derivation)
    pub whitelist_account: &'a AccountInfo,
    /// The whitelist entry account to be created (Pending)
    pub whitelist_entry_account: &'a AccountInfo,
    /// System program
    pub system_program: &'a AccountInfo,
}

impl<'a> RequestWhitelistEntry<'a> {
    #[inline(always)]
    pub fn invoke(&self) -> ProgramResult {
        self.invoke_signed(&[])
    }

    /// Creates the instruction data for the RequestWhitelistEntry instruction
    fn create_instruction_data() -> [MaybeUninit<u8>; 1] {
        let mut instruction_data = [UNINIT_BYTE; 1];

        // Set discriminator as u8 at offset [0]
        write_bytes(
            &mut instruction_data,
            &[ProgramWhitelistInstruction::REQUEST_WHITELIST_ENTRY_TAG],
        );

        instruction_data
    }

    pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
        // account metadata
        let account_metas: [AccountMeta; 5] = [
            AccountMeta::writable_signer(self.payer.key()),
            AccountMeta::readonly_signer(self.deploy_authority.key()),
            AccountMeta::readonly(self.whitelist_account.key()),
            AccountMeta::writable(self.whitelist_entry_account.key()),
            AccountMeta::readonly(self.system_program.key()),
        ];

        // Create instruction data
        let instruction_data = Self::create_instruction_data();

        let instruction = Instruction {
            program_id: &ID,
            accounts: &account_metas,
            data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 1) },
        };

        invoke_signed(
            &instruction,
            &[
                self.payer,
                self.deploy_authority,
                self.whitelist_account,
                self.whitelist_entry_account,
                self.system_program,
            ],
            signers,
        )
    }
}

/// Convenience helper for the `RequestWhitelistEntry` instruction
pub fn request_whitelist_entry(
    payer: &AccountInfo,
    deploy_authority: &AccountInfo,
    whitelist_account: &AccountInfo,
    whitelist_entry_account: &AccountInfo,
    system_program: &AccountInfo,
) -> ProgramResult {
    let ix = RequestWhitelistEntry {
        payer,
        deploy_authority,
        whitelist_account,
        whitelist_entry_account,
        system_program,
    };
    ix.invoke()
}

/// Convenience helper for the `RequestWhitelistEntry` instruction with the
/// given signers.
pub fn request_whitelist_entry_with_seed(
    payer: &AccountInfo,
    deploy_authority: &AccountInfo,
    whitelist_account: &AccountInfo,
    whitelist_entry_account: &AccountInfo,
    system_program: &AccountInfo,
    signer_seeds: &[Signer],
) -> ProgramResult {
    let ix = RequestWhitelistEntry {
        payer,
        deploy_authority,
        whitelist_account,
        whitelist_entry_account,
        system_program,
    };
    ix.invoke_signed(signer_seeds)
}

#[cfg(test)]
mod tests {
    use {super::*, crate::instructions::ProgramWhitelistInstruction};

    #[test]
    fn test_instruction_data_creation() {
        // Create instruction data using our function
        let instruction_data = RequestWhitelistEntry::create_instruction_data();

        // Create an instruction with this data for parsing
        let instruction = Instruction {
            program_id: &ID,
            accounts: &[], // Accounts not relevant for this test
            data: unsafe { from_raw_parts(instruction_data.as_ptr() as _, 1) },
        };

        // Parse the instruction data
        let parsed_ix =
            ProgramWhitelistInstruction::unpack(instruction.data).unwrap();

        // Verify that the parsed instruction matches what we expect
        match parsed_ix {
            ProgramWhitelistInstruction::RequestWhitelistEntry => {}
            _ => panic!("Parsed incorrect instruction type"),
        }
    }
}