pshenmic-dpp 1.1.0-dev.3 → 1.1.0-dev.4

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/README.md CHANGED
@@ -18,18 +18,23 @@ ___
18
18
  ### Build JS
19
19
  `yarn build:full`
20
20
 
21
- **NOTE. If you want to use ES6 module without CJS, you need to run `yarn babel`**
22
-
23
21
  ### Build with binary instead base122
24
22
  `yarn build:raw`
25
23
  Now you can import `.js` with base122 buffer for WebAssembly module, or binary which smaller
26
24
  ___
27
25
  ## Why you need to use `pshenmic-dpp` instead `wasm-dpp`
28
26
 
29
- - `pshenmic-dpp` weighs much less, currently taking up only 3.9 mb in base122 format
27
+ - `pshenmic-dpp` weighs much less, currently taking up only 1.4mb(\*) or 2.2mb(\*) in base122 format with GZIP
30
28
  - You can build only necessary modules by removing imports from `lib.rs` before building
31
29
  - More accurately replicates `rs-dpp`
32
30
  - Some sugar, like enums, which you can pass in string with any case or just use numbers
31
+
32
+ **(\*)** \- This module contains 2 builds for wasm js layer:
33
+ - JS without bundling or GZIP (2.2mb)
34
+ - Bundled JS with GZIP (1.4mb)
35
+
36
+ These variations can be imported depending on requirements.
37
+ In both cases, the binary wasm data will be compressed using gzip.
33
38
  ___
34
39
 
35
40
  ## Current features
@@ -129,8 +134,9 @@ yarn tests
129
134
 
130
135
  ## Example
131
136
 
137
+ This example show how to synchronously import module with size 2.2mb
132
138
  ```js
133
- import wasm from pshenmic_dpp';
139
+ import wasm from 'pshenmic_dpp';
134
140
 
135
141
  const document = new wasm.DocumentWASM(
136
142
  {
@@ -169,3 +175,19 @@ st.sign(privKey, pubKey)
169
175
  console.log(st.toBytes())
170
176
  console.log(st.hash(false))
171
177
  ```
178
+
179
+ This example show how to asynchronously import module with size 1.4mb
180
+ ```js
181
+ const {default: initWASM} = require('pshenmic-dpp/initAsync')
182
+
183
+ async function main() {
184
+ let wasm = await initWASM()
185
+
186
+ const dataContractIdentifier = new wasm.IdentifierWASM('6QMfQTdKpC3Y9uWBcTwXeY3KdzRLDqASUsDnQ4MEc9XC')
187
+ const ownerIdentifier = new wasm.IdentifierWASM('B7kcE1juMBWEWkuYRJhVdAE2e6RaevrGxRsa1DrLCpQH')
188
+
189
+ const documentInstance = new wasm.DocumentWASM(document, 'documentTypeName', BigInt(1), dataContractIdentifier, ownerIdentifier)
190
+ }
191
+
192
+ main().catch(console.error)
193
+ ```
@@ -18,10 +18,9 @@ const kShortened = 0b111 // Uses the illegal index to signify the last two-byte
18
18
  * with raw data bytes or a string of bytes (i.e. the type of argument to btoa())
19
19
  * @returns {Uint8Array} The data in a regular array representing byte values.
20
20
  */
21
- export function decodeBase122 (base122Data) {
21
+ export function decode (base122Data) {
22
22
  const strData = typeof (base122Data) === 'string' ? base122Data : utf8DataToString(base122Data)
23
23
  const decoded = []
24
- const decodedIndex = 0
25
24
  let curByte = 0
26
25
  let bitOfByte = 0
27
26
 
@@ -47,7 +46,7 @@ export function decodeBase122 (base122Data) {
47
46
  const illegalIndex = (c >>> 8) & 7 // 7 = 0b111.
48
47
  // We have to first check if this is a shortened two-byte character, i.e. if it only
49
48
  // encodes <= 7 bits.
50
- if (illegalIndex != kShortened) push7(kIllegals[illegalIndex])
49
+ if (illegalIndex !== kShortened) push7(kIllegals[illegalIndex])
51
50
  // Always push the rest.
52
51
  push7(c & 127)
53
52
  } else {
@@ -1,8 +1,8 @@
1
- import * as DPP from './pshenmic_dpp';
1
+ import * as DPP from './pshenmic_dpp.js';
2
2
 
3
- export * from './pshenmic_dpp'
3
+ export * from './pshenmic_dpp.js'
4
4
 
5
- export * from './base122'
5
+ export * from './base122.js'
6
6
 
7
7
  export default dpp;
8
8
 
@@ -1,12 +1,13 @@
1
1
  import * as DPP from './pshenmic_dpp.js'
2
- import { default as wasmBytes } from './pshenmic_dpp_bg.js'
3
- import { decodeBase122 } from './base122.js'
2
+ import wasmBytes from './pshenmic_dpp_bg.js'
3
+ import { decode } from './base122.js'
4
+ import { decompressSync } from 'fflate'
4
5
 
5
6
  export * from './base122.js'
6
7
  export * from './pshenmic_dpp.js'
7
8
 
8
- const bytes = new Uint8Array(decodeBase122(wasmBytes))
9
+ const bytes = new Uint8Array(decode(wasmBytes))
9
10
 
10
- const dppInstance = DPP.initSync({ module: bytes })
11
+ DPP.initSync({ module: decompressSync(bytes) })
11
12
 
12
13
  export default DPP
@@ -0,0 +1,6 @@
1
+ import type {DashPlatformProtocolWASM} from "./index.d.ts";
2
+
3
+ declare function initModule(): Promise<DashPlatformProtocolWASM>
4
+
5
+ export default initModule;
6
+ export type { DashPlatformProtocolWASM } from './index.d.ts';
@@ -0,0 +1,20 @@
1
+ import initWASM from './initWASM.js'
2
+ import initWASMInterface from './initInterface.js'
3
+
4
+ // eslint-disable-next-line
5
+ let dpp
6
+
7
+ /**
8
+ * Asynchronously initialization for compressed module (reduced size)
9
+ * @returns {Promise<*>}
10
+ */
11
+ async function initModule () {
12
+ const wasm = await initWASM()
13
+ const wasmInterface = await initWASMInterface()
14
+
15
+ dpp = wasmInterface.initSync(wasm)
16
+
17
+ return wasmInterface
18
+ }
19
+
20
+ export default initModule
@@ -0,0 +1,5 @@
1
+ import type {DashPlatformProtocolWASM} from "./index.d.ts";
2
+
3
+ declare function initModule(): Promise<DashPlatformProtocolWASM>
4
+
5
+ export default initModule;
@@ -0,0 +1,45 @@
1
+ import wasmInterfaceBytes from './pshenmic_dpp_zipped.js'
2
+ import { decode } from './base122.js'
3
+
4
+ export * from './base122.js'
5
+
6
+ const isNode = () =>
7
+ typeof process !== 'undefined' &&
8
+ !!process.versions &&
9
+ !!process.versions.node
10
+
11
+ const interfaceBytes = new Uint8Array(decode(wasmInterfaceBytes))
12
+ const interfaceBlob = new Blob([interfaceBytes])
13
+
14
+ async function initInterface () {
15
+ const decompressionStream = new DecompressionStream('gzip')
16
+ const decompressedStream = interfaceBlob.stream().pipeThrough(decompressionStream)
17
+ const decompressedResponse = new Response(decompressedStream)
18
+ const moduleText = await decompressedResponse.text()
19
+
20
+ let module
21
+ if (isNode()) {
22
+ const vm = await import('vm')
23
+
24
+ const context = vm.createContext({
25
+ crypto,
26
+ TextDecoder
27
+ })
28
+ const vmModule = new vm.Script(moduleText)
29
+
30
+ vmModule.runInContext(context)
31
+
32
+ module = context['pshenmic-dpp']
33
+ } else {
34
+ const blob = new Blob([moduleText], { type: 'application/javascript' })
35
+ const moduleUrl = URL.createObjectURL(blob)
36
+
37
+ module = await import(moduleUrl)
38
+
39
+ URL.revokeObjectURL(moduleUrl)
40
+ }
41
+
42
+ return module
43
+ }
44
+
45
+ export default initInterface
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<ArrayBuffer>;
2
+ export default _default;
@@ -0,0 +1,28 @@
1
+ import wasmBytes from './pshenmic_dpp_bg.js'
2
+ import { decode } from './base122.js'
3
+
4
+ export * from './base122.js'
5
+
6
+ // eslint-disable-next-line
7
+ const isNode = () =>
8
+ typeof process !== 'undefined' &&
9
+ !!process.versions &&
10
+ !!process.versions.node
11
+
12
+ const interfaceBytes = new Uint8Array(decode(wasmBytes))
13
+ const interfaceBlob = new Blob([interfaceBytes])
14
+
15
+ /**
16
+ * Convert wasm bin string to Uint8Array with decompression via DecompressionStream
17
+ * @returns {Promise<ArrayBuffer>}
18
+ */
19
+ async function initWASM () {
20
+ const decompressionStream = new DecompressionStream('gzip')
21
+ const decompressedStream = interfaceBlob.stream().pipeThrough(decompressionStream)
22
+ const decompressedResponse = new Response(decompressedStream)
23
+ const module = await decompressedResponse.arrayBuffer()
24
+
25
+ return module
26
+ }
27
+
28
+ export default initWASM