Skip to main content

miden_objects/conversion/
account.rs

1use alloc::string::ToString;
2
3use miden_protocol::Word;
4use miden_protocol::account::{
5    Account,
6    AccountHeader,
7    AccountId,
8    AccountIdV1,
9    AccountStorage,
10    AccountStorageHeader,
11    PartialAccount,
12    PartialStorage,
13    PartialStorageMap,
14    StorageMap,
15    StorageSlot,
16    StorageSlotContent,
17    StorageSlotId,
18    StorageSlotType,
19};
20use miden_protocol::asset::PartialVault;
21use miden_protocol::block::account_tree::AccountWitness;
22
23use crate::proto;
24
25#[cfg(test)]
26mod tests;
27
28impl From<&AccountIdV1> for proto::account::AccountIdV1 {
29    fn from(account_id: &AccountIdV1) -> Self {
30        Self {
31            suffix: Some(account_id.suffix().into()),
32            prefix: Some(account_id.prefix().as_felt().into()),
33        }
34    }
35}
36
37impl From<AccountIdV1> for proto::account::AccountIdV1 {
38    fn from(account_id: AccountIdV1) -> Self {
39        (&account_id).into()
40    }
41}
42
43impl From<&AccountId> for proto::account::AccountId {
44    fn from(account_id: &AccountId) -> Self {
45        let version = match account_id {
46            AccountId::V1(id) => proto::account::account_id::Version::V1(id.into()),
47        };
48        Self { version: Some(version) }
49    }
50}
51
52impl From<AccountId> for proto::account::AccountId {
53    fn from(account_id: AccountId) -> Self {
54        (&account_id).into()
55    }
56}
57
58// STORAGE SLOT ID
59// ================================================================================================
60
61impl From<StorageSlotId> for proto::account::StorageSlotId {
62    fn from(id: StorageSlotId) -> Self {
63        Self {
64            suffix: Some(id.suffix().into()),
65            prefix: Some(id.prefix().into()),
66        }
67    }
68}
69
70impl From<&StorageSlotId> for proto::account::StorageSlotId {
71    fn from(id: &StorageSlotId) -> Self {
72        (*id).into()
73    }
74}
75
76impl From<&AccountStorageHeader> for proto::account::AccountStorageHeader {
77    fn from(account_storage_header: &AccountStorageHeader) -> Self {
78        use proto::account::account_storage_header::storage_slot::Content;
79
80        Self {
81            slots: account_storage_header
82                .slots()
83                .map(|slot| {
84                    let content = match slot.slot_type() {
85                        StorageSlotType::Value => Content::Value(slot.value().into()),
86                        StorageSlotType::Map => Content::MapRoot(slot.value().into()),
87                    };
88                    proto::account::account_storage_header::StorageSlot {
89                        slot_name: slot.name().to_string(),
90                        content: Some(content),
91                    }
92                })
93                .collect(),
94        }
95    }
96}
97
98impl From<AccountStorageHeader> for proto::account::AccountStorageHeader {
99    fn from(account_storage_header: AccountStorageHeader) -> Self {
100        (&account_storage_header).into()
101    }
102}
103
104// PARTIAL STORAGE MAP
105// ================================================================================================
106
107impl From<&PartialStorageMap> for proto::account::PartialStorageMap {
108    fn from(map: &PartialStorageMap) -> Self {
109        Self {
110            smt: Some(map.partial_smt().clone().into()),
111            keys: map.entries().map(|(key, _)| Word::from(*key).into()).collect(),
112        }
113    }
114}
115
116impl From<PartialStorageMap> for proto::account::PartialStorageMap {
117    fn from(map: PartialStorageMap) -> Self {
118        (&map).into()
119    }
120}
121
122// PARTIAL STORAGE
123// ================================================================================================
124
125impl From<&PartialStorage> for proto::account::PartialStorage {
126    fn from(storage: &PartialStorage) -> Self {
127        Self {
128            header: Some(storage.header().into()),
129            maps: storage.maps().map(Into::into).collect(),
130        }
131    }
132}
133
134impl From<PartialStorage> for proto::account::PartialStorage {
135    fn from(storage: PartialStorage) -> Self {
136        (&storage).into()
137    }
138}
139
140// PARTIAL VAULT
141// ================================================================================================
142
143impl From<&PartialVault> for proto::account::PartialVault {
144    fn from(vault: &PartialVault) -> Self {
145        Self {
146            smt: Some(vault.partial_smt().clone().into()),
147            asset_ids: vault.asset_ids().map(|id| Word::from(id).into()).collect(),
148        }
149    }
150}
151
152impl From<PartialVault> for proto::account::PartialVault {
153    fn from(vault: PartialVault) -> Self {
154        (&vault).into()
155    }
156}
157
158// PARTIAL ACCOUNT
159// ================================================================================================
160
161impl From<&PartialAccount> for proto::account::PartialAccount {
162    fn from(account: &PartialAccount) -> Self {
163        Self {
164            account_id: Some(account.id().into()),
165            nonce: Some(account.nonce().into()),
166            code: Some(account.code().into()),
167            storage: Some(account.storage().into()),
168            vault: Some(account.vault().into()),
169            seed: account.seed().map(Into::into),
170        }
171    }
172}
173
174impl From<PartialAccount> for proto::account::PartialAccount {
175    fn from(account: PartialAccount) -> Self {
176        (&account).into()
177    }
178}
179
180impl From<&AccountHeader> for proto::account::AccountHeader {
181    fn from(account_header: &AccountHeader) -> Self {
182        Self {
183            version: proto::account::AccountVersion::V1 as i32,
184            account_id: Some(account_header.id().into()),
185            vault_root: Some(account_header.vault_root().into()),
186            storage_commitment: Some(account_header.storage_commitment().into()),
187            code_commitment: Some(account_header.code_commitment().into()),
188            nonce: account_header.nonce().as_canonical_u64(),
189        }
190    }
191}
192
193impl From<AccountHeader> for proto::account::AccountHeader {
194    fn from(account_header: AccountHeader) -> Self {
195        (&account_header).into()
196    }
197}
198
199impl From<&AccountWitness> for proto::account::AccountWitness {
200    fn from(witness: &AccountWitness) -> Self {
201        Self {
202            witness_id: Some(witness.id().into()),
203            commitment: Some(witness.state_commitment().into()),
204            path: Some(witness.path().clone().into()),
205        }
206    }
207}
208
209impl From<AccountWitness> for proto::account::AccountWitness {
210    fn from(witness: AccountWitness) -> Self {
211        (&witness).into()
212    }
213}
214
215// STORAGE MAP
216// ================================================================================================
217
218impl From<&StorageMap> for proto::account::StorageMap {
219    fn from(map: &StorageMap) -> Self {
220        Self {
221            entries: map
222                .entries()
223                .map(|(key, value)| proto::account::StorageMapEntry {
224                    key: Some(Word::from(*key).into()),
225                    value: Some(value.into()),
226                })
227                .collect(),
228        }
229    }
230}
231
232impl From<StorageMap> for proto::account::StorageMap {
233    fn from(map: StorageMap) -> Self {
234        (&map).into()
235    }
236}
237
238// STORAGE SLOT
239// ================================================================================================
240
241impl From<&StorageSlot> for proto::account::StorageSlot {
242    fn from(slot: &StorageSlot) -> Self {
243        use proto::account::storage_slot::StorageSlotContent as WireStorageSlotContent;
244
245        let content = match slot.content() {
246            StorageSlotContent::Value(value) => WireStorageSlotContent::Value(value.into()),
247            StorageSlotContent::Map(map) => WireStorageSlotContent::Map(map.into()),
248        };
249        Self {
250            slot_name: slot.name().to_string(),
251            storage_slot_content: Some(content),
252        }
253    }
254}
255
256impl From<StorageSlot> for proto::account::StorageSlot {
257    fn from(slot: StorageSlot) -> Self {
258        (&slot).into()
259    }
260}
261
262// ACCOUNT STORAGE
263// ================================================================================================
264
265impl From<&AccountStorage> for proto::account::AccountStorage {
266    fn from(storage: &AccountStorage) -> Self {
267        Self {
268            slots: storage.slots().iter().map(Into::into).collect(),
269        }
270    }
271}
272
273impl From<AccountStorage> for proto::account::AccountStorage {
274    fn from(storage: AccountStorage) -> Self {
275        (&storage).into()
276    }
277}
278
279// ACCOUNT
280// ================================================================================================
281
282impl From<&Account> for proto::account::Account {
283    fn from(account: &Account) -> Self {
284        Self {
285            version: proto::account::AccountVersion::V1 as i32,
286            account_id: Some(account.id().into()),
287            nonce: Some(account.nonce().into()),
288            code: Some(account.code().into()),
289            storage: Some(account.storage().into()),
290            vault: Some(account.vault().into()),
291            seed: account.seed().map(Into::into),
292        }
293    }
294}
295
296impl From<Account> for proto::account::Account {
297    fn from(account: Account) -> Self {
298        (&account).into()
299    }
300}