1
0

multisig.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. use {
  2. super::{Initializable, Transmutable},
  3. pinocchio::{program_error::ProgramError, pubkey::Pubkey},
  4. };
  5. /// Minimum number of multisignature signers (min N)
  6. pub const MIN_SIGNERS: u8 = 1;
  7. /// Maximum number of multisignature signers (max N)
  8. pub const MAX_SIGNERS: u8 = 11;
  9. /// Multisignature data.
  10. #[repr(C)]
  11. pub struct Multisig {
  12. /// Number of signers required.
  13. pub m: u8,
  14. /// Number of valid signers.
  15. pub n: u8,
  16. /// Is `true` if this structure has been initialized.
  17. is_initialized: u8,
  18. /// Signer public keys.
  19. pub signers: [Pubkey; MAX_SIGNERS as usize],
  20. }
  21. impl Multisig {
  22. /// Utility function that checks index is between [`MIN_SIGNERS`] and
  23. /// [`MAX_SIGNERS`].
  24. pub fn is_valid_signer_index(index: u8) -> bool {
  25. (MIN_SIGNERS..=MAX_SIGNERS).contains(&index)
  26. }
  27. #[inline]
  28. pub fn set_initialized(&mut self, value: bool) {
  29. self.is_initialized = value as u8;
  30. }
  31. }
  32. unsafe impl Transmutable for Multisig {
  33. /// The length of the `Multisig` account data.
  34. const LEN: usize = core::mem::size_of::<Multisig>();
  35. }
  36. impl Initializable for Multisig {
  37. #[inline(always)]
  38. fn is_initialized(&self) -> Result<bool, ProgramError> {
  39. match self.is_initialized {
  40. 0 => Ok(false),
  41. 1 => Ok(true),
  42. _ => Err(ProgramError::InvalidAccountData),
  43. }
  44. }
  45. }