dpp/tests/fixtures/
get_dpns_document_fixture.rs

1use std::collections::BTreeMap;
2
3use platform_value::{Identifier, Value};
4
5use crate::document::document_factory::DocumentFactory;
6use crate::document::Document;
7use crate::tests::utils::generate_random_identifier_struct;
8
9use super::get_dpns_data_contract_fixture;
10
11#[cfg(feature = "extended-document")]
12use crate::document::ExtendedDocument;
13use crate::prelude::IdentityNonce;
14use crate::util::strings::convert_to_homograph_safe_chars;
15
16pub struct ParentDocumentOptions {
17    pub label: String,
18    pub owner_id: Identifier,
19    pub identity_nonce: IdentityNonce,
20}
21
22impl Default for ParentDocumentOptions {
23    fn default() -> Self {
24        Self {
25            label: String::from("Parent"),
26            owner_id: generate_random_identifier_struct(),
27            identity_nonce: 0,
28        }
29    }
30}
31
32pub fn get_dpns_parent_document_fixture(
33    options: ParentDocumentOptions,
34    protocol_version: u32,
35) -> Document {
36    let data_contract = get_dpns_data_contract_fixture(
37        Some(options.owner_id),
38        options.identity_nonce,
39        protocol_version,
40    );
41    let document_factory =
42        DocumentFactory::new(protocol_version).expect("expected to get document factory");
43    let mut pre_order_salt = [0u8; 32];
44    let _ = getrandom::getrandom(&mut pre_order_salt);
45
46    let normalized_label = convert_to_homograph_safe_chars(options.label.as_str());
47
48    let mut map = BTreeMap::new();
49    map.insert("label".to_string(), Value::Text(options.label));
50    map.insert("normalizedLabel".to_string(), Value::Text(normalized_label));
51
52    map.insert("parentDomainName".to_string(), Value::Text(String::new()));
53    map.insert(
54        "normalizedParentDomainName".to_string(),
55        Value::Text(String::new()),
56    );
57    map.insert("preorderSalt".to_string(), Value::Bytes32(pre_order_salt));
58    map.insert(
59        "records".to_string(),
60        Value::Map(vec![(
61            Value::Text("dashUniqueIdentityId".to_string()),
62            Value::Identifier(options.owner_id.to_buffer()),
63        )]),
64    );
65    map.insert(
66        "subdomainRules".to_string(),
67        Value::Map(vec![(
68            Value::Text("allowSubdomains".to_string()),
69            Value::Bool(true),
70        )]),
71    );
72
73    document_factory
74        .create_document(
75            data_contract.data_contract(),
76            options.owner_id,
77            String::from("domain"),
78            map.into(),
79        )
80        .expect("DPNS document should be created")
81}
82
83#[cfg(feature = "extended-document")]
84pub fn get_dpns_parent_extended_document_fixture(
85    options: ParentDocumentOptions,
86    protocol_version: u32,
87) -> ExtendedDocument {
88    let data_contract = get_dpns_data_contract_fixture(
89        Some(options.owner_id),
90        options.identity_nonce,
91        protocol_version,
92    );
93    let document_factory =
94        DocumentFactory::new(protocol_version).expect("expected to get document factory");
95    let mut pre_order_salt = [0u8; 32];
96    let _ = getrandom::getrandom(&mut pre_order_salt);
97
98    let normalized_label = convert_to_homograph_safe_chars(options.label.as_str());
99
100    let mut map = BTreeMap::new();
101    map.insert("label".to_string(), Value::Text(options.label));
102    map.insert("normalizedLabel".to_string(), Value::Text(normalized_label));
103
104    map.insert("parentDomainName".to_string(), Value::Text(String::new()));
105    map.insert(
106        "normalizedParentDomainName".to_string(),
107        Value::Text(String::new()),
108    );
109
110    map.insert("preorderSalt".to_string(), Value::Bytes32(pre_order_salt));
111    map.insert(
112        "records".to_string(),
113        Value::Map(vec![(
114            Value::Text("dashUniqueIdentityId".to_string()),
115            Value::Identifier(options.owner_id.to_buffer()),
116        )]),
117    );
118    map.insert(
119        "subdomainRules".to_string(),
120        Value::Map(vec![(
121            Value::Text("allowSubdomains".to_string()),
122            Value::Bool(true),
123        )]),
124    );
125
126    document_factory
127        .create_extended_document(
128            data_contract.data_contract(),
129            options.owner_id,
130            String::from("domain"),
131            map.into(),
132        )
133        .expect("DPNS document should be created")
134}