|
| 1 | +use { |
| 2 | + crate::{ |
| 3 | + instructions::extensions::{ |
| 4 | + group_pointer::state::{ |
| 5 | + offset_group_pointer_update as OFFSET, InstructionDiscriminatorGroupPointer, |
| 6 | + }, |
| 7 | + ExtensionDiscriminator, |
| 8 | + }, |
| 9 | + instructions::MAX_MULTISIG_SIGNERS, |
| 10 | + }, |
| 11 | + core::{mem::MaybeUninit, slice}, |
| 12 | + pinocchio::{ |
| 13 | + account_info::AccountInfo, |
| 14 | + cpi::invoke_signed_with_bounds, |
| 15 | + instruction::{AccountMeta, Instruction, Signer}, |
| 16 | + program_error::ProgramError, |
| 17 | + pubkey::Pubkey, |
| 18 | + ProgramResult, |
| 19 | + }, |
| 20 | +}; |
| 21 | + |
| 22 | +/// Update the group pointer address. Only supported for mints that |
| 23 | +/// include the `GroupPointer` extension. |
| 24 | +/// |
| 25 | +/// Accounts expected by this instruction: |
| 26 | +/// |
| 27 | +/// * Single authority |
| 28 | +/// 0. `[writable]` The mint. |
| 29 | +/// 1. `[signer]` The group pointer authority. |
| 30 | +/// |
| 31 | +/// * Multisignature authority |
| 32 | +/// 0. `[writable]` The mint. |
| 33 | +/// 1. `[]` The mint's group pointer authority. |
| 34 | +/// 2. `..2+M` `[signer]` M signer accounts. |
| 35 | +pub struct Update<'a, 'b> { |
| 36 | + /// Mint Account |
| 37 | + pub mint: &'a AccountInfo, |
| 38 | + /// The group pointer authority. |
| 39 | + pub authority: &'a AccountInfo, |
| 40 | + /// The new account address that holds the group |
| 41 | + pub group_address: Option<&'b Pubkey>, |
| 42 | + /// The Signer accounts if `authority` is a multisig |
| 43 | + pub signers: &'a [AccountInfo], |
| 44 | + /// Token Program |
| 45 | + pub token_program: &'b Pubkey, |
| 46 | +} |
| 47 | + |
| 48 | +impl Update<'_, '_> { |
| 49 | + #[inline(always)] |
| 50 | + pub fn invoke(&self) -> ProgramResult { |
| 51 | + self.invoke_signed(&[]) |
| 52 | + } |
| 53 | + |
| 54 | + #[inline(always)] |
| 55 | + pub fn invoke_signed(&self, signers: &[Signer]) -> ProgramResult { |
| 56 | + let &Self { |
| 57 | + mint, |
| 58 | + authority, |
| 59 | + signers: account_signers, |
| 60 | + token_program, |
| 61 | + .. |
| 62 | + } = self; |
| 63 | + |
| 64 | + if account_signers.len() > MAX_MULTISIG_SIGNERS { |
| 65 | + Err(ProgramError::InvalidArgument)?; |
| 66 | + } |
| 67 | + |
| 68 | + let num_accounts = 2 + account_signers.len(); |
| 69 | + |
| 70 | + // Account metadata |
| 71 | + const UNINIT_META: MaybeUninit<AccountMeta> = MaybeUninit::<AccountMeta>::uninit(); |
| 72 | + let mut acc_metas = [UNINIT_META; 2 + MAX_MULTISIG_SIGNERS]; |
| 73 | + |
| 74 | + unsafe { |
| 75 | + // SAFETY: |
| 76 | + // - `account_metas` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 77 | + // - Index 0 is always present |
| 78 | + acc_metas |
| 79 | + .get_unchecked_mut(0) |
| 80 | + .write(AccountMeta::writable(mint.key())); |
| 81 | + // - Index 1 is always present |
| 82 | + if account_signers.is_empty() { |
| 83 | + acc_metas |
| 84 | + .get_unchecked_mut(1) |
| 85 | + .write(AccountMeta::readonly_signer(authority.key())); |
| 86 | + } else { |
| 87 | + acc_metas |
| 88 | + .get_unchecked_mut(1) |
| 89 | + .write(AccountMeta::readonly(authority.key())); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + for (account_meta, signer) in acc_metas[2..].iter_mut().zip(account_signers.iter()) { |
| 94 | + account_meta.write(AccountMeta::readonly_signer(signer.key())); |
| 95 | + } |
| 96 | + |
| 97 | + let mut buffer = [0u8; OFFSET::END as usize]; |
| 98 | + let data = update_instruction_data(&mut buffer, self.group_address); |
| 99 | + |
| 100 | + let instruction = Instruction { |
| 101 | + program_id: token_program, |
| 102 | + accounts: unsafe { slice::from_raw_parts(acc_metas.as_ptr() as _, num_accounts) }, |
| 103 | + data, |
| 104 | + }; |
| 105 | + |
| 106 | + // Account info array |
| 107 | + const UNINIT_INFO: MaybeUninit<&AccountInfo> = MaybeUninit::uninit(); |
| 108 | + let mut acc_infos = [UNINIT_INFO; 2 + MAX_MULTISIG_SIGNERS]; |
| 109 | + |
| 110 | + unsafe { |
| 111 | + // SAFETY: |
| 112 | + // - `account_infos` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 113 | + // - Index 0 is always present |
| 114 | + acc_infos.get_unchecked_mut(0).write(mint); |
| 115 | + // - Index 1 is always present |
| 116 | + acc_infos.get_unchecked_mut(1).write(authority); |
| 117 | + } |
| 118 | + |
| 119 | + // Fill signer accounts |
| 120 | + for (account_info, signer) in acc_infos[2..].iter_mut().zip(account_signers.iter()) { |
| 121 | + account_info.write(signer); |
| 122 | + } |
| 123 | + |
| 124 | + invoke_signed_with_bounds::<{ 2 + MAX_MULTISIG_SIGNERS }>( |
| 125 | + &instruction, |
| 126 | + unsafe { slice::from_raw_parts(acc_infos.as_ptr() as _, num_accounts) }, |
| 127 | + signers, |
| 128 | + ) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[inline(always)] |
| 133 | +fn update_instruction_data<'a>(buffer: &'a mut [u8], group_address: Option<&Pubkey>) -> &'a [u8] { |
| 134 | + let offset = OFFSET::START as usize; |
| 135 | + |
| 136 | + // Set discriminators (GroupPointer + Update) |
| 137 | + buffer[..offset].copy_from_slice(&[ |
| 138 | + ExtensionDiscriminator::GroupPointer as u8, |
| 139 | + InstructionDiscriminatorGroupPointer::Update as u8, |
| 140 | + ]); |
| 141 | + |
| 142 | + // Set group_address |
| 143 | + if let Some(x) = group_address { |
| 144 | + buffer[offset..offset + OFFSET::GROUP_ADDRESS_PUBKEY as usize].copy_from_slice(x); |
| 145 | + } |
| 146 | + |
| 147 | + buffer |
| 148 | +} |
0 commit comments