miden_standards/account/auth/no_auth.rs
1use miden_protocol::account::component::{AccountComponentCode, AccountComponentMetadata};
2use miden_protocol::account::{AccountComponent, AccountComponentName};
3
4use crate::account::account_component_code;
5
6account_component_code!(NO_AUTH_CODE, "miden-standards-auth-no-auth.masp");
7
8/// An [`AccountComponent`] implementing a no-authentication scheme.
9///
10/// This component provides **no authentication**! It only checks if the account
11/// state has actually changed during transaction execution by comparing the initial
12/// account commitment with the current commitment and increments the nonce if
13/// they differ. This avoids unnecessary nonce increments for transactions that don't
14/// modify the account state.
15///
16/// It exports the procedure `auth_no_auth`, which:
17/// - Pays the transaction fee by creating a public TX_FEE note funded from the account's vault in
18/// the native fee asset at rate 1/1 (see `miden::standards::fee::pay_fee` and
19/// `miden::standards::fee::native_conversion_info`); on chains with a zero verification base fee
20/// no note is created
21/// - Checks if the account state has changed by comparing initial and final commitments
22/// - Only increments the nonce if the account state has actually changed
23/// - Provides no cryptographic authentication
24pub struct NoAuth;
25
26impl NoAuth {
27 /// The name of the component.
28 pub const NAME: &'static str = "miden::standards::auth::no_auth";
29
30 /// Returns the canonical [`AccountComponentName`] of this component.
31 pub const fn name() -> AccountComponentName {
32 AccountComponentName::from_static_str(Self::NAME)
33 }
34
35 /// Returns the [`AccountComponentCode`] of this component.
36 pub fn code() -> &'static AccountComponentCode {
37 &NO_AUTH_CODE
38 }
39
40 /// Creates a new [`NoAuth`] component.
41 pub fn new() -> Self {
42 Self
43 }
44
45 /// Returns the [`AccountComponentMetadata`] for this component.
46 pub fn component_metadata() -> AccountComponentMetadata {
47 AccountComponentMetadata::new(Self::NAME).with_description("No authentication component")
48 }
49}
50
51impl Default for NoAuth {
52 fn default() -> Self {
53 Self::new()
54 }
55}
56
57impl From<NoAuth> for AccountComponent {
58 fn from(_: NoAuth) -> Self {
59 let metadata = NoAuth::component_metadata();
60
61 AccountComponent::new(NoAuth::code().clone(), vec![], metadata)
62 .expect("NoAuth component should satisfy the requirements of a valid account component")
63 }
64}
65
66// TESTS
67// ================================================================================================
68
69#[cfg(test)]
70mod tests {
71 use miden_protocol::account::AccountBuilder;
72
73 use super::*;
74 use crate::account::wallets::BasicWallet;
75
76 #[test]
77 fn test_no_auth_component() {
78 // Create an account using the NoAuth component
79 let _account = AccountBuilder::new([0; 32])
80 .with_component(NoAuth)
81 .with_component(BasicWallet)
82 .build()
83 .expect("account building failed");
84 }
85}