spherenet-program-whitelist-interface 0.2.0

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

use {
    crate::{
        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,
    },
};

/// Accepts the pending authority transfer for the program whitelist. The
/// `pending_whitelist_authority` must sign this instruction to assume
/// authority over the whitelist.
///
/// ### Accounts:
///   0. `[WRITE]` The whitelist account
///   1. `[SIGNER]` The pending whitelist authority
pub struct AcceptAuthorityTransfer<'a> {
    /// The whitelist account
    pub whitelist_account: &'a AccountInfo,
    /// The pending whitelist authority
    pub pending_whitelist_authority: &'a AccountInfo,
}

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

    /// Creates the instruction data for the AcceptAuthorityTransfer 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, &[2]);

        instruction_data
    }

    pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
        // account metadata
        let account_metas: [AccountMeta; 2] = [
            AccountMeta::writable(self.whitelist_account.key()),
            AccountMeta::readonly_signer(self.pending_whitelist_authority.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.whitelist_account, self.pending_whitelist_authority],
            signers,
        )
    }
}

/// Convenience helper for the `AcceptAuthorityTransfer` instruction
pub fn accept_authority_transfer(
    whitelist_account: &AccountInfo,
    pending_whitelist_authority: &AccountInfo,
) -> ProgramResult {
    let ix = AcceptAuthorityTransfer {
        whitelist_account,
        pending_whitelist_authority,
    };
    ix.invoke()
}

/// Convenience helper for the `AcceptAuthorityTransfer` instruction with the
/// given signers.
pub fn accept_authority_transfer_with_seed(
    whitelist_account: &AccountInfo,
    pending_whitelist_authority: &AccountInfo,
    signer_seeds: &[Signer],
) -> ProgramResult {
    let ix = AcceptAuthorityTransfer {
        whitelist_account,
        pending_whitelist_authority,
    };
    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 = AcceptAuthorityTransfer::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::AcceptAuthorityTransfer => (),
            _ => panic!("Parsed incorrect instruction type"),
        }
    }
}