Skip to main content

data_contracts/
lib.rs

1mod error;
2
3use serde_json::Value;
4
5use crate::error::Error;
6
7#[cfg(feature = "dashpay")]
8pub use dashpay_contract;
9
10#[cfg(feature = "dpns")]
11pub use dpns_contract;
12
13#[cfg(feature = "keyword-search")]
14pub use keyword_search_contract;
15
16#[cfg(feature = "masternode-rewards")]
17pub use masternode_reward_shares_contract;
18
19use platform_value::Identifier;
20use platform_version::version::PlatformVersion;
21
22#[cfg(feature = "token-history")]
23pub use token_history_contract;
24
25#[cfg(feature = "wallet-utils")]
26pub use wallet_utils_contract;
27
28#[cfg(feature = "withdrawals")]
29pub use withdrawals_contract;
30
31#[repr(u8)]
32#[derive(PartialEq, Eq, Clone, Copy, Debug, Ord, PartialOrd, Hash)]
33pub enum SystemDataContract {
34    Withdrawals = 0,
35    MasternodeRewards = 1,
36    /// Reserved slot — the feature-flags contract was never deployed at genesis
37    /// and its implementation has been removed. The discriminant `2` is kept to
38    /// preserve the stable numbering of subsequent variants.
39    FeatureFlags = 2,
40    DPNS = 3,
41    Dashpay = 4,
42    WalletUtils = 5,
43    TokenHistory = 6,
44    KeywordSearch = 7,
45}
46
47pub struct DataContractSource {
48    pub id_bytes: [u8; 32],
49    pub owner_id_bytes: [u8; 32],
50    pub version: u32,
51    pub definitions: Option<Value>,
52    pub document_schemas: Value,
53}
54
55impl SystemDataContract {
56    pub fn id(&self) -> Identifier {
57        let bytes = match self {
58            #[cfg(feature = "withdrawals")]
59            SystemDataContract::Withdrawals => withdrawals_contract::ID_BYTES,
60            #[cfg(not(feature = "withdrawals"))]
61            SystemDataContract::Withdrawals => [
62                54, 98, 187, 97, 225, 127, 174, 62, 162, 148, 207, 96, 49, 151, 251, 10, 171, 109,
63                81, 24, 11, 216, 182, 16, 76, 73, 68, 166, 47, 226, 217, 127,
64            ],
65
66            #[cfg(feature = "masternode-rewards")]
67            SystemDataContract::MasternodeRewards => masternode_reward_shares_contract::ID_BYTES,
68            #[cfg(not(feature = "masternode-rewards"))]
69            SystemDataContract::MasternodeRewards => [
70                12, 172, 226, 5, 36, 102, 147, 167, 200, 21, 101, 35, 98, 13, 170, 147, 125, 47,
71                34, 71, 147, 68, 99, 238, 176, 31, 247, 33, 149, 144, 149, 140,
72            ],
73
74            // Reserved: feature-flags contract was removed but the ID is kept for
75            // discriminant stability and to prevent reuse of a potentially meaningful
76            // Identifier on-chain.
77            SystemDataContract::FeatureFlags => [
78                245, 172, 216, 200, 193, 110, 185, 172, 40, 110, 7, 132, 190, 86, 127, 80, 9, 244,
79                86, 26, 243, 212, 255, 2, 91, 7, 90, 243, 68, 55, 152, 34,
80            ],
81
82            #[cfg(feature = "dpns")]
83            SystemDataContract::DPNS => dpns_contract::ID_BYTES,
84            #[cfg(not(feature = "dpns"))]
85            SystemDataContract::DPNS => [
86                230, 104, 198, 89, 175, 102, 174, 225, 231, 44, 24, 109, 222, 123, 91, 126, 10, 29,
87                113, 42, 9, 196, 13, 87, 33, 246, 34, 191, 83, 197, 49, 85,
88            ],
89
90            #[cfg(feature = "dashpay")]
91            SystemDataContract::Dashpay => dashpay_contract::ID_BYTES,
92            #[cfg(not(feature = "dashpay"))]
93            SystemDataContract::Dashpay => [
94                162, 161, 180, 172, 111, 239, 34, 234, 42, 26, 104, 232, 18, 54, 68, 179, 87, 135,
95                95, 107, 65, 44, 24, 16, 146, 129, 193, 70, 231, 178, 113, 188,
96            ],
97
98            #[cfg(feature = "wallet-utils")]
99            SystemDataContract::WalletUtils => wallet_utils_contract::ID_BYTES,
100            #[cfg(not(feature = "wallet-utils"))]
101            SystemDataContract::WalletUtils => [
102                92, 20, 14, 101, 92, 2, 101, 187, 194, 168, 8, 113, 109, 225, 132, 121, 133, 19,
103                89, 24, 173, 81, 205, 253, 11, 118, 102, 75, 169, 91, 163, 124,
104            ],
105
106            #[cfg(feature = "token-history")]
107            SystemDataContract::TokenHistory => token_history_contract::ID_BYTES,
108            #[cfg(not(feature = "token-history"))]
109            SystemDataContract::TokenHistory => [
110                45, 67, 89, 21, 34, 216, 145, 78, 156, 243, 17, 58, 202, 190, 13, 92, 61, 40, 122,
111                201, 84, 99, 187, 110, 233, 128, 63, 48, 172, 29, 210, 108,
112            ],
113
114            #[cfg(feature = "keyword-search")]
115            SystemDataContract::KeywordSearch => keyword_search_contract::ID_BYTES,
116            #[cfg(not(feature = "keyword-search"))]
117            SystemDataContract::KeywordSearch => [
118                92, 20, 14, 101, 92, 2, 101, 187, 194, 168, 8, 113, 109, 225, 132, 121, 133, 19,
119                89, 24, 173, 81, 205, 253, 11, 118, 102, 75, 169, 91, 163, 124,
120            ],
121        };
122        Identifier::new(bytes)
123    }
124    /// Returns [DataContractSource]
125    pub fn source(self, platform_version: &PlatformVersion) -> Result<DataContractSource, Error> {
126        match self {
127            #[cfg(feature = "withdrawals")]
128            SystemDataContract::Withdrawals => Ok(DataContractSource {
129                id_bytes: withdrawals_contract::ID_BYTES,
130                owner_id_bytes: withdrawals_contract::OWNER_ID_BYTES,
131                version: platform_version.system_data_contracts.withdrawals as u32,
132                definitions: withdrawals_contract::load_definitions(platform_version)?,
133                document_schemas: withdrawals_contract::load_documents_schemas(platform_version)?,
134            }),
135            #[cfg(not(feature = "withdrawals"))]
136            SystemDataContract::Withdrawals => Err(Error::ContractNotIncluded("withdrawals")),
137
138            #[cfg(feature = "masternode-rewards")]
139            SystemDataContract::MasternodeRewards => Ok(DataContractSource {
140                id_bytes: masternode_reward_shares_contract::ID_BYTES,
141                owner_id_bytes: masternode_reward_shares_contract::OWNER_ID_BYTES,
142                version: platform_version
143                    .system_data_contracts
144                    .masternode_reward_shares as u32,
145                definitions: masternode_reward_shares_contract::load_definitions(platform_version)?,
146                document_schemas: masternode_reward_shares_contract::load_documents_schemas(
147                    platform_version,
148                )?,
149            }),
150            #[cfg(not(feature = "masternode-rewards"))]
151            SystemDataContract::MasternodeRewards => {
152                Err(Error::ContractNotIncluded("masternode-rewards"))
153            }
154
155            // Reserved: feature-flags contract was removed. The variant exists only
156            // to preserve discriminant stability; loading it is not supported.
157            SystemDataContract::FeatureFlags => Err(Error::ContractReserved("feature-flags")),
158
159            #[cfg(feature = "dpns")]
160            SystemDataContract::DPNS => Ok(DataContractSource {
161                id_bytes: dpns_contract::ID_BYTES,
162                owner_id_bytes: dpns_contract::OWNER_ID_BYTES,
163                version: platform_version.system_data_contracts.dpns as u32,
164                definitions: dpns_contract::load_definitions(platform_version)?,
165                document_schemas: dpns_contract::load_documents_schemas(platform_version)?,
166            }),
167            #[cfg(not(feature = "dpns"))]
168            SystemDataContract::DPNS => Err(Error::ContractNotIncluded("dpns")),
169
170            #[cfg(feature = "dashpay")]
171            SystemDataContract::Dashpay => Ok(DataContractSource {
172                id_bytes: dashpay_contract::ID_BYTES,
173                owner_id_bytes: dashpay_contract::OWNER_ID_BYTES,
174                version: platform_version.system_data_contracts.dashpay as u32,
175                definitions: dashpay_contract::load_definitions(platform_version)?,
176                document_schemas: dashpay_contract::load_documents_schemas(platform_version)?,
177            }),
178            #[cfg(not(feature = "dashpay"))]
179            SystemDataContract::Dashpay => Err(Error::ContractNotIncluded("dashpay")),
180
181            #[cfg(feature = "wallet-utils")]
182            SystemDataContract::WalletUtils => Ok(DataContractSource {
183                id_bytes: wallet_utils_contract::ID_BYTES,
184                owner_id_bytes: wallet_utils_contract::OWNER_ID_BYTES,
185                version: platform_version.system_data_contracts.wallet as u32,
186                definitions: wallet_utils_contract::load_definitions(platform_version)?,
187                document_schemas: wallet_utils_contract::load_documents_schemas(platform_version)?,
188            }),
189            #[cfg(not(feature = "wallet-utils"))]
190            SystemDataContract::WalletUtils => Err(Error::ContractNotIncluded("wallet-utils")),
191
192            #[cfg(feature = "token-history")]
193            SystemDataContract::TokenHistory => Ok(DataContractSource {
194                id_bytes: token_history_contract::ID_BYTES,
195                owner_id_bytes: token_history_contract::OWNER_ID_BYTES,
196                version: platform_version.system_data_contracts.token_history as u32,
197                definitions: token_history_contract::load_definitions(platform_version)?,
198                document_schemas: token_history_contract::load_documents_schemas(platform_version)?,
199            }),
200            #[cfg(not(feature = "token-history"))]
201            SystemDataContract::TokenHistory => Err(Error::ContractNotIncluded("token-history")),
202
203            #[cfg(feature = "keyword-search")]
204            SystemDataContract::KeywordSearch => Ok(DataContractSource {
205                id_bytes: keyword_search_contract::ID_BYTES,
206                owner_id_bytes: keyword_search_contract::OWNER_ID_BYTES,
207                version: platform_version.system_data_contracts.keyword_search as u32,
208                definitions: keyword_search_contract::load_definitions(platform_version)?,
209                document_schemas: keyword_search_contract::load_documents_schemas(
210                    platform_version,
211                )?,
212            }),
213            #[cfg(not(feature = "keyword-search"))]
214            SystemDataContract::KeywordSearch => Err(Error::ContractNotIncluded("keyword-search")),
215        }
216    }
217}