use multiaddr::Multiaddr;
use tari_crypto::keys::SecretKey;
use super::{TestFactory, TestFactoryError};
use crate::{
peer_manager::{NodeIdentity, PeerFeatures},
types::CommsSecretKey,
};
pub fn create() -> NodeIdentityFactory {
NodeIdentityFactory::default()
}
#[derive(Default, Clone)]
pub struct NodeIdentityFactory {
control_service_address: Option<Multiaddr>,
secret_key: Option<CommsSecretKey>,
peer_features: PeerFeatures,
}
impl NodeIdentityFactory {
factory_setter!(with_control_service_address, control_service_address, Option<Multiaddr>);
factory_setter!(with_secret_key, secret_key, Option<CommsSecretKey>);
factory_setter!(with_peer_features, peer_features, PeerFeatures);
}
impl TestFactory for NodeIdentityFactory {
type Object = NodeIdentity;
fn build(self) -> Result<Self::Object, TestFactoryError> {
let secret_key = self
.secret_key
.or_else(|| Some(CommsSecretKey::random(&mut rand::rng())))
.unwrap();
let control_service_address = self
.control_service_address
.unwrap_or(super::net_address::create().build()?);
Ok(NodeIdentity::new(
secret_key,
vec![control_service_address],
self.peer_features,
))
}
}