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, 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(Serialize, Deserialize, Decode, Encode, Debug, Clone, Copy, PartialEq, Eq)]
20#[serde(rename_all = "camelCase", default)]
21pub struct DataContractConfigV0 {
22 pub can_be_deleted: bool,
26 pub readonly: bool,
29 pub keeps_history: bool,
31 pub documents_keep_history_contract_default: bool,
34 pub documents_mutable_contract_default: bool,
38 pub documents_can_be_deleted_contract_default: bool,
42 pub requires_identity_encryption_bounded_key: Option<StorageKeyRequirements>,
44 pub requires_identity_decryption_bounded_key: Option<StorageKeyRequirements>,
46}
47
48pub trait DataContractConfigGettersV0 {
50 fn can_be_deleted(&self) -> bool;
52
53 fn readonly(&self) -> bool;
55
56 fn keeps_history(&self) -> bool;
58
59 fn documents_keep_history_contract_default(&self) -> bool;
61
62 fn documents_mutable_contract_default(&self) -> bool;
64 fn documents_can_be_deleted_contract_default(&self) -> bool;
65
66 fn requires_identity_encryption_bounded_key(&self) -> Option<StorageKeyRequirements>;
68
69 fn requires_identity_decryption_bounded_key(&self) -> Option<StorageKeyRequirements>;
71}
72
73pub trait DataContractConfigSettersV0 {
75 fn set_can_be_deleted(&mut self, value: bool);
77
78 fn set_readonly(&mut self, value: bool);
80
81 fn set_keeps_history(&mut self, value: bool);
83
84 fn set_documents_keep_history_contract_default(&mut self, value: bool);
86
87 fn set_documents_mutable_contract_default(&mut self, value: bool);
89
90 fn set_documents_can_be_deleted_contract_default(&mut self, value: bool);
92
93 fn set_requires_identity_encryption_bounded_key(
95 &mut self,
96 value: Option<StorageKeyRequirements>,
97 );
98
99 fn set_requires_identity_decryption_bounded_key(
101 &mut self,
102 value: Option<StorageKeyRequirements>,
103 );
104}
105
106impl Default for DataContractConfigV0 {
107 fn default() -> Self {
108 DataContractConfigV0 {
109 can_be_deleted: DEFAULT_CONTRACT_CAN_BE_DELETED,
110 readonly: !DEFAULT_CONTRACT_MUTABILITY,
111 keeps_history: DEFAULT_CONTRACT_KEEPS_HISTORY,
112 documents_keep_history_contract_default: DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY,
113 documents_mutable_contract_default: DEFAULT_CONTRACT_DOCUMENT_MUTABILITY,
114 documents_can_be_deleted_contract_default: DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED,
115 requires_identity_encryption_bounded_key: None,
116 requires_identity_decryption_bounded_key: None,
117 }
118 }
119}
120
121impl DataContractConfigV0 {
122 pub fn default_with_version() -> DataContractConfig {
123 Self::default().into()
124 }
125}
126
127impl DataContractConfigV0 {
128 #[inline(always)]
147 pub(super) fn get_contract_configuration_properties_v0(
148 contract: &BTreeMap<String, Value>,
149 ) -> Result<DataContractConfigV0, ProtocolError> {
150 let keeps_history = contract
151 .get_optional_bool(config::property::KEEPS_HISTORY)?
152 .unwrap_or(DEFAULT_CONTRACT_KEEPS_HISTORY);
153 let can_be_deleted = contract
154 .get_optional_bool(config::property::CAN_BE_DELETED)?
155 .unwrap_or(DEFAULT_CONTRACT_CAN_BE_DELETED);
156
157 let readonly = contract
158 .get_optional_bool(config::property::READONLY)?
159 .unwrap_or(!DEFAULT_CONTRACT_MUTABILITY);
160
161 let documents_keep_history_contract_default = contract
162 .get_optional_bool(config::property::DOCUMENTS_KEEP_HISTORY_CONTRACT_DEFAULT)?
163 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_KEEPS_HISTORY);
164
165 let documents_mutable_contract_default = contract
166 .get_optional_bool(config::property::DOCUMENTS_MUTABLE_CONTRACT_DEFAULT)?
167 .unwrap_or(DEFAULT_CONTRACT_DOCUMENT_MUTABILITY);
168
169 let documents_can_be_deleted_contract_default = contract
170 .get_optional_bool(config::property::DOCUMENTS_CAN_BE_DELETED_CONTRACT_DEFAULT)?
171 .unwrap_or(DEFAULT_CONTRACT_DOCUMENTS_CAN_BE_DELETED);
172
173 let requires_identity_encryption_bounded_key = contract
174 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
175 .map(|int| int.try_into())
176 .transpose()?;
177
178 let requires_identity_decryption_bounded_key = contract
185 .get_optional_integer::<u8>(config::property::REQUIRES_IDENTITY_ENCRYPTION_BOUNDED_KEY)?
186 .map(|int| int.try_into())
187 .transpose()?;
188
189 Ok(DataContractConfigV0 {
190 can_be_deleted,
191 readonly,
192 keeps_history,
193 documents_keep_history_contract_default,
194 documents_mutable_contract_default,
195 documents_can_be_deleted_contract_default,
196 requires_identity_encryption_bounded_key,
197 requires_identity_decryption_bounded_key,
198 })
199 }
200}
201
202impl From<DataContractConfigV1> for DataContractConfigV0 {
203 fn from(value: DataContractConfigV1) -> Self {
204 DataContractConfigV0 {
205 can_be_deleted: value.can_be_deleted,
206 readonly: value.readonly,
207 keeps_history: value.keeps_history,
208 documents_keep_history_contract_default: value.documents_keep_history_contract_default,
209 documents_mutable_contract_default: value.documents_mutable_contract_default,
210 documents_can_be_deleted_contract_default: value
211 .documents_can_be_deleted_contract_default,
212 requires_identity_encryption_bounded_key: value
213 .requires_identity_encryption_bounded_key,
214 requires_identity_decryption_bounded_key: value
215 .requires_identity_decryption_bounded_key,
216 }
217 }
218}