@dashevo/wasm-sdk 2.1.0-dev.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.
- package/README.md +241 -0
- package/dist/raw/wasm_sdk.d.ts +1110 -0
- package/dist/raw/wasm_sdk.js +4585 -0
- package/dist/raw/wasm_sdk.no_url.js +4585 -0
- package/dist/raw/wasm_sdk_bg.wasm +0 -0
- package/dist/raw/wasm_sdk_bg.wasm.d.ts +238 -0
- package/dist/sdk.d.ts +1110 -0
- package/dist/sdk.js +79 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# WASM SDK
|
|
2
|
+
|
|
3
|
+
Dash Platform WebAssembly SDK for JavaScript and TypeScript. The core is implemented in Rust and compiled to WebAssembly; a thin ESM wrapper exposes a convenient JS API that works in both Node.js and modern browsers.
|
|
4
|
+
|
|
5
|
+
The SDK provides:
|
|
6
|
+
- Cryptographic utilities: mnemonic generation/validation, key derivation, key pair generation, address validation, message signing.
|
|
7
|
+
- Platform state transitions and queries: identities, documents, data contracts, tokens, groups, epochs/system, voting, proofs (when supported).
|
|
8
|
+
- A builder pattern (`WasmSdkBuilder`) to configure and construct a client for network-backed queries.
|
|
9
|
+
|
|
10
|
+
This package ships a single-file ESM build (`dist/sdk.js`) with the Wasm inlined and compiled off the main thread in browsers. Advanced users can also opt into separate raw artifacts under `dist/raw/*`.
|
|
11
|
+
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
From npm (consumer apps):
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @dashevo/wasm-sdk
|
|
20
|
+
# or
|
|
21
|
+
yarn add @dashevo/wasm-sdk
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
From this monorepo (contributors):
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
# fast for development
|
|
28
|
+
yarn build
|
|
29
|
+
# or optimized
|
|
30
|
+
yarn run build:release
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
The package is ESM-only ("type": "module"). In CommonJS, use dynamic import().
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
Always call `await init()` once before using the API. It is idempotent and safe to call multiple times.
|
|
40
|
+
|
|
41
|
+
### Node.js (ESM)
|
|
42
|
+
|
|
43
|
+
```js
|
|
44
|
+
import init, * as sdk from '@dashevo/wasm-sdk';
|
|
45
|
+
|
|
46
|
+
// Initialize Wasm (Node uses an inlined binary; no assets to load)
|
|
47
|
+
await init();
|
|
48
|
+
|
|
49
|
+
// Crypto helpers
|
|
50
|
+
const { address } = sdk.generate_key_pair('testnet');
|
|
51
|
+
console.log('Address:', address);
|
|
52
|
+
|
|
53
|
+
// Optional: enable logs (silent by default)
|
|
54
|
+
await sdk.WasmSdk.setLogLevel('warn'); // or 'info' | 'debug' | full filter
|
|
55
|
+
|
|
56
|
+
// Platform queries via a client
|
|
57
|
+
let builder = sdk.WasmSdkBuilder.testnetTrusted();
|
|
58
|
+
builder = builder.withSettings(5000, 10000, 3, true);
|
|
59
|
+
const client = await builder.withLogs('warn').build();
|
|
60
|
+
|
|
61
|
+
const DPNS = 'GWRSAVFMjXx8HpQFaNJMqBV7MBgMK4br5UESsB4S31Ec';
|
|
62
|
+
const docs = await client.getDocuments(DPNS, 'domain', null, null, 5, null, null);
|
|
63
|
+
console.log('Docs:', docs.length);
|
|
64
|
+
|
|
65
|
+
client.free();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Browser (bundler, ESM)
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
import init, * as sdk from '@dashevo/wasm-sdk';
|
|
72
|
+
|
|
73
|
+
// Initialize Wasm (browser compiles in a Web Worker; no separate .wasm files)
|
|
74
|
+
await init();
|
|
75
|
+
|
|
76
|
+
const ok = sdk.validate_address('yXXX...', 'testnet');
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Advanced: raw artifacts (separate .wasm)
|
|
80
|
+
|
|
81
|
+
If you prefer to manage the `.wasm` file via your bundler, import the raw entry explicitly:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import init, * as sdk from '@dashevo/wasm-sdk/raw';
|
|
85
|
+
await init(); // wasm-bindgen will resolve and fetch wasm_sdk_bg.wasm
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Configure your bundler accordingly. For webpack 5:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
// webpack config
|
|
92
|
+
module.exports = {
|
|
93
|
+
experiments: { asyncWebAssembly: true },
|
|
94
|
+
module: { rules: [{ test: /\.wasm$/, type: 'asset/resource' }] },
|
|
95
|
+
};
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## How bundling works
|
|
101
|
+
|
|
102
|
+
The publishable build provides two ways to consume the SDK:
|
|
103
|
+
|
|
104
|
+
1) Single file (default): `import '@dashevo/wasm-sdk'`
|
|
105
|
+
- `dist/sdk.js` inlines the Wasm (base64) to avoid asset pipelines and MIME issues.
|
|
106
|
+
- Browser: compiles the inlined bytes in a Web Worker to avoid main-thread stalls and 8MB sync limits; then instantiates on the main thread.
|
|
107
|
+
- Node: uses the inlined bytes with initSync internally; you still call `await init()` for a consistent API.
|
|
108
|
+
- The wrapper imports a sanitized variant of the wasm-bindgen glue so bundlers do not "see" any `new URL('…wasm')` and therefore will not emit a `.wasm` asset.
|
|
109
|
+
|
|
110
|
+
2) Raw artifacts (opt-in): `import '@dashevo/wasm-sdk/raw'`
|
|
111
|
+
- `dist/raw/wasm_sdk.js` (unmodified wasm-bindgen output) plus `dist/raw/wasm_sdk_bg.wasm`.
|
|
112
|
+
- Your bundler must serve the `.wasm` with the correct content type and URL rewriting.
|
|
113
|
+
|
|
114
|
+
Why this design?
|
|
115
|
+
- Eliminate flaky Wasm asset handling in test/dev and simplify consumer setup.
|
|
116
|
+
- Keep an escape hatch for asset-pipeline users.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
## Build (contributors)
|
|
121
|
+
|
|
122
|
+
Prerequisites: Rust toolchain, `wasm-pack`, and (optionally) Binaryen for optimized builds.
|
|
123
|
+
|
|
124
|
+
Commands (run at repo root or inside `packages/wasm-sdk`):
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
# Development build (fast)
|
|
128
|
+
yarn workspace @dashevo/wasm-sdk build
|
|
129
|
+
|
|
130
|
+
# Release build (optimized)
|
|
131
|
+
yarn workspace @dashevo/wasm-sdk build:release
|
|
132
|
+
```
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Test (contributors)
|
|
136
|
+
|
|
137
|
+
Unit tests (Node + browser/Karma):
|
|
138
|
+
|
|
139
|
+
```bash
|
|
140
|
+
yarn workspace @dashevo/wasm-sdk test:unit
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Functional tests (networked; some cases may be skipped if offline):
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
yarn workspace @dashevo/wasm-sdk test:functional
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|
|
151
|
+
## API highlights
|
|
152
|
+
|
|
153
|
+
Examples (after `await init()`):
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
// Addresses & keys
|
|
157
|
+
const { address, private_key_wif } = sdk.generate_key_pair('mainnet');
|
|
158
|
+
sdk.validate_address(address, 'mainnet');
|
|
159
|
+
const addr = sdk.pubkey_to_address('02...pubkeyHex', 'testnet');
|
|
160
|
+
|
|
161
|
+
// Mnemonics & derivation
|
|
162
|
+
const m = sdk.generate_mnemonic(12);
|
|
163
|
+
const r = sdk.derive_key_from_seed_with_path(m, undefined, "m/44'/5'/0'/0/0", 'mainnet');
|
|
164
|
+
|
|
165
|
+
// Network client
|
|
166
|
+
let b = sdk.WasmSdkBuilder.testnetTrusted();
|
|
167
|
+
const client = await b.withSettings(5000, 10000, 3, true).withLogs('info').build();
|
|
168
|
+
const status = await client.getStatus();
|
|
169
|
+
client.free();
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
The full surface includes identity, document, contract, token, group, epoch/system, and proof helpers.
|
|
173
|
+
|
|
174
|
+
### Logging
|
|
175
|
+
|
|
176
|
+
By default, the SDK is silent. You can enable tracing logs globally or via the builder:
|
|
177
|
+
|
|
178
|
+
```js
|
|
179
|
+
// Globally
|
|
180
|
+
await sdk.WasmSdk.setLogLevel('info');
|
|
181
|
+
|
|
182
|
+
// Or on the builder (applies immediately)
|
|
183
|
+
await sdk.WasmSdkBuilder.new_testnet().withLogs('wasm_sdk=debug,rs_dapi_client=warn').build();
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Accepted values are simple levels ('off'|'error'|'warn'|'info'|'debug'|'trace') or a full EnvFilter string.
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## Environment & compatibility
|
|
191
|
+
|
|
192
|
+
- ESM-only package ("type": "module"). Use dynamic import in CJS.
|
|
193
|
+
- Node.js: 16+ recommended (18+ preferred).
|
|
194
|
+
- Browsers: modern engines with WebAssembly + Web Workers.
|
|
195
|
+
|
|
196
|
+
---
|
|
197
|
+
|
|
198
|
+
## Troubleshooting
|
|
199
|
+
|
|
200
|
+
- "expected magic word 00 61 73 6d …" in browsers:
|
|
201
|
+
- Ensure you import the default entry (`@dashevo/wasm-sdk`), not the raw one, unless you have configured Wasm assets.
|
|
202
|
+
- Always `await init()` before calling functions.
|
|
203
|
+
|
|
204
|
+
- Karma/webpack serving errors:
|
|
205
|
+
- Not applicable with the default single-file build. If you opt into `@dashevo/wasm-sdk/raw`, configure a `.wasm` asset rule and correct MIME type.
|
|
206
|
+
|
|
207
|
+
---
|
|
208
|
+
|
|
209
|
+
## Contributing & License
|
|
210
|
+
|
|
211
|
+
This package is part of the Dash Platform monorepo. Follow the repository’s contribution guidelines and do not commit secrets. See the root repository for license details.
|
|
212
|
+
|
|
213
|
+
---
|
|
214
|
+
|
|
215
|
+
## State transitions
|
|
216
|
+
|
|
217
|
+
In addition to read-only queries, the SDK exposes helpers to construct and submit state transitions (requires a networked client and valid inputs):
|
|
218
|
+
|
|
219
|
+
```js
|
|
220
|
+
import init, * as sdk from '@dashevo/wasm-sdk';
|
|
221
|
+
await init();
|
|
222
|
+
|
|
223
|
+
const builder = sdk.WasmSdkBuilder.testnetTrusted();
|
|
224
|
+
const client = await builder.build();
|
|
225
|
+
|
|
226
|
+
// Identity create (example — requires a valid proof and keys)
|
|
227
|
+
const proofJson = JSON.stringify({ /* platform-provided proof object */ });
|
|
228
|
+
const assetLockWif = 'Kx...';
|
|
229
|
+
const pubKeysJson = JSON.stringify([
|
|
230
|
+
{ keyType: 'ECDSA_SECP256K1', purpose: 'AUTHENTICATION', securityLevel: 'MASTER', privateKeyHex: '...' },
|
|
231
|
+
]);
|
|
232
|
+
await sdk.identityCreate(proofJson, assetLockWif, pubKeysJson)
|
|
233
|
+
.then(() => {/* submitted */})
|
|
234
|
+
.catch((e) => {/* handle error */});
|
|
235
|
+
|
|
236
|
+
// Token transfer (negative example if parameters are invalid)
|
|
237
|
+
await sdk.tokenTransfer('contractId', 0, '1000', 'senderIdentityId', 'recipientIdentityId', assetLockWif, null)
|
|
238
|
+
.catch(() => {});
|
|
239
|
+
|
|
240
|
+
client.free();
|
|
241
|
+
```
|