pshenmic-dpp 1.0.3 → 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 (83) hide show
  1. package/.github/workflows/build.yml +8 -0
  2. package/.github/workflows/publish.yml +8 -0
  3. package/Cargo.lock +89 -28
  4. package/Cargo.toml +22 -18
  5. package/README.md +66 -23
  6. package/babel.config.json +19 -0
  7. package/dist/wasm/pshenmic_dpp.js +2992 -402
  8. package/dist/wasm/pshenmic_dpp_bg.js +1 -1
  9. package/package.json +13 -6
  10. package/packages/{cbor-conversation → cbor_conversation}/Cargo.toml +1 -5
  11. package/packages/data_contract/Cargo.toml +3 -7
  12. package/packages/data_contract/src/lib.rs +234 -47
  13. package/packages/{document_batch → data_contract_transitions}/Cargo.toml +6 -14
  14. package/packages/data_contract_transitions/src/create/mod.rs +161 -0
  15. package/packages/data_contract_transitions/src/lib.rs +2 -0
  16. package/packages/data_contract_transitions/src/update/mod.rs +144 -0
  17. package/packages/document/Cargo.toml +2 -7
  18. package/packages/document/src/lib.rs +1 -1
  19. package/packages/document/src/methods/mod.rs +370 -2
  20. package/packages/documents_batch/Cargo.toml +19 -0
  21. package/packages/documents_batch/src/document_base_transition/mod.rs +92 -0
  22. package/packages/documents_batch/src/document_transition/mod.rs +166 -0
  23. package/packages/documents_batch/src/generators.rs +122 -0
  24. package/packages/documents_batch/src/lib.rs +188 -0
  25. package/packages/documents_batch/src/prefunded_voting_balance/mod.rs +45 -0
  26. package/packages/documents_batch/src/transitions/create/mod.rs +123 -0
  27. package/packages/documents_batch/src/transitions/delete/mod.rs +67 -0
  28. package/packages/documents_batch/src/transitions/mod.rs +6 -0
  29. package/packages/documents_batch/src/transitions/purchase/mod.rs +91 -0
  30. package/packages/documents_batch/src/transitions/replace/mod.rs +93 -0
  31. package/packages/documents_batch/src/transitions/transfer/mod.rs +86 -0
  32. package/packages/documents_batch/src/transitions/update_price/mod.rs +75 -0
  33. package/packages/enums/Cargo.toml +2 -5
  34. package/packages/enums/src/batch/batch_enum.rs +75 -10
  35. package/packages/enums/src/keys/key_type.rs +46 -0
  36. package/packages/enums/src/keys/purpose.rs +55 -0
  37. package/packages/enums/src/keys/security_level.rs +46 -0
  38. package/packages/enums/src/lib.rs +1 -0
  39. package/packages/enums/src/network/mod.rs +67 -0
  40. package/packages/enums/src/platform/mod.rs +66 -8
  41. package/packages/identity/Cargo.toml +2 -7
  42. package/packages/identity/src/lib.rs +25 -14
  43. package/packages/{public_key → identity_public_key}/Cargo.toml +2 -6
  44. package/packages/{public_key → identity_public_key}/src/lib.rs +74 -27
  45. package/packages/identity_transitions/Cargo.toml +3 -7
  46. package/packages/identity_transitions/src/create_transition/mod.rs +5 -3
  47. package/packages/identity_transitions/src/create_transition/public_key_in_creation/mod.rs +25 -21
  48. package/packages/mock_bls/Cargo.toml +2 -6
  49. package/packages/private_key/Cargo.toml +2 -5
  50. package/packages/private_key/src/lib.rs +44 -6
  51. package/packages/state_transition/Cargo.toml +4 -7
  52. package/packages/state_transition/src/lib.rs +106 -40
  53. package/packages/utils/Cargo.toml +1 -6
  54. package/packages/utils/src/lib.rs +17 -15
  55. package/scripts/build-js.sh +8 -6
  56. package/scripts/build.sh +22 -1
  57. package/src/lib.rs +8 -1
  58. package/tests/DataContract.spec.js +175 -0
  59. package/tests/DataContractCreateStateTransition.spec.js +109 -0
  60. package/tests/DataContractUpdateStateTransition.spec.js +109 -0
  61. package/tests/Document.spec.js +232 -0
  62. package/tests/DocumentBatchTransition.spec.js +109 -0
  63. package/tests/DocumentTransition.spec.js +97 -0
  64. package/tests/DocumentsTransitions.spec.js +705 -0
  65. package/tests/Identity.spec.js +113 -0
  66. package/tests/IdentityPublicKey.spec.js +132 -0
  67. package/tests/PrivateKey.spec.js +73 -0
  68. package/tests/mocks/DataContract/DataContract.json +153 -0
  69. package/tests/mocks/DataContract/index.js +6 -0
  70. package/tests/mocks/Document/DataContract.json +23 -0
  71. package/tests/mocks/Document/document.json +3 -0
  72. package/tests/mocks/Document/document2.json +3 -0
  73. package/tests/mocks/Document/index.js +11 -0
  74. package/tests/mocks/Identity/index.js +7 -0
  75. package/tests/mocks/PrivateKey/index.js +5 -0
  76. package/tests/mocks/PublicKey/index.js +14 -0
  77. package/tests/utils/hex.js +15 -0
  78. package/tests/utils/wasm.js +14 -0
  79. package/packages/document/src/methods/js.rs +0 -260
  80. package/packages/document/src/methods/rust.rs +0 -92
  81. package/packages/document_batch/src/generators.rs +0 -63
  82. package/packages/document_batch/src/lib.rs +0 -228
  83. /package/packages/{cbor-conversation → cbor_conversation}/src/lib.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "pshenmic_dpp_private_key"
3
- version = "0.1.0"
3
+ version.workspace = true
4
4
  edition = "2024"
5
5
 
6
6
  [lib]
@@ -11,7 +11,4 @@ wasm-bindgen = { version = "=0.2.100", features = ["serde-serialize"] }
11
11
  dpp = { git = "https://github.com/dashpay/platform", branch = "master", default-features = false, features = [
12
12
  "identity-value-conversion",
13
13
  ]}
14
-
15
- [profile.release]
16
- lto = true
17
- opt-level = 'z'
14
+ pshenmic_dpp_enums = {path = "../enums"}
@@ -1,4 +1,8 @@
1
1
  use dpp::dashcore::PrivateKey;
2
+ use dpp::dashcore::hashes::hex::FromHex;
3
+ use dpp::dashcore::key::Secp256k1;
4
+ use dpp::dashcore::secp256k1::hashes::hex::{Case, DisplayHex};
5
+ use pshenmic_dpp_enums::network::NetworkWASM;
2
6
  use wasm_bindgen::JsValue;
3
7
  use wasm_bindgen::prelude::wasm_bindgen;
4
8
 
@@ -7,8 +11,8 @@ pub struct PrivateKeyWASM(PrivateKey);
7
11
 
8
12
  #[wasm_bindgen]
9
13
  impl PrivateKeyWASM {
10
- #[wasm_bindgen(constructor)]
11
- pub fn new(wif: &str) -> Result<Self, JsValue> {
14
+ #[wasm_bindgen(js_name = "fromWIF")]
15
+ pub fn from_wif(wif: &str) -> Result<Self, JsValue> {
12
16
  let pk = PrivateKey::from_wif(wif).map_err(|err| JsValue::from_str(&*err.to_string()));
13
17
 
14
18
  match pk {
@@ -16,17 +20,51 @@ impl PrivateKeyWASM {
16
20
  Err(err) => Err(err),
17
21
  }
18
22
  }
23
+
24
+ #[wasm_bindgen(js_name = "fromBytes")]
25
+ pub fn from_bytes(bytes: Vec<u8>, js_network: JsValue) -> Result<Self, JsValue> {
26
+ let network = NetworkWASM::try_from(js_network)?;
27
+
28
+ let pk = PrivateKey::from_slice(bytes.as_slice(), network.into())
29
+ .map_err(|err| JsValue::from_str(&*err.to_string()))?;
30
+
31
+ Ok(PrivateKeyWASM(pk))
32
+ }
33
+
34
+ #[wasm_bindgen(js_name = "fromHex")]
35
+ pub fn from_hex(hex_key: &str, js_network: JsValue) -> Result<Self, JsValue> {
36
+ let network = NetworkWASM::try_from(js_network)?;
37
+
38
+ let bytes = Vec::from_hex(hex_key).map_err(|err| JsValue::from(err.to_string()))?;
39
+
40
+ let pk = PrivateKey::from_slice(bytes.as_slice(), network.into())
41
+ .map_err(|err| JsValue::from_str(&*err.to_string()))?;
42
+
43
+ Ok(PrivateKeyWASM(pk))
44
+ }
19
45
  }
20
46
 
21
47
  #[wasm_bindgen]
22
48
  impl PrivateKeyWASM {
23
- #[wasm_bindgen(js_name = "getKeyWIF")]
24
- pub fn get_key_wif(&self) -> String {
49
+ #[wasm_bindgen(js_name = "getWIF")]
50
+ pub fn get_wif(&self) -> String {
25
51
  self.0.to_wif()
26
52
  }
27
53
 
28
- #[wasm_bindgen(js_name = "getKeyBytes")]
29
- pub fn get_key_bytes(&self) -> Vec<u8> {
54
+ #[wasm_bindgen(js_name = "getBytes")]
55
+ pub fn get_bytes(&self) -> Vec<u8> {
30
56
  self.0.to_bytes()
31
57
  }
58
+
59
+ #[wasm_bindgen(js_name = "getHex")]
60
+ pub fn get_hex(&self) -> String {
61
+ self.0.to_bytes().to_hex_string(Case::Upper)
62
+ }
63
+
64
+ #[wasm_bindgen(js_name = "getPublicKeyHash")]
65
+ pub fn get_public_key_hash(&self) -> String {
66
+ let secp = Secp256k1::new();
67
+
68
+ self.0.public_key(&secp).pubkey_hash().to_hex()
69
+ }
32
70
  }
@@ -1,14 +1,14 @@
1
1
  [package]
2
2
  name = "pshenmic_dpp_state_transition"
3
- version = "0.1.0"
3
+ version.workspace = true
4
4
  edition = "2024"
5
5
 
6
6
  [lib]
7
7
  crate-type = ["cdylib", "lib"]
8
8
 
9
9
  [dependencies]
10
- wasm-bindgen = { version = "=0.2.100", features = ["serde-serialize"] }
11
- pshenmic_dpp_public_key = {path = "../public_key"}
10
+ wasm-bindgen = { version = "=0.2.100", features = ["serde-serialize"], default-features = false }
11
+ pshenmic_dpp_public_key = {path = "../identity_public_key" }
12
12
  pshenmic_dpp_private_key = {path = "../private_key"}
13
13
  pshenmic_dpp_utils = {path = "../utils"}
14
14
  pshenmic_dpp_mock_bls = {path = "../mock_bls"}
@@ -16,7 +16,4 @@ pshenmic_dpp_enums = {path = "../enums"}
16
16
  dpp = { git = "https://github.com/dashpay/platform", branch = "master", default-features = false, features = [
17
17
  "state-transition-signing"
18
18
  ]}
19
-
20
- [profile.release]
21
- lto = true
22
- opt-level = 'z'
19
+ sha2 = "0.10.8"
@@ -1,14 +1,21 @@
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
 
18
+ #[derive(Clone)]
12
19
  #[wasm_bindgen(js_name = "StateTransitionWASM")]
13
20
  pub struct StateTransitionWASM(StateTransition);
14
21
 
@@ -26,65 +33,55 @@ impl From<StateTransitionWASM> for StateTransition {
26
33
 
27
34
  #[wasm_bindgen]
28
35
  impl StateTransitionWASM {
29
- #[wasm_bindgen(js_name=sign)]
36
+ #[wasm_bindgen(js_name = "sign")]
30
37
  pub fn sign(
31
38
  &mut self,
32
- private_key: PrivateKeyWASM,
33
- public_key: IdentityPublicKeyWASM,
34
- ) -> Result<JsValue, JsValue> {
35
- let sig = self.0.sign(
36
- &public_key.into(),
37
- private_key.get_key_bytes().as_slice(),
38
- &MockBLS {},
39
- );
40
-
41
- let bytes = match sig {
42
- Ok(_sig) => {
43
- self.0.set_signature(self.0.signature().clone());
44
- self.0
45
- .set_signature_public_key_id(self.0.signature_public_key_id().unwrap());
46
- self.0.serialize_to_bytes()
47
- }
48
- Err(e) => wasm_bindgen::throw_str(&e.to_string()),
49
- };
50
-
51
- match bytes {
52
- Ok(bytes) => Ok(JsValue::from(bytes.clone())),
53
- Err(e) => Ok(JsValue::from_str(&format!("{}", e))),
54
- }
39
+ private_key: &PrivateKeyWASM,
40
+ public_key: &IdentityPublicKeyWASM,
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()
55
55
  }
56
56
 
57
- #[wasm_bindgen(js_name=signByPrivateKey)]
57
+ #[wasm_bindgen(js_name = "signByPrivateKey")]
58
58
  pub fn sign_by_private_key(
59
59
  &mut self,
60
- private_key: PrivateKeyWASM,
61
- key_type: KeyTypeWASM,
62
- ) -> JsValue {
60
+ private_key: &PrivateKeyWASM,
61
+ js_key_type: JsValue,
62
+ ) -> Result<Vec<u8>, JsValue> {
63
+ let key_type = KeyTypeWASM::try_from(js_key_type)?;
64
+
63
65
  let _sig = self
64
66
  .0
65
67
  .sign_by_private_key(
66
- &private_key.get_key_bytes().as_slice(),
68
+ &private_key.get_bytes().as_slice(),
67
69
  KeyType::from(key_type),
68
70
  &MockBLS {},
69
71
  )
70
72
  .with_js_error();
71
73
 
72
- let bytes = self.0.serialize_to_bytes().with_js_error();
73
-
74
- match bytes {
75
- Ok(bytes) => JsValue::from(bytes.clone()),
76
- Err(err) => err,
77
- }
74
+ self.0.serialize_to_bytes().with_js_error()
78
75
  }
79
76
 
80
- #[wasm_bindgen(js_name=toBytes)]
77
+ #[wasm_bindgen(js_name = "toBytes")]
81
78
  pub fn to_bytes(&self) -> Result<JsValue, JsValue> {
82
- let bytes = self.0.serialize_to_bytes().expect("Serialization failed");
79
+ let bytes = self.0.serialize_to_bytes().with_js_error()?;
83
80
 
84
81
  Ok(JsValue::from(bytes.clone()))
85
82
  }
86
83
 
87
- #[wasm_bindgen(js_name=fromBytes)]
84
+ #[wasm_bindgen(js_name = "fromBytes")]
88
85
  pub fn from_bytes(bytes: Vec<u8>) -> Result<StateTransitionWASM, JsValue> {
89
86
  let st = StateTransition::deserialize_from_bytes(bytes.as_slice());
90
87
 
@@ -93,4 +90,73 @@ impl StateTransitionWASM {
93
90
  Ok(transition) => Ok(StateTransitionWASM(transition)),
94
91
  }
95
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
+ }
96
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,8 +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" }
21
-
22
- [profile.release]
23
- lto = true
24
- opt-level = 'z'
@@ -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
 
@@ -15,24 +15,26 @@ WASM_BINARY_PATH="$WASM_DIR/pshenmic_dpp_bg.wasm"
15
15
  # Create directory in dist to save transpiled wasm code and TS typings
16
16
  mkdir -p $DIST_WASM_DIR
17
17
 
18
- ## Converting wasm into base64 and saving it to dist folder
19
18
 
20
19
  rm -rf $DIST_WASM_BINARY_BASE_64
21
20
  rm -rf $DIST_WASM_BINARY_RAW
22
21
  rm -rf $DIST_WASM_JS
23
22
 
23
+ ## Converting wasm into base64 and saving it to dist folder
24
+
24
25
  if [[ "${RAW}" == "true" ]]; then
25
26
  cp $WASM_BINARY_PATH $DIST_WASM_BINARY_RAW
26
27
  else
27
28
  echo "Converting wasm binary into base64 module"
28
29
  WASM_BUILD_BASE_64=$(base64 -i "$WASM_BINARY_PATH")
29
- 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"
30
31
  fi
31
32
 
32
- ## Transpile ES Modules code to Common JS
33
- ## and save directly to dist folder to avoid re-generating TS declarations
34
- echo "Transpiling wasm ES Modules to CommonJS"
33
+ echo "Copying ES module to dist"
35
34
  cp $WASM_JS_CODE_PATH $DIST_WASM_JS
36
35
 
37
36
  echo "Cleaning wasm build"
38
- 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,9 +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
- pub use pshenmic_dpp_document_batch;
11
+ pub use pshenmic_dpp_data_contract_transitions;
6
12
  pub use pshenmic_dpp_document_search;
13
+ pub use pshenmic_dpp_documents_batch;
7
14
  pub use pshenmic_dpp_enums;
8
15
  pub use pshenmic_dpp_identity;
9
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
+ })