use crate::account::keys::agents::listed::Agent;
use crate::account::keys::agents::user_role::Role;
use crate::utils::Address;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum Reused {
#[error("this address is one of the account's agents already")]
Listed,
#[error("this address was an agent before, and what it signed then could be replayed")]
Retired,
#[error("the venue knows this address already, as {0:?}; an agent is a fresh address")]
Known(Role),
}
pub fn fresh_agent(candidate: Address, role: &Role, listed: &[Agent], retired: &[Address]) -> Result<(), Reused> {
if listed.iter().any(|agent| agent.address == candidate) {
return Err(Reused::Listed);
}
if retired.contains(&candidate) {
return Err(Reused::Retired);
}
match role {
Role::Missing => Ok(()),
known => Err(Reused::Known(known.clone())),
}
}
#[cfg(test)]
#[path = "tests/fresh.rs"]
mod tests;