use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use super::FalconPubkey;
pub const NONCE_WINDOW_SIZE: usize = 16;
pub const MAX_MULTISIG_SIGNERS: usize = 16;
#[derive(
Clone,
Copy,
Debug,
Eq,
PartialEq,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum AccountType {
Eoa = 0x00,
Contract = 0x01,
System = 0x02,
}
pub type Balance = u128;
pub type Nonce = u64;
#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug, Eq, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[borsh(use_discriminant = true)]
#[repr(u8)]
pub enum AuthKeys {
None = 0x00,
Single(FalconPubkey) = 0x01,
MultiSig {
threshold: u8,
signers: Vec<FalconPubkey>,
} = 0x02,
Programmable {
policy: Vec<u8>,
} = 0x03,
}
impl AuthKeys {
#[must_use]
pub fn is_v1_supported(&self) -> bool {
match self {
Self::None | Self::Single(_) | Self::MultiSig { .. } => true,
Self::Programmable { .. } => false,
}
}
pub fn validate(&self) -> Result<(), InvalidAuthKeys> {
if let Self::MultiSig { threshold, signers } = self {
if signers.is_empty() {
return Err(InvalidAuthKeys::NoSigners);
}
if signers.len() > MAX_MULTISIG_SIGNERS {
return Err(InvalidAuthKeys::TooManySigners {
signers: signers.len(),
max: MAX_MULTISIG_SIGNERS,
});
}
if *threshold == 0 || (*threshold as usize) > signers.len() {
return Err(InvalidAuthKeys::InvalidThreshold {
threshold: *threshold,
signers: signers.len(),
});
}
}
Ok(())
}
}
#[derive(Debug, Error)]
pub enum InvalidAuthKeys {
#[error("multisig threshold {threshold} invalid for {signers} signers")]
InvalidThreshold {
threshold: u8,
signers: usize,
},
#[error("multisig signer count {signers} exceeds maximum {max}")]
TooManySigners {
signers: usize,
max: usize,
},
#[error("multisig requires at least one signer")]
NoSigners,
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
use crate::types::FALCON_PUBKEY_LEN;
fn pk(byte: u8) -> FalconPubkey {
FalconPubkey::new([byte; FALCON_PUBKEY_LEN])
}
#[test]
fn account_type_tag_values_match_spec() {
assert_eq!(AccountType::Eoa as u8, 0x00);
assert_eq!(AccountType::Contract as u8, 0x01);
assert_eq!(AccountType::System as u8, 0x02);
}
#[test]
fn auth_keys_v1_support_flags() {
assert!(AuthKeys::None.is_v1_supported());
assert!(AuthKeys::Single(pk(1)).is_v1_supported());
assert!(AuthKeys::MultiSig {
threshold: 2,
signers: vec![pk(1), pk(2), pk(3)]
}
.is_v1_supported());
assert!(!AuthKeys::Programmable {
policy: vec![0u8; 8]
}
.is_v1_supported());
}
#[test]
fn multisig_validates_signer_bounds() {
let bad = AuthKeys::MultiSig {
threshold: 0,
signers: vec![pk(1)],
};
assert!(bad.validate().is_err());
let bad = AuthKeys::MultiSig {
threshold: 5,
signers: vec![pk(1)],
};
assert!(bad.validate().is_err());
let bad = AuthKeys::MultiSig {
threshold: 1,
signers: vec![],
};
assert!(bad.validate().is_err());
let bad = AuthKeys::MultiSig {
threshold: 1,
signers: (0..(MAX_MULTISIG_SIGNERS + 1) as u8).map(pk).collect(),
};
assert!(bad.validate().is_err());
let ok = AuthKeys::MultiSig {
threshold: 2,
signers: vec![pk(1), pk(2), pk(3)],
};
assert!(ok.validate().is_ok());
}
#[test]
fn auth_keys_borsh_round_trip() {
for variant in [
AuthKeys::None,
AuthKeys::Single(pk(0x42)),
AuthKeys::MultiSig {
threshold: 2,
signers: vec![pk(0x01), pk(0x02), pk(0x03)],
},
AuthKeys::Programmable {
policy: vec![0xDE, 0xAD, 0xBE, 0xEF],
},
] {
let bytes = borsh::to_vec(&variant).unwrap();
let decoded: AuthKeys = borsh::from_slice(&bytes).unwrap();
assert_eq!(variant, decoded);
}
}
}