@dashevo/wasm-dpp 0.24.0-dev.12 → 0.24.0-dev.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +3 -0
- package/README.md +5 -1
- package/dist/index.js +1 -1
- package/dist/index.js.LICENSE.txt +9 -0
- package/dist/wasm/wasm_dpp.d.ts +1495 -565
- package/karma.conf.js +5 -0
- package/lib/identifier/patchIdentifier.ts +5 -4
- package/lib/index.ts +1 -1
- package/lib/test/bootstrap.js +5 -0
- package/lib/test/mocks/getBlsAdapterMock.js +36 -0
- package/lib/test/utils/newDocumentsContainer.js +36 -0
- package/package.json +7 -4
- package/src/bls_adapter.rs +16 -1
- package/src/data_contract/data_contract.rs +35 -7
- package/src/data_contract/errors/invalid_document_type.rs +1 -1
- package/src/data_contract/index.rs +56 -1
- package/src/data_contract/mod.rs +1 -0
- package/src/data_contract/state_transition/data_contract_create_transition/mod.rs +2 -4
- package/src/data_contract/state_transition/data_contract_create_transition/validation.rs +3 -3
- package/src/data_contract/state_transition/data_contract_update_transition/mod.rs +2 -4
- package/src/data_contract/state_transition/data_contract_update_transition/validation.rs +9 -6
- package/src/data_contract_factory/data_contract_factory.rs +4 -6
- package/src/document/errors/invalid_document_error.rs +10 -9
- package/src/document/errors/invalid_initial_revision_error.rs +1 -1
- package/src/document/errors/mismatch_owners_ids_error.rs +14 -4
- package/src/document/errors/mod.rs +17 -13
- package/src/document/errors/no_documents_supplied_error.rs +4 -4
- package/src/document/factory.rs +192 -0
- package/src/document/mod.rs +222 -30
- package/src/document/state_transition/document_batch_transition/document_transition/document_create_transition.rs +47 -1
- package/src/document/state_transition/document_batch_transition/document_transition/document_delete_transition.rs +25 -1
- package/src/document/state_transition/document_batch_transition/document_transition/document_replace_transition.rs +65 -0
- package/src/document/state_transition/document_batch_transition/document_transition/mod.rs +123 -3
- package/src/document/state_transition/document_batch_transition/mod.rs +378 -0
- package/src/document/validator.rs +30 -0
- package/src/errors/consensus/basic/identity/identity_asset_lock_proof_locked_transaction_mismatch_error.rs +6 -2
- package/src/errors/consensus/basic/identity/identity_asset_lock_transaction_out_point_already_exists_error.rs +5 -2
- package/src/errors/consensus/basic/identity/invalid_asset_lock_proof_transaction_height_error.rs +3 -3
- package/src/errors/dpp_error.rs +17 -0
- package/src/errors/from.rs +5 -0
- package/src/errors/js_conversion.rs +19 -4
- package/src/errors/mod.rs +2 -0
- package/src/errors/protocol_error.rs +7 -4
- package/src/identifier/mod.rs +48 -3
- package/src/identity/errors/asset_lock_output_not_found_error.rs +11 -0
- package/src/identity/errors/asset_lock_transaction_is_not_found_error.rs +20 -0
- package/src/identity/errors/mod.rs +7 -0
- package/src/identity/errors/unknown_asset_lock_proof_type_error.rs +26 -0
- package/src/identity/identity_facade.rs +2 -4
- package/src/identity/identity_public_key/mod.rs +41 -7
- package/src/identity/mod.rs +14 -5
- package/src/identity/state_transition/asset_lock_proof/chain/chain_asset_lock_proof.rs +132 -0
- package/src/identity/state_transition/asset_lock_proof/chain/chain_asset_lock_proof_structure_validator.rs +72 -0
- package/src/identity/state_transition/asset_lock_proof/chain/mod.rs +4 -0
- package/src/identity/state_transition/asset_lock_proof/instant/instant_asset_lock_proof.rs +177 -0
- package/src/identity/state_transition/asset_lock_proof/instant/instant_asset_lock_proof_structure_validator.rs +72 -0
- package/src/identity/state_transition/asset_lock_proof/instant/mod.rs +4 -0
- package/src/identity/state_transition/asset_lock_proof/mod.rs +269 -0
- package/src/identity/state_transition/identity_create_transition/apply_identity_create_transition.rs +26 -0
- package/src/identity/state_transition/identity_create_transition/identity_create_transition.rs +335 -0
- package/src/identity/state_transition/identity_create_transition/identity_create_transition_basic_validator.rs +153 -0
- package/src/identity/state_transition/identity_create_transition/identity_create_transition_state_validator.rs +37 -0
- package/src/identity/state_transition/identity_create_transition/mod.rs +8 -0
- package/src/identity/state_transition/identity_create_transition/to_object.rs +46 -0
- package/src/identity/state_transition/identity_topup_transition/apply_identity_topup_transition.rs +31 -0
- package/src/identity/state_transition/identity_topup_transition/identity_topup_transition.rs +280 -0
- package/src/identity/state_transition/identity_topup_transition/identity_topup_transition_basic_validator.rs +113 -0
- package/src/identity/state_transition/identity_topup_transition/identity_topup_transition_state_validator.rs +37 -0
- package/src/identity/state_transition/identity_topup_transition/mod.rs +8 -0
- package/src/identity/state_transition/identity_topup_transition/to_object.rs +42 -0
- package/src/identity/state_transition/mod.rs +9 -0
- package/src/identity/state_transition/validate_public_key_signatures.rs +59 -0
- package/src/identity/validation/public_keys_validator.rs +16 -10
- package/src/identity_public_key/public_keys_validator.rs +44 -0
- package/src/lib.rs +2 -0
- package/src/lodash.rs +7 -0
- package/src/state_repository.rs +188 -36
- package/src/state_transition/mod.rs +12 -0
- package/src/utils.rs +102 -7
- package/src/validation/json_schema_validator.rs +41 -0
- package/src/validation/mod.rs +2 -0
- package/src/validation/validation_result.rs +23 -5
- package/src/version/mod.rs +2 -0
- package/src/version/protocol_version.rs +26 -0
- package/test/integration/dataContract/validation/validateDataContractFactory.spec.js +579 -502
- package/test/integration/identity/stateTransition/IdentityCreateTransition/validation/basic/validateIdentityCreateTransitionBasicFactory.spec.js +175 -165
- package/test/integration/identity/stateTransition/IdentityTopUpTransition/validation/basic/validateIdentityTopUpTransitionBasicFactory.spec.js +83 -114
- package/test/integration/identity/stateTransition/assetLockProof/chain/validateChainAssetLockProofStructureFactory.spec.js +82 -91
- package/test/integration/identity/stateTransition/assetLockProof/fetchAssetLockTransactionOutputFactory.spec.js +75 -31
- package/test/integration/identity/stateTransition/assetLockProof/instant/validateInstantAssetLockProofStructureFactory.spec.js +93 -148
- package/test/integration/identity/stateTransition/assetLockProof/validateAssetLockTransactionFactory.spec.js +63 -75
- package/test/integration/identity/stateTransition/validatePublicKeySignaturesFactory.spec.js +48 -19
- package/test/unit/dataContract/DataContract.spec.js +3 -3
- package/test/unit/dataContract/createDataContract.spec.js +0 -1
- package/test/unit/dataContract/stateTransition/DataContractCreateTransition/DataContractCreateTransition.spec.js +3 -3
- package/test/unit/dataContract/stateTransition/DataContractUpdateTransition/DataContractUpdateTransition.spec.js +3 -3
- package/test/unit/decodeProtocolEntityFactory.spec.js +2 -2
- package/test/unit/document/Document.spec.js +147 -155
- package/test/unit/document/DocumentFactory.spec.js +325 -91
- package/test/unit/document/stateTransition/DocumetsBatchTransition/DocumentsBatchTransition.spec.js +142 -64
- package/test/unit/identity/Identity.spec.js +2 -2
- package/test/unit/identity/stateTransition/IdentityCreateTransition/IdentityCreateTransition.spec.js +69 -30
- package/test/unit/identity/stateTransition/IdentityCreateTransition/applyIdentityCreateTransitionFactory.spec.js +32 -32
- package/test/unit/identity/stateTransition/IdentityCreateTransition/validation/state/validateIdentityCreateTransitionStateFactory.spec.js +56 -25
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/IdentityTopUpTransition.spec.js +36 -16
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/applyIdentityTopUpTransitionFactory.spec.js +48 -46
- package/test/unit/identity/stateTransition/IdentityTopUpTransition/validation/state/validateIdentityTopUpTransitionStateFactory.spec.js +24 -6
- package/test/unit/identity/stateTransition/assetLockProof/ChainAssetLockProof.spec.js +63 -0
- package/test/unit/identity/stateTransition/assetLockProof/InstantAssetLockProof.spec.js +90 -0
- package/test/unit/identity/stateTransition/assetLockProof/createAssetLockProofInstance.spec.js +18 -3
- package/test/unit/identity/stateTransition/assetLockProof/fetchAssetLockPublicKeyHashFactory.spec.js +38 -11
- package/wasm/package.json +5 -0
- package/wasm/wasm_dpp.d.ts +1495 -565
- package/wasm/wasm_dpp.js +3654 -1206
- package/wasm/wasm_dpp_bg.js +1 -1
- package/wasm/wasm_dpp_bg.wasm +0 -0
- package/wasm/wasm_dpp_bg.wasm.d.ts +557 -386
- package/webpack.config.js +9 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
use std::sync::Arc;
|
|
2
|
+
|
|
3
|
+
use dpp::{
|
|
4
|
+
document::{
|
|
5
|
+
self,
|
|
6
|
+
document_factory::{DocumentFactory, FactoryOptions},
|
|
7
|
+
document_transition::Action,
|
|
8
|
+
fetch_and_validate_data_contract::DataContractFetcherAndValidator,
|
|
9
|
+
},
|
|
10
|
+
util::json_value::{JsonValueExt, ReplaceWith},
|
|
11
|
+
};
|
|
12
|
+
use wasm_bindgen::prelude::*;
|
|
13
|
+
|
|
14
|
+
use crate::{
|
|
15
|
+
identifier::identifier_from_js_value,
|
|
16
|
+
state_repository::{ExternalStateRepositoryLike, ExternalStateRepositoryLikeWrapper},
|
|
17
|
+
utils::{ToSerdeJSONExt, WithJsError},
|
|
18
|
+
DataContractWasm, DocumentWasm, DocumentsBatchTransitionWASM, DocumentsContainer,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
use super::validator::DocumentValidatorWasm;
|
|
22
|
+
|
|
23
|
+
#[wasm_bindgen(js_name=DocumentTransitions)]
|
|
24
|
+
#[derive(Debug, Default)]
|
|
25
|
+
pub struct DocumentTransitions {
|
|
26
|
+
create: Vec<DocumentWasm>,
|
|
27
|
+
replace: Vec<DocumentWasm>,
|
|
28
|
+
delete: Vec<DocumentWasm>,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#[wasm_bindgen(js_class=DocumentTransitions)]
|
|
32
|
+
impl DocumentTransitions {
|
|
33
|
+
#[wasm_bindgen(constructor)]
|
|
34
|
+
pub fn new() -> Self {
|
|
35
|
+
Default::default()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#[wasm_bindgen(js_name = "addTransitionCreate")]
|
|
39
|
+
pub fn add_transition_create(&mut self, transition: DocumentWasm) {
|
|
40
|
+
self.create.push(transition)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[wasm_bindgen(js_name = "addTransitionReplace")]
|
|
44
|
+
pub fn add_transition_replace(&mut self, transition: DocumentWasm) {
|
|
45
|
+
self.replace.push(transition)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#[wasm_bindgen(js_name = "addTransitionDelete")]
|
|
49
|
+
pub fn add_transition_delete(&mut self, transition: DocumentWasm) {
|
|
50
|
+
self.delete.push(transition)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#[wasm_bindgen(js_name = DocumentFactory)]
|
|
55
|
+
pub struct DocumentFactoryWASM(DocumentFactory<ExternalStateRepositoryLikeWrapper>);
|
|
56
|
+
|
|
57
|
+
#[wasm_bindgen(js_class=DocumentFactory)]
|
|
58
|
+
impl DocumentFactoryWASM {
|
|
59
|
+
#[wasm_bindgen(constructor)]
|
|
60
|
+
pub fn new(
|
|
61
|
+
protocol_version: u32,
|
|
62
|
+
document_validator: DocumentValidatorWasm,
|
|
63
|
+
state_repository: ExternalStateRepositoryLike,
|
|
64
|
+
) -> DocumentFactoryWASM {
|
|
65
|
+
console_error_panic_hook::set_once();
|
|
66
|
+
let factory = DocumentFactory::new(
|
|
67
|
+
protocol_version,
|
|
68
|
+
document_validator.into(),
|
|
69
|
+
DataContractFetcherAndValidator::new(Arc::new(
|
|
70
|
+
ExternalStateRepositoryLikeWrapper::new(state_repository),
|
|
71
|
+
)),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
DocumentFactoryWASM(factory)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#[wasm_bindgen(js_name=create)]
|
|
78
|
+
pub fn create(
|
|
79
|
+
&self,
|
|
80
|
+
data_contract: DataContractWasm,
|
|
81
|
+
js_owner_id: &JsValue,
|
|
82
|
+
document_type: &str,
|
|
83
|
+
data: &JsValue,
|
|
84
|
+
) -> Result<DocumentWasm, JsValue> {
|
|
85
|
+
let owner_id = identifier_from_js_value(js_owner_id)?;
|
|
86
|
+
let dynamic_data = data.with_serde_to_json_value()?;
|
|
87
|
+
let document = self
|
|
88
|
+
.0
|
|
89
|
+
.create(
|
|
90
|
+
data_contract.into(),
|
|
91
|
+
owner_id,
|
|
92
|
+
document_type.to_string(),
|
|
93
|
+
dynamic_data,
|
|
94
|
+
)
|
|
95
|
+
.with_js_error()?;
|
|
96
|
+
|
|
97
|
+
Ok(document.into())
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
#[wasm_bindgen(js_name=createStateTransition)]
|
|
101
|
+
pub fn create_state_transition(
|
|
102
|
+
&self,
|
|
103
|
+
documents_container: DocumentsContainer,
|
|
104
|
+
) -> Result<DocumentsBatchTransitionWASM, JsValue> {
|
|
105
|
+
let mut documents_container = documents_container;
|
|
106
|
+
|
|
107
|
+
let batch_transition = self
|
|
108
|
+
.0
|
|
109
|
+
.create_state_transition([
|
|
110
|
+
(Action::Create, documents_container.take_documents_create()),
|
|
111
|
+
(
|
|
112
|
+
Action::Replace,
|
|
113
|
+
documents_container.take_documents_replace(),
|
|
114
|
+
),
|
|
115
|
+
(Action::Delete, documents_container.take_documents_delete()),
|
|
116
|
+
])
|
|
117
|
+
.with_js_error()?;
|
|
118
|
+
|
|
119
|
+
Ok(batch_transition.into())
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#[wasm_bindgen(js_name=createFromObject)]
|
|
123
|
+
pub async fn create_from_object(
|
|
124
|
+
&self,
|
|
125
|
+
raw_document_js: JsValue,
|
|
126
|
+
options: JsValue,
|
|
127
|
+
) -> Result<DocumentWasm, JsValue> {
|
|
128
|
+
let mut raw_document = raw_document_js.with_serde_to_json_value()?;
|
|
129
|
+
let options: FactoryOptions = if !options.is_undefined() && options.is_object() {
|
|
130
|
+
let raw_options = options.with_serde_to_json_value()?;
|
|
131
|
+
serde_json::from_value(raw_options).with_js_error()?
|
|
132
|
+
} else {
|
|
133
|
+
Default::default()
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Errors are ignored. When `Buffer` crosses the WASM boundary it becomes an Array.
|
|
137
|
+
// When `Identifier` crosses the WASM boundary, it becomes a String. From perspective of JS
|
|
138
|
+
// `Identifier` and `Buffer` are used interchangeably, so we we can expect the replacing may fail when `Buffer` is provided
|
|
139
|
+
let _ = raw_document
|
|
140
|
+
.replace_identifier_paths(document::IDENTIFIER_FIELDS, ReplaceWith::Bytes)
|
|
141
|
+
.with_js_error();
|
|
142
|
+
|
|
143
|
+
let mut document = self
|
|
144
|
+
.0
|
|
145
|
+
.create_from_object(raw_document, options)
|
|
146
|
+
.await
|
|
147
|
+
.with_js_error()?;
|
|
148
|
+
|
|
149
|
+
// When data contract is available, replace remaining dynamic paths
|
|
150
|
+
let mut document_data = document.data.take();
|
|
151
|
+
let (identifier_paths, binary_paths) = document
|
|
152
|
+
.get_identifiers_and_binary_paths()
|
|
153
|
+
.with_js_error()?;
|
|
154
|
+
document_data
|
|
155
|
+
.replace_identifier_paths(identifier_paths, ReplaceWith::Bytes)
|
|
156
|
+
.with_js_error()?;
|
|
157
|
+
document_data
|
|
158
|
+
.replace_binary_paths(binary_paths, ReplaceWith::Bytes)
|
|
159
|
+
.with_js_error()?;
|
|
160
|
+
document.data = document_data;
|
|
161
|
+
|
|
162
|
+
Ok(document.into())
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#[wasm_bindgen(js_name=createFromBuffer)]
|
|
166
|
+
pub async fn create_from_buffer(
|
|
167
|
+
&self,
|
|
168
|
+
buffer: Vec<u8>,
|
|
169
|
+
options: &JsValue,
|
|
170
|
+
) -> Result<DocumentWasm, JsValue> {
|
|
171
|
+
let options: FactoryOptions = if !options.is_undefined() && options.is_object() {
|
|
172
|
+
let raw_options = options.with_serde_to_json_value()?;
|
|
173
|
+
serde_json::from_value(raw_options).with_js_error()?
|
|
174
|
+
} else {
|
|
175
|
+
Default::default()
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
let document = self
|
|
179
|
+
.0
|
|
180
|
+
.create_from_buffer(buffer, options)
|
|
181
|
+
.await
|
|
182
|
+
.with_js_error()?;
|
|
183
|
+
|
|
184
|
+
Ok(document.into())
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
impl DocumentFactoryWASM {
|
|
189
|
+
pub(crate) fn into_inner(self) -> DocumentFactory<ExternalStateRepositoryLikeWrapper> {
|
|
190
|
+
self.0
|
|
191
|
+
}
|
|
192
|
+
}
|
package/src/document/mod.rs
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
|
-
use
|
|
2
|
-
|
|
1
|
+
use dpp::dashcore::anyhow::Context;
|
|
2
|
+
use dpp::prelude::Identifier;
|
|
3
|
+
use dpp::util::json_schema::JsonSchemaExt;
|
|
4
|
+
use dpp::util::json_value::{JsonValueExt, ReplaceWith};
|
|
5
|
+
use dpp::util::string_encoding::Encoding;
|
|
3
6
|
use serde::{Deserialize, Serialize};
|
|
7
|
+
use std::convert::TryInto;
|
|
4
8
|
use wasm_bindgen::prelude::*;
|
|
5
9
|
|
|
6
|
-
use dpp::document::Document;
|
|
10
|
+
use dpp::document::{property_names, Document, IDENTIFIER_FIELDS};
|
|
7
11
|
|
|
8
|
-
use crate::
|
|
12
|
+
use crate::buffer::Buffer;
|
|
13
|
+
use crate::errors::RustConversionError;
|
|
9
14
|
use crate::identifier::IdentifierWrapper;
|
|
15
|
+
use crate::lodash::lodash_set;
|
|
16
|
+
use crate::utils::WithJsError;
|
|
17
|
+
use crate::utils::{with_serde_to_json_value, ToSerdeJSONExt};
|
|
10
18
|
use crate::with_js_error;
|
|
11
19
|
use crate::{DataContractWasm, MetadataWasm};
|
|
12
20
|
|
|
13
21
|
pub mod errors;
|
|
22
|
+
pub use state_transition::*;
|
|
23
|
+
mod factory;
|
|
14
24
|
pub mod state_transition;
|
|
25
|
+
mod validator;
|
|
26
|
+
|
|
27
|
+
pub use document_batch_transition::{DocumentsBatchTransitionWASM, DocumentsContainer};
|
|
28
|
+
pub use factory::DocumentFactoryWASM;
|
|
29
|
+
pub use validator::DocumentValidatorWasm;
|
|
30
|
+
|
|
31
|
+
pub(super) enum BinaryType {
|
|
32
|
+
Identifier,
|
|
33
|
+
Buffer,
|
|
34
|
+
None,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Default)]
|
|
38
|
+
#[serde(rename_all = "camelCase")]
|
|
39
|
+
pub struct ConversionOptions {
|
|
40
|
+
skip_identifiers_conversion: bool,
|
|
41
|
+
}
|
|
15
42
|
|
|
16
43
|
#[wasm_bindgen(js_name=Document)]
|
|
17
44
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
@@ -19,6 +46,40 @@ pub struct DocumentWasm(Document);
|
|
|
19
46
|
|
|
20
47
|
#[wasm_bindgen(js_class=Document)]
|
|
21
48
|
impl DocumentWasm {
|
|
49
|
+
#[wasm_bindgen(constructor)]
|
|
50
|
+
pub fn new(
|
|
51
|
+
js_raw_document: JsValue,
|
|
52
|
+
js_data_contract: &DataContractWasm,
|
|
53
|
+
) -> Result<DocumentWasm, JsValue> {
|
|
54
|
+
let mut raw_document = with_serde_to_json_value(&js_raw_document)?;
|
|
55
|
+
|
|
56
|
+
let document_type = raw_document
|
|
57
|
+
.get_string(property_names::DOCUMENT_TYPE)
|
|
58
|
+
.with_js_error()?;
|
|
59
|
+
|
|
60
|
+
let (identifier_paths, _) = js_data_contract
|
|
61
|
+
.inner()
|
|
62
|
+
.get_identifiers_and_binary_paths(document_type)
|
|
63
|
+
.with_js_error()?;
|
|
64
|
+
|
|
65
|
+
// Errors are ignored. When `Buffer` crosses the WASM boundary it becomes an Array.
|
|
66
|
+
// When `Identifier` crosses the WASM boundary it becomes a String. From perspective of JS
|
|
67
|
+
// `Identifier` and `Buffer` are used interchangeably, so we we can expect the replacing may fail when `Buffer` is provided
|
|
68
|
+
let _ = raw_document
|
|
69
|
+
.replace_identifier_paths(
|
|
70
|
+
identifier_paths.into_iter().chain(IDENTIFIER_FIELDS),
|
|
71
|
+
ReplaceWith::Bytes,
|
|
72
|
+
)
|
|
73
|
+
.with_js_error();
|
|
74
|
+
// The binary paths are not being converted, because they always should be a `Buffer`. `Buffer` is always an Array
|
|
75
|
+
|
|
76
|
+
let document =
|
|
77
|
+
Document::from_raw_document(raw_document, js_data_contract.to_owned().into())
|
|
78
|
+
.with_js_error()?;
|
|
79
|
+
|
|
80
|
+
Ok(document.into())
|
|
81
|
+
}
|
|
82
|
+
|
|
22
83
|
#[wasm_bindgen(js_name=getProtocolVersion)]
|
|
23
84
|
pub fn get_protocol_version(&self) -> u32 {
|
|
24
85
|
self.0.protocol_version
|
|
@@ -29,6 +90,11 @@ impl DocumentWasm {
|
|
|
29
90
|
self.0.id.clone().into()
|
|
30
91
|
}
|
|
31
92
|
|
|
93
|
+
#[wasm_bindgen(js_name=setId)]
|
|
94
|
+
pub fn set_id(&mut self, js_id: IdentifierWrapper) {
|
|
95
|
+
self.0.id = js_id.inner();
|
|
96
|
+
}
|
|
97
|
+
|
|
32
98
|
#[wasm_bindgen(js_name=getType)]
|
|
33
99
|
pub fn get_type(&self) -> String {
|
|
34
100
|
self.0.document_type.clone()
|
|
@@ -44,6 +110,11 @@ impl DocumentWasm {
|
|
|
44
110
|
self.0.data_contract.clone().into()
|
|
45
111
|
}
|
|
46
112
|
|
|
113
|
+
#[wasm_bindgen(js_name=setOwnerId)]
|
|
114
|
+
pub fn set_owner_id(&mut self, owner_id: IdentifierWrapper) {
|
|
115
|
+
self.0.owner_id = owner_id.inner();
|
|
116
|
+
}
|
|
117
|
+
|
|
47
118
|
#[wasm_bindgen(js_name=getOwnerId)]
|
|
48
119
|
pub fn get_owner_id(&self) -> IdentifierWrapper {
|
|
49
120
|
self.0.owner_id.clone().into()
|
|
@@ -72,8 +143,8 @@ impl DocumentWasm {
|
|
|
72
143
|
}
|
|
73
144
|
|
|
74
145
|
#[wasm_bindgen(js_name=getEntropy)]
|
|
75
|
-
pub fn get_entropy(&mut self) ->
|
|
76
|
-
self.0.entropy
|
|
146
|
+
pub fn get_entropy(&mut self) -> Buffer {
|
|
147
|
+
Buffer::from_bytes(&self.0.entropy)
|
|
77
148
|
}
|
|
78
149
|
|
|
79
150
|
#[wasm_bindgen(js_name=setData)]
|
|
@@ -84,39 +155,98 @@ impl DocumentWasm {
|
|
|
84
155
|
|
|
85
156
|
#[wasm_bindgen(js_name=getData)]
|
|
86
157
|
pub fn get_data(&mut self) -> Result<JsValue, JsValue> {
|
|
87
|
-
|
|
158
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
159
|
+
|
|
160
|
+
Ok(with_js_error!(self.0.get_data().serialize(&serializer))?)
|
|
88
161
|
}
|
|
89
162
|
|
|
90
163
|
#[wasm_bindgen(js_name=set)]
|
|
91
|
-
pub fn set(&mut self,
|
|
92
|
-
|
|
93
|
-
|
|
164
|
+
pub fn set(&mut self, path: String, js_value_to_set: JsValue) -> Result<(), JsValue> {
|
|
165
|
+
let (identifier_paths, _) = self.0.get_identifiers_and_binary_paths().with_js_error()?;
|
|
166
|
+
for property_path in identifier_paths {
|
|
167
|
+
if property_path == path {
|
|
168
|
+
let id_value = js_value_to_set.with_serde_to_json_value()?;
|
|
169
|
+
let id_string = id_value
|
|
170
|
+
.as_str()
|
|
171
|
+
.context("the value must be a string")
|
|
172
|
+
.with_js_error()?;
|
|
173
|
+
let id = Identifier::from_string(id_string, Encoding::Base58).with_js_error()?;
|
|
174
|
+
let new_value = serde_json::to_value(id.as_bytes()).with_js_error()?;
|
|
175
|
+
|
|
176
|
+
return self.0.set(&path, new_value).with_js_error();
|
|
177
|
+
} else if property_path.starts_with(&path) {
|
|
178
|
+
let (_, suffix) = property_path.split_at(path.len() + 1);
|
|
179
|
+
let mut value = js_value_to_set.with_serde_to_json_value()?;
|
|
180
|
+
|
|
181
|
+
if value.get_value(suffix).is_ok() {
|
|
182
|
+
let id_string = value
|
|
183
|
+
.remove_path_into::<String>(suffix)
|
|
184
|
+
.with_context(|| format!("unable convert `{path}` into string"))
|
|
185
|
+
.map_err(|e| format!("{e:#}"))?;
|
|
186
|
+
let id: IdentifierWrapper =
|
|
187
|
+
Identifier::from_string(&id_string, Encoding::Base58)
|
|
188
|
+
.with_js_error()?
|
|
189
|
+
.into();
|
|
190
|
+
let new_value = serde_json::to_value(id.inner().as_bytes()).with_js_error()?;
|
|
191
|
+
value.insert_with_path(suffix, new_value).with_js_error()?;
|
|
192
|
+
|
|
193
|
+
return self.0.set(&path, value).with_js_error();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let value = js_value_to_set.with_serde_to_json_value()?;
|
|
199
|
+
self.0.set(&path, value).with_js_error()
|
|
94
200
|
}
|
|
95
201
|
|
|
96
202
|
#[wasm_bindgen(js_name=get)]
|
|
97
|
-
pub fn get(&mut self,
|
|
98
|
-
|
|
99
|
-
|
|
203
|
+
pub fn get(&mut self, path: String) -> JsValue {
|
|
204
|
+
let binary_type = self.get_binary_type_of_path(&path);
|
|
205
|
+
|
|
206
|
+
if let Some(value) = self.0.get(&path) {
|
|
207
|
+
match binary_type {
|
|
208
|
+
BinaryType::Identifier => {
|
|
209
|
+
if let Ok(bytes) = serde_json::from_value::<Vec<u8>>(value.to_owned()) {
|
|
210
|
+
let id: IdentifierWrapper = Identifier::from_bytes(&bytes).unwrap().into();
|
|
211
|
+
|
|
212
|
+
return id.into();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
BinaryType::Buffer => {
|
|
216
|
+
if let Ok(bytes) = serde_json::from_value::<Vec<u8>>(value.to_owned()) {
|
|
217
|
+
return Buffer::from_bytes(&bytes).into();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
BinaryType::None => {
|
|
221
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
222
|
+
if let Ok(js_value) = value.serialize(&serializer) {
|
|
223
|
+
return js_value;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
JsValue::undefined()
|
|
100
230
|
}
|
|
101
231
|
|
|
102
232
|
#[wasm_bindgen(js_name=setCreatedAt)]
|
|
103
|
-
pub fn set_created_at(&mut self, ts:
|
|
104
|
-
self.0.created_at = Some(ts);
|
|
233
|
+
pub fn set_created_at(&mut self, ts: f64) {
|
|
234
|
+
self.0.created_at = Some(ts as i64);
|
|
105
235
|
}
|
|
106
236
|
|
|
107
237
|
#[wasm_bindgen(js_name=setUpdatedAt)]
|
|
108
|
-
pub fn set_updated_at(&mut self, ts:
|
|
109
|
-
self.0.updated_at = Some(ts);
|
|
238
|
+
pub fn set_updated_at(&mut self, ts: f64) {
|
|
239
|
+
self.0.updated_at = Some(ts as i64);
|
|
110
240
|
}
|
|
111
241
|
|
|
112
242
|
#[wasm_bindgen(js_name=getCreatedAt)]
|
|
113
|
-
pub fn get_created_at(&self) -> Option<
|
|
114
|
-
self.0.created_at
|
|
243
|
+
pub fn get_created_at(&self) -> Option<f64> {
|
|
244
|
+
self.0.created_at.map(|v| v as f64)
|
|
115
245
|
}
|
|
116
246
|
|
|
117
247
|
#[wasm_bindgen(js_name=getUpdatedAt)]
|
|
118
|
-
pub fn get_updated_at(&self) -> Option<
|
|
119
|
-
self.0.updated_at
|
|
248
|
+
pub fn get_updated_at(&self) -> Option<f64> {
|
|
249
|
+
self.0.updated_at.map(|v| v as f64)
|
|
120
250
|
}
|
|
121
251
|
|
|
122
252
|
#[wasm_bindgen(js_name=getMetadata)]
|
|
@@ -125,29 +255,91 @@ impl DocumentWasm {
|
|
|
125
255
|
}
|
|
126
256
|
|
|
127
257
|
#[wasm_bindgen(js_name=setMetadata)]
|
|
128
|
-
pub fn set_metadata(
|
|
258
|
+
pub fn set_metadata(mut self, metadata: MetadataWasm) -> Self {
|
|
129
259
|
self.0.metadata = Some(metadata.into());
|
|
260
|
+
self
|
|
130
261
|
}
|
|
131
262
|
|
|
132
263
|
#[wasm_bindgen(js_name=toObject)]
|
|
133
|
-
pub fn to_object(&self) -> Result<JsValue, JsValue> {
|
|
134
|
-
|
|
264
|
+
pub fn to_object(&self, options: &JsValue) -> Result<JsValue, JsValue> {
|
|
265
|
+
let options: ConversionOptions = if !options.is_undefined() && options.is_object() {
|
|
266
|
+
let raw_options = options.with_serde_to_json_value()?;
|
|
267
|
+
serde_json::from_value(raw_options).with_js_error()?
|
|
268
|
+
} else {
|
|
269
|
+
Default::default()
|
|
270
|
+
};
|
|
271
|
+
let mut value = self.0.to_object().with_js_error()?;
|
|
272
|
+
|
|
273
|
+
let (identifiers_paths, binary_paths) =
|
|
274
|
+
self.0.get_identifiers_and_binary_paths().with_js_error()?;
|
|
275
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
276
|
+
let js_value = value.serialize(&serializer)?;
|
|
277
|
+
|
|
278
|
+
for path in identifiers_paths.into_iter().chain(IDENTIFIER_FIELDS) {
|
|
279
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
280
|
+
if !options.skip_identifiers_conversion {
|
|
281
|
+
let buffer = Buffer::from_bytes(&bytes);
|
|
282
|
+
lodash_set(&js_value, path, buffer.into());
|
|
283
|
+
} else {
|
|
284
|
+
let id = IdentifierWrapper::new(bytes)?;
|
|
285
|
+
lodash_set(&js_value, path, id.into());
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
for path in binary_paths {
|
|
291
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
292
|
+
let buffer = Buffer::from_bytes(&bytes);
|
|
293
|
+
lodash_set(&js_value, path, buffer.into());
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
Ok(js_value)
|
|
135
298
|
}
|
|
136
299
|
|
|
137
300
|
#[wasm_bindgen(js_name=toJSON)]
|
|
138
301
|
pub fn to_json(&self) -> Result<JsValue, JsValue> {
|
|
139
|
-
self.
|
|
302
|
+
let value = self.0.to_json().with_js_error()?;
|
|
303
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
304
|
+
|
|
305
|
+
with_js_error!(value.serialize(&serializer))
|
|
140
306
|
}
|
|
141
307
|
|
|
142
308
|
#[wasm_bindgen(js_name=toBuffer)]
|
|
143
|
-
pub fn to_buffer(&self) -> Result<
|
|
144
|
-
let
|
|
145
|
-
|
|
309
|
+
pub fn to_buffer(&self) -> Result<Buffer, JsValue> {
|
|
310
|
+
let bytes = self.0.to_buffer().with_js_error()?;
|
|
311
|
+
|
|
312
|
+
Ok(Buffer::from_bytes(&bytes))
|
|
146
313
|
}
|
|
147
314
|
|
|
148
315
|
#[wasm_bindgen(js_name=hash)]
|
|
149
|
-
pub fn hash(&self) -> Result<
|
|
150
|
-
self.0.hash().
|
|
316
|
+
pub fn hash(&self) -> Result<Buffer, JsValue> {
|
|
317
|
+
let bytes = self.0.hash().with_js_error()?;
|
|
318
|
+
Ok(Buffer::from_bytes(&bytes))
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
#[wasm_bindgen(js_name=clone)]
|
|
322
|
+
pub fn deep_clone(&self) -> Self {
|
|
323
|
+
self.clone()
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
impl DocumentWasm {
|
|
328
|
+
fn get_binary_type_of_path(&self, path: &String) -> BinaryType {
|
|
329
|
+
let maybe_binary_properties = self
|
|
330
|
+
.0
|
|
331
|
+
.data_contract
|
|
332
|
+
.get_binary_properties(&self.0.document_type);
|
|
333
|
+
|
|
334
|
+
if let Ok(binary_properties) = maybe_binary_properties {
|
|
335
|
+
if let Some(data) = binary_properties.get(path) {
|
|
336
|
+
if data.is_type_of_identifier() {
|
|
337
|
+
return BinaryType::Identifier;
|
|
338
|
+
}
|
|
339
|
+
return BinaryType::Buffer;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
BinaryType::None
|
|
151
343
|
}
|
|
152
344
|
}
|
|
153
345
|
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
use dpp::
|
|
1
|
+
use dpp::{
|
|
2
|
+
document::document_transition::{
|
|
3
|
+
document_create_transition, DocumentCreateTransition, DocumentTransitionObjectLike,
|
|
4
|
+
},
|
|
5
|
+
util::json_value::JsonValueExt,
|
|
6
|
+
};
|
|
7
|
+
use serde::Serialize;
|
|
2
8
|
use wasm_bindgen::prelude::*;
|
|
3
9
|
|
|
10
|
+
use crate::{
|
|
11
|
+
buffer::Buffer, identifier::IdentifierWrapper, lodash::lodash_set, utils::WithJsError,
|
|
12
|
+
};
|
|
13
|
+
|
|
4
14
|
#[wasm_bindgen(js_name=DocumentCreateTransition)]
|
|
5
15
|
#[derive(Debug, Clone)]
|
|
6
16
|
pub struct DocumentCreateTransitionWasm {
|
|
@@ -19,4 +29,40 @@ impl DocumentCreateTransitionWasm {
|
|
|
19
29
|
pub fn action(&self) -> u8 {
|
|
20
30
|
self.inner.base.action as u8
|
|
21
31
|
}
|
|
32
|
+
|
|
33
|
+
#[wasm_bindgen(js_name=toObject)]
|
|
34
|
+
pub fn to_object(&self) -> Result<JsValue, JsValue> {
|
|
35
|
+
let mut value = self.inner.to_object().with_js_error()?;
|
|
36
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
37
|
+
let js_value = value.serialize(&serializer)?;
|
|
38
|
+
|
|
39
|
+
let (identifiers_paths, binary_paths) = self
|
|
40
|
+
.inner
|
|
41
|
+
.base
|
|
42
|
+
.data_contract
|
|
43
|
+
.get_identifiers_and_binary_paths(&self.inner.base.document_type)
|
|
44
|
+
.with_js_error()?;
|
|
45
|
+
|
|
46
|
+
for path in identifiers_paths
|
|
47
|
+
.into_iter()
|
|
48
|
+
.chain(document_create_transition::IDENTIFIER_FIELDS)
|
|
49
|
+
{
|
|
50
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
51
|
+
let id = IdentifierWrapper::new(bytes)?;
|
|
52
|
+
lodash_set(&js_value, path, id.into());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for path in binary_paths
|
|
57
|
+
.into_iter()
|
|
58
|
+
.chain(document_create_transition::BINARY_FIELDS)
|
|
59
|
+
{
|
|
60
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
61
|
+
let buffer = Buffer::from_bytes(&bytes);
|
|
62
|
+
lodash_set(&js_value, path, buffer.into());
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Ok(js_value)
|
|
67
|
+
}
|
|
22
68
|
}
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
use dpp::
|
|
1
|
+
use dpp::{
|
|
2
|
+
document::document_transition::{
|
|
3
|
+
document_delete_transition, DocumentDeleteTransition, DocumentTransitionObjectLike,
|
|
4
|
+
},
|
|
5
|
+
util::json_value::JsonValueExt,
|
|
6
|
+
};
|
|
7
|
+
use serde::Serialize;
|
|
2
8
|
use wasm_bindgen::prelude::*;
|
|
3
9
|
|
|
10
|
+
use crate::{identifier::IdentifierWrapper, lodash::lodash_set, utils::WithJsError};
|
|
11
|
+
|
|
4
12
|
#[wasm_bindgen(js_name=DocumentDeleteTransition)]
|
|
5
13
|
#[derive(Debug, Clone)]
|
|
6
14
|
pub struct DocumentDeleteTransitionWasm {
|
|
@@ -19,4 +27,20 @@ impl DocumentDeleteTransitionWasm {
|
|
|
19
27
|
pub fn action(&self) -> u8 {
|
|
20
28
|
self.inner.base.action as u8
|
|
21
29
|
}
|
|
30
|
+
|
|
31
|
+
#[wasm_bindgen(js_name=toObject)]
|
|
32
|
+
pub fn to_object(&self) -> Result<JsValue, JsValue> {
|
|
33
|
+
let mut value = self.inner.to_object().with_js_error()?;
|
|
34
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
35
|
+
let js_value = value.serialize(&serializer)?;
|
|
36
|
+
|
|
37
|
+
for field in document_delete_transition::IDENTIFIER_FIELDS {
|
|
38
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(field) {
|
|
39
|
+
let id = IdentifierWrapper::new(bytes)?;
|
|
40
|
+
lodash_set(&js_value, field, id.into());
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Ok(js_value)
|
|
45
|
+
}
|
|
22
46
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
use dpp::{
|
|
2
|
+
document::document_transition::{
|
|
3
|
+
document_replace_transition, DocumentReplaceTransition, DocumentTransitionObjectLike,
|
|
4
|
+
},
|
|
5
|
+
util::json_value::JsonValueExt,
|
|
6
|
+
};
|
|
7
|
+
use serde::Serialize;
|
|
8
|
+
use wasm_bindgen::prelude::*;
|
|
9
|
+
|
|
10
|
+
use crate::{
|
|
11
|
+
buffer::Buffer, identifier::IdentifierWrapper, lodash::lodash_set, utils::WithJsError,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
#[wasm_bindgen(js_name=DocumentTransition)]
|
|
15
|
+
#[derive(Debug, Clone)]
|
|
16
|
+
pub struct DocumentReplaceTransitionWasm {
|
|
17
|
+
inner: DocumentReplaceTransition,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
impl From<DocumentReplaceTransition> for DocumentReplaceTransitionWasm {
|
|
21
|
+
fn from(v: DocumentReplaceTransition) -> Self {
|
|
22
|
+
Self { inner: v }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#[wasm_bindgen(js_class=DocumentTransition)]
|
|
27
|
+
impl DocumentReplaceTransitionWasm {
|
|
28
|
+
#[wasm_bindgen(js_name=getAction)]
|
|
29
|
+
pub fn action(&self) -> u8 {
|
|
30
|
+
self.inner.base.action as u8
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[wasm_bindgen(js_name=toObject)]
|
|
34
|
+
pub fn to_object(&self) -> Result<JsValue, JsValue> {
|
|
35
|
+
let mut value = self.inner.to_object().with_js_error()?;
|
|
36
|
+
let serializer = serde_wasm_bindgen::Serializer::json_compatible();
|
|
37
|
+
let js_value = value.serialize(&serializer)?;
|
|
38
|
+
|
|
39
|
+
let (identifiers_paths, binary_paths) = self
|
|
40
|
+
.inner
|
|
41
|
+
.base
|
|
42
|
+
.data_contract
|
|
43
|
+
.get_identifiers_and_binary_paths(&self.inner.base.document_type)
|
|
44
|
+
.with_js_error()?;
|
|
45
|
+
|
|
46
|
+
for path in identifiers_paths
|
|
47
|
+
.into_iter()
|
|
48
|
+
.chain(document_replace_transition::IDENTIFIER_FIELDS)
|
|
49
|
+
{
|
|
50
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
51
|
+
let id = IdentifierWrapper::new(bytes)?;
|
|
52
|
+
lodash_set(&js_value, path, id.into());
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
for path in binary_paths.into_iter() {
|
|
57
|
+
if let Ok(bytes) = value.remove_path_into::<Vec<u8>>(path) {
|
|
58
|
+
let buffer = Buffer::from_bytes(&bytes);
|
|
59
|
+
lodash_set(&js_value, path, buffer.into());
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
Ok(js_value)
|
|
64
|
+
}
|
|
65
|
+
}
|