miden_objects/conversion/
account.rs1use alloc::string::ToString;
2
3use miden_protocol::Word;
4use miden_protocol::account::{
5 AccountHeader,
6 AccountId,
7 AccountIdV1,
8 AccountStorageHeader,
9 PartialAccount,
10 PartialStorage,
11 PartialStorageMap,
12 StorageSlotId,
13 StorageSlotType,
14};
15use miden_protocol::asset::PartialVault;
16use miden_protocol::block::account_tree::AccountWitness;
17
18use crate::proto;
19
20#[cfg(test)]
21mod tests;
22
23impl From<&AccountIdV1> for proto::account::AccountIdV1 {
24 fn from(account_id: &AccountIdV1) -> Self {
25 Self {
26 suffix: Some(account_id.suffix().into()),
27 prefix: Some(account_id.prefix().as_felt().into()),
28 }
29 }
30}
31
32impl From<AccountIdV1> for proto::account::AccountIdV1 {
33 fn from(account_id: AccountIdV1) -> Self {
34 (&account_id).into()
35 }
36}
37
38impl From<&AccountId> for proto::account::AccountId {
39 fn from(account_id: &AccountId) -> Self {
40 let version = match account_id {
41 AccountId::V1(id) => proto::account::account_id::Version::V1(id.into()),
42 };
43 Self { version: Some(version) }
44 }
45}
46
47impl From<AccountId> for proto::account::AccountId {
48 fn from(account_id: AccountId) -> Self {
49 (&account_id).into()
50 }
51}
52
53impl From<StorageSlotId> for proto::account::StorageSlotId {
57 fn from(id: StorageSlotId) -> Self {
58 Self {
59 suffix: Some(id.suffix().into()),
60 prefix: Some(id.prefix().into()),
61 }
62 }
63}
64
65impl From<&StorageSlotId> for proto::account::StorageSlotId {
66 fn from(id: &StorageSlotId) -> Self {
67 (*id).into()
68 }
69}
70
71impl From<&AccountStorageHeader> for proto::account::AccountStorageHeader {
72 fn from(account_storage_header: &AccountStorageHeader) -> Self {
73 use proto::account::account_storage_header::storage_slot::Content;
74
75 Self {
76 slots: account_storage_header
77 .slots()
78 .map(|slot| {
79 let content = match slot.slot_type() {
80 StorageSlotType::Value => Content::Value(slot.value().into()),
81 StorageSlotType::Map => Content::MapRoot(slot.value().into()),
82 };
83 proto::account::account_storage_header::StorageSlot {
84 slot_name: slot.name().to_string(),
85 content: Some(content),
86 }
87 })
88 .collect(),
89 }
90 }
91}
92
93impl From<AccountStorageHeader> for proto::account::AccountStorageHeader {
94 fn from(account_storage_header: AccountStorageHeader) -> Self {
95 (&account_storage_header).into()
96 }
97}
98
99impl From<&PartialStorageMap> for proto::account::PartialStorageMap {
103 fn from(map: &PartialStorageMap) -> Self {
104 Self {
105 smt: Some(map.partial_smt().clone().into()),
106 keys: map.entries().map(|(key, _)| Word::from(*key).into()).collect(),
107 }
108 }
109}
110
111impl From<PartialStorageMap> for proto::account::PartialStorageMap {
112 fn from(map: PartialStorageMap) -> Self {
113 (&map).into()
114 }
115}
116
117impl From<&PartialStorage> for proto::account::PartialStorage {
121 fn from(storage: &PartialStorage) -> Self {
122 Self {
123 header: Some(storage.header().into()),
124 maps: storage.maps().map(Into::into).collect(),
125 }
126 }
127}
128
129impl From<PartialStorage> for proto::account::PartialStorage {
130 fn from(storage: PartialStorage) -> Self {
131 (&storage).into()
132 }
133}
134
135impl From<&PartialVault> for proto::account::PartialVault {
139 fn from(vault: &PartialVault) -> Self {
140 Self {
141 smt: Some(vault.partial_smt().clone().into()),
142 asset_ids: vault.asset_ids().map(|id| Word::from(id).into()).collect(),
143 }
144 }
145}
146
147impl From<PartialVault> for proto::account::PartialVault {
148 fn from(vault: PartialVault) -> Self {
149 (&vault).into()
150 }
151}
152
153impl From<&PartialAccount> for proto::account::PartialAccount {
157 fn from(account: &PartialAccount) -> Self {
158 Self {
159 account_id: Some(account.id().into()),
160 nonce: Some(account.nonce().into()),
161 code: Some(account.code().into()),
162 storage: Some(account.storage().into()),
163 vault: Some(account.vault().into()),
164 seed: account.seed().map(Into::into),
165 }
166 }
167}
168
169impl From<PartialAccount> for proto::account::PartialAccount {
170 fn from(account: PartialAccount) -> Self {
171 (&account).into()
172 }
173}
174
175impl From<&AccountHeader> for proto::account::AccountHeader {
176 fn from(account_header: &AccountHeader) -> Self {
177 Self {
178 version: proto::account::AccountVersion::V1 as i32,
179 account_id: Some(account_header.id().into()),
180 vault_root: Some(account_header.vault_root().into()),
181 storage_commitment: Some(account_header.storage_commitment().into()),
182 code_commitment: Some(account_header.code_commitment().into()),
183 nonce: account_header.nonce().as_canonical_u64(),
184 }
185 }
186}
187
188impl From<AccountHeader> for proto::account::AccountHeader {
189 fn from(account_header: AccountHeader) -> Self {
190 (&account_header).into()
191 }
192}
193
194impl From<&AccountWitness> for proto::account::AccountWitness {
195 fn from(witness: &AccountWitness) -> Self {
196 Self {
197 witness_id: Some(witness.id().into()),
198 commitment: Some(witness.state_commitment().into()),
199 path: Some(witness.path().clone().into()),
200 }
201 }
202}
203
204impl From<AccountWitness> for proto::account::AccountWitness {
205 fn from(witness: AccountWitness) -> Self {
206 (&witness).into()
207 }
208}