npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kaspa/wallet

v1.4.26

Published

Node module for kaspa

Downloads

114

Readme

Kaspa Wallet Framework

Kaspa Wallet implements Wallet functionality for the Kaspa Network

Kaspa Wallet is implemented in TypeScript and can be used server-side (NodeJs) and client-side (browser)

PLEASE NOTE: This project is under heavy development

Components

Kaspa Wallet uses the following modules:

Applications built on top of Kaspa Wallet Framework:

PLEASE NOTE: all Kaspa applications and libraries are under heavy development

Kaspa Wallet Framework

Before you can use Kaspa Wallet, you need to initialize the framework. Framework initialization loads various dependencies such as secp256k1-wasm and blake2b-wasm modules use in the underlying transaction cryptography.

const { Wallet, initKaspaFramework } = require('@kaspa/wallet');
const { RPC } = require('@kaspa/grpc-node');

(async () => { 
  await initKaspaFramework();
  ...
})();

Creating a wallet

Network types are identified by address prefixes:

  • kaspa (Mainnet)
  • kaspatest (Testnet)
  • kaspadev (Devnet)
  • kaspasim (Simnet)

Wallet class can be created using two static functions:

static fromMnemonic(
  seedPhrase: string, 
  networkOptions: NetworkOptions, 
  options: WalletOptions = {}): Wallet { }

static async import(
  password: string, 
  encryptedMnemonic: string, 
  networkOptions: NetworkOptions, 
  options: WalletOptions = {}): Promise <Wallet> { }

Wallet creation functions accept following configuration objects:

export interface WalletOptions{
  skipSyncBalance?:boolean;           // do not perform balance sync
  addressDiscoveryExtent?:number;     // address derivation scan (default 64)
  syncOnce?:boolean;                  // 'sync-and-exit' (true) or 'monitoring' mode
  logLevel?:string;                   // wallet log level
  disableAddressDerivation?:boolean;  // disable address derivation and discovery
}

export interface NetworkOptions{
  network:Network;                    // network: kaspa, kaspatest, kaspadev, kaspasim
  rpc?:IRPC;                          // gRPC interface (must be bound to transport before use)
}

Following options are important:

  • addressDiscoveryExtent - the number of HD address derivations to scan forward from the last known used address
  • syncOnce - allows wallet to be started temporarily, without starting monitoring services
  • disableAddressDerivation - starts wallet in a single-address mode, where receive address and change address will always be the first receive address generated from the private key.

Creating from Mnemonic:

const network = "kaspatest";
const { port } = Wallet.networkTypes[kaspatest].port; // default port for testnet
const rpc = new RPC({ clientConfig:{ host : '127.0.0.1:'+port } });

Wallet.fromMnemonic(
    "user mnemonic string",
    { network, rpc },
    {disableAddressDerivation:true}
);

Creating new wallet instance with dynamically generated mnemonic:

const wallet = new Wallet(null, null, {network, rpc});
const encryptedMnemonic = await wallet.export(cmd.password);
console.log('mnemonic:',wallet.mnemonic);
console.log('encrypted mnemonic:',encryptedMnemonic);

Restoring from encrypted mnemonic:

const password = "user password";
const encryptedMnemonic = "previously encrypted mnemonic";
let wallet = await Wallet.import(password, encryptedMnemonic, { network, rpc })

Logging and debugging

Wallet class contains an integrated logger that can be set to one of the following levels: error, warn, info, verbose, debug. The default log level is info. You can set the log level to verbose to see internal wallet data processing activity.

Wallet log level can be supplied as a part of WalletOptions (describe above) or set at runtime as follows:

wallet.setLogLevel('verbose');

Synchronizing a wallet

The function Wallet::sync(once?:boolean) can be used to perform wallet synchronization. Wallet synchronization will connect to kaspad and scan available UTXO entries for wallet addresses, update the wallet balance and if once is true, exit or if once is false, start wallet monitoring services.

When operating with monitoring enabled, wallet will retain connection to kaspad and dynamically update wallet UTXO entries as well as balances.

  • wallet.sync() - starts the wallet in monitoring mode
  • wallet.sync(true) - performs a single-time synchronization

Sending transactions

submitTransaction() function can be used to create transactions on the Kaspa network:

async submitTransaction(txParamsArg: TxSend): Promise < TxResp | null > {
  // ...
}

This function accepts TxSend object on input and returns a Promise<TxResp> object:

export interface TxSend {
  toAddr: string;
  amount: number;
  fee: number;
  changeAddrOverride? : string;
  networkFeeMax?:number;
}
  • toAddr - Destination address
  • amount - Amount of KAS in base units (sompis)
  • fee - Transaction priority fee
  • changeAddrOverride - (optional) Allows you to supply your own address for the change transaction
  • networkFeeMax - (optional) Allows you to set an upper bound for automatic network (data storage) fee calculation. Kaspa Wallet will automatically calculate appropriate fees and add them to the transaction based on the transaction size. This feature is disabled if the property is omitted or set to zero.
export interface TxResp {
  txid: string;
  rpctx?: string; // reserved
}
  • txid - Generated transaction id
try {
  let response = await this.wallet.submitTransaction({
      address, // destination address
      amount,  // amount in base units
      fee,     // user fees
  });
  if(!response)
    console.log('general error');  // if kaspad returns null (should never occur)
  else
    console.log('success:', txid);
} catch(ex) {
  console.log('error:',ex.toString());
}

On failure, submitTransaction() rejects with and error indicating the reason for failure.

Wallet balance

Wallet retains 2 types of balances:

  • available - balance contains KAS ready to be spent, comprised of UTXO records with block maturity blue score over 10.
  • pending - balance contains newly received transactions with UTXO block maturity less than 10. Upon each UTXO maturity balance is relocated from pending to available.

Wallet::balance is an object containing the following properties that are updated during wallet operation:

wallet.balance = {
  available: 5150000000000,
  pending: 247500000000,
  total: 5397500000000
}

Wallet events

Wallet::on(subject, (data) => { ... }) allows for event handler registration. Similarly to NodeJs EventEmitter you can unregister events by supplying original callback to Wallet::removeEventListener(subject, handler) as follows:

const balanceHandler = (balance)=>{ console.log(balance); }
wallet.on('balance-update', balanceHandler);
wallet.removeEventListener('balance-update', balanceHandler);

Following events are emitted by the Wallet class:

  • api-online - gPRC API is online
  • api-offline - gRPC API is offline
  • sync-start - wallet sync started (occurs each time gRPC API connects or re-connects)
  • sync-finish - wallet sync finished
  • ready - wallet is ready for use (sent after sync-finish, event data contains the balance object)
  • blue-score-changed - indicates Kaspa blue score change (new block generation)
  • utxo-change - signaled when UTXO is added or removed from the wallet UTXO set
  • balance-update - indicates wallet balance change (event data contains the balance object)