miden_client/store/
account.rs1use alloc::vec::Vec;
4use core::fmt::Display;
5
6use miden_protocol::account::{Account, AccountId, PartialAccount};
7use miden_protocol::{Felt, Word};
8
9use crate::ClientError;
10
11#[derive(Debug)]
16pub enum AccountRecordData {
17 Full(Account),
18 Partial(PartialAccount),
19}
20
21impl AccountRecordData {
22 pub fn nonce(&self) -> Felt {
23 match self {
24 AccountRecordData::Full(account) => account.nonce(),
25 AccountRecordData::Partial(partial_account) => partial_account.nonce(),
26 }
27 }
28}
29
30#[derive(Debug)]
39pub struct AccountRecord {
40 account_data: AccountRecordData,
42 status: AccountStatus,
44}
45
46impl AccountRecord {
47 pub fn new(account_data: AccountRecordData, status: AccountStatus) -> Self {
48 #[cfg(debug_assertions)]
50 {
51 let account_seed = match &account_data {
52 AccountRecordData::Full(acc) => acc.seed(),
53 AccountRecordData::Partial(acc) => acc.seed(),
54 };
55 debug_assert_eq!(account_seed, status.seed().copied(), "account seed mismatch");
56 }
57
58 Self { account_data, status }
59 }
60
61 pub fn is_locked(&self) -> bool {
62 self.status.is_locked()
63 }
64
65 pub fn nonce(&self) -> Felt {
66 self.account_data.nonce()
67 }
68}
69
70impl TryFrom<AccountRecord> for Account {
71 type Error = ClientError;
72
73 fn try_from(value: AccountRecord) -> Result<Self, Self::Error> {
74 match value.account_data {
75 AccountRecordData::Full(acc) => Ok(acc),
76 AccountRecordData::Partial(acc) => Err(ClientError::AccountRecordNotFull(acc.id())),
77 }
78 }
79}
80
81impl TryFrom<AccountRecord> for PartialAccount {
82 type Error = ClientError;
83
84 fn try_from(value: AccountRecord) -> Result<Self, Self::Error> {
85 match value.account_data {
86 AccountRecordData::Partial(acc) => Ok(acc),
87 AccountRecordData::Full(acc) => Err(ClientError::AccountRecordNotPartial(acc.id())),
88 }
89 }
90}
91
92#[derive(Debug, Clone)]
99pub enum AccountStatus {
100 New { seed: Word },
103 Tracked,
105 Locked { seed: Option<Word> },
110}
111
112impl AccountStatus {
113 pub fn is_new(&self) -> bool {
114 matches!(self, AccountStatus::New { .. })
115 }
116
117 pub fn is_locked(&self) -> bool {
118 matches!(self, AccountStatus::Locked { .. })
119 }
120
121 pub fn seed(&self) -> Option<&Word> {
122 match self {
123 AccountStatus::New { seed } => Some(seed),
124 AccountStatus::Locked { seed } => seed.as_ref(),
125 AccountStatus::Tracked => None,
126 }
127 }
128}
129
130impl Display for AccountStatus {
131 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
132 match self {
133 AccountStatus::New { .. } => write!(f, "New"),
134 AccountStatus::Tracked => write!(f, "Tracked"),
135 AccountStatus::Locked { .. } => write!(f, "Locked"),
136 }
137 }
138}
139
140pub struct AccountUpdates {
145 updated_public_accounts: Vec<Account>,
147 mismatched_private_accounts: Vec<(AccountId, Word)>,
150}
151
152impl AccountUpdates {
153 pub fn new(
155 updated_public_accounts: Vec<Account>,
156 mismatched_private_accounts: Vec<(AccountId, Word)>,
157 ) -> Self {
158 Self {
159 updated_public_accounts,
160 mismatched_private_accounts,
161 }
162 }
163
164 pub fn updated_public_accounts(&self) -> &[Account] {
166 &self.updated_public_accounts
167 }
168
169 pub fn mismatched_private_accounts(&self) -> &[(AccountId, Word)] {
171 &self.mismatched_private_accounts
172 }
173}