transactions – Step by Step to Hook up with ElectrumX through Nodejs

0
131
transactions – Step by Step to Hook up with ElectrumX through Nodejs


This script get the quantity of an tackle

necessities: NodeJs >= 14.x

npm set up bitcoinjs-lib

create file electrumx-client.js accommodates this code:

const internet = require('internet');
const bitcoin = require('bitcoinjs-lib');

perform toScripthash(addr) {
  const script = bitcoin.tackle.toOutputScript(addr)
    , hash = bitcoin.crypto.sha256(script)
    , reversedHash = Buffer.from(hash.reverse());
  return reversedHash.toString('hex');
}
const [host, port] = course of.argv[2].cut up(':')
    , tackle = course of.argv[3];
const consumer = new internet.Socket();
consumer.on('knowledge', perform(chunk) {
    const response = JSON.parse(chunk.toString())
        , tackle = response.id
        , quantity = Quantity(response.consequence.confirmed)/100000000;
    consumer.destroy();
    console.log(`tackle:${tackle} quantity:${quantity} btc`);
})
.join(Quantity(port), host, () => {
    const scripthash = toScripthash(tackle)
        , physique = JSON.stringify({
            jsonrpc: '2.0',
            methodology: 'blockchain.scripthash.get_balance',
            id: tackle,
            params: [ scripthash ]
        });
    consumer.write(physique +'n');
});

Utilization:

node electrumx-client.js <servername>:50001 385cR5DM96n1HvBDMzLHPYcw89fZAXULJP

Output:

tackle:385cR5DM96n1HvBDMzLHPYcw89fZAXULJP quantity:36101.86 btc

exchange <servername> with a hostname of a Electrumx server, right here you will discover an inventory of public Electrum servers that may reply
https://1209k.com/bitcoin-eye/ele.php

LEAVE A REPLY

Please enter your comment!
Please enter your name here