dpp/data_contract/config/v0/
mod.rs1use crate::data_contract::config;
2use crate::data_contract::config::v1::DataContractConfigV1;
3use crate::data_contract::config::{
4 DataContractConfig, DEFAULT_CONTRACT_CAN_BE_DELETED, DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
5 DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY, DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
6 DEFAULT_CONTRACT_KEEPS_HISTORY, DEFAULT_CONTRACT_MUTABILITY,
7};
8use crate::data_contract::storage_requirements::keys_for_document_type::StorageKeyRequirements;
9#[cfg(feature = "json-conversion")]
10use crate::serialization::json_safe_fields;
11use crate::ProtocolError;
12use bincode::{Decode, DecodeUntrusted, Encode};
13use platform_value::btreemap_extensions::BTreeValueMapHelper;
14use platform_value::Value;
15use serde::{Deserialize, Serialize};
16use std::collections::BTreeMap;
17
18#[cfg_attr(feature = "json-conversion", json_safe_fields)]
19#[derive(
20 Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq, DecodeUntrusted,
21)]
22#[serde(rename_all = "camelCase", default)]
23pub struct DataContractConfigV0 {
24 pub can_be_deleted: bool,
28 pub readonly: bool,
31 pub keeps_history: bool,
33 pub documents_keep_history_contract_default: bool,
36 pub documents_mutable_contract_default: bool,
40 pub documents_can_be_deleted_contract_default: bool,
44 pub requires_identity_encryption_bounded_key: Option<StorageKeyRequirements>,
46 pub requires_identity_decryption_bounded_key: Option<StorageKeyRequirements>,
48}
49
50pub trait DataContractConfigGettersV0 {
52 fn can_be_deleted(&self) -> bool;
54
55 fn readonly(&self) -> bool;
57
58 fn keeps_history(&self) -> bool;
60
61 fn documents_keep_history_contract_default(&self) -> bool;
63
64 fn documents_mutable_contract_default(&self) -> bool;
66 fn documents_can_be_deleted_contract_default(&self) -> bool;
67
68 fn requires_identity_encryption_bounded_key(&self) -> Option<StorageKeyRequirements>;
70
71 fn requires_identity_decryption_bounded_key(&self) -> Option<StorageKeyRequirements>;
73}
74
75pub trait DataContractConfigSettersV0 {
77 fn set_can_be_deleted(&mut self, value: bool);
79
80 fn set_readonly(&mut self, value: bool);
82
83 fn set_keeps_history(&mut self, value: bool);
85
86 fn set_documents_keep_history_contract_default(&mut self, value: bool);
88
89 fn set_documents_mutable_contract_default(&mut self, value: bool);
91
92 fn set_documents_can_be_deleted_contract_default(&mut self, value: bool);
94
95 fn set_requires_identity_encryption_bounded_key(
97 &mut self,
98 value: Option<StorageKeyRequirements>,
99 );
100
101 fn set_requires_identity_decryption_bounded_key(
103 &mut self,
104 value: Option<StorageKeyRequirements>,
105 );
106}
107
108impl Default for DataContractConfigV0 {
109 fn default() -> Self {
110 DataContractConfigV0 {
111 can_be_deleted: DEFAULT_CONTRACT_CAN_BE_DELETED,
112 readonly: !DEFAULT_CONTRACT_MUTABILITY,
113 keeps_history: DEFAULT_CONTRACT_KEEPS_HISTORY,
114 documents_keep_history_contract_default: DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY,
115 documents_mutable_contract_default: DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
116 documents_can_be_deleted_contract_default: DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
117 requires_identity_encryption_bounded_key: None,
118 requires_identity_decryption_bounded_key: None,
119 }
120 }
121}
122
123impl DataContractConfigV0 {
124 pub fn default_with_version() -> DataContractConfig {
125 Self::default().into()
126 }
127}
128
129impl DataContractConfigV0 {
130 #[inline(always)]
149 pub(super) fn get_contract_configuration_properties_v0(
150 contract: &BTreeMap<String, Value>,
151 ) -> Result<DataContractConfigV0, ProtocolError> {
152 let keeps_history = contract
153 .get_optional_bool(config::property::KEEPS_HISTORY)?
154 .unwrap_or(DEFAULT_CONTRACT_KEEPS_HISTORY);
155 let can_be_deleted = contract
156 .get_optional_bool(config::property::CAN_BE_DELETED)?
157 .unwrap_or(DEFAULT_CONTRACT_CAN_BE_DELETED);
158
159 let readonly = contract
160 .get_optional_bool(config::property::READONLY)?
161 .unwrap_or(!DEFAULT_CONTRACT_MUTABILITY);
162
163 let documents_keep_history_contract_default = contract
164 .get_optional_bool(config::property::DOCUMENTS_KEEP_HISTORY_CONTRACT_DEFAULT)?
165 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY);
166
167 let documents_mutable_contract_default = contract
168 .get_optional_bool(config::property::DOCUMENTS_MUTABLE_CONTRACT_DEFAULT)?
169 .unwrap_or(DEFAULT_CONTRACT_DOCUMENT_MUTABILITY);
170
171 let documents_can_be_deleted_contract_default = contract
172 .get_optional_bool(config::property::DOCUMENTS_CAN_BE_DELETED_CONTRACT_DEFAULT)?
173 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED);
174
175 let requires_identity_encryption_bounded_key = contract
176 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
177 .map(|int| int.try_into())
178 .transpose()?;
179
180 let requires_identity_decryption_bounded_key = contract
187 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
188 .map(|int| int.try_into())
189 .transpose()?;
190
191 Ok(DataContractConfigV0 {
192 can_be_deleted,
193 readonly,
194 keeps_history,
195 documents_keep_history_contract_default,
196 documents_mutable_contract_default,
197 documents_can_be_deleted_contract_default,
198 requires_identity_encryption_bounded_key,
199 requires_identity_decryption_bounded_key,
200 })
201 }
202}
203
204impl From<DataContractConfigV1> for DataContractConfigV0 {
205 fn from(value: DataContractConfigV1) -> Self {
206 DataContractConfigV0 {
207 can_be_deleted: value.can_be_deleted,
208 readonly: value.readonly,
209 keeps_history: value.keeps_history,
210 documents_keep_history_contract_default: value.documents_keep_history_contract_default,
211 documents_mutable_contract_default: value.documents_mutable_contract_default,
212 documents_can_be_deleted_contract_default: value
213 .documents_can_be_deleted_contract_default,
214 requires_identity_encryption_bounded_key: value
215 .requires_identity_encryption_bounded_key,
216 requires_identity_decryption_bounded_key: value
217 .requires_identity_decryption_bounded_key,
218 }
219 }
220}