Skip to main content

KnightShieldWallet

The main wallet class. Extends the DPO2U wallet pattern with Midnight Network integration.

Import

import { KnightShieldWallet } from '@knight-shield/core';

Creating a Wallet

From Seed

const wallet = await KnightShieldWallet.fromSeed(seedHex, 'preprod');

Creates a wallet from an existing hex-encoded seed. Derives HD keys for three roles:

  • Zswap — main transaction signing
  • Dust — dust collection
  • NightExternal — external-facing operations

New Wallet

const wallet = await KnightShieldWallet.create('preprod');

Generates a new random seed and creates the wallet. The seed can be retrieved for backup before it is wiped.

Methods

sync(): Promise<void>

Synchronizes wallet state with the Midnight network. Queries the indexer for current balances and transaction history.

await wallet.sync();

getAddress(): string

Returns the wallet's public address.

const address = wallet.getAddress();

getBalances(): Map<string, bigint>

Returns balances for all registered tokens.

const balances = wallet.getBalances();
for (const [token, balance] of balances) {
console.log(`${token}: ${balance}`);
}

getNativeBalance(): bigint

Returns the native token (tDUST) balance.

const balance = wallet.getNativeBalance();

onStateChange(callback): Subscription

Subscribes to wallet state changes. Returns an RxJS subscription.

const sub = wallet.onStateChange((state: WalletState) => {
console.log('New state:', state);
});

// Cleanup
sub.unsubscribe();

createProviderBridge(): ProviderBridge

Creates a provider bridge for contract interactions.

const bridge = wallet.createProviderBridge();

requestFaucet(): Promise<void>

Requests test tokens from the preprod faucet.

await wallet.requestFaucet();

wipeSeed(): void

Securely overwrites the seed in memory with zeros. Called automatically on stop().

wallet.wipeSeed();

stop(): Promise<void>

Stops the wallet, wipes the seed, and cleans up subscriptions.

await wallet.stop();

Network Support

NetworkNodeIndexerFaucet
preprodHTTPSHTTPSYes
previewHTTPSHTTPSYes
standalonelocalhost:9944localhost:8088No

Configuration

The wallet uses the NETWORKS constant for endpoint configuration:

import { NETWORKS } from '@knight-shield/core';

const config = NETWORKS.preprod;
// { node, indexer, proofServer, faucet }