@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
package/src/utils.rs
CHANGED
|
@@ -1,15 +1,37 @@
|
|
|
1
|
-
use dpp::
|
|
1
|
+
use dpp::{
|
|
2
|
+
dashcore::{anyhow, anyhow::Context},
|
|
3
|
+
ProtocolError,
|
|
4
|
+
};
|
|
5
|
+
|
|
2
6
|
use js_sys::Function;
|
|
7
|
+
use serde::de::DeserializeOwned;
|
|
3
8
|
use serde_json::Value;
|
|
9
|
+
use wasm_bindgen::convert::RefFromWasmAbi;
|
|
4
10
|
use wasm_bindgen::prelude::*;
|
|
5
11
|
|
|
12
|
+
use crate::errors::{from_dpp_err, RustConversionError};
|
|
13
|
+
|
|
6
14
|
pub trait ToSerdeJSONExt {
|
|
7
|
-
fn
|
|
15
|
+
fn with_serde_to_json_value(&self) -> Result<Value, JsValue>;
|
|
16
|
+
fn with_serde_into<D: DeserializeOwned>(&self) -> Result<D, JsValue>
|
|
17
|
+
where
|
|
18
|
+
D: for<'de> serde::de::Deserialize<'de> + 'static;
|
|
8
19
|
}
|
|
9
20
|
|
|
10
21
|
impl ToSerdeJSONExt for JsValue {
|
|
11
|
-
|
|
12
|
-
|
|
22
|
+
/// Converts the `JsValue` into `serde_json::Value`. It's an expensive conversion,
|
|
23
|
+
/// as `JsValue` must be stringified first
|
|
24
|
+
fn with_serde_to_json_value(&self) -> Result<Value, JsValue> {
|
|
25
|
+
with_serde_to_json_value(self)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/// converts the `JsValue` into any type that is supported by serde. It's an expensive conversion
|
|
29
|
+
/// as the `jsValue` must be stringified first
|
|
30
|
+
fn with_serde_into<D>(&self) -> Result<D, JsValue>
|
|
31
|
+
where
|
|
32
|
+
D: for<'de> serde::de::Deserialize<'de> + 'static,
|
|
33
|
+
{
|
|
34
|
+
with_serde_into(self)
|
|
13
35
|
}
|
|
14
36
|
}
|
|
15
37
|
|
|
@@ -25,7 +47,7 @@ pub fn to_vec_of_serde_values(
|
|
|
25
47
|
) -> Result<Vec<Value>, JsValue> {
|
|
26
48
|
values
|
|
27
49
|
.into_iter()
|
|
28
|
-
.map(|v| v.as_ref().
|
|
50
|
+
.map(|v| v.as_ref().with_serde_to_json_value())
|
|
29
51
|
.collect()
|
|
30
52
|
}
|
|
31
53
|
|
|
@@ -38,7 +60,7 @@ where
|
|
|
38
60
|
.collect()
|
|
39
61
|
}
|
|
40
62
|
|
|
41
|
-
pub fn
|
|
63
|
+
pub fn with_serde_to_json_value(data: &JsValue) -> Result<Value, JsValue> {
|
|
42
64
|
let data = stringify(data)?;
|
|
43
65
|
let value: Value = serde_json::from_str(&data)
|
|
44
66
|
.with_context(|| format!("cant convert {data:#?} to serde json value"))
|
|
@@ -46,10 +68,21 @@ pub fn to_serde_json_value(data: &JsValue) -> Result<Value, JsValue> {
|
|
|
46
68
|
Ok(value)
|
|
47
69
|
}
|
|
48
70
|
|
|
71
|
+
pub fn with_serde_into<D>(data: &JsValue) -> Result<D, JsValue>
|
|
72
|
+
where
|
|
73
|
+
D: for<'de> serde::de::Deserialize<'de> + 'static,
|
|
74
|
+
{
|
|
75
|
+
let data = stringify(data)?;
|
|
76
|
+
let value: D = serde_json::from_str(&data)
|
|
77
|
+
.with_context(|| format!("cant convert {:#?} to serde json value", data))
|
|
78
|
+
.map_err(|e| format!("{:#}", e))?;
|
|
79
|
+
Ok(value)
|
|
80
|
+
}
|
|
81
|
+
|
|
49
82
|
pub fn stringify(data: &JsValue) -> Result<String, JsValue> {
|
|
50
83
|
let replacer_func = Function::new_with_args(
|
|
51
84
|
"key, value",
|
|
52
|
-
"return value.type=='Buffer' ? value.data : value ",
|
|
85
|
+
"return value && value.type=='Buffer' ? value.data : value ",
|
|
53
86
|
);
|
|
54
87
|
|
|
55
88
|
let data_string: String =
|
|
@@ -57,3 +90,65 @@ pub fn stringify(data: &JsValue) -> Result<String, JsValue> {
|
|
|
57
90
|
|
|
58
91
|
Ok(data_string)
|
|
59
92
|
}
|
|
93
|
+
|
|
94
|
+
pub trait WithJsError<T> {
|
|
95
|
+
/// Converts the error into JsValue
|
|
96
|
+
fn with_js_error(self) -> Result<T, JsValue>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
impl<T> WithJsError<T> for Result<T, anyhow::Error> {
|
|
100
|
+
fn with_js_error(self) -> Result<T, JsValue> {
|
|
101
|
+
match self {
|
|
102
|
+
Ok(ok) => Ok(ok),
|
|
103
|
+
Err(error) => Err(format!("{error:#}").into()),
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
impl<T> WithJsError<T> for Result<T, ProtocolError> {
|
|
109
|
+
fn with_js_error(self) -> Result<T, JsValue> {
|
|
110
|
+
match self {
|
|
111
|
+
Ok(ok) => Ok(ok),
|
|
112
|
+
Err(error) => Err(from_dpp_err(error)),
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
impl<T> WithJsError<T> for Result<T, serde_json::Error> {
|
|
118
|
+
fn with_js_error(self) -> Result<T, JsValue> {
|
|
119
|
+
match self {
|
|
120
|
+
Ok(ok) => Ok(ok),
|
|
121
|
+
Err(error) => Err(RustConversionError::from(error).to_js_value()),
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub fn generic_of_js_val<T: RefFromWasmAbi<Abi = u32>>(
|
|
127
|
+
js_value: &JsValue,
|
|
128
|
+
class_name: &str,
|
|
129
|
+
) -> Result<T::Anchor, JsValue> {
|
|
130
|
+
if !js_value.is_object() {
|
|
131
|
+
return Err(JsValue::from_str(
|
|
132
|
+
format!("Value supplied as {} is not an object", class_name).as_str(),
|
|
133
|
+
));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
let ctor_name = js_sys::Object::get_prototype_of(js_value)
|
|
137
|
+
.constructor()
|
|
138
|
+
.name();
|
|
139
|
+
|
|
140
|
+
if ctor_name == class_name {
|
|
141
|
+
let ptr = js_sys::Reflect::get(js_value, &JsValue::from_str("ptr"))?;
|
|
142
|
+
let ptr_u32: u32 =
|
|
143
|
+
ptr.as_f64()
|
|
144
|
+
.ok_or_else(|| JsValue::from("Invalid JS object pointer"))? as u32;
|
|
145
|
+
let reference = unsafe { T::ref_from_abi(ptr_u32) };
|
|
146
|
+
Ok(reference)
|
|
147
|
+
} else {
|
|
148
|
+
let error_string = format!(
|
|
149
|
+
"JS object constructor name mismatch. Expected {}, provided {}.",
|
|
150
|
+
class_name, ctor_name
|
|
151
|
+
);
|
|
152
|
+
Err(JsValue::from(&error_string))
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
use anyhow::Context;
|
|
2
|
+
use dpp::validation::JsonSchemaValidator;
|
|
3
|
+
use serde_json::Value;
|
|
4
|
+
use wasm_bindgen::prelude::*;
|
|
5
|
+
|
|
6
|
+
use crate::utils::{ToSerdeJSONExt, WithJsError};
|
|
7
|
+
|
|
8
|
+
#[wasm_bindgen(js_name=JsonSchemaValidator)]
|
|
9
|
+
pub struct JsonSchemaValidatorWasm(JsonSchemaValidator);
|
|
10
|
+
|
|
11
|
+
#[wasm_bindgen(js_class=JsonSchemaValidator)]
|
|
12
|
+
impl JsonSchemaValidatorWasm {
|
|
13
|
+
#[wasm_bindgen(constructor)]
|
|
14
|
+
pub fn new(
|
|
15
|
+
schema_js: &JsValue,
|
|
16
|
+
definitions: &JsValue,
|
|
17
|
+
) -> Result<JsonSchemaValidatorWasm, JsValue> {
|
|
18
|
+
let schema = schema_js.with_serde_to_json_value()?;
|
|
19
|
+
let maybe_defs: Option<Value> = if definitions.is_object() {
|
|
20
|
+
Some(definitions.with_serde_to_json_value()?)
|
|
21
|
+
} else {
|
|
22
|
+
None
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
let validator = if let Some(defs) = maybe_defs {
|
|
26
|
+
let map = defs
|
|
27
|
+
.as_object()
|
|
28
|
+
.context("definitions are not a map")
|
|
29
|
+
.with_js_error()?;
|
|
30
|
+
JsonSchemaValidator::new_with_definitions(schema, map)
|
|
31
|
+
.context("Schema Validator creation with definitions failed")
|
|
32
|
+
.with_js_error()?
|
|
33
|
+
} else {
|
|
34
|
+
JsonSchemaValidator::new(schema)
|
|
35
|
+
.context("Schema Validator creation failed")
|
|
36
|
+
.with_js_error()?
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Ok(JsonSchemaValidatorWasm(validator))
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/validation/mod.rs
CHANGED
|
@@ -4,7 +4,17 @@ use js_sys::JsString;
|
|
|
4
4
|
use wasm_bindgen::prelude::*;
|
|
5
5
|
|
|
6
6
|
#[wasm_bindgen(js_name=ValidationResult)]
|
|
7
|
-
|
|
7
|
+
#[derive(Debug)]
|
|
8
|
+
pub struct ValidationResultWasm(ValidationResult<JsValue>);
|
|
9
|
+
|
|
10
|
+
impl<T> From<ValidationResult<T>> for ValidationResultWasm
|
|
11
|
+
where
|
|
12
|
+
T: Into<JsValue> + Clone,
|
|
13
|
+
{
|
|
14
|
+
fn from(validation_result: ValidationResult<T>) -> Self {
|
|
15
|
+
ValidationResultWasm(validation_result.map(Into::into))
|
|
16
|
+
}
|
|
17
|
+
}
|
|
8
18
|
|
|
9
19
|
#[wasm_bindgen(js_class=ValidationResult)]
|
|
10
20
|
impl ValidationResultWasm {
|
|
@@ -32,10 +42,18 @@ impl ValidationResultWasm {
|
|
|
32
42
|
.map(from_consensus_error_ref)
|
|
33
43
|
.collect()
|
|
34
44
|
}
|
|
35
|
-
}
|
|
36
45
|
|
|
37
|
-
|
|
38
|
-
fn
|
|
39
|
-
|
|
46
|
+
#[wasm_bindgen(js_name=getData)]
|
|
47
|
+
pub fn get_data(&self) -> JsValue {
|
|
48
|
+
self.0.data().unwrap_or(&JsValue::undefined()).to_owned()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[wasm_bindgen(js_name=getFirstError)]
|
|
52
|
+
pub fn get_first_error(&self) -> JsValue {
|
|
53
|
+
if !self.0.errors.is_empty() {
|
|
54
|
+
from_consensus_error_ref(&self.0.errors[0])
|
|
55
|
+
} else {
|
|
56
|
+
JsValue::undefined()
|
|
57
|
+
}
|
|
40
58
|
}
|
|
41
59
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
use dpp::version::ProtocolVersionValidator;
|
|
2
|
+
use wasm_bindgen::prelude::*;
|
|
3
|
+
|
|
4
|
+
#[wasm_bindgen(js_name=ProtocolVersionValidator)]
|
|
5
|
+
pub struct ProtocolVersionValidatorWasm(ProtocolVersionValidator);
|
|
6
|
+
|
|
7
|
+
#[wasm_bindgen(js_class=ProtocolVersionValidator)]
|
|
8
|
+
impl ProtocolVersionValidatorWasm {
|
|
9
|
+
// TODO should the constructor be without parameters?
|
|
10
|
+
#[wasm_bindgen(constructor)]
|
|
11
|
+
pub fn new() -> ProtocolVersionValidatorWasm {
|
|
12
|
+
ProtocolVersionValidatorWasm(ProtocolVersionValidator::default())
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
impl From<ProtocolVersionValidator> for ProtocolVersionValidatorWasm {
|
|
17
|
+
fn from(doc_validator: ProtocolVersionValidator) -> Self {
|
|
18
|
+
ProtocolVersionValidatorWasm(doc_validator)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
impl From<ProtocolVersionValidatorWasm> for ProtocolVersionValidator {
|
|
23
|
+
fn from(val: ProtocolVersionValidatorWasm) -> Self {
|
|
24
|
+
val.0
|
|
25
|
+
}
|
|
26
|
+
}
|