use pinocchio::{
account_info::AccountInfo,
instruction::{AccountMeta, Instruction, Signer},
program::invoke_signed,
pubkey::Pubkey,
ProgramResult,
};
pub struct AssignWithSeed<'a, 'b, 'c> {
pub account: &'a AccountInfo,
pub base: &'a AccountInfo,
pub seed: &'b str,
pub owner: &'c Pubkey,
}
impl AssignWithSeed<'_, '_, '_> {
#[inline(always)]
pub fn invoke(&self) -> ProgramResult {
self.invoke_signed(&[])
}
#[inline(always)]
pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult {
let account_metas: [AccountMeta; 2] = [
AccountMeta::writable(self.account.key()),
AccountMeta::readonly_signer(self.base.key()),
];
let mut instruction_data = [0; 104];
instruction_data[0] = 10;
instruction_data[4..36].copy_from_slice(self.base.key());
instruction_data[36..44].copy_from_slice(&u64::to_le_bytes(self.seed.len() as u64));
let offset = 44 + self.seed.len();
instruction_data[44..offset].copy_from_slice(self.seed.as_bytes());
instruction_data[offset..offset + 32].copy_from_slice(self.owner.as_ref());
let instruction = Instruction {
program_id: &crate::ID,
accounts: &account_metas,
data: &instruction_data[..offset + 32],
};
invoke_signed(&instruction, &[self.account, self.base], signers)
}
}