use hopper_runtime::{error::ProgramError, AccountView, ProgramResult};
#[inline(always)]
pub fn count_signers(accounts: &[&AccountView]) -> u8 {
let mut n: u8 = 0;
let mut i = 0;
while i < accounts.len() {
if accounts[i].is_signer() {
n = n.saturating_add(1);
}
i += 1;
}
n
}
#[inline(always)]
pub fn check_threshold(accounts: &[&AccountView], threshold: u8) -> ProgramResult {
let len = accounts.len();
let mut i = 0;
while i < len {
let mut j = i + 1;
while j < len {
if accounts[i].address() == accounts[j].address() {
return Err(ProgramError::InvalidArgument);
}
j += 1;
}
i += 1;
}
let signers = count_signers(accounts);
if signers < threshold {
return Err(ProgramError::MissingRequiredSignature);
}
Ok(())
}
#[inline(always)]
pub fn check_all_signers(accounts: &[&AccountView]) -> ProgramResult {
let len = accounts.len();
if len > u8::MAX as usize {
return Err(ProgramError::InvalidArgument);
}
check_threshold(accounts, len as u8)
}
#[inline(always)]
pub fn check_any_signer(accounts: &[&AccountView]) -> ProgramResult {
check_threshold(accounts, 1)
}