miden_testing/mock_chain/
auth.rs

1// AUTH
2// ================================================================================================
3
4use miden_crypto::dsa::rpo_falcon512::SecretKey;
5use miden_lib::account::auth::RpoFalcon512;
6use miden_objects::account::{AccountComponent, AuthSecretKey};
7use miden_tx::auth::BasicAuthenticator;
8use rand::SeedableRng;
9use rand_chacha::ChaCha20Rng;
10
11/// Specifies which authentication mechanism is desired for accounts
12#[derive(Debug, Clone, Copy)]
13pub enum Auth {
14    /// Creates a [SecretKey] for the account and creates a [BasicAuthenticator] that gets used
15    /// for authenticating the account.
16    BasicAuth,
17
18    /// Does not create any authentication mechanism for the account.
19    NoAuth,
20}
21
22impl Auth {
23    /// Converts `self` into its corresponding authentication [`AccountComponent`] and a
24    /// [`BasicAuthenticator`] or `None` when [`Auth::NoAuth`] is passed.
25    pub(super) fn build_component(
26        &self,
27    ) -> Option<(AccountComponent, BasicAuthenticator<ChaCha20Rng>)> {
28        match self {
29            Auth::BasicAuth => {
30                let mut rng = ChaCha20Rng::from_seed(Default::default());
31                let sec_key = SecretKey::with_rng(&mut rng);
32                let pub_key = sec_key.public_key();
33
34                let component = RpoFalcon512::new(pub_key).into();
35
36                let authenticator = BasicAuthenticator::<ChaCha20Rng>::new_with_rng(
37                    &[(pub_key.into(), AuthSecretKey::RpoFalcon512(sec_key))],
38                    rng,
39                );
40
41                Some((component, authenticator))
42            },
43            Auth::NoAuth => None,
44        }
45    }
46}