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::DecodedAccountCodeUpgrade as AccountCodeUpgrade;
45
46impl Verify for AccountCodeUpgrade {
47    type Verified = miden_protocol::account::AccountCodeUpgrade;
48    type Error = VerificationError;
49    fn verify(self) -> Result<Self::Verified, Self::Error> {
50        Ok(Self::Verified::new(self.code.verify()?))
51    }
52}
53
54pub use proto::account::DecodedAccountWitness as AccountWitness;
55
56impl Verify for AccountWitness {
57    type Verified = miden_protocol::block::account_tree::AccountWitness;
58    type Error = VerificationError;
59    fn verify(self) -> Result<Self::Verified, Self::Error> {
60        Ok(Self::Verified::new(
61            self.witness_id.verify()?,
62            self.commitment,
63            self.path.verify()?,
64        )?)
65    }
66}
67
68pub use proto::account::DecodedAccountHeader as AccountHeader;
69
70impl Verify for AccountHeader {
71    type Verified = miden_protocol::account::AccountHeader;
72    type Error = VerificationError;
73    fn verify(self) -> Result<Self::Verified, Self::Error> {
74        match self.version {
75            proto::account::AccountVersion::V1 => {},
76            proto::account::AccountVersion::Unspecified => {
77                return Err(AccountHeaderError::UnspecifiedVersion.into());
78            },
79        }
80        Ok(Self::Verified::new(
81            self.account_id.verify()?,
82            self.nonce.try_into().map_err(AccountHeaderError::Nonce)?,
83            self.vault_root,
84            self.storage_commitment,
85            self.code_commitment,
86        ))
87    }
88}
89
90#[derive(Debug, thiserror::Error)]
91pub enum AccountHeaderError {
92    #[error("account header version is unspecified")]
93    UnspecifiedVersion,
94    #[error("invalid account nonce: {0}")]
95    Nonce(#[source] <miden_protocol::Felt as TryFrom<u64>>::Error),
96}
97
98pub use proto::account::DecodedAccount as Account;
99
100impl Verify for Account {
101    type Verified = miden_protocol::account::Account;
102    type Error = VerificationError;
103    fn verify(self) -> Result<Self::Verified, Self::Error> {
104        self.version.ensure_specified()?;
105        Ok(Self::Verified::new(
106            self.account_id.verify()?,
107            self.vault.verify()?,
108            self.storage.verify()?,
109            self.code.verify()?,
110            self.nonce,
111            self.seed.into_inner(),
112        )?)
113    }
114}
115
116impl proto::account::AccountVersion {
117    pub(super) fn ensure_specified(self) -> Result<(), AccountVersionError> {
118        match self {
119            Self::V1 => Ok(()),
120            Self::Unspecified => Err(AccountVersionError::Unspecified),
121        }
122    }
123}
124
125/// The message format version of an [`Account`] or [`PartialAccount`](super::PartialAccount),
126/// distinct from the domain's `AccountIdVersion`.
127#[derive(Debug, thiserror::Error)]
128pub enum AccountVersionError {
129    #[error("account version is unspecified")]
130    Unspecified,
131}