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