|
| 1 | +use core::{mem::MaybeUninit, slice}; |
| 2 | + |
| 3 | +use pinocchio::{ |
| 4 | + account_info::AccountInfo, |
| 5 | + cpi::invoke_with_bounds, |
| 6 | + instruction::{AccountMeta, Instruction}, |
| 7 | + program_error::ProgramError, |
| 8 | + ProgramResult, |
| 9 | +}; |
| 10 | + |
| 11 | +/// Maximum number of multisignature signers. |
| 12 | +pub const MAX_MULTISIG_SIGNERS: usize = 11; |
| 13 | + |
| 14 | +/// Initialize a new Multisig. |
| 15 | +/// |
| 16 | +/// ### Accounts: |
| 17 | +/// 0. `[writable]` The multisig account to initialize. |
| 18 | +/// 1. `[]` Rent sysvar |
| 19 | +/// 2. ..`2+N`. `[]` The N signer accounts, where N is between 1 and 11. |
| 20 | +pub struct InitializeMultisig<'a, 'b> |
| 21 | +where |
| 22 | + 'a: 'b, |
| 23 | +{ |
| 24 | + /// Multisig Account. |
| 25 | + pub multisig: &'a AccountInfo, |
| 26 | + /// Rent sysvar Account. |
| 27 | + pub rent_sysvar: &'a AccountInfo, |
| 28 | + /// Signer Accounts |
| 29 | + pub signers: &'b [&'a AccountInfo], |
| 30 | + /// The number of signers (M) required to validate this multisignature |
| 31 | + /// account. |
| 32 | + pub m: u8, |
| 33 | +} |
| 34 | + |
| 35 | +impl InitializeMultisig<'_, '_> { |
| 36 | + #[inline(always)] |
| 37 | + pub fn invoke(&self) -> ProgramResult { |
| 38 | + let &Self { |
| 39 | + multisig, |
| 40 | + rent_sysvar, |
| 41 | + signers, |
| 42 | + m, |
| 43 | + } = self; |
| 44 | + |
| 45 | + if signers.len() > MAX_MULTISIG_SIGNERS { |
| 46 | + return Err(ProgramError::InvalidArgument); |
| 47 | + } |
| 48 | + |
| 49 | + let num_accounts = 2 + signers.len(); |
| 50 | + |
| 51 | + // Account metadata |
| 52 | + const UNINIT_META: MaybeUninit<AccountMeta> = MaybeUninit::<AccountMeta>::uninit(); |
| 53 | + let mut acc_metas = [UNINIT_META; 2 + MAX_MULTISIG_SIGNERS]; |
| 54 | + |
| 55 | + unsafe { |
| 56 | + // SAFETY: |
| 57 | + // - `account_metas` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 58 | + // - Index 0 and 1 are always present |
| 59 | + acc_metas |
| 60 | + .get_unchecked_mut(0) |
| 61 | + .write(AccountMeta::writable(multisig.key())); |
| 62 | + acc_metas |
| 63 | + .get_unchecked_mut(1) |
| 64 | + .write(AccountMeta::readonly(rent_sysvar.key())); |
| 65 | + } |
| 66 | + |
| 67 | + for (account_meta, signer) in acc_metas[2..].iter_mut().zip(signers.iter()) { |
| 68 | + account_meta.write(AccountMeta::readonly(signer.key())); |
| 69 | + } |
| 70 | + |
| 71 | + // Instruction data layout: |
| 72 | + // - [0]: instruction discriminator (1 byte, u8) |
| 73 | + // - [1]: m (1 byte, u8) |
| 74 | + let data = &[2, m]; |
| 75 | + |
| 76 | + let instruction = Instruction { |
| 77 | + program_id: &crate::ID, |
| 78 | + accounts: unsafe { slice::from_raw_parts(acc_metas.as_ptr() as _, num_accounts) }, |
| 79 | + data, |
| 80 | + }; |
| 81 | + |
| 82 | + // Account info array |
| 83 | + const UNINIT_INFO: MaybeUninit<&AccountInfo> = MaybeUninit::uninit(); |
| 84 | + let mut acc_infos = [UNINIT_INFO; 2 + MAX_MULTISIG_SIGNERS]; |
| 85 | + |
| 86 | + unsafe { |
| 87 | + // SAFETY: |
| 88 | + // - `account_infos` is sized to 2 + MAX_MULTISIG_SIGNERS |
| 89 | + // - Index 0 and 1 are always present |
| 90 | + acc_infos.get_unchecked_mut(0).write(multisig); |
| 91 | + acc_infos.get_unchecked_mut(1).write(rent_sysvar); |
| 92 | + } |
| 93 | + |
| 94 | + // Fill signer accounts |
| 95 | + for (account_info, signer) in acc_infos[2..].iter_mut().zip(signers.iter()) { |
| 96 | + account_info.write(signer); |
| 97 | + } |
| 98 | + |
| 99 | + invoke_with_bounds::<{ 2 + MAX_MULTISIG_SIGNERS }>(&instruction, unsafe { |
| 100 | + slice::from_raw_parts(acc_infos.as_ptr() as _, num_accounts) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments