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            .into_iter()
40            .map(miden_protocol::account::AccountProcedureRoot::from_raw)
41            .collect();
42        Ok(Self::Verified::from_parts(alloc::sync::Arc::new(self.mast.verify()?), roots)?)
43    }
44}
45
46pub use proto::account::DecodedAccountWitness as AccountWitness;
47
48impl Verify for AccountWitness {
49    type Verified = miden_protocol::block::account_tree::AccountWitness;
50    type Error = VerificationError;
51    fn verify(self) -> Result<Self::Verified, Self::Error> {
52        Ok(Self::Verified::new(
53            self.witness_id.verify()?,
54            self.commitment,
55            self.path.verify()?,
56        )?)
57    }
58}
59
60pub use proto::account::DecodedAccountHeader as AccountHeader;
61
62impl Verify for AccountHeader {
63    type Verified = miden_protocol::account::AccountHeader;
64    type Error = VerificationError;
65    fn verify(self) -> Result<Self::Verified, Self::Error> {
66        match self.version {
67            proto::account::AccountVersion::V1 => {},
68            proto::account::AccountVersion::Unspecified => {
69                return Err(AccountHeaderError::UnspecifiedVersion.into());
70            },
71        }
72        Ok(Self::Verified::new(
73            self.account_id.verify()?,
74            self.nonce.try_into().map_err(AccountHeaderError::Nonce)?,
75            self.vault_root,
76            self.storage_commitment,
77            self.code_commitment,
78        ))
79    }
80}
81
82#[derive(Debug, thiserror::Error)]
83pub enum AccountHeaderError {
84    #[error("account header version is unspecified")]
85    UnspecifiedVersion,
86    #[error("invalid account nonce: {0}")]
87    Nonce(#[source] <miden_protocol::Felt as TryFrom<u64>>::Error),
88}