switchboard-on-demand 0.6.0

A Rust library to interact with the Switchboard Solana program.
Documentation
#[cfg(feature = "anchor")]
use anchor_lang::solana_program;
// AccountInfo only needed for Rust fallback (which is cfg-gated)
use anyhow::Error as AnyhowError;
use crate::sysvar::ed25519_sysvar::{Ed25519Sysvar, ParsedEd25519SignatureDataRef};
use solana_sdk_ids::ed25519_program::ID as ED25519_PROGRAM_ID;
use solana_sdk_ids::sysvar::instructions::ID as INSTRUCTIONS_SYSVAR_ID;

/// Pointer to ED25519 program ID as u64 array for efficient comparison
const ED25519_PROGRAM_ID_U64_PTR: *const u64 = &ED25519_PROGRAM_ID.to_bytes()[0] as *const u8 as *const u64;
/// Pointer to Instructions sysvar ID as u64 array for efficient comparison
const INSTRUCTIONS_SYSVAR_ID_U64_PTR: *const u64 = &INSTRUCTIONS_SYSVAR_ID.to_bytes()[0] as *const u8 as *const u64;

extern "C" {
    #[cfg(target_os = "solana")]
    fn write_instruction_0_data_asm_extern(base_ptr: *const u8, dst_ptr: *mut u8, dst_len: usize) -> u16;
}

/// Optimized wrapper for Solana's Instructions sysvar with fast instruction data extraction.
/// 
/// This struct provides high-performance access to instruction data from the Instructions sysvar,
/// specifically optimized for ED25519 signature verification workflows.
#[derive(Copy, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Instructions;

#[cfg(feature = "anchor")]
impl anchor_lang::solana_program::sysvar::SysvarId for Instructions {
    fn id() -> solana_program::pubkey::Pubkey {
        solana_program::sysvar::instructions::id().to_bytes().into()
    }

    fn check_id(id: &solana_program::pubkey::Pubkey) -> bool {
        solana_program::sysvar::instructions::id() == id.to_bytes().into()
    }
}
#[cfg(feature = "anchor")]
impl anchor_lang::solana_program::sysvar::Sysvar for Instructions {
    // override
    fn size_of() -> usize {
        // hard-coded so that we don't have to construct an empty
        0 // golden, update if MAX_ENTRIES changes
    }
    fn from_account_info(
        _account_info: &anchor_lang::prelude::AccountInfo,
    ) -> Result<Self, solana_program::program_error::ProgramError> {
        Ok(Self {})
    }
}


/// Fast pubkey equality check using u64 XOR operations.
/// 
/// # Safety
/// 
/// Both pointers must point to valid 32-byte pubkey data aligned as u64 arrays.
#[inline(always)]
unsafe fn assert_pubkey_eq_u64(pid_ptr: *const u64, rhs_ptr: *const u64) {
    use core::ptr::read_unaligned;
    assert!((
     read_unaligned(pid_ptr)         ^ read_unaligned(rhs_ptr) |
     read_unaligned(pid_ptr.add(1))  ^ read_unaligned(rhs_ptr.add(1)) |
     read_unaligned(pid_ptr.add(2))  ^ read_unaligned(rhs_ptr.add(2)) |
     read_unaligned(pid_ptr.add(3))  ^ read_unaligned(rhs_ptr.add(3))) == 0)
}

impl Instructions {

    /// Extracts instruction data from the first instruction in the Instructions sysvar.
    /// 
    /// # Safety
    /// 
    /// This function performs unsafe memory operations and assumes:
    /// - `account_info` points to a valid Instructions sysvar account
    /// - The account data contains at least one instruction with ED25519 program ID
    /// - The instruction has zero accounts
    /// - `dst` buffer is large enough to hold the instruction data
    /// 
    /// # Arguments
    /// 
    /// * `account_info` - Account info for the Instructions sysvar
    /// * `dst` - Destination buffer for the instruction data
    /// * `return_len` - Returns the actual length of instruction data copied
    /// 
    /// # Panics
    /// 
    /// Panics if:
    /// - Account is not the Instructions sysvar
    /// - First instruction program ID is not ED25519
    /// - First instruction has non-zero accounts
    /// - Instruction data exceeds destination buffer size
    #[inline(always)]
    pub fn write_ix_0_from_account_info(
        account_info: &solana_program::account_info::AccountInfo,
        dst: &mut [u8],
        return_len: &mut u16,
    ) {
        unsafe {
            assert_pubkey_eq_u64(account_info.key.as_ref().as_ptr() as *const u64, INSTRUCTIONS_SYSVAR_ID_U64_PTR);
            let base = (*account_info.data.as_ptr()).as_ptr();
            let p = base.add((base.add(2) as *const u16).read_unaligned() as usize);
            assert!((p as *const u16).read_unaligned() == 0); // assert account len is 0
            assert_pubkey_eq_u64(p.add(2) as *const u64, ED25519_PROGRAM_ID_U64_PTR);

            *return_len = (p.add(34) as *const u16).read_unaligned();
            assert!(*return_len as usize <= dst.len());
            solana_define_syscall::definitions::sol_memcpy_(dst.as_mut_ptr(), p.add(36), *return_len as u64);
        }
    }
}