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            version: proto::account::AccountVersion::V1 as i32,
165            account_id: Some(account.id().into()),
166            nonce: Some(account.nonce().into()),
167            code: Some(account.code().into()),
168            storage: Some(account.storage().into()),
169            vault: Some(account.vault().into()),
170            seed: account.seed().map(Into::into),
171        }
172    }
173}
174
175impl From<PartialAccount> for proto::account::PartialAccount {
176    fn from(account: PartialAccount) -> Self {
177        (&account).into()
178    }
179}
180
181impl From<&AccountHeader> for proto::account::AccountHeader {
182    fn from(account_header: &AccountHeader) -> Self {
183        Self {
184            version: proto::account::AccountVersion::V1 as i32,
185            account_id: Some(account_header.id().into()),
186            vault_root: Some(account_header.vault_root().into()),
187            storage_commitment: Some(account_header.storage_commitment().into()),
188            code_commitment: Some(account_header.code_commitment().into()),
189            nonce: account_header.nonce().as_canonical_u64(),
190        }
191    }
192}
193
194impl From<AccountHeader> for proto::account::AccountHeader {
195    fn from(account_header: AccountHeader) -> Self {
196        (&account_header).into()
197    }
198}
199
200impl From<&AccountWitness> for proto::account::AccountWitness {
201    fn from(witness: &AccountWitness) -> Self {
202        Self {
203            witness_id: Some(witness.id().into()),
204            commitment: Some(witness.state_commitment().into()),
205            path: Some(witness.path().clone().into()),
206        }
207    }
208}
209
210impl From<AccountWitness> for proto::account::AccountWitness {
211    fn from(witness: AccountWitness) -> Self {
212        (&witness).into()
213    }
214}
215
216// STORAGE MAP
217// ================================================================================================
218
219impl From<&StorageMap> for proto::account::StorageMap {
220    fn from(map: &StorageMap) -> Self {
221        Self {
222            entries: map
223                .entries()
224                .map(|(key, value)| proto::account::StorageMapEntry {
225                    key: Some(Word::from(*key).into()),
226                    value: Some(value.into()),
227                })
228                .collect(),
229        }
230    }
231}
232
233impl From<StorageMap> for proto::account::StorageMap {
234    fn from(map: StorageMap) -> Self {
235        (&map).into()
236    }
237}
238
239// STORAGE SLOT
240// ================================================================================================
241
242impl From<&StorageSlot> for proto::account::StorageSlot {
243    fn from(slot: &StorageSlot) -> Self {
244        use proto::account::storage_slot::StorageSlotContent as WireStorageSlotContent;
245
246        let content = match slot.content() {
247            StorageSlotContent::Value(value) => WireStorageSlotContent::Value(value.into()),
248            StorageSlotContent::Map(map) => WireStorageSlotContent::Map(map.into()),
249        };
250        Self {
251            slot_name: slot.name().to_string(),
252            storage_slot_content: Some(content),
253        }
254    }
255}
256
257impl From<StorageSlot> for proto::account::StorageSlot {
258    fn from(slot: StorageSlot) -> Self {
259        (&slot).into()
260    }
261}
262
263// ACCOUNT STORAGE
264// ================================================================================================
265
266impl From<&AccountStorage> for proto::account::AccountStorage {
267    fn from(storage: &AccountStorage) -> Self {
268        Self {
269            slots: storage.slots().iter().map(Into::into).collect(),
270        }
271    }
272}
273
274impl From<AccountStorage> for proto::account::AccountStorage {
275    fn from(storage: AccountStorage) -> Self {
276        (&storage).into()
277    }
278}
279
280// ACCOUNT
281// ================================================================================================
282
283impl From<&Account> for proto::account::Account {
284    fn from(account: &Account) -> Self {
285        Self {
286            version: proto::account::AccountVersion::V1 as i32,
287            account_id: Some(account.id().into()),
288            nonce: Some(account.nonce().into()),
289            code: Some(account.code().into()),
290            storage: Some(account.storage().into()),
291            vault: Some(account.vault().into()),
292            seed: account.seed().map(Into::into),
293        }
294    }
295}
296
297impl From<Account> for proto::account::Account {
298    fn from(account: Account) -> Self {
299        (&account).into()
300    }
301}