XChain Platform SDK

What is xchain-sdk

xchain-sdk is the developer-facing Software Development Kit for the XChain Platform. It lets you generate XChain ACTION commands (SEND, ISSUE, MINT, ORDER, and more), encode them into unsigned Bitcoin, Dogecoin, or Litecoin transactions (as PSBTs), and query live blockchain data — balances, token information, transaction history, market orders — from the XChain Explorer API. The SDK runs both as a Node.js library you import directly into your own code and as a standalone JSON-RPC microservice.

Features

  • Generate all 27 XChain ACTION command strings (SEND, ISSUE, MINT, DESTROY, ORDER, DISPENSER, DIVIDEND, SWEEP, SWAP, CALLBACK, SLEEP, AIRDROP, MESSAGE, LIST, LINK, FILE, BROADCAST, ADDRESS, BATCH, DEPLOY, EXECUTE, DEPOSIT, WITHDRAW, COINPAY, STAKE, UNSTAKE, DELEGATE, COLLECT)
  • Transaction Lifecycle Manager (submitAction): full encode → sign → broadcast → wait pipeline in a single call, with automatic P2SH two-phase handling and progress callbacks
  • Wallet Sessions (sdk.session(wif)): bound wallet object that bundles address/key/UTXO state with action convenience methods — “I am this address, do things”
  • Fee Estimation (estimateFees): dry-run fee calculation via encoder, returns fee in satoshis plus reusable PSBT
  • UTXO-aware transaction chaining: in-memory UTXO cache with speculative change outputs prevents double-spend on rapid sequential transactions
  • Workflow Recipes: multi-step helpers — issueAndDistribute, issueAndMint, stakeAndDelegate, deployAndFund, createDispenser, createOrder, cancelOrder, distributeDividend
  • Cross-Chain Helper: coordinate actions across BTC, LTC, DOGE SDK instances — createSwap, link, parallel, waitForAll, getAllBalances
  • Event-driven confirmation (waitForAction): WebSocket + polling hybrid that resolves when the indexer processes a transaction
  • Interactive REPL (npm run repl): live session with pre-configured SDK, custom .actions, .status, .fields commands
  • Staking actions: capability staking (STAKE v1/v2, UNSTAKE v0, DELEGATE v0/v2, COLLECT) is BTC-only for the platform validator set; contract staking (STAKE v3, UNSTAKE v1, DELEGATE v1/v3) works on any chain as a developer primitive
  • Smart contract support: deploy contracts, execute methods, deposit/withdraw tokens via the xchain-vm integration
  • Contract authoring utilities: syntax validation, float detection, hex encoding, gas estimation
  • Bound ContractClient for repeated interactions with a specific deployed contract
  • Encode actions into unsigned PSBTs via the xchain-encoder service
  • Support for all encoding formats: OP_RETURN, P2SH, P2WSH, multisign
  • Fluent BatchBuilder for constructing multi-action BATCH transactions
  • Full Explorer API client: balances, tokens, transactions, history, markets, orderbook, contracts
  • Automatic retry with exponential backoff and jitter (respects Retry-After headers)
  • Connection pooling via configurable HTTP keep-alive agent
  • Hub-based service discovery: auto-resolve explorer and encoder endpoints from xchain-hub
  • Live config polling: re-resolve endpoints when hub config changes
  • Real-time event streaming via WebSocket: subscribe to blocks, addresses, tokens, markets, dispensers with type/status filters, catch-up on reconnect, snapshot-on-subscribe
  • Convenience methods for common real-time patterns: onBlock(), onAddress(), onCoinpayRequired(), onOrderMatch(), onToken(), onMarket(), onDispenser()
  • Request/response/error/retry lifecycle hooks for logging and instrumentation
  • JSON-RPC microservice mode (server)
  • Browser bundle (minified and development builds)
  • Wallet key management: generate key pairs, import WIF keys, derive P2PKH/P2WPKH/P2SH-P2WPKH addresses
  • Address validation against all supported networks (Bitcoin, Litecoin, Dogecoin)
  • PSBT signing: sign unsigned PSBTs from the encoder with a WIF private key, finalize, and extract raw transaction hex
  • Transaction broadcasting: broadcast signed transactions to the coin node via the encoder
  • UTXO queries: fetch address UTXOs via the encoder (proxied from xchain-utxo-tracker)
  • Challenge-response wallet ownership verification: generate challenges, sign messages, verify signatures
  • Custom message signing: signMessage and verifyMessage work with any arbitrary string — no SDK lock-in on the verification side
  • Encrypted messaging: ECIES (default, multi-device), ECDH (session-based), and AES (pre-shared key) encryption for MESSAGE actions
  • Public key resolution: automatic lookup of recipient public keys from on-chain transaction data
  • High-level messaging API: sendMessage() handles pubkey lookup, encryption, PSBT signing, and broadcasting in one call
  • TypeScript type definitions included (index.d.ts)
  • Supports Bitcoin, Dogecoin, and Litecoin on mainnet, testnet, and regtest

Documentation

Document Description
Configuration Constructor options, environment variables, hub discovery, retry and pooling config
Actions All 29 supported ACTION types, parameters, and version formats
Transaction Lifecycle submitAction, fee estimation, UTXO chaining, P2SH two-phase handling
Wallet Sessions Bound wallet sessions, convenience methods, UTXO cache
Workflows High-level recipes: issueAndDistribute, deployAndFund, stakeAndDelegate
Cross-Chain Multi-chain coordination: parallel actions, swaps, links
Explorer Explorer API client methods: balances, tokens, transactions, markets
Encoder Encoding actions into PSBTs, encoding formats, P2SH two-phase flow
Batch Builder Fluent API for constructing multi-action BATCH transactions
Format Selection How the SDK picks the optimal format version
Contracts VM smart contract integration: deploy, execute, deposit, withdraw, ContractClient
WebSocket Real-time event client: connection, convenience methods, filters, reconnection, hooks
Wallet & Auth Key management, address validation, PSBT signing, message signing, challenge-response verification
Messaging ECIES/ECDH/AES encryption, public key lookup, high-level send/receive for MESSAGE actions
Errors Error types, codes, and handling patterns
Examples Complete worked examples for common use cases

Installation

Clone the repository and install dependencies from within the xchain-sdk directory:

git clone https://github.com/XChain-Platform/xchain-sdk.git
cd xchain-sdk
npm install

Quick Start

Library mode — generate an action and query balances

const XChainSDK = require('./index.js');

const sdk = new XChainSDK({
    network:     'bitcoin-mainnet',
    explorerUrl: 'explorer.example.com',
    explorerPort: 8080
});

// Build a SEND action string
const result = await sdk.send({
    tick:        'MYTOKEN',
    amount:      '100',
    destination: 'bc1qrecipientaddress'
});
console.log(result.actionString);
// → "SEND|0|MYTOKEN|100|bc1qrecipientaddress"

// Query token balances for an address
const balances = await sdk.getBalances('bc1qmyaddress');
console.log(balances);

Library mode — generate a PSBT via the encoder

const XChainSDK = require('./index.js');

const sdk = new XChainSDK({
    network:      'bitcoin-mainnet',
    explorerUrl:  'explorer.example.com',
    explorerPort: 8080,
    encoderUrl:   'encoder.example.com',
    encoderPort:  3000
});

// Build a SEND action and encode it into a PSBT in one call
const result = await sdk.send(
    {
        tick:        'MYTOKEN',
        amount:      '100',
        destination: 'bc1qrecipientaddress'
    },
    {
        pubkey: '02abc123...',       // sender's compressed public key
        change: 'bc1qmyaddress',    // change address
        utxos:  [                   // UTXOs to fund the transaction
            {
                txid:  'aabbcc...',
                vout:  0,
                value: 100000
            }
        ],
        encoding: 'opreturn'        // encoding format (default: opreturn)
    }
);

console.log(result.actionString);   // the raw action string
console.log(result.psbt);          // base64-encoded unsigned PSBT
console.log(result.encoding);      // encoding format used

Library mode — hub discovery

When using xchain-hub for service discovery, call init() after construction. The SDK fetches endpoint config from the hub and automatically re-resolves clients if config changes.

const XChainSDK = require('./index.js');

const sdk = new XChainSDK({
    network: 'bitcoin-mainnet',
    hubUrl:  'hub.example.com',
    hubPort: 8001
});

// init() fetches config from hub and starts polling for updates
await sdk.init();

// Explorer and encoder are now configured automatically
const balances = await sdk.getBalances('bc1qmyaddress');

Server mode

Run the SDK as a JSON-RPC microservice:

npm run api

The server listens on SDK_API_PORT (default 3100) and exposes all SDK methods as JSON-RPC 2.0 endpoints over HTTP POST.

Usage Modes

Library import (Node.js)

Require the SDK directly in your application. All action and explorer methods are available as async functions on the SDK instance. Suitable for backend services, scripts, and testing.

const XChainSDK = require('xchain-sdk');
const sdk = new XChainSDK({ network: 'dogecoin-mainnet', explorerUrl: 'localhost' });

JSON-RPC microservice

Run npm run api to start a standalone HTTP server. Other services can call SDK methods via JSON-RPC 2.0 POST requests without importing the library directly. Suitable for polyglot environments or when you want to isolate the SDK as a service.

Browser bundle

Build a minified browser bundle for use in web applications:

npm run build       # production bundle → dist/xchain_sdk.min.js
npm run build:dev   # development bundle → dist/xchain_sdk.js

Load the bundle in a <script> tag; the SDK is exposed as the global XChainSDK.

<script src="dist/xchain_sdk.min.js"></script>
<script>
    const sdk = new XChainSDK({ network: 'bitcoin-mainnet', explorerUrl: 'localhost' });
</script>

Scripts

Command Description
npm run api Start the SDK as a JSON-RPC microservice
npm test Run the Mocha test suite
npm run repl Start interactive REPL with a pre-configured SDK instance
npm run build Build a production minified browser bundle to dist/xchain_sdk.min.js
npm run build:dev Build a development (unminified) browser bundle to dist/xchain_sdk.js

Dependencies

Runtime

Package Purpose
axios HTTP client for Explorer, Encoder, and Hub requests
bitcoinjs-lib Bitcoin transaction parsing, PSBT signing, address derivation and validation
bitcoinjs-message Bitcoin message signing and verification (challenge-response auth)
ecpair EC key pair creation and WIF import/export
@bitcoinerlab/secp256k1 Pure-JS secp256k1 elliptic curve operations (no WASM)
express HTTP server for JSON-RPC microservice mode
express-json-rpc-router JSON-RPC 2.0 routing for the API server
helmet HTTP security headers for the API server
cors Cross-origin resource sharing for the API server
mathjs Arbitrary-precision big-number arithmetic for token amounts
dotenv .env file loading for environment-based configuration

Development

Package Purpose
mocha Test runner
chai Assertion library
nock HTTP request mocking for tests
sinon Spies, stubs, and mocks for tests
browserify Bundles Node.js code for browser use
babelify / @babel/core / @babel/preset-env Transpiles modern JS for browser compatibility
uglify-js Minifies the production browser bundle

Copyright © 2025–2026 Dankest, LLC

Based on XChain Platform by Dankest, LLC – https://dankest.llc

Licensed under the GNU Affero General Public License v3.0 (AGPL-3.0-or-later) with a commercial license available for proprietary use.

You may use, modify, and distribute this material under the terms of the License. See LICENSE and NOTICE for full terms. See the licensing overview.

Edit this page on GitHub ↗