@kronsdk/kron-sdk 0.1.1

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.
@@ -0,0 +1,206 @@
1
+
2
+ ## WASM32 bindings for Rusty Kaspa SDK
3
+
4
+ [<img alt="github" src="https://img.shields.io/badge/github-kaspanet/rusty--kaspa-8da0cb?style=for-the-badge&labelColor=555555&color=8da0cb&logo=github" height="20">](https://github.com/kaspanet/rusty-kaspa/tree/master/wasm)
5
+ [<img alt="crates.io" src="https://img.shields.io/crates/v/kaspa-wasm.svg?maxAge=2592000&style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/kaspa-wasm)
6
+ [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-kaspa--wasm-56c2a5?maxAge=2592000&style=for-the-badge&logo=docs.rs" height="20">](https://docs.rs/kaspa-wasm)
7
+ <img alt="license" src="https://img.shields.io/crates/l/kaspa-wasm.svg?maxAge=2592000&color=6ac&style=for-the-badge&logoColor=fff" height="20">
8
+
9
+ Rusty-Kaspa WASM32 bindings offer direct integration of Rust code and Rusty-Kaspa
10
+ codebase within JavaScript and TypeScript environments such as Node.js and Web Browsers.
11
+
12
+ ## Documentation
13
+
14
+ - [**integrating with Kaspa** guide](https://kaspa.aspectron.org/)
15
+ - [**Rust** documentation](https://docs.rs/kaspa-wasm/latest/kaspa_wasm/index.html)
16
+ - [**TypeScript** documentation](https://kaspa.aspectron.org/docs/)
17
+
18
+ Please note that while WASM directly binds JavaScript and Rust resources, their names on JavaScript side
19
+ are different from their name in Rust as they conform to the 'camelCase' convention in JavaScript and
20
+ to the 'snake_case' convention in Rust.
21
+
22
+ The WASM32 bindings can be used in both TypeScript and JavaScript environments, where in JavaScript
23
+ types will not be constrained by TypeScript type definitions.
24
+
25
+ ## Interfaces
26
+
27
+ The SDK is currently separated into the following top-level categories:
28
+
29
+ - **RPC API** — RPC API for the Kaspa node using WebSockets.
30
+ - **Wallet SDK** — Bindings for primitives related to key management and transactions.
31
+ - **Wallet API** — API for the Rusty Kaspa Wallet framework.
32
+
33
+ ## WASM32 SDK release packages
34
+
35
+ The SDK is built as 4 packages for Web Browsers as follows:
36
+ - KeyGen - Key & Address Generation only
37
+ - RPC - RPC only
38
+ - Core - RPC + Key & Address Generation + Wallet SDK
39
+ - Full - Full SDK + Integrated Wallet
40
+ For NodeJS, the SDK is built as a single package containing all features.
41
+
42
+ ## SDK folder structure
43
+
44
+ The following is a brief overview of the SDK folder structure (as available in the release):
45
+
46
+ - `web/kaspa` - **full** Rusty Kaspa WASM32 SDK bindings for use in web browsers.
47
+ - `web/kaspa-rpc` - only the RPC bindings for use in web browsers (reduced WASM binary size).
48
+ - `nodejs/kaspa` - **full** Rusty Kaspa WASM32 SDK bindings for use with NodeJS.
49
+ - `docs` - Rusty Kaspa WASM32 SDK documentation.
50
+ - `examples` folders contain examples for NodeJS and web browsers.
51
+ - `examples/data` - folder used by examples for configuration and wallet data storage.
52
+ - `examples/javascript` - JavaScript examples.
53
+ - `examples/javascript/general` - General SDK examples (keys & derivation, addresses, encryption, etc.).
54
+ - `examples/javascript/transactions` - Creating, sending and receiving transactions.
55
+ - `examples/javascript/wallet` - Interfacing with the Rusty Kaspa Wallet framework.
56
+ - `examples/typescript` - TypeScript examples.
57
+
58
+ If you are using JavaScript and Visual Studio Code, it is highly recommended you replicate
59
+ the `jsconfig.json` configuration file as is done in the SDK examples. This file allows
60
+ Visual Studio to provide TypeScript-like code completion, type checking and documentation.
61
+
62
+ Included documentation in the release can be accessed by loading the `docs/kaspa/index.html`
63
+ file in a web browser.
64
+
65
+ ## Building from Source
66
+
67
+ To build the WASM32 SDK from source, you need to have the Rust environment installed. To do that,
68
+ follow instructions in the [Rusty Kaspa README](https://github.com/kaspanet/rusty-kaspa).
69
+
70
+ Once you have Rust installed, you can build the WASM32 SDK as follows:
71
+
72
+ - `./build-release` - build the release version of the WASM32 SDK + Docs. The release version also contains `debug` builds of the libraries.
73
+ - `./build-web` - build the web package (ES6 module)
74
+ - `./build-node` - build the NodeJS package (CommonJS module)
75
+ - `./build-docs` - runs `build-web` and then generates TypeDoc documentation from the resulting build.
76
+
77
+ Please note that to build from source, you need to have TypeDoc installed globally via `npm install -g typedoc` (see below).
78
+
79
+ ## Running Web examples
80
+
81
+ **IMPORTANT:** To view web examples, you need to serve them from a local web server and
82
+ serve them from the root of the SDK folder (`kaspa-wasm32-sdk` if using a redistributable or
83
+ `rusty-kaspa/wasm` if building from source). This is because examples use relative paths.
84
+ WASM32 currently can not be loaded using the `file://` protocol.
85
+
86
+ You can use any web server of your choice. If you don't have one, you can run one as follows:
87
+ ```bash
88
+ cargo install http-server
89
+ http-server
90
+ ```
91
+ Access the examples at [http://localhost:7878/examples/web/index.html](http://localhost:7878/examples/web/index.html).
92
+ (Make sure to change the port if you are using a different server. Many servers will serve on
93
+ [http://localhost:8000/examples/web/index.html](http://localhost:8000/examples/web/index.html) by default)
94
+
95
+ If building from source, you must run `build-release` or `build-web` scripts before running the examples.
96
+
97
+ ## Running NodeJs examples
98
+
99
+ This applies to running examples while building the project from source as some dependencies are instantiated as a part of the build process. You just need to run `node init` to initialize a local config.
100
+
101
+ NOTES:
102
+ - `npm install` will install NodeJs types for TypeScript and W3C websocket modules
103
+ - `npm install -g typedoc` is needed for the release build to generate documentation
104
+ - `node init` creates a local `examples/data/config.json` that contains a private key (mnemonic) use across NodeJS examples. You can override address used in some examples by specifying the address as a command line argument.
105
+ - Majority of examples will accept following arguments: `node <script> [address] [mainnet|testnet-10|testnet-11] [--address <address>] [--network <mainnet|testnet-10|testnet-11>] [--encoding <borsh|json>]`.
106
+
107
+ By default all wRPC connections use Borsh binary encoding.
108
+
109
+ Example:
110
+ ```bash
111
+ cd wasm
112
+ ./build-release
113
+ cd examples
114
+ npm install
115
+ node init
116
+ node nodejs/javascript/general/rpc.js
117
+ ```
118
+
119
+ ## Using RPC
120
+
121
+ There are multiple ways to use RPC:
122
+ - Control over WebSocket-framed JSON-RPC protocol (you have to manually handle serialization)
123
+ - Use `RpcClient` class that handles the connectivity automatically and provides RPC interfaces in a form of async function calls.
124
+
125
+ **NODEJS:** To use WASM RPC client in the Node.js environment, you need to introduce a W3C WebSocket object
126
+ before loading the WASM32 library. The compatible WebSocket library is [WebSocket](https://www.npmjs.com/package/websocket) and is included in the `kaspa` NPM package. `kaspa` package is a wrapper around `kaspa-wasm` that imports and installs this WebSocket shim in the `globalThis` object and then re-exports `kaspa-wasm` exports.
127
+
128
+
129
+ ## Loading in a Web App
130
+
131
+ ```html
132
+ <html>
133
+ <head>
134
+ <script type="module">
135
+ import * as kaspa from './kaspa/kaspa-wasm.js';
136
+ (async () => {
137
+ await kaspa.default('./kaspa/kaspa-wasm_bg.wasm');
138
+ console.log(kaspa.version());
139
+ // ...
140
+ })();
141
+ </script>
142
+ </head>
143
+ <body></body>
144
+ </html>
145
+ ```
146
+
147
+ ## Loading in a Node.js App
148
+
149
+ ```javascript
150
+ //
151
+ // W3C WebSocket module shim
152
+ // this is provided by NPM `kaspa` module and is only needed
153
+ // if you are building WASM libraries for NodeJS from source
154
+ //
155
+ // @ts-ignore
156
+ // globalThis.WebSocket = require('websocket').w3cwebsocket;
157
+ //
158
+
159
+ let {
160
+ RpcClient,
161
+ Encoding,
162
+ initConsolePanicHook
163
+ } = require('./kaspa');
164
+
165
+ // enabling console panic hooks allows WASM to print panic details to console
166
+ // initConsolePanicHook();
167
+ // enabling browser panic hooks will create a full-page DIV with panic details
168
+ // this is useful for mobile devices where console is not available
169
+ // initBrowserPanicHook();
170
+ ```
171
+
172
+ ```javascript
173
+ // if port is not specified, it will use the default port for the specified network
174
+ const rpc = new RpcClient({
175
+ url: "127.0.0.1",
176
+ encoding: Encoding.Borsh,
177
+ network : "testnet-10"
178
+ });
179
+
180
+ (async () => {
181
+ try {
182
+ await rpc.connect();
183
+ let info = await rpc.getInfo();
184
+ console.log(info);
185
+ } finally {
186
+ await rpc.disconnect();
187
+ }
188
+ })();
189
+ ```
190
+
191
+ For more details, please follow the [**integrating with Kaspa**](https://kaspa.aspectron.org/) guide.
192
+
193
+ ## Creating Documentation
194
+
195
+ Please note that to build documentation from source you need to have the Rust environment installed.
196
+ The build script will first build the WASM32 SDK and then generate typedoc documentation from it.
197
+
198
+ You can build documentation from source as follows:
199
+
200
+ ```bash
201
+ npm install -g typedoc
202
+ ./build-docs
203
+ ```
204
+
205
+ The resulting documentation will be located in `docs/typedoc/`
206
+