Electrum Client

0.0.6 · abandoned · verified Tue Apr 21

The package `electrum-client` (npm: `electrum-client`, GitHub: `you21979/node-electrum-client`) provides a Node.js client for interacting with ElectrumX servers using the Electrum protocol. It supports core functionalities like TCP/TLS connections, JSON-RPC method calls for querying blockchain data (e.g., server version), and EventEmitter for handling real-time subscription messages (e.g., blockchain headers). As of its latest version, 0.0.6, the package has not seen updates since its last publish date approximately eight years ago (February 2018), indicating an abandoned state. A key differentiator initially was its explicit lack of external dependencies. However, due to its age, developers should be aware that more actively maintained and feature-rich alternatives exist under similar names, such as `@samouraiwallet/electrum-client`, `@bitcoinerlab/electrum-client`, and `@mempool/electrum-client`, many of which offer ES Module compatibility and TypeScript support. The original `electrum-client` is strictly a CommonJS module.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an ElectrumX server, subscribe to new block headers, and make a basic JSON-RPC call to fetch the server's version.

const ElectrumCli = require('electrum-client');

const main = async () => {
    // Connect to an Electrum server (e.g., btc.smsys.me on port 995 with TLS)
    // Note: Use a reliable and trusted Electrum server.
    const ecl = new ElectrumCli(995, 'btc.smsys.me', 'tls'); // 'tcp' or 'tls'
    try {
        await ecl.connect(); // Establish connection
        console.log('Connected to Electrum server.');

        // Subscribe to blockchain header updates
        ecl.subscribe.on('blockchain.headers.subscribe', (header) => {
            console.log('New block header received:', header);
        });

        // Make a JSON-RPC call to get the server version
        const ver = await ecl.server_version("2.7.11", "1.0");
        console.log('Server version:', ver);

        // Example: Get balance for a script hash (replace with a real script hash)
        // Note: You would typically derive this from a Bitcoin address.
        // const scriptHash = '716decbe1660861c3d93906cb1d98ee68b154fd4d23aed9783859c1271b52a9c';
        // const balance = await ecl.blockchainScripthash_getBalance(scriptHash);
        // console.log('Script hash balance:', balance);

        // Keep the connection alive for a few seconds to receive subscriptions
        await new Promise(resolve => setTimeout(resolve, 10000));

    } catch (e) {
        console.error('Electrum client error:', e);
    } finally {
        await ecl.close(); // Disconnect
        console.log('Disconnected from Electrum server.');
    }
};

main();

view raw JSON →