Skip to content

A TypeScript/JavaScript SDK for interacting with the Zenon Network of Momentum (NoM)

License

Notifications You must be signed in to change notification settings

digitalSloth/znn-typescript-sdk

Repository files navigation

ZNN TypeScript SDK

Version Tests Coverage

A TypeScript/JavaScript SDK for interacting with the Zenon Network of Momentum (NoM).

Features

  • 🚀 Modern ESM-first architecture – Optimized for tree-shaking and modern JavaScript
  • 🔌 Dual protocol support – HTTP and WebSocket connections
  • 💼 Wallet management – Create, import, and manage wallets with BIP39 mnemonic support
  • 🔐 Transaction signing – Sign and send transactions with automatic PoW generation
  • ⌨️ CLI Included – CLI for wallet management and sending transactions
  • 📡 Real-time subscriptions – Subscribe to momentums and account blocks via WebSocket
  • 🌐 Universal – Works in Node.js and browsers (ESM & UMD)
  • 📝 TypeScript native – Full type definitions included

Installation

npm install znn-typescript-sdk

Quick Start

Node.js

import { Zenon } from 'znn-typescript-sdk';

const zenon = Zenon.getInstance();
await zenon.initialize('wss://node.zenonhub.io:35998');

Browser

npm create vite@latest my-zenon-app -- --template vanilla
cd my-zenon-app
npm install znn-typescript-sdk
npm run dev
import { Zenon } from 'znn-typescript-sdk';

const zenon = Zenon.getInstance();
await zenon.initialize('wss://node.zenonhub.io:35998');

Browser Builds (ESM vs UMD)

The SDK ships two browser bundles:

  • ESM (dist/browser/bundle.browser.mjs): Modern module build for Vite/Rollup/Webpack. Import from znn-typescript-sdk or the .mjs bundle.
  • UMD (dist/browser/bundle.browser.js): Legacy global build that exposes window.ZnnSDK for script-tag usage.

Use ESM when possible. Use UMD only if you must load the SDK via a <script> tag without a bundler.

Browser PoW Configuration

The Proof of Work (PoW) module requires two external files in browser environments: pow.js and pow.wasm. These files must be accessible at runtime.

Setup:

  1. The PoW files are located in node_modules/znn-typescript-sdk/dist/browser
  2. Set the base path before any operations that require PoW:
import { Zenon } from 'znn-typescript-sdk';

// Point to where pow.js and pow.wasm are located
Zenon.setPowBasePath('node_modules/znn-typescript-sdk/dist/browser');

// Now you can send transactions (which use PoW)
const zenon = Zenon.getInstance();
await zenon.initialize('wss://node.zenonhub.io:35998');
const tx = await zenon.send(blockTemplate, keyPair);

For UMD:

window.ZnnSDK.Zenon.setPowBasePath('node_modules/znn-typescript-sdk/dist/browser');

Alternative – Copy to Public Folder:

For production apps, copy the PoW files to your public/static folder:

cp node_modules/znn-typescript-sdk/dist/browser/pow.* public/

Then set the path:

Zenon.setPowBasePath('/'); // or 'assets' for relative paths

Note: Node.js environments don't need this configuration – PoW files are loaded automatically from the installation directory.

Connection Options

  • HTTP: https://node.zenonhub.io:35997 - For simple API calls
  • WebSocket: wss://node.zenonhub.io:35998 - For real-time subscriptions and transactions

Core Classes

The main entry point is the Zenon singleton.

import { Zenon } from 'znn-typescript-sdk';

const zenon = Zenon.getInstance();

Static Methods

These methods configure SDK-level settings and should be called before initializing the Zenon instance.

Zenon.setNetworkID(networkId: number): void

Set the network ID for transaction signing. Default is 1.

Zenon.setNetworkID(3); // Set to testnet
Zenon.getNetworkID(): number

Get the current network ID.

const networkId = Zenon.getNetworkID();
Zenon.setChainID(chainId: number): void

Set the chain ID for transaction signing. Default is 1.

Zenon.setChainID(100); // Set to custom chain
Zenon.getChainIdentifier(): number

Get the current chain ID.

const chainId = Zenon.getChainIdentifier();
Zenon.setPowBasePath(basePath: string): void

Set the base path for loading PoW files in browser environments. Only needed for browser usage. The path is automatically normalized to meet browser module specifier requirements (adding ./ prefix and / suffix as needed).

// Before initialization in browser
// These are all valid and will be normalized automatically:
Zenon.setPowBasePath('node_modules/znn-typescript-sdk/dist/browser');
Zenon.setPowBasePath('/assets');
Zenon.setPowBasePath('./public');
Zenon.getPowBasePath(): string

Get the current PoW base path.

const path = Zenon.getPowBasePath();

Instance Methods

initialize(url: string, timeout?: number, wsOptions?: WsClientOptions): Promise<void>

Connect to a Zenon node via HTTP or WebSocket.

// WebSocket (for subscriptions and transactions)
await zenon.initialize('wss://node.zenonhub.io:35998');

// HTTP (for simple requests)
await zenon.initialize('https://node.zenonhub.io:35997');

Note: WebSocket connections automatically reconnect if dropped during long-running operations (e.g., PoW generation). The default settings are suitable for most use cases.

clearConnection(): void

Disconnect and clean up resources.

zenon.clearConnection();
send(blockTemplate: AccountBlockTemplate, keyPair: KeyPair): Promise<AccountBlockTemplate>

Sign and send a transaction. Automatically generates PoW if needed.

const tx = await zenon.send(blockTemplate, keyPair);
console.log('Hash:', tx.hash.toString());

Documentation


Development

git clone https://github.com/digitalSloth/znn-typescript-sdk.git
cd znn-typescript-sdk
npm install
npm run build
npm test

Requirements

  • Node.js 18+ (ESM support)
  • Modern browser with WebAssembly support
  • Bundler for browser production apps (Vite, Webpack, etc.)

License

BSD-3-Clause

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

About

A TypeScript/JavaScript SDK for interacting with the Zenon Network of Momentum (NoM)

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors