Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc doc updates #407

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wallet/core/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use workflow_core::abortable::Abortable;
/// Notification callback type used by [`Account::sweep`] and [`Account::send`].
/// Allows tracking in-flight transactions during transaction generation.
pub type GenerationNotifier = Arc<dyn Fn(&PendingTransaction) + Send + Sync>;
/// Scan notification callback type used by [`Account::derivation_scan`].
/// Scan notification callback type used by [`DerivationCapableAccount::derivation_scan`].
/// Provides derivation discovery scan progress information.
pub type ScanNotifier = Arc<dyn Fn(usize, usize, u64, Option<TransactionId>) + Send + Sync>;

Expand Down
7 changes: 4 additions & 3 deletions wallet/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
//! - <https://www.npmjs.com/package/kaspa-wasm>
//!
//! The `kaspa-wasm` module is a pure WASM32 module that includes
//! the entire wallet framework, but does not support RPC, while
//! the `kaspa` module also includes `isomorphic-ws` simulating
//! the W3C WebSocket available natively in browsers and supports RPC.
//! the entire wallet framework, but does not support RPC due to an absence
//! of a native WebSocket in NodeJs environment, while
//! the `kaspa` module includes `isomorphic-ws` dependency simulating
//! the W3C WebSocket and thus supports RPC.
//!
//! JavaScript examples for using this framework can be found at:
//! <https://github.com/kaspanet/rusty-kaspa/tree/master/wasm/nodejs>
Expand Down
57 changes: 40 additions & 17 deletions wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ codebase within JavaScript environments such as Node.js and Web Browsers.

## Documentation

- [**integrating with Kaspa** guide](https://kaspa-mdbook.aspectron.com/)
- [**integrating with Kaspa** guide](https://kaspa.aspectron.org/)
- [**Rustdoc** documentation](https://docs.rs/kaspa-wasm/latest/kaspa-wasm)
- [**JSDoc** documentation](https://aspectron.com/docs/kaspa-wasm/)
- [**JSDoc** documentation](https://kaspa.aspectron.org/jsdoc/)

Please note that while WASM directly binds JacaScript and Rust resources, their names on JavaScript side
Please note that while WASM directly binds JavaScript and Rust resources, their names on JavaScript side
are different from their name in Rust as they conform to the 'camelCase' convention in JavaScript and
to the 'snake_case' convention in Rust.

Expand All @@ -24,16 +24,9 @@ to the 'snake_case' convention in Rust.
The APIs are currently separated into the following groups:

- **Transaction API** — Bindings for primitives related to transactions.
This includes basic primitives related to consensus transactions, as well as
`MutableTransaction` and `VirtualTransaction` primitives usable for
transaction creation.

- **RPC API** — [RPC interface bindings](https://docs.rs/kaspa-wasm/latest/kaspa-wasm/rpc) for the Kaspa node using WebSocket (wRPC) connections.
- **Wallet API** — API for async core wallet processing tasks.

- **RPC API** — [RPC interface bindings](https://docs.rs/kaspa-wasm/latest/kaspa-wasm/rpc) for the Kaspa node using WebSocket connections.
Compatible with Rusty Kaspa as well as with the Golang node (kaspad) via the `kaspa-wrpc-proxy`
WebSocket / gRPC proxy (located in `rpc/wrpc/proxy`).

## Using RPC

There are multiple ways to use RPC:
Expand All @@ -46,13 +39,43 @@ WebSocket implementation. Two of such modules are [WebSocket](https://www.npmjs.
(provides a custom implementation) and [isomorphic-ws](https://www.npmjs.com/package/isomorphic-ws)
(built on top of the ws WebSocket module).

You can use the following shims:

```js
// `websocket` module
## Loading in a Web App

```html
<html>
<head>
<script type="module">
import * as kaspa_wasm from './kaspa/kaspa-wasm.js';
(async () => {
const kaspa = await kaspa_wasm.default('./kaspa/kaspa-wasm_bg.wasm');
})();
</script>
</head>
<body></body>
</html>
```

## Loading in a Node.js App

```javascript
// W3C WebSocket module shim
globalThis.WebSocket = require('websocket').w3cwebsocket;
// `isomorphic-ws` module
globalThis.WebSocket = require('isomorphic-ws');

let {RpcClient,Encoding,init_console_panic_hook,defer} = require('./kaspa-rpc');
// init_console_panic_hook();
aspect marked this conversation as resolved.
Show resolved Hide resolved

let rpc = new RpcClient(Encoding.Borsh,"ws://127.0.0.1:17110");

(async () => {
await rpc.connect();

let info = await rpc.getInfo();
console.log(info);

await rpc.disconnect();
aspect marked this conversation as resolved.
Show resolved Hide resolved
})();
```

For more details, please follow the [**integrating with Kaspa**](https://kaspa-mdbook.aspectron.com/) guide.

For more details, please follow the [**integrating with Kaspa**](https://kaspa.aspectron.org/) guide.
51 changes: 35 additions & 16 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ codebase within JavaScript environments such as Node.js and Web Browsers.

## Documentation

- [**integrating with Kaspa** guide](https://kaspa-mdbook.aspectron.com/)
- [**integrating with Kaspa** guide](https://kaspa.aspectron.org/)
- [**Rustdoc** documentation](https://docs.rs/kaspa-wasm/latest/kaspa-wasm)
- [**JSDoc** documentation](https://aspectron.com/docs/kaspa-wasm/)
- [**JSDoc** documentation](https://kaspa.aspectron.org/jsdoc/)

Please note that while WASM directly binds JacaScript and Rust resources, their names on JavaScript side
Please note that while WASM directly binds JavaScript and Rust resources, their names on JavaScript side
are different from their name in Rust as they conform to the 'camelCase' convention in JavaScript and
to the 'snake_case' convention in Rust.

Expand All @@ -26,23 +26,42 @@ to the 'snake_case' convention in Rust.
The APIs are currently separated into the following groups (this will be expanded in the future):

- **Transaction API** — Bindings for primitives related to transactions.
This includes basic primitives related to consensus transactions, as well as
`MutableTransaction` and `VirtualTransaction` primitives usable for
transaction creation.

- **RPC API** — [RPC interface bindings](rpc) for the Kaspa node using WebSocket (wRPC) connections.
- **Wallet API** — API for async core wallet processing tasks.

- **RPC API** — [RPC interface bindings](rpc) for the Kaspa node using WebSocket connections.
Compatible with Rusty Kaspa as well as with the Golang node (kaspad) via the `kaspa-wrpc-proxy`
WebSocket / gRPC proxy (located in `rpc/wrpc/proxy`).
## NPM Modules

For JavaScript / TypeScript environments, there are two
available NPM modules:

- <https://www.npmjs.com/package/kaspa>
- <https://www.npmjs.com/package/kaspa-wasm>

The `kaspa-wasm` module is a pure WASM32 module that includes
the entire wallet framework, but does not support RPC due to an absence
of a native WebSocket in NodeJs environment, while
the `kaspa` module includes `isomorphic-ws` dependency simulating
the W3C WebSocket and thus supports RPC.

## Examples

JavaScript examples for using this framework can be found at:
<https://github.com/kaspanet/rusty-kaspa/tree/master/wasm/nodejs>

## WASM32 Binaries

For pre-built browser-compatible WASM32 redistributables of this
framework please see the releases section of the Rusty Kaspa
repository at <https://github.com/kaspanet/rusty-kaspa/releases>.

## Using RPC

**NODEJS:** To use WASM RPC client in the Node.js environment, you need to introduce a W3C WebSocket object
before loading the WASM32 library. You can use any Node.js module that exposes a W3C-compatible
WebSocket implementation. Two of such modules are [WebSocket](https://www.npmjs.com/package/websocket)
(provides a custom implementation) and [isomorphic-ws](https://www.npmjs.com/package/isomorphic-ws)
(built on top of the ws WebSocket module).
**NODEJS:** If you are building from source, to use WASM RPC client in the NodeJS environment,
you need to introduce a W3C WebSocket object before loading the WASM32 library. You can use
any Node.js module that exposes a W3C-compatible WebSocket implementation. Two of such modules
are [WebSocket](https://www.npmjs.com/package/websocket) (provides a custom implementation)
and [isomorphic-ws](https://www.npmjs.com/package/isomorphic-ws) (built on top of the ws
WebSocket module).

You can use the following shims:

Expand Down Expand Up @@ -90,7 +109,7 @@ let rpc = new RpcClient(Encoding.Borsh,"ws://127.0.0.1:17110");
})();
```

For more details, please follow the [**integrating with Kaspa**](https://kaspa-mdbook.aspectron.com/) guide.
For more details, please follow the [**integrating with Kaspa**](https://kaspa.aspectron.org/) guide.

*/

Expand Down
2 changes: 1 addition & 1 deletion wasm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use wasm_bindgen::prelude::*;
// https://github.com/tmrlvi/kaspa-miner/blob/bf361d02a46c580f55f46b5dfa773477634a5753/src/client/stratum.rs#L36
const DIFFICULTY_1_TARGET: (u64, i16) = (0xffffu64, 208); // 0xffff 2^208

/// `calculate_difficulty` is based on set_difficulty function: https://github.com/tmrlvi/kaspa-miner/blob/bf361d02a46c580f55f46b5dfa773477634a5753/src/client/stratum.rs#L375
/// `calculate_difficulty` is based on set_difficulty function: <https://github.com/tmrlvi/kaspa-miner/blob/bf361d02a46c580f55f46b5dfa773477634a5753/src/client/stratum.rs#L375>
#[wasm_bindgen(js_name = calculateDifficulty)]
pub fn calculate_difficulty(difficulty: f32) -> Result<BigInt, JsError> {
let mut buf = [0u64, 0u64, 0u64, 0u64];
Expand Down