Skip to main content

miden_objects/decoded/account/
core.rs

1pub use proto::account::{DecodedAccountId as AccountId, DecodedAccountIdV1 as AccountIdV1};
2
3use crate::decoded::VerificationError;
4use crate::{Verify, proto};
5
6#[cfg(test)]
7mod tests;
8
9impl Verify for AccountId {
10    type Verified = miden_protocol::account::AccountId;
11    type Error = miden_protocol::errors::AccountIdError;
12
13    fn verify(self) -> Result<Self::Verified, Self::Error> {
14        match self.version {
15            proto::account::account_id::DecodedVersion::V1(id) => {
16                Ok(Self::Verified::V1(id.verify()?))
17            },
18        }
19    }
20}
21
22impl Verify for AccountIdV1 {
23    type Verified = miden_protocol::account::AccountIdV1;
24    type Error = miden_protocol::errors::AccountIdError;
25
26    fn verify(self) -> Result<Self::Verified, Self::Error> {
27        Self::Verified::try_from_elements(self.suffix, self.prefix)
28    }
29}
30
31pub use proto::account::DecodedAccountCode as AccountCode;
32
33impl Verify for AccountCode {
34    type Verified = miden_protocol::account::AccountCode;
35    type Error = VerificationError;
36    fn verify(self) -> Result<Self::Verified, Self::Error> {
37        let roots = self
38            .procedure_roots
39            .map(miden_protocol::account::AccountProcedureRoot::from_raw);
40        Ok(Self::Verified::from_parts(alloc::sync::Arc::new(self.mast.verify()?), roots)?)
41    }
42}
43
44pub use proto::account::DecodedAccountWitness as AccountWitness;
45
46impl Verify for AccountWitness {
47    type Verified = miden_protocol::block::account_tree::AccountWitness;
48    type Error = VerificationError;
49    fn verify(self) -> Result<Self::Verified, Self::Error> {
50        Ok(Self::Verified::new(
51            self.witness_id.verify()?,
52            self.commitment,
53            self.path.verify()?,
54        )?)
55    }
56}
57
58pub use proto::account::DecodedAccountHeader as AccountHeader;
59
60impl Verify for AccountHeader {
61    type Verified = miden_protocol::account::AccountHeader;
62    type Error = VerificationError;
63    fn verify(self) -> Result<Self::Verified, Self::Error> {
64        match self.version {
65            proto::account::AccountVersion::V1 => {},
66            proto::account::AccountVersion::Unspecified => {
67                return Err(AccountHeaderError::UnspecifiedVersion.into());
68            },
69        }
70        Ok(Self::Verified::new(
71            self.account_id.verify()?,
72            self.nonce.try_into().map_err(AccountHeaderError::Nonce)?,
73            self.vault_root,
74            self.storage_commitment,
75            self.code_commitment,
76        ))
77    }
78}
79
80#[derive(Debug, thiserror::Error)]
81pub enum AccountHeaderError {
82    #[error("account header version is unspecified")]
83    UnspecifiedVersion,
84    #[error("invalid account nonce: {0}")]
85    Nonce(#[source] <miden_protocol::Felt as TryFrom<u64>>::Error),
86}
87
88pub use proto::account::DecodedAccount as Account;
89
90impl Verify for Account {
91    type Verified = miden_protocol::account::Account;
92    type Error = VerificationError;
93    fn verify(self) -> Result<Self::Verified, Self::Error> {
94        match self.version {
95            proto::account::AccountVersion::V1 => {},
96            proto::account::AccountVersion::Unspecified => {
97                return Err(AccountVersionError::Unspecified.into());
98            },
99        }
100        Ok(Self::Verified::new(
101            self.account_id.verify()?,
102            self.vault.verify()?,
103            self.storage.verify()?,
104            self.code.verify()?,
105            self.nonce,
106            self.seed.into_inner(),
107        )?)
108    }
109}
110
111/// The message format version of an [`Account`], distinct from the domain's `AccountIdVersion`.
112#[derive(Debug, thiserror::Error)]
113pub enum AccountVersionError {
114    #[error("account version is unspecified")]
115    Unspecified,
116}