Crate kaspa_wasm

Source
Expand description

§Rusty Kaspa WASM32 bindings

github crates.io docs.rs license


Rusty-Kaspa WASM32 bindings offer direct integration of Rust code and Rusty-Kaspa codebase within JavaScript environments such as Node.js and Web Browsers.

§Documentation

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.

§Interfaces

The APIs are currently separated into the following groups (this will be expanded in the future):

  • Consensus Client API — Bindings for primitives related to transactions.
  • RPC APIRPC interface bindings for the Kaspa node using WebSocket (wRPC) connections.
  • Wallet SDK — API for async core wallet processing tasks.
  • Wallet API — A rust implementation of the fully-featured wallet usable in the native Rust, Browser or NodeJs and Bun environments.

§NPM Modules

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

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 websocket package dependency simulating the W3C WebSocket and due to this supports RPC.

NOTE: for security reasons it is always recommended to build WASM SDK from source or download pre-built redistributables from releases or development builds.

§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.

§Development Builds

The latest development builds from https://kaspa.aspectron.org/nightly/downloads/. Development builds typically contain fixes and improvements that are not yet available in stable releases. Additional information can be found at https://aspectron.org/en/projects/kaspa-wasm.html.

§Using RPC

No special handling is required to use the RPC client in Browser or Bun environments due to the fact that these environments provide native WebSocket support.

NODEJS: If you are building from source, to use WASM RPC client in the NodeJS environment, you need to introduce a global W3C WebSocket object before loading the WASM32 library (to simulate the browser behavior). You can the WebSocket module that offers W3C WebSocket compatibility and is compatible with Kaspa RPC implementation.

You can use the following shims:

// WebSocket
globalThis.WebSocket = require('websocket').w3cwebsocket;

§Loading in a Web App

<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

// W3C WebSocket module shim
// this is provided by NPM `kaspa` module and is only needed
// if you are building WASM libraries for NodeJS from source
// globalThis.WebSocket = require('websocket').w3cwebsocket;

let {RpcClient,Encoding,initConsolePanicHook} = require('./kaspa-rpc');

// enabling console panic hooks allows WASM to print panic details to console
// initConsolePanicHook();
// enabling browser panic hooks will create a full-page DIV with panic details
// this is useful for mobile devices where console is not available
// initBrowserPanicHook();

// if port is not specified, it will use the default port for the specified network
const rpc = new RpcClient("127.0.0.1", Encoding.Borsh, "testnet-10");
const rpc = new RpcClient({
    url : "127.0.0.1",
    encoding : Encoding.Borsh,
    networkId : "testnet-10"
});


(async () => {
    try {
        await rpc.connect();
        let info = await rpc.getInfo();
        console.log(info);
    } finally {
        await rpc.disconnect();
    }
})();

For more details, please follow the Integrating with Kaspa guide.

Functions§

version
Returns the version of the Rusty Kaspa framework. @category General