pshenmic-dpp 1.0.4 → 1.0.5

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.
Files changed (81) hide show
  1. package/.github/workflows/build.yml +8 -0
  2. package/.github/workflows/publish.yml +8 -0
  3. package/Cargo.lock +79 -31
  4. package/Cargo.toml +20 -17
  5. package/README.md +66 -23
  6. package/babel.config.json +19 -0
  7. package/dist/wasm/pshenmic_dpp.js +2487 -355
  8. package/dist/wasm/pshenmic_dpp_bg.js +1 -1
  9. package/package.json +13 -6
  10. package/packages/cbor_conversation/Cargo.toml +1 -1
  11. package/packages/data_contract/Cargo.toml +2 -2
  12. package/packages/data_contract/src/lib.rs +195 -23
  13. package/packages/data_contract_transitions/Cargo.toml +1 -1
  14. package/packages/data_contract_transitions/src/create/mod.rs +23 -16
  15. package/packages/data_contract_transitions/src/update/mod.rs +21 -12
  16. package/packages/document/Cargo.toml +1 -1
  17. package/packages/document/src/lib.rs +1 -1
  18. package/packages/document/src/methods/mod.rs +370 -2
  19. package/packages/{document_batch → documents_batch}/Cargo.toml +6 -4
  20. package/packages/documents_batch/src/document_base_transition/mod.rs +92 -0
  21. package/packages/documents_batch/src/document_transition/mod.rs +166 -0
  22. package/packages/documents_batch/src/generators.rs +122 -0
  23. package/packages/documents_batch/src/lib.rs +188 -0
  24. package/packages/documents_batch/src/prefunded_voting_balance/mod.rs +45 -0
  25. package/packages/documents_batch/src/transitions/create/mod.rs +123 -0
  26. package/packages/documents_batch/src/transitions/delete/mod.rs +67 -0
  27. package/packages/documents_batch/src/transitions/mod.rs +6 -0
  28. package/packages/documents_batch/src/transitions/purchase/mod.rs +91 -0
  29. package/packages/documents_batch/src/transitions/replace/mod.rs +93 -0
  30. package/packages/documents_batch/src/transitions/transfer/mod.rs +86 -0
  31. package/packages/documents_batch/src/transitions/update_price/mod.rs +75 -0
  32. package/packages/enums/Cargo.toml +3 -3
  33. package/packages/enums/src/batch/batch_enum.rs +75 -10
  34. package/packages/enums/src/keys/key_type.rs +46 -0
  35. package/packages/enums/src/keys/purpose.rs +55 -0
  36. package/packages/enums/src/keys/security_level.rs +46 -0
  37. package/packages/enums/src/lib.rs +1 -0
  38. package/packages/enums/src/network/mod.rs +67 -0
  39. package/packages/enums/src/platform/mod.rs +66 -8
  40. package/packages/identity/Cargo.toml +3 -4
  41. package/packages/identity/src/lib.rs +25 -14
  42. package/packages/{public_key → identity_public_key}/Cargo.toml +1 -1
  43. package/packages/{public_key → identity_public_key}/src/lib.rs +74 -27
  44. package/packages/identity_transitions/Cargo.toml +2 -2
  45. package/packages/identity_transitions/src/create_transition/mod.rs +3 -1
  46. package/packages/identity_transitions/src/create_transition/public_key_in_creation/mod.rs +25 -21
  47. package/packages/mock_bls/Cargo.toml +1 -1
  48. package/packages/private_key/Cargo.toml +3 -2
  49. package/packages/private_key/src/lib.rs +44 -6
  50. package/packages/state_transition/Cargo.toml +5 -4
  51. package/packages/state_transition/src/lib.rs +101 -36
  52. package/packages/utils/Cargo.toml +1 -2
  53. package/packages/utils/src/lib.rs +17 -15
  54. package/scripts/build-js.sh +6 -5
  55. package/scripts/build.sh +22 -1
  56. package/src/lib.rs +7 -1
  57. package/tests/DataContract.spec.js +175 -0
  58. package/tests/DataContractCreateStateTransition.spec.js +109 -0
  59. package/tests/DataContractUpdateStateTransition.spec.js +109 -0
  60. package/tests/Document.spec.js +232 -0
  61. package/tests/DocumentBatchTransition.spec.js +109 -0
  62. package/tests/DocumentTransition.spec.js +97 -0
  63. package/tests/DocumentsTransitions.spec.js +705 -0
  64. package/tests/Identity.spec.js +113 -0
  65. package/tests/IdentityPublicKey.spec.js +132 -0
  66. package/tests/PrivateKey.spec.js +73 -0
  67. package/tests/mocks/DataContract/DataContract.json +153 -0
  68. package/tests/mocks/DataContract/index.js +6 -0
  69. package/tests/mocks/Document/DataContract.json +23 -0
  70. package/tests/mocks/Document/document.json +3 -0
  71. package/tests/mocks/Document/document2.json +3 -0
  72. package/tests/mocks/Document/index.js +11 -0
  73. package/tests/mocks/Identity/index.js +7 -0
  74. package/tests/mocks/PrivateKey/index.js +5 -0
  75. package/tests/mocks/PublicKey/index.js +14 -0
  76. package/tests/utils/hex.js +15 -0
  77. package/tests/utils/wasm.js +14 -0
  78. package/packages/document/src/methods/js.rs +0 -260
  79. package/packages/document/src/methods/rust.rs +0 -92
  80. package/packages/document_batch/src/generators.rs +0 -63
  81. package/packages/document_batch/src/lib.rs +0 -228
@@ -1,11 +1,17 @@
1
- use dpp::identity::KeyType;
2
- use dpp::serialization::{PlatformDeserializable, PlatformSerializable};
1
+ use dpp::dashcore::secp256k1::hashes::hex::Case::Lower;
2
+ use dpp::dashcore::secp256k1::hashes::hex::DisplayHex;
3
+ use dpp::identity::{KeyID, KeyType};
4
+ use dpp::platform_value::BinaryData;
5
+ use dpp::prelude::UserFeeIncrease;
6
+ use dpp::serialization::{PlatformDeserializable, PlatformSerializable, Signable};
3
7
  use dpp::state_transition::StateTransition;
4
8
  use pshenmic_dpp_enums::keys::key_type::KeyTypeWASM;
9
+ use pshenmic_dpp_enums::keys::purpose::PurposeWASM;
5
10
  use pshenmic_dpp_mock_bls::MockBLS;
6
11
  use pshenmic_dpp_private_key::PrivateKeyWASM;
7
12
  use pshenmic_dpp_public_key::IdentityPublicKeyWASM;
8
13
  use pshenmic_dpp_utils::WithJsError;
14
+ use sha2::{Digest, Sha256};
9
15
  use wasm_bindgen::JsValue;
10
16
  use wasm_bindgen::prelude::wasm_bindgen;
11
17
 
@@ -27,65 +33,55 @@ impl From<StateTransitionWASM> for StateTransition {
27
33
 
28
34
  #[wasm_bindgen]
29
35
  impl StateTransitionWASM {
30
- #[wasm_bindgen(js_name=sign)]
36
+ #[wasm_bindgen(js_name = "sign")]
31
37
  pub fn sign(
32
38
  &mut self,
33
39
  private_key: &PrivateKeyWASM,
34
40
  public_key: &IdentityPublicKeyWASM,
35
- ) -> Result<JsValue, JsValue> {
36
- let sig = self.0.sign(
37
- &public_key.clone().into(),
38
- private_key.get_key_bytes().as_slice(),
39
- &MockBLS {},
40
- );
41
-
42
- let bytes = match sig {
43
- Ok(_sig) => {
44
- self.0.set_signature(self.0.signature().clone());
45
- self.0
46
- .set_signature_public_key_id(self.0.signature_public_key_id().unwrap());
47
- self.0.serialize_to_bytes()
48
- }
49
- Err(e) => wasm_bindgen::throw_str(&e.to_string()),
50
- };
51
-
52
- match bytes {
53
- Ok(bytes) => Ok(JsValue::from(bytes.clone())),
54
- Err(e) => Ok(JsValue::from_str(&format!("{}", e))),
55
- }
41
+ ) -> Result<Vec<u8>, JsValue> {
42
+ self.0
43
+ .sign(
44
+ &public_key.clone().into(),
45
+ private_key.get_bytes().as_slice(),
46
+ &MockBLS {},
47
+ )
48
+ .with_js_error()?;
49
+
50
+ self.0.set_signature(self.0.signature().clone());
51
+ self.0
52
+ .set_signature_public_key_id(self.0.signature_public_key_id().unwrap());
53
+
54
+ self.0.serialize_to_bytes().with_js_error()
56
55
  }
57
56
 
58
- #[wasm_bindgen(js_name=signByPrivateKey)]
57
+ #[wasm_bindgen(js_name = "signByPrivateKey")]
59
58
  pub fn sign_by_private_key(
60
59
  &mut self,
61
60
  private_key: &PrivateKeyWASM,
62
- key_type: KeyTypeWASM,
63
- ) -> JsValue {
61
+ js_key_type: JsValue,
62
+ ) -> Result<Vec<u8>, JsValue> {
63
+ let key_type = KeyTypeWASM::try_from(js_key_type)?;
64
+
64
65
  let _sig = self
65
66
  .0
66
67
  .sign_by_private_key(
67
- &private_key.get_key_bytes().as_slice(),
68
+ &private_key.get_bytes().as_slice(),
68
69
  KeyType::from(key_type),
69
70
  &MockBLS {},
70
71
  )
71
72
  .with_js_error();
72
73
 
73
- let bytes = self.0.serialize_to_bytes().with_js_error();
74
-
75
- match bytes {
76
- Ok(bytes) => JsValue::from(bytes.clone()),
77
- Err(err) => err,
78
- }
74
+ self.0.serialize_to_bytes().with_js_error()
79
75
  }
80
76
 
81
- #[wasm_bindgen(js_name=toBytes)]
77
+ #[wasm_bindgen(js_name = "toBytes")]
82
78
  pub fn to_bytes(&self) -> Result<JsValue, JsValue> {
83
79
  let bytes = self.0.serialize_to_bytes().with_js_error()?;
84
80
 
85
81
  Ok(JsValue::from(bytes.clone()))
86
82
  }
87
83
 
88
- #[wasm_bindgen(js_name=fromBytes)]
84
+ #[wasm_bindgen(js_name = "fromBytes")]
89
85
  pub fn from_bytes(bytes: Vec<u8>) -> Result<StateTransitionWASM, JsValue> {
90
86
  let st = StateTransition::deserialize_from_bytes(bytes.as_slice());
91
87
 
@@ -94,4 +90,73 @@ impl StateTransitionWASM {
94
90
  Ok(transition) => Ok(StateTransitionWASM(transition)),
95
91
  }
96
92
  }
93
+
94
+ #[wasm_bindgen(js_name = "hash")]
95
+ pub fn get_hash(&self, skip_signature: bool) -> Result<String, JsValue> {
96
+ let payload: Vec<u8>;
97
+
98
+ if skip_signature {
99
+ payload = self.0.signable_bytes().with_js_error()?;
100
+ } else {
101
+ payload = dpp::serialization::PlatformSerializable::serialize_to_bytes(&self.0)
102
+ .with_js_error()?;
103
+ }
104
+
105
+ Ok(Sha256::digest(payload).to_hex_string(Lower))
106
+ }
107
+
108
+ #[wasm_bindgen(js_name = "getActionType")]
109
+ pub fn get_action_type(&self) -> String {
110
+ self.0.name()
111
+ }
112
+
113
+ #[wasm_bindgen(js_name = "getOwnerId")]
114
+ pub fn get_owner_id(&self) -> Vec<u8> {
115
+ self.0.owner_id().to_vec()
116
+ }
117
+
118
+ #[wasm_bindgen(getter = "signature")]
119
+ pub fn get_signature(&self) -> Vec<u8> {
120
+ self.0.signature().to_vec()
121
+ }
122
+
123
+ #[wasm_bindgen(getter = "signaturePublicKeyId")]
124
+ pub fn get_signature_public_key_id(&self) -> Option<KeyID> {
125
+ self.0.signature_public_key_id()
126
+ }
127
+
128
+ #[wasm_bindgen(getter = "userFeeIncrease")]
129
+ pub fn get_user_fee_increase(&self) -> UserFeeIncrease {
130
+ self.0.user_fee_increase()
131
+ }
132
+
133
+ #[wasm_bindgen(js_name = "getPurposeRequirement")]
134
+ pub fn get_purpose_requirement(&self) -> Option<Vec<String>> {
135
+ let requirements = self.0.purpose_requirement();
136
+
137
+ match requirements {
138
+ None => None,
139
+ Some(req) => Some(
140
+ req.iter()
141
+ .map(|purpose| PurposeWASM::from(purpose.clone()))
142
+ .map(String::from)
143
+ .collect(),
144
+ ),
145
+ }
146
+ }
147
+
148
+ #[wasm_bindgen(setter = "signature")]
149
+ pub fn set_signature(&mut self, signature: Vec<u8>) {
150
+ self.0.set_signature(BinaryData::from(signature))
151
+ }
152
+
153
+ #[wasm_bindgen(setter = "signaturePublicKeyId")]
154
+ pub fn set_signature_public_key_id(&mut self, key_id: KeyID) {
155
+ self.0.set_signature_public_key_id(key_id)
156
+ }
157
+
158
+ #[wasm_bindgen(setter = "userFeeIncrease")]
159
+ pub fn set_user_fee_increase(&mut self, user_fee_increase: UserFeeIncrease) {
160
+ self.0.set_user_fee_increase(user_fee_increase)
161
+ }
97
162
  }
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "pshenmic_dpp_utils"
3
- version = "0.1.0"
3
+ version.workspace = true
4
4
  edition = "2024"
5
5
 
6
6
  [lib]
@@ -17,4 +17,3 @@ js-sys = "0.3.53"
17
17
  serde_json = { version = "1.0", features = ["preserve_order"] }
18
18
  serde-wasm-bindgen = { git = "https://github.com/QuantumExplorer/serde-wasm-bindgen", branch = "feat/not_human_readable" }
19
19
  serde = { version = "1.0.197", features = ["derive"] }
20
- itertools = { version = "0.13" }
@@ -4,10 +4,9 @@ use anyhow::{Context, anyhow, bail};
4
4
  use dpp::ProtocolError;
5
5
  use dpp::identifier::Identifier;
6
6
  use dpp::platform_value::Value;
7
- use dpp::platform_value::string_encoding::Encoding;
7
+ use dpp::platform_value::string_encoding::Encoding::Base58;
8
8
  use dpp::util::hash::hash_double_to_vec;
9
- use itertools::Itertools;
10
- use js_sys::Function;
9
+ use js_sys::{Function, Uint8Array};
11
10
  use serde::de::DeserializeOwned;
12
11
  use serde_json::Value as JsonValue;
13
12
  use std::collections::BTreeMap;
@@ -274,7 +273,7 @@ pub fn generate_document_id_v0(
274
273
  owner_id: &Identifier,
275
274
  document_type_name: &str,
276
275
  entropy: &[u8],
277
- ) -> Identifier {
276
+ ) -> Result<Identifier, JsValue> {
278
277
  let mut buf: Vec<u8> = vec![];
279
278
 
280
279
  buf.extend_from_slice(&contract_id.to_buffer());
@@ -282,7 +281,7 @@ pub fn generate_document_id_v0(
282
281
  buf.extend_from_slice(document_type_name.as_bytes());
283
282
  buf.extend_from_slice(entropy);
284
283
 
285
- Identifier::from_bytes(&hash_double_to_vec(&buf)).unwrap()
284
+ Identifier::from_bytes(&hash_double_to_vec(&buf)).map_err(|e| JsValue::from(e.to_string()))
286
285
  }
287
286
 
288
287
  #[wasm_bindgen(raw_module = "../identifier_utils/Identifier.js")]
@@ -325,18 +324,21 @@ pub fn identifier_from_js_value(js_value: &JsValue) -> Result<Identifier, JsValu
325
324
  ));
326
325
  }
327
326
 
328
- let value: serde_json::Value = js_value.with_serde_to_json_value()?;
329
- match value {
330
- serde_json::Value::Array(arr) => {
331
- let bytes: Vec<u8> = arr.into_iter().map(value_to_u8).try_collect()?;
332
- Identifier::from_bytes(&bytes)
333
- .map_err(ProtocolError::ValueError)
334
- .with_js_error()
335
- }
336
- serde_json::Value::String(string) => Identifier::from_string(&string, Encoding::Base58)
327
+ match js_value.is_string() {
328
+ true => Identifier::from_string(js_value.as_string().unwrap_or("".into()).as_str(), Base58)
337
329
  .map_err(ProtocolError::ValueError)
338
330
  .with_js_error(),
339
- _ => Err(JsValue::from_str("Invalid ID. Expected array or string")),
331
+ false => match js_value.is_object() || js_value.is_array() {
332
+ true => {
333
+ let uint8_array = Uint8Array::from(js_value.clone());
334
+ let bytes = uint8_array.to_vec();
335
+
336
+ Identifier::from_bytes(&bytes)
337
+ .map_err(ProtocolError::ValueError)
338
+ .with_js_error()
339
+ }
340
+ false => Err(JsValue::from_str("Invalid ID. Expected array or string")),
341
+ },
340
342
  }
341
343
  }
342
344
 
@@ -27,13 +27,14 @@ if [[ "${RAW}" == "true" ]]; then
27
27
  else
28
28
  echo "Converting wasm binary into base64 module"
29
29
  WASM_BUILD_BASE_64=$(base64 -i "$WASM_BINARY_PATH")
30
- echo 'module.exports = "'${WASM_BUILD_BASE_64}'"' > "$DIST_WASM_BINARY_BASE_64"
30
+ echo 'export default "'${WASM_BUILD_BASE_64}'"' > "$DIST_WASM_BINARY_BASE_64"
31
31
  fi
32
32
 
33
- ## Transpile ES Modules code to Common JS
34
- ## and save directly to dist folder to avoid re-generating TS declarations
35
- echo "Transpiling wasm ES Modules to CommonJS"
33
+ echo "Copying ES module to dist"
36
34
  cp $WASM_JS_CODE_PATH $DIST_WASM_JS
37
35
 
38
36
  echo "Cleaning wasm build"
39
- #rm -rf $WASM_DIR
37
+ rm -rf $WASM_DIR
38
+
39
+ echo "Total build size: "
40
+ du -sh $DIST_WASM_DIR
package/scripts/build.sh CHANGED
@@ -9,6 +9,8 @@ PROFILE=release
9
9
 
10
10
  OUTPUT_FILE="${PWD}/wasm/pshenmic_dpp_bg.wasm"
11
11
 
12
+ RUSTFLAGS="-C target-feature=+crt-static -C embed-bitcode=no -C metadata=reduced -C link-dead-code=no -C panic=abort"
13
+
12
14
  BUILD_COMMAND="cargo build --config net.git-fetch-with-cli=true --target=${TARGET} ${PROFILE_ARG}"
13
15
  BINDGEN_COMMAND="wasm-bindgen --out-dir=${OUTPUT_DIR} --target=web --omit-default-module-path ${PWD}/target/${TARGET}/${PROFILE}/pshenmic_dpp.wasm"
14
16
 
@@ -28,7 +30,26 @@ fi
28
30
 
29
31
  if command -v wasm-opt &> /dev/null; then
30
32
  echo "Optimizing wasm using Binaryen"
31
- wasm-opt -tnh --flatten --rereloop -Oz --gufa -Oz --gufa -Oz "$OUTPUT_FILE" -o "${OUTPUT_FILE}"
33
+ wasm-opt \
34
+ --generate-global-effects \
35
+ -Oz \
36
+ -tnh \
37
+ --flatten \
38
+ --rereloop \
39
+ -Oz \
40
+ --converge \
41
+ --vacuum \
42
+ --dce \
43
+ --inlining-optimizing \
44
+ --merge-blocks \
45
+ --simplify-locals \
46
+ --optimize-instructions \
47
+ -Oz \
48
+ --gufa \
49
+ -Oz \
50
+ "$OUTPUT_FILE" \
51
+ -o \
52
+ "${OUTPUT_FILE}"
32
53
  else
33
54
  echo "wasm-opt command not found. Skipping wasm optimization."
34
55
  fi
package/src/lib.rs CHANGED
@@ -1,10 +1,16 @@
1
1
  #![no_std]
2
2
  #![no_main]
3
3
 
4
+ extern crate wee_alloc;
5
+
6
+ // allows to save 7 kb
7
+ #[global_allocator]
8
+ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
9
+
4
10
  pub use pshenmic_dpp_data_contract;
5
11
  pub use pshenmic_dpp_data_contract_transitions;
6
- pub use pshenmic_dpp_document_batch;
7
12
  pub use pshenmic_dpp_document_search;
13
+ pub use pshenmic_dpp_documents_batch;
8
14
  pub use pshenmic_dpp_enums;
9
15
  pub use pshenmic_dpp_identity;
10
16
  pub use pshenmic_dpp_identity_transitions;
@@ -0,0 +1,175 @@
1
+ const assert = require('assert')
2
+ const { describe, it, before } = require('mocha')
3
+ const initWasm = require('./utils/wasm')
4
+ const { value, id, ownerId } = require('./mocks/DataContract')
5
+ const { PlatformVersionWASM } = require('../dist/cjs/wasm/pshenmic_dpp')
6
+ const { base58 } = require('@scure/base')
7
+ const { fromHexString } = require('./utils/hex')
8
+
9
+ let wasm
10
+
11
+ let dataContractsBytes
12
+
13
+ describe('DataContract', function () {
14
+ before(async function () {
15
+ wasm = initWasm()
16
+
17
+ dataContractsBytes = ['003662bb61e17fae3ea294cf603197fb0aab6d51180bd8b6104c4944a62fe2d97f00000000000101000001000000000000000000000000000000000000000000000000000000000000000000010a7769746864726177616c1607120b6465736372697074696f6e12805769746864726177616c20646f63756d656e7420746f20747261636b20756e6465726c79696e67207769746864726177616c207472616e73616374696f6e732e205769746864726177616c732073686f756c6420626520637265617465642077697468204964656e746974795769746864726177616c5472616e736974696f6e12176372656174696f6e5265737472696374696f6e4d6f6465020212047479706512066f626a6563741207696e64696365731504160312046e616d65120e6964656e74697479537461747573120a70726f70657274696573150316011208246f776e6572496412036173631601120673746174757312036173631601120a2463726561746564417412036173631206756e697175651300160312046e616d65120e6964656e74697479526563656e74120a70726f70657274696573150316011208246f776e6572496412036173631601120a2475706461746564417412036173631601120673746174757312036173631206756e697175651300160312046e616d651207706f6f6c696e67120a70726f70657274696573150416011206737461747573120361736316011207706f6f6c696e6712036173631601120e636f72654665655065724279746512036173631601120a2475706461746564417412036173631206756e697175651300160312046e616d65120b7472616e73616374696f6e120a70726f706572746965731502160112067374617475731203617363160112107472616e73616374696f6e496e64657812036173631206756e697175651300120a70726f70657274696573160712107472616e73616374696f6e496e64657816041204747970651207696e7465676572120b6465736372697074696f6e127953657175656e7469616c20696e646578206f6620617373657420756e6c6f636b20287769746864726177616c29207472616e73616374696f6e2e20506f70756c61746564207768656e2061207769746864726177616c20706f6f6c656420696e746f207769746864726177616c207472616e73616374696f6e12076d696e696d756d02011208706f736974696f6e020012157472616e73616374696f6e5369676e48656967687416041204747970651207696e7465676572120b6465736372697074696f6e122f54686520436f726520686569676874206f6e207768696368207472616e73616374696f6e20776173207369676e656412076d696e696d756d02011208706f736974696f6e02011206616d6f756e7416041204747970651207696e7465676572120b6465736372697074696f6e121a54686520616d6f756e7420746f2062652077697468647261776e12076d696e696d756d02fb03e81208706f736974696f6e0202120e636f72654665655065724279746516051204747970651207696e7465676572120b6465736372697074696f6e1250546869732069732074686520666565207468617420796f75206172652077696c6c696e6720746f207370656e6420666f722074686973207472616e73616374696f6e20696e2044756666732f4279746512076d696e696d756d020112076d6178696d756d02fcffffffff1208706f736974696f6e02031207706f6f6c696e6716041204747970651207696e7465676572120b6465736372697074696f6e124e5468697320696e6469636174656420746865206c6576656c20617420776869636820506c6174666f726d2073686f756c642074727920746f20706f6f6c2074686973207472616e73616374696f6e1204656e756d15030200020102021208706f736974696f6e0204120c6f75747075745363726970741605120474797065120561727261791209627974654172726179130112086d696e4974656d73021712086d61784974656d7302191208706f736974696f6e0205120673746174757316041204747970651207696e74656765721204656e756d150502000201020202030204120b6465736372697074696f6e124330202d2050656e64696e672c2031202d205369676e65642c2032202d2042726f61646361737465642c2033202d20436f6d706c6574652c2034202d20457870697265641208706f736974696f6e020612146164646974696f6e616c50726f706572746965731300120872657175697265641507120a24637265617465644174120a247570646174656441741206616d6f756e74120e636f7265466565506572427974651207706f6f6c696e67120c6f75747075745363726970741206737461747573']
18
+ })
19
+
20
+ describe('serialization / deserialization', function () {
21
+ it('should allows to create DataContract from schema without full validation', function () {
22
+ const dataContract = new wasm.DataContractWASM(value.ownerId, BigInt(2), value.documentSchemas, null, false)
23
+
24
+ assert.notEqual(dataContract.__wbg_ptr, 0)
25
+ })
26
+
27
+ it('should allows to create DataContract from schema with full validation', function () {
28
+ const dataContract = new wasm.DataContractWASM(value.ownerId, BigInt(2), value.documentSchemas, null, true)
29
+
30
+ assert.notEqual(dataContract.__wbg_ptr, 0)
31
+ })
32
+
33
+ it('should allows to create DataContract from value with full validation and without platform version', function () {
34
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
35
+
36
+ assert.notEqual(dataContract.__wbg_ptr, 0)
37
+ })
38
+
39
+ it('should allows to convert DataContract to bytes and from bytes', function () {
40
+ const [dataContractBytes] = dataContractsBytes
41
+
42
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
43
+
44
+ assert.deepEqual(dataContract.toBytes(), fromHexString(dataContractBytes))
45
+
46
+ const dataContractFromBytes = wasm.DataContractWASM.fromBytes(dataContract.toBytes(), false, PlatformVersionWASM.PLATFORM_V1)
47
+
48
+ assert.notEqual(dataContract.__wbg_ptr, 0)
49
+
50
+ assert.deepEqual(dataContractFromBytes.toBytes(), fromHexString(dataContractBytes))
51
+ })
52
+
53
+ it('should allows to create DataContract from bytes without full validation', function () {
54
+ const [dataContractBytes] = dataContractsBytes
55
+
56
+ const dataContractFromBytes = wasm.DataContractWASM.fromBytes(fromHexString(dataContractBytes), false, PlatformVersionWASM.PLATFORM_V1)
57
+ const dataContractFromValue = wasm.DataContractWASM.fromValue(value, true)
58
+
59
+ assert.deepEqual(dataContractFromBytes.toValue(), dataContractFromValue.toValue())
60
+ })
61
+
62
+ it('should allows to create DataContract from bytes with full validation and without version', function () {
63
+ const [dataContractBytes] = dataContractsBytes
64
+
65
+ const dataContractFromBytes = wasm.DataContractWASM.fromBytes(fromHexString(dataContractBytes), true)
66
+ const dataContractFromValue = wasm.DataContractWASM.fromValue(value, true)
67
+
68
+ assert.deepEqual(dataContractFromBytes.toValue(), dataContractFromValue.toValue())
69
+ })
70
+
71
+ it('should allow to get json', function () {
72
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
73
+
74
+ assert.deepEqual(dataContract.toJson(), value)
75
+ })
76
+ })
77
+
78
+ describe('getters', function () {
79
+ it('should allow to get schemas', function () {
80
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
81
+
82
+ assert.deepEqual(dataContract.getSchema(), value.documentSchemas)
83
+ })
84
+
85
+ it('should allow to get version', function () {
86
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
87
+
88
+ assert.deepEqual(dataContract.getVersion(), value.version)
89
+ })
90
+
91
+ it('should allow to get id', function () {
92
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
93
+
94
+ assert.deepEqual(dataContract.getId(), base58.decode(id))
95
+ })
96
+
97
+ it('should allow to get owner id', function () {
98
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
99
+
100
+ assert.deepEqual(dataContract.getOwnerId(), base58.decode(ownerId))
101
+ })
102
+
103
+ it('should allow to get config', function () {
104
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
105
+
106
+ assert.deepEqual(dataContract.getConfig(), value.config)
107
+ })
108
+ })
109
+
110
+ describe('setters', function () {
111
+ it('should allow to set id', function () {
112
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
113
+
114
+ const valueId = base58.decode('7ckT6Y19HnjfqoPFmfL995i4z2HwgZ8UttNmP99LtCBH')
115
+
116
+ dataContract.setId(Array.from(valueId))
117
+
118
+ assert.deepEqual(dataContract.getId(), valueId)
119
+ })
120
+
121
+ it('should allow to set owner id', function () {
122
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
123
+
124
+ const valueId = base58.decode('3bx13Wd5k4LwHAvXJrayc5HdKPyiccKWYECPQGGYfnVL')
125
+
126
+ dataContract.setOwnerId('3bx13Wd5k4LwHAvXJrayc5HdKPyiccKWYECPQGGYfnVL')
127
+
128
+ assert.deepEqual(dataContract.getOwnerId(), valueId)
129
+ })
130
+
131
+ it('should allow to set version', function () {
132
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
133
+
134
+ dataContract.setVersion(20)
135
+
136
+ assert.equal(dataContract.getVersion(), 20)
137
+ })
138
+
139
+ it('should allow to set config', function () {
140
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
141
+
142
+ const oldConfig = dataContract.getConfig()
143
+
144
+ const newConfig = { ...oldConfig, canBeDeleted: !oldConfig.canBeDeleted }
145
+
146
+ dataContract.setConfig(newConfig)
147
+
148
+ assert.deepEqual(dataContract.getConfig(), newConfig)
149
+ })
150
+
151
+ it('should allow to set schema', function () {
152
+ const dataContract = wasm.DataContractWASM.fromValue(value, true)
153
+
154
+ const oldSchema = dataContract.getSchema()
155
+
156
+ const newSchema = {
157
+ pupup: oldSchema.withdrawal
158
+ }
159
+
160
+ dataContract.setSchema(newSchema)
161
+
162
+ assert.deepEqual(dataContract.getSchema(), newSchema)
163
+ })
164
+ })
165
+
166
+ describe('static', function () {
167
+ it('should allow to generate id', function () {
168
+ const id = wasm.DataContractWASM.generateId('3bx13Wd5k4LwHAvXJrayc5HdKPyiccKWYECPQGGYfnVL', BigInt(4))
169
+
170
+ const valueId = base58.decode('7ckT6Y19HnjfqoPFmfL995i4z2HwgZ8UttNmP99LtCBH')
171
+
172
+ assert.deepEqual(id, valueId)
173
+ })
174
+ })
175
+ })
@@ -0,0 +1,109 @@
1
+ const assert = require('assert')
2
+ const { describe, it, before } = require('mocha')
3
+ const initWasm = require('./utils/wasm')
4
+ const { value, dataContractsBytes } = require('./mocks/DataContract')
5
+ const { PlatformVersionWASM } = require('../dist/cjs/wasm/pshenmic_dpp')
6
+ const { fromHexString } = require('./utils/hex')
7
+
8
+ let wasm
9
+
10
+ describe('DataContract Create Transition', function () {
11
+ before(async function () {
12
+ wasm = initWasm()
13
+ })
14
+
15
+ describe('serialization / deserialization', function () {
16
+ it('should allow to create transitions from data contract', () => {
17
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
18
+
19
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
20
+
21
+ assert.notEqual(dataContractTransition.__wbg_ptr, 0)
22
+ assert.notEqual(dataContract.__wbg_ptr, 0)
23
+ })
24
+
25
+ it('should allow to convert transitions to bytes and create from bytes', () => {
26
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
27
+
28
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
29
+
30
+ const bytes = dataContractTransition.toBytes()
31
+
32
+ const newDataContractTransition = wasm.DataContractCreateTransitionWASM.fromBytes(bytes)
33
+
34
+ assert.deepEqual(newDataContractTransition.toBytes(), bytes)
35
+ assert.notEqual(newDataContractTransition.__wbg_ptr, 0)
36
+ assert.notEqual(dataContractTransition.__wbg_ptr, 0)
37
+ assert.notEqual(dataContract.__wbg_ptr, 0)
38
+ })
39
+
40
+ it('should allow to convert data contract transition to state transitions and create data contract transition from state transition', () => {
41
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
42
+
43
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
44
+
45
+ const stateTransition = dataContractTransition.toStateTransition()
46
+
47
+ const newDataContractTransition = wasm.DataContractCreateTransitionWASM.fromStateTransition(stateTransition)
48
+
49
+ assert.deepEqual(dataContractTransition.toBytes(), newDataContractTransition.toBytes())
50
+ })
51
+ })
52
+
53
+ describe('getters', function () {
54
+ it('should allow to get feature version', () => {
55
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
56
+
57
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
58
+
59
+ assert.equal(dataContractTransition.getFeatureVersion(), 0)
60
+ })
61
+
62
+ it('should allow to verify protocol version', () => {
63
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
64
+
65
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
66
+
67
+ assert.equal(dataContractTransition.verifyProtocolVersion(1), true)
68
+ })
69
+
70
+ it('should allow to verify incorrect protocol version', () => {
71
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
72
+
73
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
74
+
75
+ try {
76
+ dataContractTransition.verifyProtocolVersion(20)
77
+ assert.equal(true, false)
78
+ } catch (error) {
79
+ assert.equal(false, false)
80
+ }
81
+ })
82
+
83
+ it('should allow to get data contract', () => {
84
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
85
+
86
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
87
+
88
+ const newDataContract = dataContractTransition.getDataContract()
89
+
90
+ assert.deepEqual(dataContract.toBytes(), newDataContract.toBytes())
91
+ })
92
+ })
93
+
94
+ describe('setters', function () {
95
+ it('should allow to set the data contract', () => {
96
+ const [dataContractBytes] = dataContractsBytes
97
+
98
+ const dataContract = wasm.DataContractWASM.fromValue(value, false, PlatformVersionWASM.PLATFORM_V1)
99
+
100
+ const dataContractTransition = new wasm.DataContractCreateTransitionWASM(dataContract, BigInt(1))
101
+
102
+ const newDataContract = wasm.DataContractWASM.fromBytes(fromHexString(dataContractBytes), false, PlatformVersionWASM.PLATFORM_V1)
103
+
104
+ dataContractTransition.setDataContract(newDataContract)
105
+
106
+ assert.deepEqual(fromHexString(dataContractBytes), dataContractTransition.getDataContract().toBytes())
107
+ })
108
+ })
109
+ })