miden_testing/mock_chain/
auth.rs1use alloc::vec::Vec;
4
5use miden_lib::{
6 account::auth::{RpoFalcon512, RpoFalcon512ProcedureAcl},
7 transaction::TransactionKernel,
8};
9use miden_objects::{
10 Digest,
11 account::{AccountComponent, AuthSecretKey},
12 crypto::dsa::rpo_falcon512::SecretKey,
13 testing::account_component::{
14 ConditionalAuthComponent, IncrNonceAuthComponent, NoopAuthComponent,
15 },
16};
17use miden_tx::auth::BasicAuthenticator;
18use rand::SeedableRng;
19use rand_chacha::ChaCha20Rng;
20
21#[derive(Debug, Clone)]
23pub enum Auth {
24 BasicAuth,
27
28 ProcedureAcl { auth_trigger_procedures: Vec<Digest> },
32
33 IncrNonce,
35
36 Noop,
38
39 Conditional,
41}
42
43impl Auth {
44 pub fn build_component(&self) -> (AccountComponent, Option<BasicAuthenticator<ChaCha20Rng>>) {
48 match self {
49 Auth::BasicAuth => {
50 let mut rng = ChaCha20Rng::from_seed(Default::default());
51 let sec_key = SecretKey::with_rng(&mut rng);
52 let pub_key = sec_key.public_key();
53
54 let component = RpoFalcon512::new(pub_key).into();
55 let authenticator = BasicAuthenticator::<ChaCha20Rng>::new_with_rng(
56 &[(pub_key.into(), AuthSecretKey::RpoFalcon512(sec_key))],
57 rng,
58 );
59
60 (component, Some(authenticator))
61 },
62 Auth::ProcedureAcl { auth_trigger_procedures } => {
63 let mut rng = ChaCha20Rng::from_seed(Default::default());
64 let sec_key = SecretKey::with_rng(&mut rng);
65 let pub_key = sec_key.public_key();
66
67 let component =
68 RpoFalcon512ProcedureAcl::new(pub_key, auth_trigger_procedures.clone())
69 .expect("component creation failed")
70 .into();
71 let authenticator = BasicAuthenticator::<ChaCha20Rng>::new_with_rng(
72 &[(pub_key.into(), AuthSecretKey::RpoFalcon512(sec_key))],
73 rng,
74 );
75
76 (component, Some(authenticator))
77 },
78 Auth::IncrNonce => {
79 let assembler = TransactionKernel::assembler();
80 let component = IncrNonceAuthComponent::new(assembler).unwrap();
81 (component.into(), None)
82 },
83
84 Auth::Noop => {
85 let assembler = TransactionKernel::assembler();
86 let component = NoopAuthComponent::new(assembler).unwrap();
87 (component.into(), None)
88 },
89 Auth::Conditional => {
90 let assembler = TransactionKernel::assembler();
91 let component = ConditionalAuthComponent::new(assembler).unwrap();
92 (component.into(), None)
93 },
94 }
95 }
96}
97
98impl From<Auth> for AccountComponent {
99 fn from(auth: Auth) -> Self {
100 let (component, _) = auth.build_component();
101 component
102 }
103}